Debian Codegraph @

t/integration/hooks/engine.t

raw
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Fatal qw<exception>;
6 use Plack::Test;
7 use HTTP::Request::Common;
8 use Path::Tiny ();
9
10 # Engine hooks - before_template_render and friends - do not belong to the app,
11 # they belong to an engine the app may not have built yet. App's add_hook
12 # modifier (Dancer2/Core/App.pm:714-746) handles both cases:
13 #
14 # engine already built -> register straight onto it via hook_candidates
15 # engine not yet built -> stash it in postponed_hooks, and
16 # Hookable::_add_postponed_hooks applies it when the
17 # engine is finally constructed
18 #
19 # Which branch is taken depends only on whether 'set template => ...' has run
20 # yet, because that setting's trigger builds the engine immediately. So the two
21 # apps below differ *only* in the order of two lines, and both must end up with
22 # a working hook. The postponed branch is the one that silently drops the hook
23 # if it regresses, and nothing about the app's own behavior would reveal it.
24
25 my $VIEWS;
26 BEGIN {
27 $VIEWS = Path::Tiny->tempdir;
28 $VIEWS->child('page.tt')->spew_utf8('VIEW[[% marker %]]');
29 }
30
31 # Hook first, engine second: exercises the postponed path.
32 {
33 package PostponedApp;
34 use Dancer2;
35 set logger => 'null';
36 set views => $VIEWS->stringify;
37
38 our @trace;
39
40 hook before_template_render => sub {
41 my $tokens = shift;
42 push @trace, 'before_render';
43 $tokens->{marker} = 'FROM-POSTPONED-HOOK';
44 };
45 hook after_template_render => sub { push @trace, 'after_render' };
46
47 # The engine is built here, after both hooks were registered.
48 set template => 'template_toolkit';
49
50 get '/page' => sub { template 'page' };
51 }
52
53 # Engine first, hook second: exercises the direct path.
54 {
55 package DirectApp;
56 use Dancer2;
57 set logger => 'null';
58 set views => $VIEWS->stringify;
59
60 # The engine is built here, before either hook is registered.
61 set template => 'template_toolkit';
62
63 our @trace;
64
65 hook before_template_render => sub {
66 my $tokens = shift;
67 push @trace, 'before_render';
68 $tokens->{marker} = 'FROM-DIRECT-HOOK';
69 };
70 hook after_template_render => sub { push @trace, 'after_render' };
71
72 get '/page' => sub { template 'page' };
73 }
74
75 subtest 'a hook registered before its engine exists still reaches it' => sub {
76 my $test = Plack::Test->create( PostponedApp->to_app );
77
78 @PostponedApp::trace = ();
79 my $response = $test->request( GET '/page' );
80
81 is( $response->code, 200, 'the page renders' );
82
83 # Both halves matter. The trace proves the hook was called at all; the
84 # body proves it was called with the real token hash, early enough for its
85 # change to reach the template.
86 is_deeply( \@PostponedApp::trace, [ 'before_render', 'after_render' ],
87 'both engine hooks ran, in order' );
88 is( $response->content, 'VIEW[FROM-POSTPONED-HOOK]',
89 'and the token the hook set reached the template' );
90 };
91
92 subtest 'a hook registered after its engine exists also reaches it' => sub {
93 my $test = Plack::Test->create( DirectApp->to_app );
94
95 @DirectApp::trace = ();
96 my $response = $test->request( GET '/page' );
97
98 is( $response->code, 200, 'the page renders' );
99 is_deeply( \@DirectApp::trace, [ 'before_render', 'after_render' ],
100 'both engine hooks ran, in order' );
101 is( $response->content, 'VIEW[FROM-DIRECT-HOOK]',
102 'and the token the hook set reached the template' );
103 };
104
105 # The postponed mechanism is not template-specific. Hookable's
106 # _add_postponed_hooks works out which engine it is being built for by matching
107 # the class name against a list of engine types (Dancer2/Core/Role/Hookable.pm:39),
108 # and every type in that list depends on being named there. A hook for a type
109 # that falls out of it is dropped in complete silence - no croak, no warning,
110 # the callback simply never runs. So each type gets its own assertion rather
111 # than trusting that "engine hooks work" because the template one does.
112 {
113 package AllEnginesApp;
114 use Dancer2;
115 set logger => 'null';
116 set views => $VIEWS->stringify;
117
118 our @trace;
119
120 # All four registered before any of these engines is built.
121 hook 'engine.template.before_render' => sub { push @trace, 'template' };
122 hook 'engine.session.before_flush' => sub { push @trace, 'session' };
123 hook 'engine.serializer.before' => sub { push @trace, 'serializer' };
124 hook 'engine.logger.before' => sub { push @trace, 'logger' };
125
126 set template => 'template_toolkit';
127 set session => 'Simple';
128 set serializer => 'JSON';
129
130 get '/all' => sub {
131 session touched => 1; # forces a session flush
132 info 'a log line'; # forces the logger
133 return { rendered => template 'page' };
134 };
135 }
136
137 subtest 'postponed hooks reach every kind of engine, not just templates' => sub {
138 my $test = Plack::Test->create( AllEnginesApp->to_app );
139
140 @AllEnginesApp::trace = ();
141 my $response = $test->request( GET '/all' );
142
143 is( $response->code, 200, 'the request succeeds' );
144
145 my %fired = map { $_ => 1 } @AllEnginesApp::trace;
146 ok( $fired{template}, 'the template engine got its postponed hook' );
147 ok( $fired{session}, 'the session engine got its postponed hook' );
148 ok( $fired{serializer}, 'the serializer engine got its postponed hook' );
149 ok( $fired{logger}, 'the logger engine got its postponed hook' );
150 };
151
152 subtest 'an unsupported engine hook name is refused, not ignored' => sub {
153 # A typo in an engine hook name must not be silently dropped. It is caught
154 # when the engine is built and applies its postponed hooks
155 # (Hookable::_add_postponed_hooks), so the failure happens at
156 # 'set template' rather than at the hook registration.
157 my $err = exception {
158 package TypoApp;
159 use Dancer2;
160 set logger => 'null';
161 hook 'engine.template.no_such_hook' => sub { 1 };
162 set template => 'template_toolkit';
163 };
164
165 ok( defined $err, 'registering an unknown engine hook is fatal' );
166 like( $err, qr/does not support the hook/,
167 'the message says the hook is not supported' );
168 like( $err, qr/no_such_hook/, 'and names the hook that was asked for' );
169
170 # The caller is reported, so a typo in a large app is findable.
171 like( $err, qr/TypoApp/, 'and names the package that registered it' );
172 };
173
174 done_testing();