Ensure ACCEPT_CONTEXT is called for results from component()
Brian Cassidy [Fri, 18 Jul 2008 12:29:44 +0000 (12:29 +0000)]
Changes
lib/Catalyst.pm
t/unit_core_component.t
t/unit_core_mvc.t

diff --git a/Changes b/Changes
index e3660d2..046589a 100644 (file)
--- a/Changes
+++ b/Changes
@@ -4,6 +4,7 @@
         - Fix regressions for regexp fallback in model(), view() and controller()
         - Added the supplied argument to the regexp fallback warning for easier
           debugging
+        - Ensure ACCEPT_CONTEXT is called for results from component()
 
 5.7099_02 2008-07-16 19:10:00
         - Added PathPrefix attribute
index ff5b3cd..fe7a6d2 100644 (file)
@@ -683,11 +683,13 @@ sub component {
 
         if( !ref $name ) {
             # is it the exact name?
-            return $comps->{ $name } if exists $comps->{ $name };
+            return $c->_filter_component( $comps->{ $name }, @args )
+                       if exists $comps->{ $name };
 
             # perhaps we just omitted "MyApp"?
             my $composed = ( ref $c || $c ) . "::${name}";
-            return $comps->{ $composed } if exists $comps->{ $composed };
+            return $c->_filter_component( $comps->{ $composed }, @args )
+                       if exists $comps->{ $composed };
 
             # search all of the models, views and controllers
             my( $comp ) = $c->_comp_search_prefixes( $name, qw/Model M Controller C View V/ );
@@ -698,12 +700,13 @@ sub component {
         my $query = ref $name ? $name : qr{$name}i;
 
         my @result = grep { m{$query} } keys %{ $c->components };
-        return @result if ref $name;
+        return map { $c->_filter_component( $_, @args ) } @result if ref $name;
 
         if( $result[ 0 ] ) {
+            $c->log->warn( qq(Found results for "${name}" using regexp fallback.) );
             $c->log->warn( 'Relying on the regexp fallback behavior for component resolution' );
             $c->log->warn( 'is unreliable and unsafe. You have been warned' );
-            return $result[ 0 ];
+            return $c->_filter_component( $result[ 0 ], @args );
         }
 
         # I would expect to return an empty list here, but that breaks back-compat
index a4cb067..250960a 100644 (file)
@@ -1,4 +1,4 @@
-use Test::More tests => 14;
+use Test::More tests => 22;
 use strict;
 use warnings;
 
@@ -23,16 +23,6 @@ is(MyApp->comp('C::Controller'), 'MyApp::C::Controller', 'Two-part return ok');
 
 is(MyApp->comp('Model'), 'MyApp::M::Model', 'Single part return ok');
 
-# regexp fallback
-{
-    my $warnings = 0;
-    no warnings 'redefine';
-    local *Catalyst::Log::warn = sub { $warnings++ };
-
-    is(MyApp->comp('::M::'), 'MyApp::M::Model', 'Regex return ok');
-    ok( $warnings, 'regexp fallback for comp() warns' );
-}
-
 is_deeply([ MyApp->comp() ], \@complist, 'Empty return ok');
 
 # Is this desired behaviour?
@@ -44,6 +34,27 @@ is_deeply([ MyApp->comp('Foo') ], \@complist, 'Fallthrough return ok');
     is_deeply( [ MyApp->comp('MyApp::V::View$') ], [ 'MyApp::V::View' ], 'Explicit return ok');
     is_deeply( [ MyApp->comp('MyApp::C::Controller$') ], [ 'MyApp::C::Controller' ], 'Explicit return ok');
     is_deeply( [ MyApp->comp('MyApp::M::Model$') ], [ 'MyApp::M::Model' ], 'Explicit return ok');
+
+    # a couple other varieties for regexp fallback
+    is_deeply( [ MyApp->comp('M::Model') ], [ 'MyApp::M::Model' ], 'Explicit return ok');
+
+    {
+        my $warnings = 0;
+        no warnings 'redefine';
+        local *Catalyst::Log::warn = sub { $warnings++ };
+
+        is_deeply( [ MyApp->comp('::M::Model') ], [ 'MyApp::M::Model' ], 'Explicit return ok');
+        ok( $warnings, 'regexp fallback warnings' );
+
+        $warnings = 0;
+        is_deeply( [ MyApp->comp('Mode') ], [ 'MyApp::M::Model' ], 'Explicit return ok');
+        ok( $warnings, 'regexp fallback warnings' );
+
+        $warnings = 0;
+        is(MyApp->comp('::M::'), 'MyApp::M::Model', 'Regex return ok');
+        ok( $warnings, 'regexp fallback for comp() warns' );
+    }
+
 }
 
 # multiple returns
@@ -57,3 +68,21 @@ is_deeply([ MyApp->comp('Foo') ], \@complist, 'Fallthrough return ok');
     is_deeply( scalar MyApp->comp( qr{DNE} ), 0, 'no results for failed search' );
 }
 
+
+#checking @args passed to ACCEPT_CONTEXT
+{
+    my $args;
+
+    no warnings; 
+    *MyApp::M::Model::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args};
+
+    MyApp->component('MyApp::M::Model', qw/foo bar/);
+    is_deeply($args, [qw/foo bar/], 'args passed to ACCEPT_CONTEXT ok');
+
+    MyApp->component('M::Model', qw/foo2 bar2/);
+    is_deeply($args, [qw/foo2 bar2/], 'args passed to ACCEPT_CONTEXT ok');
+
+    MyApp->component('Mode', qw/foo3 bar3/);
+    is_deeply($args, [qw/foo3 bar3/], 'args passed to ACCEPT_CONTEXT ok');
+} 
+
index 9d35067..4c83859 100644 (file)
@@ -108,7 +108,7 @@ is ( MyApp->model , 'MyApp::Model::M', 'default_model in class method ok');
     is_deeply( [ MyApp->controller( qr{Dummy\::Model$} ) ], [ 'MyApp::Controller::Model::Dummy::Model' ], 'regexp controller ok' );
     is_deeply( [ MyApp->model( qr{Dum{2}y} ) ], [ 'MyApp::Model::Dummy::Model' ], 'regexp model ok' );
     
-    # ACCEPT_CONTEXT w/ qr{}
+    # object w/ qr{}
     is_deeply( [ MyApp->model( qr{Test} ) ], [ MyApp->components->{'MyApp::Model::Test::Object'} ], 'Object returned' );
 
     {
@@ -116,7 +116,7 @@ is ( MyApp->model , 'MyApp::Model::M', 'default_model in class method ok');
         no warnings 'redefine';
         local *Catalyst::Log::warn = sub { $warnings++ };
 
-        # ACCEPT_CONTEXT w/ regexp fallback
+        # object w/ regexp fallback
         is_deeply( [ MyApp->model( 'Test' ) ], [ MyApp->components->{'MyApp::Model::Test::Object'} ], 'Object returned' );
         ok( $warnings, 'regexp fallback warnings' );
     }