Skip to content

Commit

Permalink
Merge branch 'mathmarques-route-callable' into 3.x
Browse files Browse the repository at this point in the history
Closes #1789
Closes #1785
  • Loading branch information
akrabat committed Feb 29, 2016
2 parents 835425f + f5109d8 commit 53a4dd5
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Slim/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ public function any($pattern, $callable)
*/
public function map(array $methods, $pattern, $callable)
{
$callable = new DeferredCallable($callable, $this->container);
if ($callable instanceof Closure) {
$callable = $callable->bindTo($this->container);
}

$route = $this->container->get('router')->map($methods, $pattern, $callable);
if (is_callable([$route, 'setContainer'])) {
Expand Down
2 changes: 2 additions & 0 deletions Slim/Routable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
abstract class Routable
{
use CallableResolverAwareTrait;

/**
* Route callable
*
Expand Down
2 changes: 2 additions & 0 deletions Slim/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ public function run(ServerRequestInterface $request, ResponseInterface $response
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
{
$this->callable = $this->resolveCallable($this->callable);

/** @var InvocationStrategyInterface $handler */
$handler = isset($this->container) ? $this->container->get('foundHandler') : new RequestResponse();

Expand Down
1 change: 0 additions & 1 deletion Slim/RouteGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
class RouteGroup extends Routable implements RouteGroupInterface
{
use CallableResolverAwareTrait;
/**
* Create a new RouteGroup
*
Expand Down
39 changes: 39 additions & 0 deletions tests/Mocks/InvocationStrategyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Slim Framework (http://slimframework.com)
*
* @link https://github.com/slimphp/Slim
* @copyright Copyright (c) 2011-2016 Josh Lockhart
* @license https://github.com/slimphp/Slim/blob/master/LICENSE.md (MIT License)
*/
namespace Slim\Tests\Mocks;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Interfaces\InvocationStrategyInterface;

class InvocationStrategyTest implements InvocationStrategyInterface
{
public static $LastCalledFor = null;

/**
* Invoke a route callable.
*
* @param callable $callable The callable to invoke using the strategy.
* @param ServerRequestInterface $request The request object.
* @param ResponseInterface $response The response object.
* @param array $routeArguments The route's placholder arguments
*
* @return ResponseInterface|string The response from the callable.
*/
public function __invoke(
callable $callable,
ServerRequestInterface $request,
ResponseInterface $response,
array $routeArguments
) {
static::$LastCalledFor = $callable;

return $response;
}
}
26 changes: 25 additions & 1 deletion tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Slim\Http\Uri;
use Slim\Route;
use Slim\Tests\Mocks\CallableTest;
use Slim\Tests\Mocks\InvocationStrategyTest;
use Slim\Tests\Mocks\MiddlewareStub;

class RouteTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -130,7 +131,6 @@ public function testRefinalizing()
}



public function testIdentifier()
{
$route = $this->routeFactory();
Expand Down Expand Up @@ -384,4 +384,28 @@ public function testInvokeWhenDisablingOutputBuffer()
$output = ob_get_clean();
$this->assertEquals('foo', $output);
}

/**
* Ensure that `foundHandler` is called on actual callable
*/
public function testInvokeDeferredCallable()
{
$container = new Container();
$container['CallableTest'] = new CallableTest;
$container['foundHandler'] = function () {
return new InvocationStrategyTest();
};

$route = new Route(['GET'], '/', 'CallableTest:toCall');
$route->setContainer($container);

$uri = Uri::createFromString('https://example.com:80');
$body = new Body(fopen('php://temp', 'r+'));
$request = new Request('GET', $uri, new Headers(), [], Environment::mock()->all(), $body);

$result = $route->callMiddlewareStack($request, new Response);

$this->assertInstanceOf('Slim\Http\Response', $result);
$this->assertEquals([$container['CallableTest'], 'toCall'], InvocationStrategyTest::$LastCalledFor);
}
}

0 comments on commit 53a4dd5

Please sign in to comment.