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 use Test::More tests => 2 + 6 * 24 + 5;
12 my $libdir = 'test_trash';
13 unshift(@INC, $libdir);
15 my $appclass = 'TestComponents';
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' },
43 sub write_component_file {
44 my ($dir_list, $module_name, $content) = @_;
46 my $dir = File::Spec->catdir(@$dir_list);
47 my $file = File::Spec->catfile($dir, $module_name . '.pm');
49 mkpath(join(q{/}, @$dir_list) );
50 open(my $fh, '>', $file) or die "Could not open file $file for writing: $!";
55 sub make_component_file {
56 my ($type, $prefix, $name) = @_;
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);
64 write_component_file(\@dir_list, $name_final, <<EOF);
69 my \$self = shift->next::method(\@_);
71 *{\__PACKAGE__ . "::whoami"} = sub { return \__PACKAGE__; };
79 foreach my $component (@components) {
80 make_component_file($component->{type},
85 my $shut_up_deprecated_warnings = q{
86 __PACKAGE__->log(Catalyst::Log->new('fatal'));
89 eval "package $appclass; use Catalyst; $shut_up_deprecated_warnings __PACKAGE__->setup";
91 can_ok( $appclass, 'components');
93 my $complist = $appclass->components;
95 # the +1 below is for the app class itself
96 is(scalar keys %$complist, 24+1, "Correct number of components loaded");
98 foreach (keys %$complist) {
100 # Skip the component which happens to be the app itself
101 next if $_ eq $appclass;
103 my $instance = $appclass->component($_);
104 isa_ok($instance, $_);
105 can_ok($instance, 'whoami');
106 is($instance->whoami, $_);
108 if($_ =~ /^${appclass}::(?:V|View)::(.*)/) {
110 isa_ok($instance, 'Catalyst::View');
111 can_ok($appclass->view($moniker), 'whoami');
112 is($appclass->view($moniker)->whoami, $_);
114 elsif($_ =~ /^${appclass}::(?:M|Model)::(.*)/) {
116 isa_ok($instance, 'Catalyst::Model');
117 can_ok($appclass->model($moniker), 'whoami');
118 is($appclass->model($moniker)->whoami, $_);
120 elsif($_ =~ /^${appclass}::(?:C|Controller)::(.*)/) {
122 isa_ok($instance, 'Catalyst::Controller');
123 can_ok($appclass->controller($moniker), 'whoami');
124 is($appclass->controller($moniker)->whoami, $_);
127 die "Something is wrong with this test, this should"
128 . " have been unreachable";
134 # test extra component loading options
136 $appclass = 'ExtraOptions';
137 push @components, { type => 'View', prefix => 'Extra', name => 'Foo' };
139 foreach my $component (@components) {
140 make_component_file($component->{type},
141 $component->{prefix},
148 $shut_up_deprecated_warnings
149 __PACKAGE__->config->{ setup_components } = {
150 search_extra => [ '::Extra' ],
151 except => [ "${appclass}::Controller::Foo" ]
156 can_ok( $appclass, 'components');
158 $complist = $appclass->components;
160 is(scalar keys %$complist, 24+1, "Correct number of components loaded");
162 ok( !exists $complist->{ "${appclass}::Controller::Foo" }, 'Controller::Foo was skipped' );
163 ok( exists $complist->{ "${appclass}::Extra::Foo" }, 'Extra::Foo was loaded' );
167 $appclass = "ComponentOnce";
169 write_component_file([$libdir, $appclass, 'Model'], 'TopLevel', <<EOF);
170 package ${appclass}::Model::TopLevel;
171 use base 'Catalyst::Model';
174 my \$self = shift->next::method(\@_);
176 *{\__PACKAGE__ . "::whoami"} = sub { return \__PACKAGE__; };
180 package ${appclass}::Model::TopLevel::Nested;
182 sub COMPONENT { die "COMPONENT called in the wrong order!"; }
188 write_component_file([$libdir, $appclass, 'Model', 'TopLevel'], 'Nested', <<EOF);
189 package ${appclass}::Model::TopLevel::Nested;
190 use base 'Catalyst::Model';
192 no warnings 'redefine';
193 sub COMPONENT { return shift->next::method(\@_); }
198 eval "package $appclass; use Catalyst; __PACKAGE__->setup";
200 is($@, '', "Didn't load component twice");