Permalink
Browse files

Merge branch 'mathmarques-route-callable' into 3.x

Closes #1789
Closes #1785
  • Loading branch information...
akrabat committed Feb 29, 2016
2 parents 835425f + f5109d8 commit 53a4dd576dd3add4efd113ca77c3c21fa559b33e
Showing with 71 additions and 3 deletions.
  1. +3 −1 Slim/App.php
  2. +2 −0 Slim/Routable.php
  3. +2 −0 Slim/Route.php
  4. +0 −1 Slim/RouteGroup.php
  5. +39 −0 tests/Mocks/InvocationStrategyTest.php
  6. +25 −1 tests/RouteTest.php
View
@@ -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'])) {
View
@@ -18,6 +18,8 @@
*/
abstract class Routable
{
use CallableResolverAwareTrait;
/**
* Route callable
*
View
@@ -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();
View
@@ -18,7 +18,6 @@
*/
class RouteGroup extends Routable implements RouteGroupInterface
{
use CallableResolverAwareTrait;
/**
* Create a new RouteGroup
*
@@ -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;
}
}
View
@@ -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
@@ -130,7 +131,6 @@ public function testRefinalizing()
}
public function testIdentifier()
{
$route = $this->routeFactory();
@@ -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.