Debian Codegraph @

t/multiapp_template_hooks.t

raw
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use Path::Tiny qw< path >;
7 use Test::More tests => 32;
8 use Plack::Test;
9 use HTTP::Request::Common;
10
11 my $views = path( __FILE__ )->parent->child('views')->stringify;
12
13 my %called_hooks = ();
14 my $hook_name = 'engine.template.before_render';
15
16 {
17 package App1;
18 use Dancer2;
19
20 set views => $views;
21
22 hook before => sub { $called_hooks{'App1'}{'before'}++ };
23
24 hook before_template => sub {
25 my $tokens = shift;
26 ::isa_ok( $tokens, 'HASH', '[App1] Tokens' );
27
28 my $app = app;
29
30 ::isa_ok( $app, 'Dancer2::Core::App', 'Got app object inside App1' );
31
32 # we accept anything that goes to App1, even if not only to App1
33 ::like(
34 $tokens->{'request'}->param('to'),
35 qr/^App1/,
36 'Request reached to correct App (App1)',
37 );
38
39 ::is(
40 scalar @{ $app->template_engine->hooks->{$hook_name} },
41 1,
42 'App1 has a single before_template hook defined',
43 );
44
45 $tokens->{'myname'} = 'App1';
46 $called_hooks{'App1'}{'before_template'}++;
47 };
48
49 get '/' => sub {
50 template beforetemplate => { it => 'App1' }, { layout => undef };
51 };
52 }
53
54 {
55 package App2;
56 use Dancer2;
57
58 set views => $views;
59
60 hook before => sub { $called_hooks{'App2'}{'before'}++ };
61
62 hook before_template => sub {
63 my $tokens = shift;
64 ::isa_ok( $tokens, 'HASH', '[App2] Tokens' );
65
66 my $app = app;
67 ::isa_ok( $app, 'Dancer2::Core::App', 'Got app object inside App2' );
68
69 ::is(
70 $tokens->{'request'}->param('to'),
71 'App2',
72 'Request reached to correct App (App2)',
73 );
74
75 ::is(
76 scalar @{ $app->template_engine->hooks->{$hook_name} },
77 1,
78 'App2 has a single before_template hook defined',
79 );
80
81 $tokens->{'myname'} = 'App2';
82 $called_hooks{'App2'}{'before_template'}++;
83 };
84
85 get '/' => sub {
86 template beforetemplate => { it => 'App2' }, { layout => undef };
87 };
88
89 get '/2' => sub {
90 template beforetemplate => { it => 'App2' }, { layout => undef };
91 };
92 }
93
94 note 'Check App1 only calls first hook, not both'; {
95 # clear
96 %called_hooks = ();
97
98 my $app = App1->to_app;
99 isa_ok( $app, 'CODE', 'Got app for test' );
100
101 test_psgi $app, sub {
102 my $cb = shift;
103 my $res = $cb->( GET '/?to=App1' );
104
105 is( $res->code, 200, '[GET /] Successful' );
106
107 is(
108 $res->content,
109 "App is App1, again, it is App1\n",
110 '[GET /] Correct content',
111 );
112
113 is_deeply(
114 \%called_hooks,
115 { App1 => { before => 1, before_template => 1 } },
116 'Only App1\'s before_template hook was called',
117 );
118 };
119 }
120
121 note 'Check App2 only calls second hook, not both'; {
122 # clear
123 %called_hooks = ();
124
125 my $app = App2->to_app;
126 isa_ok( $app, 'CODE', 'Got app for test' );
127
128 test_psgi $app, sub {
129 my $cb = shift;
130 my $res = $cb->( GET '/?to=App2' );
131
132 is( $res->code, 200, '[GET /] Successful' );
133
134 is(
135 $res->content,
136 "App is App2, again, it is App2\n",
137 '[GET /] Correct content',
138 );
139
140 is_deeply(
141 \%called_hooks,
142 { App2 => { before => 1, before_template => 1 } },
143 'Only App2\'s before_template hook was called',
144 );
145 };
146 }
147
148 note 'Check both apps only call the first hook (correct app), not both'; {
149 # clear
150 %called_hooks = ();
151
152 my $app = Dancer2->psgi_app;
153 isa_ok( $app, 'CODE', 'Got app for test' );
154
155 test_psgi $app, sub {
156 my $cb = shift;
157 my $res = $cb->( GET '/?to=App1:App2' );
158
159 is( $res->code, 200, '[GET /] Successful' );
160
161 is(
162 $res->content,
163 "App is App1, again, it is App1\n",
164 '[GET /] Correct content',
165 );
166
167 is_deeply(
168 \%called_hooks,
169 { App1 => { before => 1, before_template => 1 } },
170 'Only App1\'s before_template hook was called (full PSGI app)',
171 );
172 };
173 }
174
175 note 'Check both apps only call the second hook (correct app), not both'; {
176 # clear
177 %called_hooks = ();
178
179 my $app = Dancer2->psgi_app;
180 isa_ok( $app, 'CODE', 'Got app for test' );
181
182 test_psgi $app, sub {
183 my $cb = shift;
184 my $res = $cb->( GET '/2?to=App2' );
185
186 is( $res->code, 200, '[GET /2] Successful' );
187
188 is(
189 $res->content,
190 "App is App2, again, it is App2\n",
191 '[GET /2] Correct content',
192 );
193
194 # INFO: %called_hooks does not contain any counts for App1;
195 # no routes match, so no hooks are called.
196 is_deeply(
197 \%called_hooks,
198 {
199 App2 => { before => 1, before_template => 1 },
200 },
201 'Only App2\'s before_template hook was called (full PSGI app)',
202 );
203 };
204 }
205