Skip to content

Commit

Permalink
Added test
Browse files Browse the repository at this point in the history
Based on #1787 by @astax-t
  • Loading branch information
mathmarques committed Feb 29, 2016
1 parent 932c012 commit f5109d8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
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 f5109d8

Please sign in to comment.