From: Hans Dieter Pearcey Date: Sat, 28 Mar 2009 04:49:21 +0000 (+0000) Subject: add uri_for_action and switch tests to use it X-Git-Tag: 5.80001~43^2~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=833b385e3d6454bd249acf859869bf1792109b82 add uri_for_action and switch tests to use it --- diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index f8cc836..005df7a 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -1224,6 +1224,35 @@ sub uri_for { $res; } +=head2 $c->uri_for_action( $path, \@captures?, @args?, \%query_values? ) + +=head2 $c->uri_for_action( $action, \@captures?, @args?, \%query_values? ) + +=over + +=item $path + +A private path to the Catalyst action you want to create a URI for. + +This is a shortcut for calling C<< $c->dispatcher->get_action_by_path($path) +>> and passing the resulting C<$action> and the remaining arguments to C<< +$c->uri_for >>. + +You can also pass in a Catalyst::Action object, in which case it is passed to +C<< $c->uri_for >>. + +=back + +=cut + +sub uri_for_action { + my ( $c, $path, @args ) = @_; + my $action = blessed($path) + ? $path + : $c->dispatcher->get_action_by_path($path); + return $c->uri_for( $action, @args ); +} + =head2 $c->welcome_message Returns the Catalyst welcome HTML page. diff --git a/t/aggregate/unit_core_uri_for_action.t b/t/aggregate/unit_core_uri_for_action.t index d4f4148..8336779 100644 --- a/t/aggregate/unit_core_uri_for_action.t +++ b/t/aggregate/unit_core_uri_for_action.t @@ -120,52 +120,50 @@ is($context->uri_for($chained_action, [ 1 ], 2, { q => 1 }), # More Chained with Context Tests # { - sub __action { shift->get_action_by_path( @_ ) } - - is( $context->uri_for( __action( $dispatcher, '/action/chained/endpoint2' ), [1,2], (3,4), { x => 5 } ), + is( $context->uri_for_action( '/action/chained/endpoint2', [1,2], (3,4), { x => 5 } ), 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5', - 'uri_for correct for chained with multiple captures and args' ); + 'uri_for_action correct for chained with multiple captures and args' ); - is( $context->uri_for( __action( $dispatcher, '/action/chained/three_end' ), [1,2,3], (4,5,6) ), + is( $context->uri_for_action( '/action/chained/three_end', [1,2,3], (4,5,6) ), 'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6', - 'uri_for correct for chained with multiple capturing actions' ); + 'uri_for_action correct for chained with multiple capturing actions' ); - my $action_needs_two = __action( $dispatcher, '/action/chained/endpoint2' ); + my $action_needs_two = '/action/chained/endpoint2'; - ok( ! defined( $context->uri_for($action_needs_two, [1], (2,3)) ), - 'uri_for returns undef for not enough captures' ); + ok( ! defined( $context->uri_for_action($action_needs_two, [1], (2,3)) ), + 'uri_for_action returns undef for not enough captures' ); - is( $context->uri_for($action_needs_two, [1,2], (2,3)), + is( $context->uri_for_action($action_needs_two, [1,2], (2,3)), 'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3', - 'uri_for returns correct uri for correct captures' ); + 'uri_for_action returns correct uri for correct captures' ); - ok( ! defined( $context->uri_for($action_needs_two, [1,2,3], (2,3)) ), - 'uri_for returns undef for too many captures' ); + ok( ! defined( $context->uri_for_action($action_needs_two, [1,2,3], (2,3)) ), + 'uri_for_action returns undef for too many captures' ); - is( $context->uri_for($action_needs_two, [1,2], (3)), + is( $context->uri_for_action($action_needs_two, [1,2], (3)), 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3', - 'uri_for returns uri with lesser args than specified on action' ); + 'uri_for_action returns uri with lesser args than specified on action' ); - is( $context->uri_for($action_needs_two, [1,2], (3,4,5)), + is( $context->uri_for_action($action_needs_two, [1,2], (3,4,5)), 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5', - 'uri_for returns uri with more args than specified on action' ); + 'uri_for_action returns uri with more args than specified on action' ); - is( $context->uri_for($action_needs_two, [1,''], (3,4)), + is( $context->uri_for_action($action_needs_two, [1,''], (3,4)), 'http://127.0.0.1/foo/chained/foo2/1//end2/3/4', - 'uri_for returns uri with empty capture on undef capture' ); + 'uri_for_action returns uri with empty capture on undef capture' ); - is( $context->uri_for($action_needs_two, [1,2], ('',3)), + is( $context->uri_for_action($action_needs_two, [1,2], ('',3)), 'http://127.0.0.1/foo/chained/foo2/1/2/end2//3', - 'uri_for returns uri with empty arg on undef argument' ); + 'uri_for_action returns uri with empty arg on undef argument' ); - is( $context->uri_for($action_needs_two, [1,2], (3,'')), + is( $context->uri_for_action($action_needs_two, [1,2], (3,'')), 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/', - 'uri_for returns uri with empty arg on undef last argument' ); + 'uri_for_action returns uri with empty arg on undef last argument' ); - my $complex_chained = __action( $dispatcher, '/action/chained/empty_chain_f' ); - is( $context->uri_for( $complex_chained, [23], (13), {q => 3} ), + my $complex_chained = '/action/chained/empty_chain_f'; + is( $context->uri_for_action( $complex_chained, [23], (13), {q => 3} ), 'http://127.0.0.1/foo/chained/empty/23/13?q=3', - 'uri_for returns correct uri for chain with many empty path parts' ); + 'uri_for_action returns correct uri for chain with many empty path parts' ); } diff --git a/t/lib/TestApp/Controller/Action/Chained/Foo.pm b/t/lib/TestApp/Controller/Action/Chained/Foo.pm index 840a619..2f917c1 100644 --- a/t/lib/TestApp/Controller/Action/Chained/Foo.pm +++ b/t/lib/TestApp/Controller/Action/Chained/Foo.pm @@ -30,8 +30,7 @@ sub cross2 :PathPart('end') :Chained('/action/chained/bar/cross1') :Args(1) { } # sub to_root : Chained('/') PathPart('action/chained/to_root') { my ( $self, $c ) = @_; - my $uri = $c->uri_for( - $c->controller('Root')->action_for('chain_root_index') ); + my $uri = $c->uri_for_action('/chain_root_index'); $c->res->body( "URI:$uri" ); $c->stash->{no_end}++; }