a few fixes in the test, and the actual method
[catagits/Catalyst-Runtime.git] / t / aggregate / unit_core_container_get_all_component_services.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4
5 # FIXME
6 # the exact return value of get_all_component_services
7 # needs to be reviewed and discussed
8 #
9 # See also note in Catalyst::IOC::Container
10 #
11 # the temporary solution is
12 # {
13 #     class_name => {
14 #         type               => (model|controller|view),
15 #         service            => Catalyst::IOC::*Injection instance, # most likely BlockInjection
16 #         backcompat_service => Catalyst::IOC::ConstructorInjection instance or undef,
17 #     },
18 #     class_name => ...
19 # }
20
21 use Test::More;
22 use Scalar::Util 'blessed';
23 use Test::Moose;
24 use FindBin '$Bin';
25 use lib "$Bin/../lib";
26 use TestAppGetAllComponentServices;
27
28 ok(my $c = TestAppGetAllComponentServices->container, 'get the container');
29 can_ok($c, 'get_all_component_services');
30 ok(my $comp_services = $c->get_all_component_services, 'component services are fetched');
31
32 my $expected = {
33     'TestAppGetAllComponentServices::Controller::Root' => {
34         type               => 'controller',
35         service_isa        => 'Catalyst::IOC::BlockInjection',
36         bcpt_service_isa   => 'Catalyst::IOC::ConstructorInjection',
37     },
38     'TestAppGetAllComponentServices::Model::Foo' => {
39         type               => 'model',
40         service_isa        => 'Catalyst::IOC::BlockInjection',
41         bcpt_service_isa   => 'Catalyst::IOC::ConstructorInjection',
42     },
43     'TestAppGetAllComponentServices::Model::Bar' => {
44         type               => 'model',
45         service_isa        => 'Catalyst::IOC::BlockInjection',
46         bcpt_service_isa   => 'Catalyst::IOC::ConstructorInjection',
47     },
48     'TestAppGetAllComponentServices::Model::Baz' => {
49         type               => 'model',
50         service_isa        => 'Catalyst::IOC::BlockInjection',
51         bcpt_service_isa   => 'Catalyst::IOC::ConstructorInjection',
52     },
53     'TestAppGetAllComponentServices::View::Wibble' => {
54         type               => 'view',
55         service_isa        => 'Catalyst::IOC::BlockInjection',
56         bcpt_service_isa   => 'Catalyst::IOC::ConstructorInjection',
57     },
58     'TestAppGetAllComponentServices::View::Wobble' => {
59         type               => 'view',
60         service_isa        => 'Catalyst::IOC::BlockInjection',
61         bcpt_service_isa   => 'Catalyst::IOC::ConstructorInjection',
62     },
63     'TestAppGetAllComponentServices::View::Wubble' => {
64         type               => 'view',
65         service_isa        => 'Catalyst::IOC::BlockInjection',
66         bcpt_service_isa   => 'Catalyst::IOC::ConstructorInjection',
67     },
68 };
69
70 while (my ($class, $info) = each %$expected) {
71     ok(exists $comp_services->{$class}, "class $class exists in the returned hash");
72     my $received_info = $comp_services->{$class};
73     is($received_info->{type}, $info->{type}, 'type is ok');
74     isa_ok($received_info->{service}, $info->{service_isa}, 'service');
75     isa_ok($received_info->{backcompat_service}, $info->{bcpt_service_isa}, 'backcompat_service');
76 }
77
78 my %singleton_component_classes;
79 can_ok($c, 'get_all_singleton_lifecycle_components');
80 ok(my $singleton_comps = $c->get_all_singleton_lifecycle_components, 'singleton components are fetched');
81 while (my ($class, $comp) = each %$singleton_comps) {
82     ok(blessed $comp, "it's an object"); # it just happens this particular app has all components as objects, so I test it remains true
83     is($class, ref $comp, "of class $class");
84
85     ok(exists $expected->{$class}, "it's one of the existing components");
86     ok(!exists $singleton_component_classes{$class}, "it's the first instance of class $class");
87     $singleton_component_classes{$class} = 1;
88 }
89 is_deeply([ sort keys %$expected ], [ sort keys %singleton_component_classes ], 'all components returned');
90
91 done_testing;