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)
3 # 5 extra tests for the loading options
4 # One test for components in inner packages
5 use Test::More tests => 2 + 6 * 24 + 8 + 1;
13 my $libdir = 'test_trash';
14 unshift(@INC, $libdir);
16 my $appclass = 'TestComponents';
18 { type => 'Controller', prefix => 'C', name => 'Bar' },
19 { type => 'Controller', prefix => 'C', name => 'Foo::Bar' },
20 { type => 'Controller', prefix => 'C', name => 'Foo::Foo::Bar' },
21 { type => 'Controller', prefix => 'C', name => 'Foo::Foo::Foo::Bar' },
22 { type => 'Controller', prefix => 'Controller', name => 'Bar::Bar::Bar::Foo' },
23 { type => 'Controller', prefix => 'Controller', name => 'Bar::Bar::Foo' },
24 { type => 'Controller', prefix => 'Controller', name => 'Bar::Foo' },
25 { type => 'Controller', prefix => 'Controller', name => 'Foo' },
26 { type => 'Model', prefix => 'M', name => 'Bar' },
27 { type => 'Model', prefix => 'M', name => 'Foo::Bar' },
28 { type => 'Model', prefix => 'M', name => 'Foo::Foo::Bar' },
29 { type => 'Model', prefix => 'M', name => 'Foo::Foo::Foo::Bar' },
30 { type => 'Model', prefix => 'Model', name => 'Bar::Bar::Bar::Foo' },
31 { type => 'Model', prefix => 'Model', name => 'Bar::Bar::Foo' },
32 { type => 'Model', prefix => 'Model', name => 'Bar::Foo' },
33 { type => 'Model', prefix => 'Model', name => 'Foo' },
34 { type => 'View', prefix => 'V', name => 'Bar' },
35 { type => 'View', prefix => 'V', name => 'Foo::Bar' },
36 { type => 'View', prefix => 'V', name => 'Foo::Foo::Bar' },
37 { type => 'View', prefix => 'V', name => 'Foo::Foo::Foo::Bar' },
38 { type => 'View', prefix => 'View', name => 'Bar::Bar::Bar::Foo' },
39 { type => 'View', prefix => 'View', name => 'Bar::Bar::Foo' },
40 { type => 'View', prefix => 'View', name => 'Bar::Foo' },
41 { type => 'View', prefix => 'View', name => 'Foo' },
44 sub write_component_file {
45 my ($dir_list, $module_name, $content) = @_;
47 my $dir = File::Spec->catdir(@$dir_list);
48 my $file = File::Spec->catfile($dir, $module_name . '.pm');
50 mkpath(join(q{/}, @$dir_list) );
51 open(my $fh, '>', $file) or die "Could not open file $file for writing: $!";
56 sub make_component_file {
57 my ($type, $prefix, $name) = @_;
59 my $compbase = "Catalyst::${type}";
60 my $fullname = "${appclass}::${prefix}::${name}";
61 my @namedirs = split(/::/, $name);
62 my $name_final = pop(@namedirs);
63 my @dir_list = ($libdir, $appclass, $prefix, @namedirs);
65 write_component_file(\@dir_list, $name_final, <<EOF);
70 my \$self = shift->next::method(\@_);
72 *{\__PACKAGE__ . "::whoami"} = sub { return \__PACKAGE__; };
80 foreach my $component (@components) {
81 make_component_file($component->{type},
86 my $shut_up_deprecated_warnings = q{
87 __PACKAGE__->log(Catalyst::Log->new('fatal'));
90 eval "package $appclass; use Catalyst; $shut_up_deprecated_warnings __PACKAGE__->setup";
92 can_ok( $appclass, 'components');
94 my $complist = $appclass->components;
96 # the +1 below is for the app class itself
97 is(scalar keys %$complist, 24+1, "Correct number of components loaded");
99 foreach (keys %$complist) {
101 # Skip the component which happens to be the app itself
102 next if $_ eq $appclass;
104 my $instance = $appclass->component($_);
105 isa_ok($instance, $_);
106 can_ok($instance, 'whoami');
107 is($instance->whoami, $_);
109 if($_ =~ /^${appclass}::(?:V|View)::(.*)/) {
111 isa_ok($instance, 'Catalyst::View');
112 can_ok($appclass->view($moniker), 'whoami');
113 is($appclass->view($moniker)->whoami, $_);
115 elsif($_ =~ /^${appclass}::(?:M|Model)::(.*)/) {
117 isa_ok($instance, 'Catalyst::Model');
118 can_ok($appclass->model($moniker), 'whoami');
119 is($appclass->model($moniker)->whoami, $_);
121 elsif($_ =~ /^${appclass}::(?:C|Controller)::(.*)/) {
123 isa_ok($instance, 'Catalyst::Controller');
124 can_ok($appclass->controller($moniker), 'whoami');
125 is($appclass->controller($moniker)->whoami, $_);
128 die "Something is wrong with this test, this should"
129 . " have been unreachable";
135 # test extra component loading options
137 $appclass = 'ExtraOptions';
138 push @components, { type => 'View', prefix => 'Extra', name => 'Foo' };
140 foreach my $component (@components) {
141 make_component_file($component->{type},
142 $component->{prefix},
149 $shut_up_deprecated_warnings
150 __PACKAGE__->config->{ setup_components } = {
151 search_extra => [ '::Extra' ],
152 except => [ "${appclass}::Controller::Foo" ]
157 can_ok( $appclass, 'components');
159 $complist = $appclass->components;
161 is(scalar keys %$complist, 24+1, "Correct number of components loaded");
163 ok( !exists $complist->{ "${appclass}::Controller::Foo" }, 'Controller::Foo was skipped' );
164 ok( exists $complist->{ "${appclass}::Extra::Foo" }, 'Extra::Foo was loaded' );
168 $appclass = "ComponentOnce";
170 write_component_file([$libdir, $appclass, 'Model'], 'TopLevel', <<EOF);
171 package ${appclass}::Model::TopLevel;
172 use base 'Catalyst::Model';
175 my \$self = shift->next::method(\@_);
177 *{\__PACKAGE__ . "::whoami"} = sub { return \__PACKAGE__; };
178 *${appclass}::Model::TopLevel::GENERATED::ACCEPT_CONTEXT = sub {
179 return bless {}, 'FooBarBazQuux';
184 package ${appclass}::Model::TopLevel::Nested;
186 sub COMPONENT { die "COMPONENT called in the wrong order!"; }
192 write_component_file([$libdir, $appclass, 'Model', 'TopLevel'], 'Nested', <<EOF);
193 package ${appclass}::Model::TopLevel::Nested;
194 use base 'Catalyst::Model';
197 no warnings 'redefine';
198 sub COMPONENT { \$called++;return shift->next::method(\@_); }
199 sub called { return \$called };
204 eval "package $appclass; use Catalyst; __PACKAGE__->setup";
206 is($@, '', "Didn't load component twice");
207 is($appclass->model('TopLevel::Nested')->called,1, 'COMPONENT called once');
209 ok($appclass->model('TopLevel::Generated'), 'Have generated model');
210 is(ref($appclass->model('TopLevel::Generated')), 'FooBarBazQuux',
211 'ACCEPT_CONTEXT in generated inner package fired as expected');
213 $appclass = "InnerComponent";
216 package InnerComponent::Controller::Test;
217 use base 'Catalyst::Controller';
220 $INC{'InnerComponent/Controller/Test.pm'} = 1;
222 eval "package $appclass; use Catalyst; __PACKAGE__->setup";
224 isa_ok($appclass->controller('Test'), 'Catalyst::Controller');