tests for calling controller() with no args
[catagits/Catalyst-Runtime.git] / t / aggregate / unit_core_mvc.t
index 876d31a..fa1683c 100644 (file)
@@ -1,29 +1,54 @@
-use Test::More tests => 46;
+use Test::More;
 use strict;
 use warnings;
 
+use Moose::Meta::Class;
+
 use_ok('Catalyst');
 
-my @complist =
+our @complist =
   map { "MyMVCTestApp::$_"; }
   qw/C::Controller M::Model V::View Controller::C Model::M View::V Controller::Model::Dummy::Model Model::Dummy::Model/;
 
-{
+foreach my $comp (@complist) {
+    Moose::Meta::Class->create(
+        $comp =>
+            version => '0.1',
+    );
+}
+our $warnings = 0;
+our $loaded   = 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 } );
-
-    my $thingie={};
-    bless $thingie, 'Some::Test::Object';
-    __PACKAGE__->components->{'MyMVCTestApp::Model::Test::Object'} = $thingie;
+    sub locate_components {
+        return (@::complist, 'MyMVCTestApp::Model::Test::Object');
+    }
 
-    # allow $c->log->warn to work
-    __PACKAGE__->setup_log;
+    no warnings 'redefine';
+    *Catalyst::Log::warn = sub { $::warnings++ };
+    *Catalyst::Utils::ensure_class_loaded = sub {
+        my $class = shift;
+        $::loaded++
+            if Class::MOP::is_class_loaded($class) && $class =~ /^MyMVCTestApp/
+    };
+
+    __PACKAGE__->setup;
 }
 
+ok( $warnings, 'Issues deprecated warnings' );
+is( $loaded, scalar @complist + 1, 'Loaded all components' );
+
 is( MyMVCTestApp->view('View'), 'MyMVCTestApp::V::View', 'V::View ok' );
 
 is( MyMVCTestApp->controller('Controller'),
@@ -61,9 +86,7 @@ is_deeply( [ sort MyMVCTestApp->models ],
            'models ok');
 
 {
-    my $warnings = 0;
-    no warnings 'redefine';
-    local *Catalyst::Log::warn = sub { $warnings++ };
+    $warnings = 0;
 
     like (MyMVCTestApp->view , qr/^MyMVCTestApp\::(V|View)\::/ , 'view() with no defaults returns *something*');
     ok( $warnings, 'view() w/o a default is random, warnings thrown' );
@@ -78,9 +101,7 @@ is ( bless ({stash=>{current_view_instance=> $view, current_view=>'MyMVCTestApp:
   'current_view_instance precedes current_view ok');
 
 {
-    my $warnings = 0;
-    no warnings 'redefine';
-    local *Catalyst::Log::warn = sub { $warnings++ };
+    $warnings = 0;
 
     ok( my $model = MyMVCTestApp->model );
 
@@ -99,6 +120,15 @@ 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');
 
+{
+    use FindBin '$Bin';
+    use lib "$Bin/../lib";
+
+    use Catalyst::Test 'TestAppController';
+
+    is( get('/foo/test_controller'), 'bar', 'controller() with empty args returns current controller' );
+}
+
 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');
@@ -118,18 +148,18 @@ is ( MyMVCTestApp->model , 'MyMVCTestApp::Model::M', 'default_model in class met
     is_deeply( [ MyMVCTestApp->model( qr{Test} ) ], [ MyMVCTestApp->components->{'MyMVCTestApp::Model::Test::Object'} ], 'Object returned' );
 
     {
-        my $warnings = 0;
-        no warnings 'redefine';
-        local *Catalyst::Log::warn = sub { $warnings++ };
+        $warnings = 0;
 
         # 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,9 +205,35 @@ 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');
+}
+
+{
+    package MyApp::WithoutRegexFallback;
+
+    use base qw/Catalyst/;
+
+    __PACKAGE__->config( { disable_component_resolution_regex_fallback => 1 } );
+
+    __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) }
+        qw/MyApp::WithoutRegexFallback::Controller::Another::Foo/ } );
+
+    # allow $c->log->warn to work
+    __PACKAGE__->setup_log;
+}
 
+{
+    # test if non-regex component retrieval still works
+    is( MyApp::WithoutRegexFallback->controller('Another::Foo'),
+        'MyApp::WithoutRegexFallback::Controller::Another::Foo', 'controller Another::Foo found');
+}
 
+{
+    my $warnings = 0;
+    no warnings 'redefine';
+    local *Catalyst::Log::warn = sub { $warnings++ };
+
+    # try to get nonexisting object w/o regexp fallback
+    is( MyApp::WithoutRegexFallback->controller('Foo'), undef, 'no controller Foo found');
 }
+
+done_testing;