From c057d81c117789b789b8873e89c065287a6107a8 Mon Sep 17 00:00:00 2001 From: zimt28 Date: Tue, 15 Jan 2013 23:57:38 +0100 Subject: [PATCH 1/6] Get current controller@method --- src/Illuminate/Support/helpers.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Illuminate/Support/helpers.php b/src/Illuminate/Support/helpers.php index ddffdf7f9c50..bc0c2ec03b31 100644 --- a/src/Illuminate/Support/helpers.php +++ b/src/Illuminate/Support/helpers.php @@ -45,6 +45,16 @@ function app_path() return app('path'); } +/** + * Get the current controller and method. + * + * @return mixed + */ +function current_action() +{ + return app('router')->getCurrentRoute()->getOption('_uses'); +} + /** * Divide an array into two arrays. One with keys and the other with values. * From 04ec7a73c7466e636187fdd31415bec139331cc0 Mon Sep 17 00:00:00 2001 From: zimt28 Date: Wed, 16 Jan 2013 23:23:27 +0100 Subject: [PATCH 2/6] Mock Test --- tests/Support/HelpersTest.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/Support/HelpersTest.php b/tests/Support/HelpersTest.php index 5b570f1619d6..c155220f0f45 100644 --- a/tests/Support/HelpersTest.php +++ b/tests/Support/HelpersTest.php @@ -1,7 +1,26 @@ shouldReceive('getOption')->andReturn(null); + + $router = m::mock('Illuminate\Routing\Router'); + $router->shouldReceive('getCurrentRoute')->andReturn($route); + + $app = m::mock('Illuminate\Foundation\Application'); + $app->shouldReceive('make')->once()->with('router')->andReturn($router); + + Illuminate\Support\Facades\Facade::setFacadeApplication($app); + + $this->assertNull(current_action()); + } + public function testArrayDot() { $array = array_dot(array('name' => 'taylor', 'languages' => array('php' => true))); From 2c6f370ede34575aa61073d93244b068050dbc42 Mon Sep 17 00:00:00 2001 From: zimt28 Date: Wed, 16 Jan 2013 23:24:04 +0100 Subject: [PATCH 3/6] current_action() tests --- tests/Support/HelpersTest.php | 37 ++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/tests/Support/HelpersTest.php b/tests/Support/HelpersTest.php index c155220f0f45..08503e4f4f9c 100644 --- a/tests/Support/HelpersTest.php +++ b/tests/Support/HelpersTest.php @@ -5,13 +5,14 @@ class HelpersTest extends PHPUnit_Framework_TestCase { - public function testCurrentAction() + public function testCurrentActionOnClosure() { - $route = m::mock('Illuminate\Routing\Route'); - $route->shouldReceive('getOption')->andReturn(null); + $router = new Illuminate\Routing\Router; + $router->get('/', function() {}); - $router = m::mock('Illuminate\Routing\Router'); - $router->shouldReceive('getCurrentRoute')->andReturn($route); + $request = Request::create('/', 'GET'); + + $router->dispatch($request); $app = m::mock('Illuminate\Foundation\Application'); $app->shouldReceive('make')->once()->with('router')->andReturn($router); @@ -21,6 +22,32 @@ public function testCurrentAction() $this->assertNull(current_action()); } + public function testCurrentActionOnController() + { + $router = new Illuminate\Routing\Router; + + $container = m::mock('Illuminate\Container\Container'); + + $controller = m::mock('stdClass'); + $controller->shouldReceive('callAction')->once()->with($container, $router, 'getIndex', array()); + + $container->shouldReceive('make')->once()->with('Controllers\Home')->andReturn($controller); + + $router->setContainer($container); + $router->get('/', 'Controllers\Home@getIndex'); + + $request = Request::create('/', 'GET'); + + $router->dispatch($request); + + $app = m::mock('Illuminate\Foundation\Application'); + $app->shouldReceive('make')->with('router')->andReturn($router); + + Illuminate\Support\Facades\Facade::setFacadeApplication($app); + + $this->assertEquals('Controllers\Home@getIndex', current_action()); + } + public function testArrayDot() { $array = array_dot(array('name' => 'taylor', 'languages' => array('php' => true))); From 242918874680e5d21756d460a9a714ab9a004d0d Mon Sep 17 00:00:00 2001 From: zimt28 Date: Wed, 16 Jan 2013 23:27:55 +0100 Subject: [PATCH 4/6] add once() --- tests/Support/HelpersTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/HelpersTest.php b/tests/Support/HelpersTest.php index 08503e4f4f9c..d07897ca8c1f 100644 --- a/tests/Support/HelpersTest.php +++ b/tests/Support/HelpersTest.php @@ -41,7 +41,7 @@ public function testCurrentActionOnController() $router->dispatch($request); $app = m::mock('Illuminate\Foundation\Application'); - $app->shouldReceive('make')->with('router')->andReturn($router); + $app->shouldReceive('make')->once()->with('router')->andReturn($router); Illuminate\Support\Facades\Facade::setFacadeApplication($app); From f94cba7fd7ff1a90c29148bd2172409db7088a1a Mon Sep 17 00:00:00 2001 From: zimt28 Date: Thu, 17 Jan 2013 00:06:27 +0100 Subject: [PATCH 5/6] One more for Travis -.- --- tests/Support/HelpersTest.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/Support/HelpersTest.php b/tests/Support/HelpersTest.php index d07897ca8c1f..277fb7d9ac1d 100644 --- a/tests/Support/HelpersTest.php +++ b/tests/Support/HelpersTest.php @@ -16,23 +16,23 @@ public function testCurrentActionOnClosure() $app = m::mock('Illuminate\Foundation\Application'); $app->shouldReceive('make')->once()->with('router')->andReturn($router); - + Illuminate\Support\Facades\Facade::setFacadeApplication($app); - + $this->assertNull(current_action()); } public function testCurrentActionOnController() { $router = new Illuminate\Routing\Router; - + $container = m::mock('Illuminate\Container\Container'); - + $controller = m::mock('stdClass'); $controller->shouldReceive('callAction')->once()->with($container, $router, 'getIndex', array()); - + $container->shouldReceive('make')->once()->with('Controllers\Home')->andReturn($controller); - + $router->setContainer($container); $router->get('/', 'Controllers\Home@getIndex'); @@ -42,9 +42,9 @@ public function testCurrentActionOnController() $app = m::mock('Illuminate\Foundation\Application'); $app->shouldReceive('make')->once()->with('router')->andReturn($router); - + Illuminate\Support\Facades\Facade::setFacadeApplication($app); - + $this->assertEquals('Controllers\Home@getIndex', current_action()); } From ebeeba5bb38784fa5cffc6745562c8f45c9a95fc Mon Sep 17 00:00:00 2001 From: zimt28 Date: Tue, 22 Jan 2013 18:47:35 +0100 Subject: [PATCH 6/6] Return null on errors etc. --- src/Illuminate/Support/helpers.php | 4 +++- tests/Support/HelpersTest.php | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Support/helpers.php b/src/Illuminate/Support/helpers.php index bc0c2ec03b31..ea20226557e1 100644 --- a/src/Illuminate/Support/helpers.php +++ b/src/Illuminate/Support/helpers.php @@ -52,7 +52,9 @@ function app_path() */ function current_action() { - return app('router')->getCurrentRoute()->getOption('_uses'); + $currentRoute = app('router')->getCurrentRoute(); + + return ($currentRoute === null) ? null : $currentRoute->getOption('_uses'); } /** diff --git a/tests/Support/HelpersTest.php b/tests/Support/HelpersTest.php index 277fb7d9ac1d..53ba46f6e8cc 100644 --- a/tests/Support/HelpersTest.php +++ b/tests/Support/HelpersTest.php @@ -48,6 +48,18 @@ public function testCurrentActionOnController() $this->assertEquals('Controllers\Home@getIndex', current_action()); } + public function testCurrentActionOnNoCurrentRoute() + { + $router = new Illuminate\Routing\Router; + + $app = m::mock('Illuminate\Foundation\Application'); + $app->shouldReceive('make')->once()->with('router')->andReturn($router); + + Illuminate\Support\Facades\Facade::setFacadeApplication($app); + + $this->assertNull(current_action()); + } + public function testArrayDot() { $array = array_dot(array('name' => 'taylor', 'languages' => array('php' => true)));