added a warning to ->components(), and changed tests which called it to be after...
[catagits/Catalyst-Runtime.git] / t / aggregate / unit_core_mvc.t
index 407f1bd..ea2d916 100644 (file)
@@ -2,6 +2,11 @@ use Test::More;
 use strict;
 use warnings;
 
+{
+    no warnings 'redefine';
+    *Catalyst::Utils::ensure_class_loaded = sub { };
+}
+
 use Moose::Meta::Class;
 
 our @complist_suffix = qw/C::Controller M::Model V::View Controller::C Model::M View::V Controller::Model::Dummy::Model Model::Dummy::Model/;
@@ -15,7 +20,6 @@ foreach my $comp (@complist) {
     );
 }
 our $warnings = 0;
-our $loaded   = 0;
 
 Moose::Meta::Class->create('Some::Test::Object');
 
@@ -29,23 +33,33 @@ Moose::Meta::Class->create(
 
     use base qw/Catalyst/;
 
-    sub locate_components {
-        return (@complist, 'MyMVCTestApp::Model::Test::Object');
-    }
-
     no warnings 'redefine';
-    local *Catalyst::Log::warn = sub { $warnings++ };
-    local *Catalyst::Utils::ensure_class_loaded = sub {
-        my $class = shift;
-        $loaded++
-            if Class::MOP::is_class_loaded($class) && $class =~ /^MyMVCTestApp/
+
+    local *Catalyst::IOC::Container::build_locate_components_service = sub {
+        my $self = shift;
+
+        return Bread::Board::BlockInjection->new(
+            lifecycle => 'Singleton',
+            name      => 'locate_components',
+            block     => sub {
+                return [@complist, 'MyMVCTestApp::Model::Test::Object'];
+
+            },
+        );
     };
+    local *Catalyst::Log::warn = sub { $warnings++ };
 
     __PACKAGE__->setup;
 }
 
 ok( $warnings, 'Issues deprecated warnings' );
-is( $loaded, scalar @complist + 1, 'Loaded all components' );
+is( @{[ MyMVCTestApp->component_list ]}, scalar @complist + 1, 'Loaded all components' );
+
+{
+    package MyStringThing;
+
+    use overload '""' => sub { $_[0]->{string} }, fallback => 1;
+}
 
 is( MyMVCTestApp->view('View'), 'MyMVCTestApp::V::View', 'V::View ok' );
 
@@ -133,18 +147,20 @@ our @complist_default_view =
     package MyMVCTestAppDefaultView;
 
     use base qw/Catalyst/;
+    no warnings 'redefine';
 
-    sub locate_components {
-        return @complist_default_view;
-    }
+    local *Catalyst::IOC::Container::build_locate_components_service = sub {
+        my $self = shift;
 
-    no warnings 'redefine';
-    local *Catalyst::Log::warn = sub { $warnings++ };
-    local *Catalyst::Utils::ensure_class_loaded = sub {
-        my $class = shift;
-        $loaded++
-            if Class::MOP::is_class_loaded($class) && $class =~ /^MyMVCTestAppDefaultView/
+        return Bread::Board::BlockInjection->new(
+            lifecycle => 'Singleton',
+            name      => 'locate_components',
+            block     => sub {
+                return \@complist_default_view;
+            },
+        );
     };
+    local *Catalyst::Log::warn = sub { $warnings++ };
 
     __PACKAGE__->config( default_view => 'V' );
 
@@ -162,17 +178,20 @@ our @complist_default_model =
 
     use base qw/Catalyst/;
 
-    sub locate_components {
-        return @complist_default_model;
-    }
-
     no warnings 'redefine';
-    local *Catalyst::Log::warn = sub { $warnings++ };
-    local *Catalyst::Utils::ensure_class_loaded = sub {
-        my $class = shift;
-        $loaded++
-            if Class::MOP::is_class_loaded($class) && $class =~ /^MyMVCTestAppDefaultModel/
+
+    local *Catalyst::IOC::Container::build_locate_components_service = sub {
+        my $self = shift;
+
+        return Bread::Board::BlockInjection->new(
+            lifecycle => 'Singleton',
+            name      => 'locate_components',
+            block     => sub {
+                return \@complist_default_model;
+            },
+        );
     };
+    local *Catalyst::Log::warn = sub { $warnings++ };
 
     __PACKAGE__->config( default_model => 'M' );
 
@@ -192,6 +211,18 @@ is( MyMVCTestAppDefaultModel->model , 'MyMVCTestAppDefaultModel::Model::M', 'def
     # object w/ qr{}
     is_deeply( [ MyMVCTestApp->model( qr{Test} ) ], [ MyMVCTestApp->components->{'MyMVCTestApp::Model::Test::Object'} ], 'Object returned' );
 
+    is_deeply([ MyMVCTestApp->model( bless({ string => 'Model' }, 'MyStringThing') ) ], [ MyMVCTestApp->components->{'MyMVCTestApp::M::Model'} ], 'Explicit model search with overloaded object');
+
+    {
+        my $warnings = 0;
+        no warnings 'redefine';
+        local *Catalyst::Log::warn = sub { $warnings++ };
+
+        # object w/ regexp fallback
+        is_deeply( [ MyMVCTestApp->model( bless({ string => 'Test' }, 'MyStringThing') ) ], [ MyMVCTestApp->components->{'MyMVCTestApp::Model::Test::Object'} ], 'Object returned' );
+        ok( $warnings, 'regexp fallback warnings' );
+    }
+
     {
         my $warnings = 0;
         no warnings 'redefine';
@@ -259,13 +290,15 @@ is( MyMVCTestAppDefaultModel->model , 'MyMVCTestAppDefaultModel::Model::M', 'def
 
     use base qw/Catalyst/;
 
-    __PACKAGE__->config( { disable_component_resolution_regex_fallback => 1 } );
+    no warnings 'redefine';
 
-    __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) }
-        qw/MyApp::WithoutRegexFallback::Controller::Another::Foo/ } );
+    __PACKAGE__->config( { disable_component_resolution_regex_fallback => 1 } );
 
     # allow $c->log->warn to work
     __PACKAGE__->setup_log;
+
+    __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) }
+        qw/MyApp::WithoutRegexFallback::Controller::Another::Foo/ } );
 }
 
 {