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 b04c3a3..ea2d916 100644 (file)
@@ -1,27 +1,64 @@
-use Test::More tests => 51;
+use Test::More;
 use strict;
 use warnings;
 
-use_ok('Catalyst');
+{
+    no warnings 'redefine';
+    *Catalyst::Utils::ensure_class_loaded = sub { };
+}
 
-my @complist =
-  map { "MyMVCTestApp::$_"; }
-  qw/C::Controller M::Model V::View Controller::C Model::M View::V Controller::Model::Dummy::Model Model::Dummy::Model/;
+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/;
+
+our @complist = map { "MyMVCTestApp::$_" } @complist_suffix;
 
+foreach my $comp (@complist) {
+    Moose::Meta::Class->create(
+        $comp =>
+            version => '0.1',
+    );
+}
+our $warnings = 0;
+
+Moose::Meta::Class->create('Some::Test::Object');
+
+Moose::Meta::Class->create(
+    'MyMVCTestApp::Model::Test::Object' =>
+        superclasses => [ 'Catalyst::Model', 'Some::Test::Object' ],
+);
+
+{
     package MyMVCTestApp;
 
     use base qw/Catalyst/;
 
-    __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) } @complist } );
+    no warnings 'redefine';
 
-    my $thingie={};
-    bless $thingie, 'Some::Test::Object';
-    __PACKAGE__->components->{'MyMVCTestApp::Model::Test::Object'} = $thingie;
+    local *Catalyst::IOC::Container::build_locate_components_service = sub {
+        my $self = shift;
 
-    # allow $c->log->warn to work
-    __PACKAGE__->setup_log;
+        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( @{[ 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' );
@@ -65,8 +102,8 @@ is_deeply( [ sort MyMVCTestApp->models ],
     no warnings 'redefine';
     local *Catalyst::Log::warn = sub { $warnings++ };
 
-    like (MyMVCTestApp->view , qr/^MyMVCTestApp\::(V|View)\::/ , 'view() with no defaults returns *something*');
-    ok( $warnings, 'view() w/o a default is random, warnings thrown' );
+    is( MyMVCTestApp->view , undef, 'view() w/o a default is undef' );
+    ok( $warnings, 'warnings thrown for view() w/o a default' );
 }
 
 is ( bless ({stash=>{current_view=>'V'}}, 'MyMVCTestApp')->view , 'MyMVCTestApp::View::V', 'current_view ok');
@@ -82,13 +119,8 @@ is ( bless ({stash=>{current_view_instance=> $view, current_view=>'MyMVCTestApp:
     no warnings 'redefine';
     local *Catalyst::Log::warn = sub { $warnings++ };
 
-    ok( my $model = MyMVCTestApp->model );
-
-    ok( (($model =~ /^MyMVCTestApp\::(M|Model)\::/) ||
-        $model->isa('Some::Test::Object')),
-        'model() with no defaults returns *something*' );
-
-    ok( $warnings, 'model() w/o a default is random, warnings thrown' );
+    is( MyMVCTestApp->model, undef, 'model() w/o a default is undef' );
+    ok( $warnings, 'warnings thrown for model() w/o a default' );
 }
 
 is ( bless ({stash=>{current_model=>'M'}}, 'MyMVCTestApp')->model , 'MyMVCTestApp::Model::M', 'current_model ok');
@@ -99,13 +131,75 @@ is ( bless ({stash=>{current_model_instance=> $model }}, 'MyMVCTestApp')->model
 is ( bless ({stash=>{current_model_instance=> $model, current_model=>'MyMVCTestApp::M::Model' }}, 'MyMVCTestApp')->model , $model,
   'current_model_instance precedes current_model ok');
 
-MyMVCTestApp->config->{default_view} = 'V';
-is ( bless ({stash=>{}}, 'MyMVCTestApp')->view , 'MyMVCTestApp::View::V', 'default_view ok');
-is ( MyMVCTestApp->view , 'MyMVCTestApp::View::V', 'default_view in class method ok');
+{
+    use FindBin '$Bin';
+    use lib "$Bin/../lib";
+
+    use Catalyst::Test 'TestAppController';
+
+    is( get('/foo/test_controller'), 'bar', 'controller() with empty args returns current controller' );
+}
+
+our @complist_default_view =
+    map { "MyMVCTestAppDefaultView::$_" } @complist_suffix;
+
+{
+    package MyMVCTestAppDefaultView;
+
+    use base qw/Catalyst/;
+    no warnings 'redefine';
+
+    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_view;
+            },
+        );
+    };
+    local *Catalyst::Log::warn = sub { $warnings++ };
+
+    __PACKAGE__->config( default_view => 'V' );
+
+    __PACKAGE__->setup;
+}
+
+is( bless ({stash=>{}}, 'MyMVCTestAppDefaultView')->view, 'MyMVCTestAppDefaultView::View::V', 'default_view ok' );
+is( MyMVCTestAppDefaultView->view , 'MyMVCTestAppDefaultView::View::V', 'default_view in class method ok' );
+
+our @complist_default_model =
+    map { "MyMVCTestAppDefaultModel::$_" } @complist_suffix;
 
-MyMVCTestApp->config->{default_model} = 'M';
-is ( bless ({stash=>{}}, 'MyMVCTestApp')->model , 'MyMVCTestApp::Model::M', 'default_model ok');
-is ( MyMVCTestApp->model , 'MyMVCTestApp::Model::M', 'default_model in class method ok');
+{
+    package MyMVCTestAppDefaultModel;
+
+    use base qw/Catalyst/;
+
+    no warnings 'redefine';
+
+    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' );
+
+    __PACKAGE__->setup;
+}
+
+is( bless ({stash=>{}}, 'MyMVCTestAppDefaultModel')->model , 'MyMVCTestAppDefaultModel::Model::M', 'default_model ok' );
+is( MyMVCTestAppDefaultModel->model , 'MyMVCTestAppDefaultModel::Model::M', 'default_model in class method ok' );
 
 # regexp behavior tests
 {
@@ -117,19 +211,33 @@ is ( MyMVCTestApp->model , 'MyMVCTestApp::Model::M', 'default_model in class met
     # 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';
         local *Catalyst::Log::warn = sub { $warnings++ };
 
         # object w/ regexp fallback
-        is_deeply( [ MyMVCTestApp->model( 'Test' ) ], [ MyMVCTestApp->components->{'MyMVCTestApp::Model::Test::Object'} ], 'Object returned' );
+        is( MyMVCTestApp->model( 'Test' ), undef, 'no regexp fallback' );
         ok( $warnings, 'regexp fallback warnings' );
     }
 
-    is_deeply( [ MyMVCTestApp->view('MyMVCTestApp::V::View$') ], [ 'MyMVCTestApp::V::View' ], 'Explicit return ok');
-    is_deeply( [ MyMVCTestApp->controller('MyMVCTestApp::C::Controller$') ], [ 'MyMVCTestApp::C::Controller' ], 'Explicit return ok');
-    is_deeply( [ MyMVCTestApp->model('MyMVCTestApp::M::Model$') ], [ 'MyMVCTestApp::M::Model' ], 'Explicit return ok');
+    is( MyMVCTestApp->view('MyMVCTestApp::V::View$'), undef, 'no regexp fallback');
+
+    is( MyMVCTestApp->controller('MyMVCTestApp::C::Controller$'), undef, 'no regexp fallback');
+
+    is( MyMVCTestApp->model('MyMVCTestApp::M::Model$'), undef, 'no regexp fallback');
 }
 
 {
@@ -175,25 +283,6 @@ is ( MyMVCTestApp->model , 'MyMVCTestApp::Model::M', 'default_model in class met
     my $x = $c->view('V', qw/foo2 bar2/);
     is_deeply($args, [qw/foo2 bar2/], '$c->view args passed to ACCEPT_CONTEXT ok');
 
-    # regexp fallback
-    $c->view('::View::V', qw/foo3 bar3/);
-    is_deeply($args, [qw/foo3 bar3/], 'args passed to ACCEPT_CONTEXT ok');
-
-
-}
-
-{
-    my $warn = '';
-    no warnings 'redefine';
-    local *Catalyst::Log::warn = sub { $warn .= $_[1] };
-
-    is_deeply (MyMVCTestApp->controller('MyMVCTestApp::Controller::C'),
-        MyMVCTestApp->components->{'MyMVCTestApp::Controller::C'},
-        'controller by fully qualified name ok');
-
-    # You probably meant $c->controller('C') instead of $c->controller({'MyMVCTestApp::Controller::C'})
-    my ($suggested_comp_name, $orig_comp_name) = $warn =~ /You probably meant (.*) instead of (.*) /;
-    isnt($suggested_comp_name, $orig_comp_name, 'suggested fix in warning for fully qualified component names makes sense' );
 }
 
 {
@@ -201,13 +290,15 @@ is ( MyMVCTestApp->model , 'MyMVCTestApp::Model::M', 'default_model in class met
 
     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/ } );
 }
 
 {
@@ -223,5 +314,6 @@ is ( MyMVCTestApp->model , 'MyMVCTestApp::Model::M', 'default_model in class met
 
     # try to get nonexisting object w/o regexp fallback
     is( MyApp::WithoutRegexFallback->controller('Foo'), undef, 'no controller Foo found');
-    ok( !$warnings, 'no regexp fallback warnings' );
 }
+
+done_testing;