a few fixes in the test, and the actual method
André Walker [Sun, 22 Jul 2012 20:03:29 +0000 (17:03 -0300)]
lib/Catalyst/IOC/Container.pm
t/aggregate/unit_core_container_get_all_component_services.t

index 3ad90a2..8a49438 100644 (file)
@@ -710,6 +710,34 @@ sub get_all_component_services {
     return lock_hash %components;
 }
 
+sub get_all_singleton_lifecycle_components {
+    my $self = shift;
+
+    my %components;
+    my $components_container = $self->get_sub_container('component');
+
+    foreach my $type (qw/model view controller /) {
+        my $container = $self->get_sub_container($type);
+
+        for my $component ($container->get_service_list) {
+            my $comp_service = $container->get_service($component);
+
+            my $key       = $comp_service->catalyst_component_name;
+            my $lifecycle = $comp_service->lifecycle;
+            my $comp_name = "${type}_${component}";
+
+            if (defined $lifecycle && $lifecycle eq 'Singleton') {
+                $components{$key} = $comp_service->get;
+            }
+            elsif ($components_container->has_service($comp_name)) {
+                $components{$key} = $components_container->get_service($comp_name)->get;
+            }
+        }
+    }
+
+    return lock_hash %components;
+}
+
 sub get_all_components {
     my ($self, $class) = @_;
     my %components;
index b97c57f..ffe172b 100644 (file)
@@ -19,6 +19,7 @@ use warnings;
 # }
 
 use Test::More;
+use Scalar::Util 'blessed';
 use Test::Moose;
 use FindBin '$Bin';
 use lib "$Bin/../lib";
@@ -76,15 +77,15 @@ while (my ($class, $info) = each %$expected) {
 
 my %singleton_component_classes;
 can_ok($c, 'get_all_singleton_lifecycle_components');
-ok(my @singleton_comps = $c->get_all_singleton_lifecycle_components, 'singleton components are fetched');
-foreach my $comp (@singleton_comps) {
-    blessed_ok($comp, "it's an object");
-    my $class = ref $comp;
+ok(my $singleton_comps = $c->get_all_singleton_lifecycle_components, 'singleton components are fetched');
+while (my ($class, $comp) = each %$singleton_comps) {
+    ok(blessed $comp, "it's an object"); # it just happens this particular app has all components as objects, so I test it remains true
+    is($class, ref $comp, "of class $class");
 
     ok(exists $expected->{$class}, "it's one of the existing components");
     ok(!exists $singleton_component_classes{$class}, "it's the first instance of class $class");
     $singleton_component_classes{$class} = 1;
 }
-is_deeply([ keys %$expected ], [ keys %singleton_component_classes ]);
+is_deeply([ sort keys %$expected ], [ sort keys %singleton_component_classes ], 'all components returned');
 
 done_testing;