Handle $c->error called as a class method.
[catagits/Catalyst-Runtime.git] / t / unit_core_component_loading.t
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 use Test::More tests => 2 + 6 * 24;
4
5 use strict;
6 use warnings;
7
8 use File::Spec;
9 use File::Path;
10
11 my $libdir = 'test_trash';
12 unshift(@INC, $libdir);
13
14 my $appclass = 'TestComponents';
15 my @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
42 sub make_component_file {
43     my ($type, $prefix, $name) = @_;
44
45     my $compbase = "Catalyst::${type}";
46     my $fullname = "${appclass}::${prefix}::${name}";
47     my @namedirs = split(/::/, $name);
48     my $name_final = pop(@namedirs);
49     my @dir_list = ($libdir, $appclass, $prefix, @namedirs);
50     my $dir_ux   = join(q{/}, @dir_list);
51     my $dir      = File::Spec->catdir(@dir_list);
52     my $file     = File::Spec->catfile($dir, $name_final . '.pm');
53
54     mkpath($dir_ux); # mkpath wants unix '/' seperators :p
55     open(my $fh, '>', $file) or die "Could not open file $file for writing: $!";
56     print $fh <<EOF;
57 package $fullname;
58 use base '$compbase';
59 sub COMPONENT {
60     my \$self = shift->NEXT::COMPONENT(\@_);
61     no strict 'refs';
62     *{\__PACKAGE__ . "::whoami"} = sub { return \__PACKAGE__; };
63     \$self;
64 }
65 1;
66
67 EOF
68
69     close($fh);
70 }
71
72 foreach my $component (@components) {
73     make_component_file($component->{type},
74                         $component->{prefix},
75                         $component->{name});
76 }
77
78 eval "package $appclass; use Catalyst; __PACKAGE__->setup";
79
80 can_ok( $appclass, 'components');
81
82 my $complist = $appclass->components;
83
84 # the +1 below is for the app class itself
85 is(scalar keys %$complist, 24+1, "Correct number of components loaded");
86
87 foreach (keys %$complist) {
88
89     # Skip the component which happens to be the app itself
90     next if $_ eq $appclass;
91
92     my $instance = $appclass->component($_);
93     isa_ok($instance, $_);
94     can_ok($instance, 'whoami');
95     is($instance->whoami, $_);
96
97     if($_ =~ /^${appclass}::(?:V|View)::(.*)/) {
98         my $moniker = $1;
99         isa_ok($instance, 'Catalyst::View');
100         can_ok($appclass->view($moniker), 'whoami');
101         is($appclass->view($moniker)->whoami, $_);
102     }
103     elsif($_ =~ /^${appclass}::(?:M|Model)::(.*)/) {
104         my $moniker = $1;
105         isa_ok($instance, 'Catalyst::Model');
106         can_ok($appclass->model($moniker), 'whoami');
107         is($appclass->model($moniker)->whoami, $_);
108     }
109     elsif($_ =~ /^${appclass}::(?:C|Controller)::(.*)/) {
110         my $moniker = $1;
111         isa_ok($instance, 'Catalyst::Controller');
112         can_ok($appclass->controller($moniker), 'whoami');
113         is($appclass->controller($moniker)->whoami, $_);
114     }
115     else {
116         die "Something is wrong with this test, this should"
117             . " have been unreachable";
118     }
119 }
120
121 rmtree($libdir);