File: multiapp_template_hooks.t

package info (click to toggle)
libdancer2-perl 0.400001%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,580 kB
  • sloc: perl: 8,461; makefile: 9
file content (208 lines) | stat: -rw-r--r-- 5,070 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!perl

use strict;
use warnings;

use File::Spec;
use File::Basename 'dirname';
use Test::More tests => 32;
use Plack::Test;
use HTTP::Request::Common;

my $views = File::Spec->rel2abs(
    File::Spec->catfile( dirname(__FILE__), 'views' )
);

my %called_hooks = ();
my $hook_name    = 'engine.template.before_render';

{
    package App1;
    use Dancer2;

    set views => $views;

    hook before => sub { $called_hooks{'App1'}{'before'}++ };

    hook before_template => sub {
        my $tokens = shift;
        ::isa_ok( $tokens, 'HASH', '[App1] Tokens' );

        my $app = app;

        ::isa_ok( $app, 'Dancer2::Core::App', 'Got app object inside App1' );

        # we accept anything that goes to App1, even if not only to App1
        ::like(
            $tokens->{'request'}->param('to'),
            qr/^App1/,
            'Request reached to correct App (App1)',
        );

        ::is(
            scalar @{ $app->template_engine->hooks->{$hook_name} },
            1,
            'App1 has a single before_template hook defined',
        );

        $tokens->{'myname'} = 'App1';
        $called_hooks{'App1'}{'before_template'}++;
    };

    get '/' => sub {
        template beforetemplate => { it => 'App1' }, { layout => undef };
    };
}

{
    package App2;
    use Dancer2;

    set views => $views;

    hook before => sub { $called_hooks{'App2'}{'before'}++ };

    hook before_template => sub {
        my $tokens = shift;
        ::isa_ok( $tokens, 'HASH', '[App2] Tokens' );

        my $app = app;
        ::isa_ok( $app, 'Dancer2::Core::App', 'Got app object inside App2' );

        ::is(
            $tokens->{'request'}->param('to'),
            'App2',
            'Request reached to correct App (App2)',
        );

        ::is(
            scalar @{ $app->template_engine->hooks->{$hook_name} },
            1,
            'App2 has a single before_template hook defined',
        );

        $tokens->{'myname'} = 'App2';
        $called_hooks{'App2'}{'before_template'}++;
    };

    get '/' => sub {
        template beforetemplate => { it => 'App2' }, { layout => undef };
    };

    get '/2' => sub {
        template beforetemplate => { it => 'App2' }, { layout => undef };
    };
}

note 'Check App1 only calls first hook, not both'; {
    # clear
    %called_hooks = ();

    my $app = App1->to_app;
    isa_ok( $app, 'CODE', 'Got app for test' );

    test_psgi $app, sub {
        my $cb  = shift;
        my $res = $cb->( GET '/?to=App1' );

        is( $res->code, 200, '[GET /] Successful' );

        is(
            $res->content,
            "App is App1, again, it is App1\n",
            '[GET /] Correct content',
        );

        is_deeply(
            \%called_hooks,
            { App1 => { before => 1, before_template => 1 } },
            'Only App1\'s before_template hook was called',
        );
    };
}

note 'Check App2 only calls second hook, not both'; {
    # clear
    %called_hooks = ();

    my $app = App2->to_app;
    isa_ok( $app, 'CODE', 'Got app for test' );

    test_psgi $app, sub {
        my $cb  = shift;
        my $res = $cb->( GET '/?to=App2' );

        is( $res->code, 200, '[GET /] Successful' );

        is(
            $res->content,
            "App is App2, again, it is App2\n",
            '[GET /] Correct content',
        );

        is_deeply(
            \%called_hooks,
            { App2 => { before => 1, before_template => 1 } },
            'Only App2\'s before_template hook was called',
        );
    };
}

note 'Check both apps only call the first hook (correct app), not both'; {
    # clear
    %called_hooks = ();

    my $app = Dancer2->psgi_app;
    isa_ok( $app, 'CODE', 'Got app for test' );

    test_psgi $app, sub {
        my $cb  = shift;
        my $res = $cb->( GET '/?to=App1:App2' );

        is( $res->code, 200, '[GET /] Successful' );

        is(
            $res->content,
            "App is App1, again, it is App1\n",
            '[GET /] Correct content',
        );

        is_deeply(
            \%called_hooks,
            { App1 => { before => 1, before_template => 1 } },
            'Only App1\'s before_template hook was called (full PSGI app)',
        );
    };
}

note 'Check both apps only call the second hook (correct app), not both'; {
    # clear
    %called_hooks = ();

    my $app = Dancer2->psgi_app;
    isa_ok( $app, 'CODE', 'Got app for test' );

    test_psgi $app, sub {
        my $cb  = shift;
        my $res = $cb->( GET '/2?to=App2' );

        is( $res->code, 200, '[GET /2] Successful' );

        is(
            $res->content,
            "App is App2, again, it is App2\n",
            '[GET /2] Correct content',
        );

        # INFO: %called_hooks does not contain any counts for App1;
        #       no routes match, so no hooks are called.
        is_deeply(
            \%called_hooks,
            {
                App2 => { before => 1, before_template => 1 },
            },
            'Only App2\'s before_template hook was called (full PSGI app)',
        );
    };
}