summaryrefslogtreecommitdiffstats
path: root/tests/RouterTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/RouterTest.php')
-rw-r--r--tests/RouterTest.php342
1 files changed, 152 insertions, 190 deletions
diff --git a/tests/RouterTest.php b/tests/RouterTest.php
index 46574c4..c036c71 100644
--- a/tests/RouterTest.php
+++ b/tests/RouterTest.php
@@ -1,267 +1,229 @@
<?php
+namespace Jasny\Router;
+
use Jasny\Router;
use Jasny\Router\Route;
-use Jasny\Router\Runner\RunnerFactory;
+use Jasny\Router\Routes;
+use Jasny\Router\RunnerFactory;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
-class RouterTest extends PHPUnit_Framework_TestCase
+use Jasny\Router\TestHelpers;
+
+/**
+ * @covers Jasny\Router
+ */
+class RouterTest extends \PHPUnit_Framework_TestCase
{
+ use TestHelpers;
+
/**
* Test creating Router
*/
- public function testConstruct()
+ public function testGetRoutes()
{
- $routes = [
- '/foo' => ['fn' => 'test_function'],
- '/foo/bar' => ['controller' => 'TestController']
- ];
+ $routes = $this->createMock(Routes::class);
$router = new Router($routes);
- $this->assertEquals($routes, $router->getRoutes(), "Routes were not set correctly");
- }
-
- /**
- * Test that on router 'handle', method '__invoke' is called
- */
- public function testHandle()
- {
- $router = $this->createMock(Router::class, ['__invoke']);
- list($request, $response) = $this->getRequests();
-
- $router->method('__invoke')->will($this->returnCallback(function($arg1, $arg2) {
- return ['request' => $arg1, 'response' => $arg2];
- }));
-
- $result = $router->handle($request, $response);
-
- $this->assertEquals($request, $result['request'], "Request was not processed correctly");
- $this->assertEquals($response, $result['response'], "Response was not processed correctly");
+ $this->assertSame($routes, $router->getRoutes(), "Routes were not set correctly");
}
+
/**
- * Test '__invoke' method
+ * Test getting runner factory
*/
- public function testInvoke()
+ public function testGetFactory()
{
- $routes = [
- '/foo/bar' => Route::create(['controller' => 'TestController']),
- '/foo' => Route::create(['fn' => function($arg1, $arg2) {
- return ['request' => $arg1, 'response' => $arg2];
- }])
- ];
-
- list($request, $response) = $this->getRequests();
- $this->expectRequestRoute($request, $routes['/foo']);
-
- $router = new Router($routes);
- $result = $router($request, $response);
+ $router = new Router($this->createMock(Routes::class));
+ $factory = $router->getFactory();
- $this->assertEquals($request, $result['request'], "Request was not processed correctly");
- $this->assertEquals($response, $result['response'], "Response was not processed correctly");
+ $this->assertInstanceOf(RunnerFactory::class, $factory);
}
-
+
/**
- * Test '__invoke' method with 'next' callback
+ * Test setting runner factory
*/
- public function testInvokeNext()
+ public function testSetFactory()
{
- $routes = [
- '/foo/bar' => Route::create(['controller' => 'TestController']),
- '/foo' => Route::create(['fn' => function($request, $response) {
- return $response;
- }])
- ];
-
- list($request, $response) = $this->getRequests();
- $this->expectRequestRoute($request, $routes['/foo']);
-
- $router = new Router($routes);
- $result = $router($request, $response, function($arg1, $arg2) {
- return ['request' => $arg1, 'response' => $arg2];
- });
-
- $this->assertEquals($request, $result['request'], "Request was not processed correctly");
- $this->assertEquals($response, $result['response'], "Response was not processed correctly");
+ $factoryMock = $this->createCallbackMock($this->never());
+
+ $router = new Router($this->createMock(Routes::class));
+
+ $ret = $router->setFactory($factoryMock);
+ $this->assertSame($router, $ret);
+
+ $this->assertSame($factoryMock, $router->getFactory());
}
/**
- * Test case when route is not found
+ * @expectedException \InvalidArgumentException
*/
- public function testNotFound()
+ public function testSetInvalidFactory()
{
- $routes = [
- '/foo/bar' => Route::create(['controller' => 'TestController'])
- ];
-
- list($request, $response) = $this->getRequests();
- $this->expectNotFound($response);
-
- $router = new Router($routes);
- $result = $router($request, $response);
-
- $this->assertEquals(get_class($response), get_class($result), "Returned result is not an instance of 'ServerRequestInterface'");
+ $router = new Router($this->createMock(Routes::class));
+ $router->setFactory('foo bar zoo');
}
+
/**
- * Test adding middleware action
- *
- * @dataProvider addProvider
- * @param mixed $middleware1
- * @param callable $middleware2
- * @param boolean $positive
+ * Test that on router 'handle', method '__invoke' is called
*/
- public function testAdd($middleware1, $middleware2, $positive)
+ public function testHandle()
{
- $router = new Router([]);
- $this->assertEquals(0, count($router->getMiddlewares()), "Middlewares array should be empty");
-
- if (!$positive) $this->expectException(\InvalidArgumentException::class);
-
- $result = $router->add($middleware1);
- $this->assertEquals(1, count($router->getMiddlewares()), "There should be only one item in middlewares array");
- $this->assertEquals($middleware1, reset($router->getMiddlewares()), "Wrong item in middlewares array");
- $this->assertEquals($router, $result, "'Add' should return '\$this'");
+ $request = $this->createMock(ServerRequestInterface::class);
+ $response = $this->createMock(ResponseInterface::class);
+ $finalResponse = $this->createMock(ResponseInterface::class);
+
+ $router = $this->getMockBuilder(Router::class)->disableOriginalConstructor()
+ ->setMethods(['__invoke'])->getMock();
+ $router->expects($this->once())->method('__invoke')->with($request, $response)->willReturn($finalResponse);
- if (!$middleware2) return;
+ $result = $router->handle($request, $response);
- $router->add($middleware2);
- $this->assertEquals(2, count($router->getMiddlewares()), "There should be two items in middlewares array");
- foreach ($router->getMiddlewares() as $action) {
- $this->assertTrue($action == $middleware1 || $action == $middleware2, "Wrong item in middlewares array");
- }
+ $this->assertSame($finalResponse, $result);
}
- /**
- * Provide data for testing 'add' method
- */
- public function addProvider()
+ public function nextProvider()
{
return [
- ['wrong_callback', null, false],
- [[$this, 'getMiddlewareCalledFirst'], null, true],
- [[$this, 'getMiddlewareCalledFirst'], [$this, 'getMiddlewareCalledLast'], true]
- ];
- }
-
- /**
- * Test executing router with middlewares chain (test only execution order)
- */
- public function testRunMiddlewares()
- {
- $routes = [
- '/foo' => Route::create(['fn' => function($request, $response) {
- $response->testMiddlewareCalls[] = 'run';
- return $response;
- }])
+ [null],
+ [$this->createCallbackMock($this->any())]
];
-
- list($request, $response) = $this->getRequests();
- $this->expectRequestRoute($request, $routes['/foo']);
-
- $router = new Router($routes);
- $router->add([$this, 'getMiddlewareCalledLast'])->add([$this, 'getMiddlewareCalledFirst']);
-
- $result = $router($request, $response, function($request, $response) {
- $response->testMiddlewareCalls[] = 'outer';
- return $response;
- });
-
- $this->assertEquals(['first','last','run','outer'], $response->testMiddlewareCalls, "Actions were executed in wrong order");
}
/**
- * Test getting and setting runner factory
+ * Test '__invoke' method
+ *
+ * @dataProvider nextProvider
*/
- public function testRunnerFactory()
+ public function testInvoke($next)
{
- $router = new Router([]);
- $factory = $router->getFactory();
-
- $this->assertEquals(RunnerFactory::class, get_class($factory), "By default 'getFactory' should return 'RunnerFactory' instance, not " . get_class($factory));
-
- $self = $router->setFactory(function() {
- return 'test';
- });
- $factory = $router->getFactory();
+ $route = $this->createMock(Route::class);
+
+ $request = $this->createMock(ServerRequestInterface::class);
+ $requestWithRoute = $this->createMock(ServerRequestInterface::class);
+ $request->expects($this->once())->method('withAttribute')->with('route')->willReturn($requestWithRoute);
+
+ $response = $this->createMock(ResponseInterface::class);
+ $finalResponse = $this->createMock(ResponseInterface::class);
+
+ $runner = $this->createCallbackMock($this->once(), [$requestWithRoute, $response, $next], $finalResponse);
+ $factory = $this->createCallbackMock($this->once(), [$route], $runner);
- $this->assertEquals($router, $self, "'setFactory' must return an instance of router");
- $this->assertEquals('test', $factory(), "Factory was not set or got correctly");
+ $routes = $this->createMock(Routes::class);
+ $routes->expects($this->once())->method('getRoute')->with($request)->willReturn($route);
- $this->expectException(\InvalidArgumentException::class);
- $router->setFactory('test');
+ $router = new Router($routes);
+ $router->setFactory($factory);
+
+ $result = $router($request, $response, $next);
+
+ $this->assertSame($finalResponse, $result);
}
/**
- * Get requests for testing
- *
- * @return array
+ * Test case when route is not found
*/
- public function getRequests()
+ public function testNotFound()
{
$request = $this->createMock(ServerRequestInterface::class);
$response = $this->createMock(ResponseInterface::class);
+ $finalResponse = $this->createMock(ResponseInterface::class);
+ $body = $this->createMock(StreamInterface::class);
- $request->method('getUri')->will($this->returnValue('/foo'));
- $request->method('getMethod')->will($this->returnValue('GET'));
+ $response->expects($this->once())->method('withStatus')->with(404)->willReturn($finalResponse);
+ $finalResponse->expects($this->once())->method('getBody')->willReturn($body);
+ $body->expects($this->once())->method('write')->with('Not Found');
+
+ $factory = $this->createCallbackMock($this->never());
- return [$request, $response];
+ $routes = $this->createMock(Routes::class);
+ $routes->expects($this->once())->method('getRoute')->with($request)->willReturn(null);
+
+ $router = new Router($routes);
+ $router->setFactory($factory);
+
+ $result = $router($request, $response);
+
+ $this->assertSame($finalResponse, $result);
}
/**
- * Get middleware action, that should ba called first in middleware chain
- *
- * @param ServerRequestInterface $request
- * @param ResponseInterface $response
- * @param callback $next
- * @return ResponseInterface
+ * Test adding middleware action
*/
- public function getMiddlewareCalledFirst(ServerRequestInterface $request, ResponseInterface $response, $next)
+ public function testAdd()
{
- $response->testMiddlewareCalls[] = 'first';
- return $next($request, $response);
+ $middlewareOne = $this->createCallbackMock($this->never());
+ $middlewareTwo = $this->createCallbackMock($this->never());
+
+ $router = new Router($this->createMock(Routes::class));
+
+ $this->assertEquals([], $router->getMiddlewares(), "Middlewares array should be empty");
+
+ $ret = $router->add($middlewareOne);
+ $this->assertSame($router, $ret);
+
+ $router->add($middlewareTwo);
+
+ $this->assertSame([$middlewareOne, $middlewareTwo], $router->getMiddlewares());
}
/**
- * Get middleware action, that should be called last in middleware chain
- *
- * @param ServerRequestInterface $request
- * @param ResponseInterface $response
- * @param callback $next
- * @return ResponseInterface
+ * @expectedException \InvalidArgumentException
*/
- public function getMiddlewareCalledLast(ServerRequestInterface $request, ResponseInterface $response, $next)
+ public function testAddInvalidMiddleware()
{
- $response->testMiddlewareCalls[] = 'last';
- return $next($request, $response);
+ $router = new Router($this->createMock(Routes::class));
+ $router->add('foo bar zoo');
}
/**
- * Expect 'not found' response
- *
- * @param ResponseInterface
+ * Test executing router with middlewares chain (test only execution order)
*/
- public function expectNotFound(ResponseInterface $response)
+ public function testRunMiddlewares()
{
- $stream = $this->createMock(StreamInterface::class);
- $stream->expects($this->once())->method('rewind');
- $stream->expects($this->once())->method('write')->with($this->equalTo('Not Found'));
+ $route = $this->createMock(Route::class);
- $response->method('getBody')->will($this->returnValue($stream));
- $response->expects($this->once())->method('withBody')->with($this->equalTo($stream))->will($this->returnSelf());
- $response->expects($this->once())->method('withStatus')->with($this->equalTo(404), $this->equalTo('Not Found'))->will($this->returnSelf());
- }
+ $request = $this->createMock(ServerRequestInterface::class);
+ $request->expects($this->once())->method('withAttribute')->with('route')->willReturn($request);
+ $requestOne = $this->createMock(ServerRequestInterface::class);
+ $requestTwo = $this->createMock(ServerRequestInterface::class);
+
+ $response = $this->createMock(ResponseInterface::class);
+ $responseOne = $this->createMock(ResponseInterface::class);
+ $responseTwo = $this->createMock(ResponseInterface::class);
+ $finalResponse = $this->createMock(ResponseInterface::class);
+
+ $middlewareOne = $this->getMockBuilder(\stdClass::class)->setMethods(['__invoke'])->getMock();
+ $middlewareOne->expects($this->once())->method('__invoke')->id('one')
+ ->with($request, $response, $this->isInstanceOf(Closure::class))
+ ->will($this->returnCallback(function($a, $b, $next) use ($requestOne, $responseOne) {
+ return $next($requestOne, $responseOne);
+ }));
+
+ $middlewareTwo = $this->getMockBuilder(\stdClass::class)->setMethods(['__invoke'])->getMock();
+ $middlewareTwo->expects($this->once())->method('__invoke')->id('two')->after('one')
+ ->with($request, $response, $this->isInstanceOf(Closure::class))
+ ->will($this->returnCallback(function($a, $b, $next) use ($requestTwo, $responseTwo) {
+ return $next($requestTwo, $responseTwo);
+ }));
+
+ $runner = $this->createCallbackMock($this->once(), [$requestTwo, $responseTwo], $finalResponse);
+ $factory = $this->createCallbackMock($this->once(), [$route], $runner);
+
+ $routes = $this->createMock(Routes::class);
+ $routes->expects($this->once())->method('getRoute')->with($request)->willReturn($route);
- /**
- * Expect that request will return given route
- *
- * @param ServerRequestInterface $request
- * @param Route $route
- */
- public function expectRequestRoute(ServerRequestInterface $request, $route)
- {
- $request->expects($this->once())->method('getAttribute')->with($this->equalTo('route'))->will($this->returnValue($route));
+ $router = new Router($routes);
+ $router->setFactory($factory);
+
+ $router->add($middlewareOne);
+ $router->add($middlewareTwo);
+
+ $result = $router($request, $response);
+
+ $this->assertSame($finalResponse, $result);
}
}