Commit | Line | Data |
0756fe3b |
1 | # 2 initial tests, and 6 per component in the loop below |
2 | # (do not forget to update the number of components in test 3 as well) |
b94b200c |
3 | # 5 extra tests for the loading options |
e1dd56e6 |
4 | # One test for components in inner packages |
ac424253 |
5 | use Test::More tests => 2 + 6 * 24 + 8 + 1; |
0756fe3b |
6 | |
7 | use strict; |
8 | use warnings; |
9 | |
10 | use File::Spec; |
11 | use File::Path; |
12 | |
13 | my $libdir = 'test_trash'; |
06400669 |
14 | local @INC = @INC; |
0756fe3b |
15 | unshift(@INC, $libdir); |
16 | |
17 | my $appclass = 'TestComponents'; |
18 | my @components = ( |
19 | { type => 'Controller', prefix => 'C', name => 'Bar' }, |
20 | { type => 'Controller', prefix => 'C', name => 'Foo::Bar' }, |
21 | { type => 'Controller', prefix => 'C', name => 'Foo::Foo::Bar' }, |
22 | { type => 'Controller', prefix => 'C', name => 'Foo::Foo::Foo::Bar' }, |
23 | { type => 'Controller', prefix => 'Controller', name => 'Bar::Bar::Bar::Foo' }, |
24 | { type => 'Controller', prefix => 'Controller', name => 'Bar::Bar::Foo' }, |
25 | { type => 'Controller', prefix => 'Controller', name => 'Bar::Foo' }, |
26 | { type => 'Controller', prefix => 'Controller', name => 'Foo' }, |
27 | { type => 'Model', prefix => 'M', name => 'Bar' }, |
28 | { type => 'Model', prefix => 'M', name => 'Foo::Bar' }, |
29 | { type => 'Model', prefix => 'M', name => 'Foo::Foo::Bar' }, |
30 | { type => 'Model', prefix => 'M', name => 'Foo::Foo::Foo::Bar' }, |
31 | { type => 'Model', prefix => 'Model', name => 'Bar::Bar::Bar::Foo' }, |
32 | { type => 'Model', prefix => 'Model', name => 'Bar::Bar::Foo' }, |
33 | { type => 'Model', prefix => 'Model', name => 'Bar::Foo' }, |
34 | { type => 'Model', prefix => 'Model', name => 'Foo' }, |
35 | { type => 'View', prefix => 'V', name => 'Bar' }, |
36 | { type => 'View', prefix => 'V', name => 'Foo::Bar' }, |
37 | { type => 'View', prefix => 'V', name => 'Foo::Foo::Bar' }, |
38 | { type => 'View', prefix => 'V', name => 'Foo::Foo::Foo::Bar' }, |
39 | { type => 'View', prefix => 'View', name => 'Bar::Bar::Bar::Foo' }, |
40 | { type => 'View', prefix => 'View', name => 'Bar::Bar::Foo' }, |
41 | { type => 'View', prefix => 'View', name => 'Bar::Foo' }, |
42 | { type => 'View', prefix => 'View', name => 'Foo' }, |
43 | ); |
44 | |
5d50f369 |
45 | sub write_component_file { |
b94b200c |
46 | my ($dir_list, $module_name, $content) = @_; |
47 | |
48 | my $dir = File::Spec->catdir(@$dir_list); |
49 | my $file = File::Spec->catfile($dir, $module_name . '.pm'); |
50 | |
51 | mkpath(join(q{/}, @$dir_list) ); |
52 | open(my $fh, '>', $file) or die "Could not open file $file for writing: $!"; |
53 | print $fh $content; |
54 | close $fh; |
55 | } |
56 | |
0756fe3b |
57 | sub make_component_file { |
06400669 |
58 | my ($libdir, $appclass, $type, $prefix, $name) = @_; |
0756fe3b |
59 | |
60 | my $compbase = "Catalyst::${type}"; |
61 | my $fullname = "${appclass}::${prefix}::${name}"; |
62 | my @namedirs = split(/::/, $name); |
63 | my $name_final = pop(@namedirs); |
64 | my @dir_list = ($libdir, $appclass, $prefix, @namedirs); |
0756fe3b |
65 | |
b94b200c |
66 | write_component_file(\@dir_list, $name_final, <<EOF); |
0756fe3b |
67 | package $fullname; |
f27f2c6d |
68 | use MRO::Compat; |
0756fe3b |
69 | use base '$compbase'; |
70 | sub COMPONENT { |
dbb2d5cd |
71 | my \$self = shift->next::method(\@_); |
0756fe3b |
72 | no strict 'refs'; |
73 | *{\__PACKAGE__ . "::whoami"} = sub { return \__PACKAGE__; }; |
74 | \$self; |
75 | } |
76 | 1; |
77 | |
78 | EOF |
0756fe3b |
79 | } |
80 | |
81 | foreach my $component (@components) { |
06400669 |
82 | make_component_file( |
83 | $libdir, |
84 | $appclass, |
85 | $component->{type}, |
86 | $component->{prefix}, |
87 | $component->{name}, |
88 | ); |
0756fe3b |
89 | } |
90 | |
f27f2c6d |
91 | my $shut_up_deprecated_warnings = q{ |
0f52a840 |
92 | __PACKAGE__->log(Catalyst::Log->new('fatal')); |
f27f2c6d |
93 | }; |
94 | |
95 | eval "package $appclass; use Catalyst; $shut_up_deprecated_warnings __PACKAGE__->setup"; |
0756fe3b |
96 | |
97 | can_ok( $appclass, 'components'); |
98 | |
99 | my $complist = $appclass->components; |
100 | |
101 | # the +1 below is for the app class itself |
102 | is(scalar keys %$complist, 24+1, "Correct number of components loaded"); |
103 | |
104 | foreach (keys %$complist) { |
105 | |
106 | # Skip the component which happens to be the app itself |
107 | next if $_ eq $appclass; |
108 | |
109 | my $instance = $appclass->component($_); |
110 | isa_ok($instance, $_); |
111 | can_ok($instance, 'whoami'); |
112 | is($instance->whoami, $_); |
113 | |
114 | if($_ =~ /^${appclass}::(?:V|View)::(.*)/) { |
115 | my $moniker = $1; |
116 | isa_ok($instance, 'Catalyst::View'); |
117 | can_ok($appclass->view($moniker), 'whoami'); |
118 | is($appclass->view($moniker)->whoami, $_); |
119 | } |
120 | elsif($_ =~ /^${appclass}::(?:M|Model)::(.*)/) { |
121 | my $moniker = $1; |
122 | isa_ok($instance, 'Catalyst::Model'); |
123 | can_ok($appclass->model($moniker), 'whoami'); |
124 | is($appclass->model($moniker)->whoami, $_); |
125 | } |
126 | elsif($_ =~ /^${appclass}::(?:C|Controller)::(.*)/) { |
127 | my $moniker = $1; |
128 | isa_ok($instance, 'Catalyst::Controller'); |
129 | can_ok($appclass->controller($moniker), 'whoami'); |
130 | is($appclass->controller($moniker)->whoami, $_); |
131 | } |
132 | else { |
133 | die "Something is wrong with this test, this should" |
134 | . " have been unreachable"; |
135 | } |
136 | } |
137 | |
138 | rmtree($libdir); |
18de900e |
139 | |
140 | # test extra component loading options |
141 | |
142 | $appclass = 'ExtraOptions'; |
143 | push @components, { type => 'View', prefix => 'Extra', name => 'Foo' }; |
144 | |
145 | foreach my $component (@components) { |
06400669 |
146 | make_component_file( |
147 | $libdir, |
148 | $appclass, |
149 | $component->{type}, |
150 | $component->{prefix}, |
151 | $component->{name}, |
152 | ); |
18de900e |
153 | } |
154 | |
155 | eval qq( |
156 | package $appclass; |
157 | use Catalyst; |
f27f2c6d |
158 | $shut_up_deprecated_warnings |
18de900e |
159 | __PACKAGE__->config->{ setup_components } = { |
160 | search_extra => [ '::Extra' ], |
161 | except => [ "${appclass}::Controller::Foo" ] |
162 | }; |
163 | __PACKAGE__->setup; |
164 | ); |
165 | |
166 | can_ok( $appclass, 'components'); |
167 | |
168 | $complist = $appclass->components; |
169 | |
170 | is(scalar keys %$complist, 24+1, "Correct number of components loaded"); |
171 | |
2d486591 |
172 | ok( !exists $complist->{ "${appclass}::Controller::Foo" }, 'Controller::Foo was skipped' ); |
173 | ok( exists $complist->{ "${appclass}::Extra::Foo" }, 'Extra::Foo was loaded' ); |
18de900e |
174 | |
b94b200c |
175 | rmtree($libdir); |
176 | |
177 | $appclass = "ComponentOnce"; |
178 | |
179 | write_component_file([$libdir, $appclass, 'Model'], 'TopLevel', <<EOF); |
180 | package ${appclass}::Model::TopLevel; |
181 | use base 'Catalyst::Model'; |
182 | sub COMPONENT { |
5d50f369 |
183 | |
dbb2d5cd |
184 | my \$self = shift->next::method(\@_); |
b94b200c |
185 | no strict 'refs'; |
186 | *{\__PACKAGE__ . "::whoami"} = sub { return \__PACKAGE__; }; |
e7e4c469 |
187 | *${appclass}::Model::TopLevel::GENERATED::ACCEPT_CONTEXT = sub { |
188 | return bless {}, 'FooBarBazQuux'; |
189 | }; |
b94b200c |
190 | \$self; |
191 | } |
192 | |
193 | package ${appclass}::Model::TopLevel::Nested; |
194 | |
195 | sub COMPONENT { die "COMPONENT called in the wrong order!"; } |
196 | |
197 | 1; |
198 | |
199 | EOF |
200 | |
201 | write_component_file([$libdir, $appclass, 'Model', 'TopLevel'], 'Nested', <<EOF); |
202 | package ${appclass}::Model::TopLevel::Nested; |
203 | use base 'Catalyst::Model'; |
204 | |
ac424253 |
205 | my \$called=0; |
b94b200c |
206 | no warnings 'redefine'; |
ac424253 |
207 | sub COMPONENT { \$called++;return shift->next::method(\@_); } |
208 | sub called { return \$called }; |
b94b200c |
209 | 1; |
210 | |
211 | EOF |
212 | |
213 | eval "package $appclass; use Catalyst; __PACKAGE__->setup"; |
214 | |
215 | is($@, '', "Didn't load component twice"); |
ac424253 |
216 | is($appclass->model('TopLevel::Nested')->called,1, 'COMPONENT called once'); |
b94b200c |
217 | |
e7e4c469 |
218 | ok($appclass->model('TopLevel::Generated'), 'Have generated model'); |
219 | is(ref($appclass->model('TopLevel::Generated')), 'FooBarBazQuux', |
220 | 'ACCEPT_CONTEXT in generated inner package fired as expected'); |
221 | |
e1dd56e6 |
222 | $appclass = "InnerComponent"; |
223 | |
224 | { |
225 | package InnerComponent::Controller::Test; |
226 | use base 'Catalyst::Controller'; |
227 | } |
228 | |
229 | $INC{'InnerComponent/Controller/Test.pm'} = 1; |
230 | |
231 | eval "package $appclass; use Catalyst; __PACKAGE__->setup"; |
232 | |
233 | isa_ok($appclass->controller('Test'), 'Catalyst::Controller'); |
234 | |
b94b200c |
235 | rmtree($libdir); |