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