ffbbb99656ae8f59dbb94ac68363ec88a25f9341
[catagits/Catalyst-Runtime.git] / t / aggregate / unit_core_component_setup_components.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use Moose::Meta::Class;
5
6 Moose::Meta::Class->create( TestAppComponents => (
7     superclasses => ['Catalyst'],
8     methods      => {
9         locate_components => \&overriden_locate_components,
10     },
11 ));
12
13 TestAppComponents->components( {} );
14
15 # this is so TestAppComponents->container will work
16 TestAppComponents->setup_config;
17
18 # this is so TestAppComponents->log->warn will work
19 TestAppComponents->setup_log;
20
21 my @comps = TestAppComponents->locate_components;
22
23 for my $component (@comps) {
24     Moose::Meta::Class->create( $component => (
25         superclasses => ['Catalyst::Component'],
26     ));
27 }
28
29 {
30     my @loaded_comps;
31     my $warnings = 0;
32
33     no warnings 'redefine', 'once';
34
35     local *Catalyst::Log::warn = sub { $warnings++ };
36     local *Catalyst::Utils::ensure_class_loaded = sub { my $class = shift; push @loaded_comps, $class; };
37
38     eval { TestAppComponents->setup_components };
39
40     ok( !$@, "setup_components doesnt die" );
41     ok( $warnings, "it warns about deprecated names" );
42
43     # FIXME - do I need the original sort in locate_components service?
44     is_deeply( [ sort @comps ], [ sort @loaded_comps ], 'all components loaded' );
45 }
46
47 my @comps_copy  = @comps;
48
49 my @controllers = map { s/^TestAppComponents::(C|Controller):://; $_; } @comps_copy[0..7];
50 my @models      = map { s/^TestAppComponents::(M|Model):://; $_; }      @comps_copy[8..15];
51 my @views       = map { s/^TestAppComponents::(V|View):://; $_; }       @comps_copy[16..23];
52 my $container   = TestAppComponents->container;
53
54 is_deeply(
55     [ sort $container->get_sub_container('controller')->get_service_list ],
56     [ sort @controllers ],
57     'controllers are in the container',
58 );
59
60 is_deeply(
61     [ sort TestAppComponents->controllers ],
62     [ sort @controllers ],
63     'controllers are listed correctly by $c->controllers()',
64 );
65
66 is_deeply(
67     [ sort $container->get_sub_container('model')->get_service_list ],
68     [ sort @models ],
69     'models are in the container',
70 );
71
72 is_deeply(
73     [ sort TestAppComponents->models ],
74     [ sort @models ],
75     'models are listed correctly by $c->models()',
76 );
77
78 is_deeply(
79     [ sort $container->get_sub_container('view')->get_service_list ],
80     [ sort @views ],
81     'views are in the container',
82 );
83
84 is_deeply(
85     [ sort TestAppComponents->views ],
86     [ sort @views ],
87     'views are listed correctly by $c->views()',
88 );
89
90 is_deeply(
91     [ sort keys %{ TestAppComponents->components } ],
92     [ sort @comps ],
93     'all components are in the components accessor'
94 );
95
96 done_testing();
97
98 sub overriden_locate_components {
99     my @comps;
100     push @comps, "TestAppComponents::$_" for qw/
101         C::Bar
102         C::Foo::Bar
103         C::Foo::Foo::Bar
104         C::Foo::Foo::Foo::Bar
105         Controller::Bar::Bar::Bar::Foo
106         Controller::Bar::Bar::Foo
107         Controller::Bar::Foo
108         Controller::Foo
109         M::Bar
110         M::Foo::Bar
111         M::Foo::Foo::Bar
112         M::Foo::Foo::Foo::Bar
113         Model::Bar::Bar::Bar::Foo
114         Model::Bar::Bar::Foo
115         Model::Bar::Foo
116         Model::Foo
117         V::Bar
118         V::Foo::Bar
119         V::Foo::Foo::Bar
120         V::Foo::Foo::Foo::Bar
121         View::Bar::Bar::Bar::Foo
122         View::Bar::Bar::Foo
123         View::Bar::Foo
124         View::Foo
125     /;
126     return @comps;
127 }