tests and patch for Controller to accept action_args from constructor
skaufman [Mon, 16 Apr 2012 17:22:58 +0000 (17:22 +0000)]
lib/Catalyst/Controller.pm
t/aggregate/live_component_controller_action_action.t
t/lib/TestApp.pm
t/lib/TestApp/Action/TestActionArgsFromConstructor.pm [new file with mode: 0644]
t/lib/TestApp/Controller/Action/Action.pm

index 1442649..a6bba64 100644 (file)
@@ -36,6 +36,8 @@ has actions =>
      init_arg => undef,
     );
 
+has action_args => (is => 'ro');
+
 # ->config(actions => { '*' => ...
 has _all_actions_attributes => (
     is       => 'ro',
@@ -279,7 +281,11 @@ sub create_action {
     my %args = @_;
 
     my $class = $self->action_class(%args);
-    my $action_args = $self->config->{action_args};
+    my $action_args = (
+        ref($self)
+            ? $self->action_args
+            : $self->config->{action_args}
+    );
 
     my %extra_args = (
         %{ $action_args->{'*'}           || {} },
index bc2038e..eebfe48 100644 (file)
@@ -193,6 +193,25 @@ sub run_tests {
         is_deeply $action->attributes->{extra_attribute}, [13];
         is_deeply $action->attributes->{another_extra_attribute}, ['foo'];
     }
+    {
+        ok( my $response = request('http://localhost/action_action_nine'),
+            'Request' );
+        ok( $response->is_success, 'Response Successful 2xx' );
+        is( $response->content_type, 'text/plain', 'Response Content-Type' );
+        is( $response->header('X-Catalyst-Action'),
+            'action_action_nine', 'Test Action' );
+        is(
+            $response->header('X-Test-Class'),
+            'TestApp::Controller::Action::Action',
+            'Test Class'
+        );
+        is( $response->header('X-TestExtraArgsAction'), '42,13', 'Extra args get passed to action constructor' );
+        like(
+            $response->content,
+            qr/^bless\( .* 'Catalyst::Request' \)$/s,
+            'Content is a serialized Catalyst::Request'
+        );
+    }
 }
 
 done_testing;
index 3bd3763..25203e1 100644 (file)
@@ -1,5 +1,4 @@
 package TestApp;
-
 use strict;
 use Catalyst qw/
     Test::MangleDollarUnderScore
@@ -42,7 +41,16 @@ has 'my_greeting_obj_lazy' => (
 
 our $VERSION = '0.01';
 
-TestApp->config( name => 'TestApp', root => '/some/dir', use_request_uri_for_path => 1 );
+TestApp->config( 
+    name => 'TestApp', 
+    root => '/some/dir', 
+    use_request_uri_for_path => 1, 
+    'Controller::Action::Action' => {
+        action_args => {
+            action_action_nine => { another_extra_arg => 13 }
+        }
+    }
+);
 
 # Test bug found when re-adjusting the metaclass compat code in Moose
 # in 292360. Test added to Moose in 4b760d6, but leave this attribute
diff --git a/t/lib/TestApp/Action/TestActionArgsFromConstructor.pm b/t/lib/TestApp/Action/TestActionArgsFromConstructor.pm
new file mode 100644 (file)
index 0000000..67f8a13
--- /dev/null
@@ -0,0 +1,18 @@
+package TestApp::Action::TestActionArgsFromConstructor;
+
+use Moose;
+use namespace::autoclean;
+
+extends 'Catalyst::Action';
+
+has [qw/extra_arg another_extra_arg/] => ( is => 'ro' );
+
+after execute => sub {
+    my ($self, $controller, $ctx) = @_;
+    $ctx->response->header('X-TestExtraArgsAction' => join q{,} => $self->extra_arg, $self->another_extra_arg);
+};
+
+__PACKAGE__->meta->make_immutable;
+
+1;
+
index 6cee5f5..515fb2a 100644 (file)
@@ -58,4 +58,8 @@ sub action_action_eight : Global  {
     $c->forward('TestApp::View::Dump::Action');
 }
 
+sub action_action_nine : Global : ActionClass('~TestActionArgsFromConstructor') {
+    my ( $self, $c ) = @_;
+    $c->forward('TestApp::View::Dump::Request');
+}
 1;