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