Fix bug where a nested component would be setup twice
[catagits/Catalyst-Runtime.git] / t / unit_core_component_loading.t
CommitLineData
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
4use Test::More tests => 2 + 6 * 24 + 5;
0756fe3b 5
6use strict;
7use warnings;
8
9use File::Spec;
10use File::Path;
11
12my $libdir = 'test_trash';
13unshift(@INC, $libdir);
14
15my $appclass = 'TestComponents';
16my @components = (
17 { type => 'Controller', prefix => 'C', name => 'Bar' },
18 { type => 'Controller', prefix => 'C', name => 'Foo::Bar' },
19 { type => 'Controller', prefix => 'C', name => 'Foo::Foo::Bar' },
20 { type => 'Controller', prefix => 'C', name => 'Foo::Foo::Foo::Bar' },
21 { type => 'Controller', prefix => 'Controller', name => 'Bar::Bar::Bar::Foo' },
22 { type => 'Controller', prefix => 'Controller', name => 'Bar::Bar::Foo' },
23 { type => 'Controller', prefix => 'Controller', name => 'Bar::Foo' },
24 { type => 'Controller', prefix => 'Controller', name => 'Foo' },
25 { type => 'Model', prefix => 'M', name => 'Bar' },
26 { type => 'Model', prefix => 'M', name => 'Foo::Bar' },
27 { type => 'Model', prefix => 'M', name => 'Foo::Foo::Bar' },
28 { type => 'Model', prefix => 'M', name => 'Foo::Foo::Foo::Bar' },
29 { type => 'Model', prefix => 'Model', name => 'Bar::Bar::Bar::Foo' },
30 { type => 'Model', prefix => 'Model', name => 'Bar::Bar::Foo' },
31 { type => 'Model', prefix => 'Model', name => 'Bar::Foo' },
32 { type => 'Model', prefix => 'Model', name => 'Foo' },
33 { type => 'View', prefix => 'V', name => 'Bar' },
34 { type => 'View', prefix => 'V', name => 'Foo::Bar' },
35 { type => 'View', prefix => 'V', name => 'Foo::Foo::Bar' },
36 { type => 'View', prefix => 'V', name => 'Foo::Foo::Foo::Bar' },
37 { type => 'View', prefix => 'View', name => 'Bar::Bar::Bar::Foo' },
38 { type => 'View', prefix => 'View', name => 'Bar::Bar::Foo' },
39 { type => 'View', prefix => 'View', name => 'Bar::Foo' },
40 { type => 'View', prefix => 'View', name => 'Foo' },
41);
42
b94b200c 43sub write_component_file {
44 my ($dir_list, $module_name, $content) = @_;
45
46 my $dir = File::Spec->catdir(@$dir_list);
47 my $file = File::Spec->catfile($dir, $module_name . '.pm');
48
49 mkpath(join(q{/}, @$dir_list) );
50 open(my $fh, '>', $file) or die "Could not open file $file for writing: $!";
51 print $fh $content;
52 close $fh;
53}
54
0756fe3b 55sub make_component_file {
56 my ($type, $prefix, $name) = @_;
57
58 my $compbase = "Catalyst::${type}";
59 my $fullname = "${appclass}::${prefix}::${name}";
60 my @namedirs = split(/::/, $name);
61 my $name_final = pop(@namedirs);
62 my @dir_list = ($libdir, $appclass, $prefix, @namedirs);
0756fe3b 63
b94b200c 64 write_component_file(\@dir_list, $name_final, <<EOF);
0756fe3b 65package $fullname;
66use base '$compbase';
67sub COMPONENT {
68 my \$self = shift->NEXT::COMPONENT(\@_);
69 no strict 'refs';
70 *{\__PACKAGE__ . "::whoami"} = sub { return \__PACKAGE__; };
71 \$self;
72}
731;
74
75EOF
0756fe3b 76}
77
78foreach my $component (@components) {
79 make_component_file($component->{type},
80 $component->{prefix},
81 $component->{name});
82}
83
84eval "package $appclass; use Catalyst; __PACKAGE__->setup";
85
86can_ok( $appclass, 'components');
87
88my $complist = $appclass->components;
89
90# the +1 below is for the app class itself
91is(scalar keys %$complist, 24+1, "Correct number of components loaded");
92
93foreach (keys %$complist) {
94
95 # Skip the component which happens to be the app itself
96 next if $_ eq $appclass;
97
98 my $instance = $appclass->component($_);
99 isa_ok($instance, $_);
100 can_ok($instance, 'whoami');
101 is($instance->whoami, $_);
102
103 if($_ =~ /^${appclass}::(?:V|View)::(.*)/) {
104 my $moniker = $1;
105 isa_ok($instance, 'Catalyst::View');
106 can_ok($appclass->view($moniker), 'whoami');
107 is($appclass->view($moniker)->whoami, $_);
108 }
109 elsif($_ =~ /^${appclass}::(?:M|Model)::(.*)/) {
110 my $moniker = $1;
111 isa_ok($instance, 'Catalyst::Model');
112 can_ok($appclass->model($moniker), 'whoami');
113 is($appclass->model($moniker)->whoami, $_);
114 }
115 elsif($_ =~ /^${appclass}::(?:C|Controller)::(.*)/) {
116 my $moniker = $1;
117 isa_ok($instance, 'Catalyst::Controller');
118 can_ok($appclass->controller($moniker), 'whoami');
119 is($appclass->controller($moniker)->whoami, $_);
120 }
121 else {
122 die "Something is wrong with this test, this should"
123 . " have been unreachable";
124 }
125}
126
127rmtree($libdir);
18de900e 128
129# test extra component loading options
130
131$appclass = 'ExtraOptions';
132push @components, { type => 'View', prefix => 'Extra', name => 'Foo' };
133
134foreach my $component (@components) {
135 make_component_file($component->{type},
136 $component->{prefix},
137 $component->{name});
138}
139
140eval qq(
141package $appclass;
142use Catalyst;
143__PACKAGE__->config->{ setup_components } = {
144 search_extra => [ '::Extra' ],
145 except => [ "${appclass}::Controller::Foo" ]
146};
147__PACKAGE__->setup;
148);
149
150can_ok( $appclass, 'components');
151
152$complist = $appclass->components;
153
154is(scalar keys %$complist, 24+1, "Correct number of components loaded");
155
2d486591 156ok( !exists $complist->{ "${appclass}::Controller::Foo" }, 'Controller::Foo was skipped' );
157ok( exists $complist->{ "${appclass}::Extra::Foo" }, 'Extra::Foo was loaded' );
18de900e 158
b94b200c 159rmtree($libdir);
160
161$appclass = "ComponentOnce";
162
163write_component_file([$libdir, $appclass, 'Model'], 'TopLevel', <<EOF);
164package ${appclass}::Model::TopLevel;
165use base 'Catalyst::Model';
166sub COMPONENT {
167
168 my \$self = shift->NEXT::COMPONENT(\@_);
169 no strict 'refs';
170 *{\__PACKAGE__ . "::whoami"} = sub { return \__PACKAGE__; };
171 \$self;
172}
173
174package ${appclass}::Model::TopLevel::Nested;
175
176sub COMPONENT { die "COMPONENT called in the wrong order!"; }
177
1781;
179
180EOF
181
182write_component_file([$libdir, $appclass, 'Model', 'TopLevel'], 'Nested', <<EOF);
183package ${appclass}::Model::TopLevel::Nested;
184use base 'Catalyst::Model';
185
186no warnings 'redefine';
187sub COMPONENT { return shift->NEXT::COMPONENT(\@_); }
1881;
189
190EOF
191
192eval "package $appclass; use Catalyst; __PACKAGE__->setup";
193
194is($@, '', "Didn't load component twice");
195
196rmtree($libdir);