renamed to create setup_component' tests
[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     is_deeply( \@comps, \@loaded_comps, 'all components loaded' );
43 }
44
45 my @controllers = @comps[0..7];
46 my @models      = @comps[8..15];
47 my @views       = @comps[16..23];
48 my $container   = TestAppComponents->container;
49
50 is_deeply(
51     [ sort $container->get_sub_container('controller')->get_service_list ],
52     [ sort @controllers ],
53     'controllers are in the container',
54 );
55
56 is_deeply(
57     [ sort TestAppComponents->controllers ],
58     [ sort @controllers ],
59     'controllers are listed correctly by $c->controllers()',
60 );
61
62 is_deeply(
63     [ sort $container->get_sub_container('model')->get_service_list ],
64     [ sort @models ],
65     'models are in the container',
66 );
67
68 is_deeply(
69     [ sort TestAppComponents->models ],
70     [ sort @models ],
71     'models are listed correctly by $c->models()',
72 );
73
74 is_deeply(
75     [ sort $container->get_sub_container('view')->get_service_list ],
76     [ sort @views ],
77     'views are in the container',
78 );
79
80 is_deeply(
81     [ sort TestAppComponents->views ],
82     [ sort @views ],
83     'views are listed correctly by $c->views()',
84 );
85
86 is_deeply(
87     [ sort keys %{ TestAppComponents->components } ],
88     [ sort @comps ],
89     'all components are in the components accessor'
90 );
91
92 done_testing();
93
94 sub overriden_locate_components {
95     my @comps;
96     push @comps, "TestAppComponents::$_" for qw/
97         C::Bar
98         C::Foo::Bar
99         C::Foo::Foo::Bar
100         C::Foo::Foo::Foo::Bar
101         Controller::Bar::Bar::Bar::Foo
102         Controller::Bar::Bar::Foo
103         Controller::Bar::Foo
104         Controller::Foo
105         M::Bar
106         M::Foo::Bar
107         M::Foo::Foo::Bar
108         M::Foo::Foo::Foo::Bar
109         Model::Bar::Bar::Bar::Foo
110         Model::Bar::Bar::Foo
111         Model::Bar::Foo
112         Model::Foo
113         V::Bar
114         V::Foo::Bar
115         V::Foo::Foo::Bar
116         V::Foo::Foo::Foo::Bar
117         View::Bar::Bar::Bar::Foo
118         View::Bar::Bar::Foo
119         View::Bar::Foo
120         View::Foo
121     /;
122     return @comps;
123 }