auto/forward/detach tests for Chained
Matt S Trout [Thu, 22 Jun 2006 14:52:57 +0000 (14:52 +0000)]
r10132@cain (orig r4425):  phaylon | 2006-06-20 20:02:11 +0000

t/lib/TestApp/Controller/Action/Chained.pm
t/lib/TestApp/Controller/Action/Chained/Auto.pm [new file with mode: 0644]
t/lib/TestApp/Controller/Action/Chained/Auto/Bar.pm [new file with mode: 0644]
t/lib/TestApp/Controller/Action/Chained/Auto/Detach.pm [new file with mode: 0644]
t/lib/TestApp/Controller/Action/Chained/Auto/Foo.pm [new file with mode: 0644]
t/lib/TestApp/Controller/Action/Chained/Auto/Forward.pm [new file with mode: 0644]
t/live_component_controller_action_chained.t

index 01fb363..eaaf3e9 100644 (file)
@@ -106,6 +106,32 @@ sub rootdef :Chained :PathPart('chained/rootdef') :Args(1) { }
 #
 sub parentchain :Chained('/') :PathPart('chained/parentchain') :Captures(1) { }
 
+#
+#   This is just for a test that a loose end is not callable
+#
+sub loose :Chained :PathPart('chained/loose') Captures(1) { }
+
+#
+#   Forwarding out of the middle of a chain.
+#
+sub chain_fw_a :Chained :PathPart('chained/chain_fw') :Captures(1) {
+    $_[1]->forward( '/action/chained/fw_dt_target' );
+}
+sub chain_fw_b :Chained('chain_fw_a') :PathPart('end') :Args(1) { }
+
+#
+#   Detaching out of the middle of a chain.
+#
+sub chain_dt_a :Chained :PathPart('chained/chain_dt') :Captures(1) {
+    $_[1]->detach( '/action/chained/fw_dt_target' );
+}
+sub chain_dt_b :Chained('chain_dt_a') :PathPart('end') :Args(1) { }
+
+#
+#   Target for former forward and chain tests.
+#
+sub fw_dt_target :Private { }
+
 sub end :Private {
   my ($self, $c) = @_;
   my $out = join('; ', map { join(', ', @$_) }
diff --git a/t/lib/TestApp/Controller/Action/Chained/Auto.pm b/t/lib/TestApp/Controller/Action/Chained/Auto.pm
new file mode 100644 (file)
index 0000000..d8e42df
--- /dev/null
@@ -0,0 +1,33 @@
+package TestApp::Controller::Action::Chained::Auto;
+use warnings;
+use strict;
+
+use base qw( Catalyst::Controller );
+
+#
+#   Provided for sub-auto tests. This just always returns true.
+#
+sub auto    : Private { 1 }
+
+#
+#   Simple chains with auto actions returning 1 and 0
+#
+sub foo     : Chained PathPart('chained/autochain1') Captures(1) { }
+sub bar     : Chained PathPart('chained/autochain2') Captures(1) { }
+
+#
+#   Detaching out of an auto action.
+#
+sub dt1     : Chained PathPart('chained/auto_detach') Captures(1) { }
+
+#
+#   Forwarding out of an auto action.
+#
+sub fw1     : Chained PathPart('chained/auto_forward') Captures(1) { }
+
+#
+#   Target for dispatch and forward tests.
+#
+sub fw3     : Private { }
+
+1;
diff --git a/t/lib/TestApp/Controller/Action/Chained/Auto/Bar.pm b/t/lib/TestApp/Controller/Action/Chained/Auto/Bar.pm
new file mode 100644 (file)
index 0000000..45ec400
--- /dev/null
@@ -0,0 +1,16 @@
+package TestApp::Controller::Action::Chained::Auto::Bar;
+use warnings;
+use strict;
+
+use base qw( Catalyst::Controller );
+
+#
+#   Test chain reaction if auto action returns 0.
+#
+sub auto        : Private { 0 }
+
+sub barend      : Chained('.') Args(1) { }
+
+sub crossloose  : Chained PathPart('chained/auto_cross') Captures(1) { }
+
+1;
diff --git a/t/lib/TestApp/Controller/Action/Chained/Auto/Detach.pm b/t/lib/TestApp/Controller/Action/Chained/Auto/Detach.pm
new file mode 100644 (file)
index 0000000..752ced4
--- /dev/null
@@ -0,0 +1,18 @@
+package TestApp::Controller::Action::Chained::Auto::Detach;
+use warnings;
+use strict;
+
+use base qw( Catalyst::Controller );
+
+#
+#   For testing behaviour of a detaching auto action in a chain.
+#
+sub auto    : Private {
+    my ( $self, $c ) = @_;
+    $c->detach( '/action/chained/auto/fw3' );
+    return 1;
+}
+
+sub detachend  : Chained('/action/chained/auto/dt1') Args(1) { }
+
+1;
diff --git a/t/lib/TestApp/Controller/Action/Chained/Auto/Foo.pm b/t/lib/TestApp/Controller/Action/Chained/Auto/Foo.pm
new file mode 100644 (file)
index 0000000..cad104e
--- /dev/null
@@ -0,0 +1,16 @@
+package TestApp::Controller::Action::Chained::Auto::Foo;
+use warnings;
+use strict;
+
+use base qw( Catalyst::Controller );
+
+#
+#   Test chain reaction if auto action returns 1.
+#
+sub auto        : Private { 1 }
+
+sub fooend      : Chained('.') Args(1) { }
+
+sub crossend    : Chained('/action/chained/auto/bar/crossloose') Args(1) { }
+
+1;
diff --git a/t/lib/TestApp/Controller/Action/Chained/Auto/Forward.pm b/t/lib/TestApp/Controller/Action/Chained/Auto/Forward.pm
new file mode 100644 (file)
index 0000000..982439c
--- /dev/null
@@ -0,0 +1,18 @@
+package TestApp::Controller::Action::Chained::Auto::Forward;
+use warnings;
+use strict;
+
+use base qw( Catalyst::Controller );
+
+#
+#   For testing behaviour of a forwarding auto action in a chain.
+#
+sub auto    : Private {
+    my ( $self, $c ) = @_;
+    $c->forward( '/action/chained/auto/fw3' );
+    return 1;
+}
+
+sub forwardend  : Chained('/action/chained/auto/fw1') Args(1) { }
+
+1;
index 0a8d32d..d51ba0f 100644 (file)
@@ -10,7 +10,7 @@ our $iters;
 
 BEGIN { $iters = $ENV{CAT_BENCH_ITERS} || 2; }
 
-use Test::More tests => 72*$iters;
+use Test::More tests => 96*$iters;
 use Catalyst::Test 'TestApp';
 
 if ( $ENV{CAT_BENCHMARK} ) {
@@ -504,4 +504,167 @@ sub run_tests {
             $expected, 'Executed actions' );
         is( $response->content, '1; 2', 'Content OK' );
     }
+
+    #
+    #   Test behaviour of auto actions returning '1' for the chain.
+    #
+    {
+        my @expected = qw[
+          TestApp::Controller::Action::Chained->begin
+          TestApp::Controller::Action::Chained::Auto->auto
+          TestApp::Controller::Action::Chained::Auto::Foo->auto
+          TestApp::Controller::Action::Chained::Auto->foo
+          TestApp::Controller::Action::Chained::Auto::Foo->fooend
+          TestApp::Controller::Action::Chained->end
+        ];
+
+        my $expected = join( ", ", @expected );
+
+        ok( my $response = request('http://localhost/chained/autochain1/1/fooend/2'),
+            "Behaviour when auto returns 1 correct" );
+        is( $response->header('X-Catalyst-Executed'),
+            $expected, 'Executed actions' );
+        is( $response->content, '1; 2', 'Content OK' );
+    }
+
+    #
+    #   Test behaviour of auto actions returning '0' for the chain.
+    #
+    {
+        my @expected = qw[
+          TestApp::Controller::Action::Chained->begin
+          TestApp::Controller::Action::Chained::Auto->auto
+          TestApp::Controller::Action::Chained::Auto::Bar->auto
+          TestApp::Controller::Action::Chained->end
+        ];
+
+        my $expected = join( ", ", @expected );
+
+        ok( my $response = request('http://localhost/chained/autochain2/1/barend/2'),
+            "Behaviour when auto returns 0 correct" );
+        is( $response->header('X-Catalyst-Executed'),
+            $expected, 'Executed actions' );
+        is( $response->content, '1; 2', 'Content OK' );
+    }
+
+    #
+    #   Test what auto actions are run when namespaces are changed
+    #   horizontally.
+    #
+    {
+        my @expected = qw[
+          TestApp::Controller::Action::Chained->begin
+          TestApp::Controller::Action::Chained::Auto->auto
+          TestApp::Controller::Action::Chained::Auto::Foo->auto
+          TestApp::Controller::Action::Chained::Auto::Bar->crossloose
+          TestApp::Controller::Action::Chained::Auto::Foo->crossend
+          TestApp::Controller::Action::Chained->end
+        ];
+
+        my $expected = join( ", ", @expected );
+
+        ok( my $response = request('http://localhost/chained/auto_cross/1/crossend/2'),
+            "Correct auto actions are run on cross controller dispatch" );
+        is( $response->header('X-Catalyst-Executed'),
+            $expected, 'Executed actions' );
+        is( $response->content, '1; 2', 'Content OK' );
+    }
+
+    #
+    #   Test forwarding from auto action in chain dispatch.
+    #
+    {
+        my @expected = qw[
+          TestApp::Controller::Action::Chained->begin
+          TestApp::Controller::Action::Chained::Auto->auto
+          TestApp::Controller::Action::Chained::Auto::Forward->auto
+          TestApp::Controller::Action::Chained::Auto->fw3
+          TestApp::Controller::Action::Chained::Auto->fw1
+          TestApp::Controller::Action::Chained::Auto::Forward->forwardend
+          TestApp::Controller::Action::Chained->end
+        ];
+
+        my $expected = join( ", ", @expected );
+
+        ok( my $response = request('http://localhost/chained/auto_forward/1/forwardend/2'),
+            "Forwarding out of auto in chain" );
+        is( $response->header('X-Catalyst-Executed'),
+            $expected, 'Executed actions' );
+        is( $response->content, '1; 2', 'Content OK' );
+    }
+
+    #
+    #   Detaching out of the auto action of a chain.
+    #
+    {
+        my @expected = qw[
+          TestApp::Controller::Action::Chained->begin
+          TestApp::Controller::Action::Chained::Auto->auto
+          TestApp::Controller::Action::Chained::Auto::Detach->auto
+          TestApp::Controller::Action::Chained::Auto->fw3
+          TestApp::Controller::Action::Chained->end
+        ];
+
+        my $expected = join( ", ", @expected );
+
+        ok( my $response = request('http://localhost/chained/auto_detach/1/detachend/2'),
+            "Detaching out of auto in chain" );
+        is( $response->header('X-Catalyst-Executed'),
+            $expected, 'Executed actions' );
+        is( $response->content, '1; 2', 'Content OK' );
+    }
+
+    #
+    #   Test forwarding from auto action in chain dispatch.
+    #
+    {
+        my $expected = undef;
+
+        ok( my $response = request('http://localhost/chained/loose/23'),
+            "Loose end is not callable" );
+        is( $response->header('X-Catalyst-Executed'),
+            $expected, 'Executed actions' );
+        is( $response->header('Status'), 500, 'Status OK' );
+    }
+
+    #
+    #   Test forwarding out of a chain.
+    #
+    {
+        my @expected = qw[
+          TestApp::Controller::Action::Chained->begin
+          TestApp::Controller::Action::Chained->chain_fw_a
+          TestApp::Controller::Action::Chained->fw_dt_target
+          TestApp::Controller::Action::Chained->chain_fw_b
+          TestApp::Controller::Action::Chained->end
+        ];
+
+        my $expected = join( ", ", @expected );
+
+        ok( my $response = request('http://localhost/chained/chain_fw/1/end/2'),
+            "Forwarding out a chain" );
+        is( $response->header('X-Catalyst-Executed'),
+            $expected, 'Executed actions' );
+        is( $response->content, '1; 2', 'Content OK' );
+    }
+
+    #
+    #   Test detaching out of a chain.
+    #
+    {
+        my @expected = qw[
+          TestApp::Controller::Action::Chained->begin
+          TestApp::Controller::Action::Chained->chain_dt_a
+          TestApp::Controller::Action::Chained->fw_dt_target
+          TestApp::Controller::Action::Chained->end
+        ];
+
+        my $expected = join( ", ", @expected );
+
+        ok( my $response = request('http://localhost/chained/chain_dt/1/end/2'),
+            "Forwarding out a chain" );
+        is( $response->header('X-Catalyst-Executed'),
+            $expected, 'Executed actions' );
+        is( $response->content, '1; 2', 'Content OK' );
+    }
 }