From 5b85e393c8a78945b1b35e08684b054dadf4fc28 Mon Sep 17 00:00:00 2001 From: Rougin Royce Gutib Date: Thu, 15 Feb 2018 15:35:38 +0800 Subject: [PATCH] Update test cases --- tests/Middleware/DispatcherTest.php | 10 +- tests/Middleware/DispatcherTestCases.php | 118 +++++++++--------- .../Middleware/StratigilityDispatcherTest.php | 14 ++- tests/Routing/DispatcherTest.php | 33 +++-- tests/Routing/DispatcherTestCases.php | 72 +++++++---- tests/Routing/FastRouteDispatcherTest.php | 36 ++++-- tests/Routing/PhrouteDispatcherTest.php | 35 ++++-- tests/Routing/RouterTestCases.php | 85 ++++++++----- tests/Template/RendererTest.php | 73 +++++++++++ 9 files changed, 322 insertions(+), 154 deletions(-) create mode 100644 tests/Template/RendererTest.php diff --git a/tests/Middleware/DispatcherTest.php b/tests/Middleware/DispatcherTest.php index a01852a6..63f8d8df 100644 --- a/tests/Middleware/DispatcherTest.php +++ b/tests/Middleware/DispatcherTest.php @@ -11,15 +11,17 @@ class DispatcherTest extends DispatcherTestCases { /** - * Sets up the middleware dispatcher. + * Sets up the middleware dispatcher instance. * * @return void */ public function setUp() { - if (! interface_exists('Interop\Http\ServerMiddleware\MiddlewareInterface')) { - $this->markTestSkipped('Interop Middleware is not installed.'); - } + $interface = 'Interop\Http\ServerMiddleware\MiddlewareInterface'; + + $message = 'http-interop/http-middleware (v0.4.0) is not installed.'; + + interface_exists($interface) || $this->markTestSkipped($message); $this->dispatcher = new Dispatcher; } diff --git a/tests/Middleware/DispatcherTestCases.php b/tests/Middleware/DispatcherTestCases.php index 98897631..485a7ad1 100644 --- a/tests/Middleware/DispatcherTestCases.php +++ b/tests/Middleware/DispatcherTestCases.php @@ -2,6 +2,8 @@ namespace Rougin\Slytherin\Middleware; +use Rougin\Slytherin\Http\ServerRequest; + /** * Dispatcher Test Cases * @@ -22,17 +24,17 @@ class DispatcherTestCases extends \PHPUnit_Framework_TestCase */ public function testProcessMethodWithDoublePassCallback() { - $this->exists(get_class($this->dispatcher)); - - $callback = function ($request, $response, $next) { + $this->dispatcher->push(function ($request, $response, $next) { $response = $next($request, $response)->withStatus(404); return $response->withHeader('X-Slytherin', time()); - }; + }); - $this->dispatcher->push($callback); + $expected = (integer) 404; - $this->assertEquals(404, $this->response()->getStatusCode()); + $result = $this->process()->getStatusCode(); + + $this->assertEquals($expected, $result); } /** @@ -42,59 +44,63 @@ public function testProcessMethodWithDoublePassCallback() */ public function testProcessMethodWithSinglePassCallback() { - $this->exists(get_class($this->dispatcher)); - $stratigility = 'Rougin\Slytherin\Middleware\StratigilityDispatcher'; $wrapper = 'Zend\Stratigility\Middleware\CallableMiddlewareWrapper'; if (is_a($this->dispatcher, $stratigility) && ! class_exists($wrapper)) { - $message = 'Stratigility\'s current version does not accept single pass middlewares.'; + $message = 'Stratigility\'s current installed version'; - $this->markTestSkipped($message); + $message .= ' does not accept single pass middlewares'; + + $this->markTestSkipped((string) $message); } - $time = time(); + $time = (integer) time(); - $callback = function ($request, $next) use ($time) { + $this->dispatcher->push(function ($request, $next) use ($time) { $response = $next($request); return $response->withHeader('X-Slytherin', $time); - }; + }); + + $expected = array((integer) $time); - $this->dispatcher->push($callback); + $result = $this->process()->getHeader('X-Slytherin'); - $this->assertEquals(array($time), $this->response()->getHeader('X-Slytherin')); + $this->assertEquals($expected, $result); } /** - * Tests DispatcherInterface::process with \Interop\Http\ServerMiddleware\DelegateInterface callback. + * Tests DispatcherInterface::process with DelegateInterface callback. * * @return void */ public function testProcessMethodWithDelagateInterfaceCallback() { - $this->exists(get_class($this->dispatcher)); - $stratigility = 'Rougin\Slytherin\Middleware\StratigilityDispatcher'; $wrapper = 'Zend\Stratigility\Middleware\CallableMiddlewareWrapper'; if (is_a($this->dispatcher, $stratigility) && ! class_exists($wrapper)) { - $message = 'Stratigility\'s current version does not accept delegates.'; + $message = 'Stratigility\'s current version'; + + $message .= (string) ' does not accept delegates'; - $this->markTestSkipped($message); + $this->markTestSkipped((string) $message); } - $callback = function ($request, $delegate) { + $this->dispatcher->push(function ($request, $delegate) { $response = $delegate->process($request); return $response->withHeader('Content-Type', 'application/json'); - }; + }); - $this->dispatcher->push($callback); + $expected = array('application/json'); - $this->assertEquals(array('application/json'), $this->response()->getHeader('Content-Type')); + $result = $this->process()->getHeader('Content-Type'); + + $this->assertEquals($expected, $result); } /** @@ -104,21 +110,27 @@ public function testProcessMethodWithDelagateInterfaceCallback() */ public function testProcessMethodWithString() { - $this->exists(get_class($this->dispatcher)); - $stratigility = 'Rougin\Slytherin\Middleware\StratigilityDispatcher'; $wrapper = 'Zend\Stratigility\Middleware\CallableMiddlewareWrapper'; if (is_a($this->dispatcher, $stratigility) && ! class_exists($wrapper)) { - $message = 'Stratigility\'s current version does not PSR-15 middlewares.'; + $message = 'Stratigility\'s current version'; - $this->markTestSkipped($message); + $message .= ' does not accept PSR-15 middlewares'; + + $this->markTestSkipped((string) $message); } - $this->dispatcher->push('Rougin\Slytherin\Fixture\Middlewares\InteropMiddleware'); + $interop = 'Rougin\Slytherin\Fixture\Middlewares\InteropMiddleware'; + + $this->dispatcher->push($interop); + + $expected = 500; + + $result = $this->process()->getStatusCode(); - $this->assertEquals(500, $this->response()->getStatusCode()); + $this->assertEquals($expected, $result); } /** @@ -128,15 +140,15 @@ public function testProcessMethodWithString() */ public function testPushMethodWithArray() { - $this->exists(get_class($this->dispatcher)); + $expected = array('Rougin\Slytherin\Fixture\Middlewares\InteropMiddleware'); - $stack = array('Rougin\Slytherin\Fixture\Middlewares\InteropMiddleware'); + $expected[] = 'Rougin\Slytherin\Middleware\FinalResponse'; - $stack[] = 'Rougin\Slytherin\Middleware\FinalResponse'; + $this->dispatcher->push($expected); - $this->dispatcher->push($stack); + $result = $this->dispatcher->stack(); - $this->assertEquals($stack, $this->dispatcher->stack()); + $this->assertEquals($expected, $result); } /** @@ -146,12 +158,15 @@ public function testPushMethodWithArray() */ public function testStackMethod() { - $this->exists(get_class($this->dispatcher)); - $this->dispatcher->push('Rougin\Slytherin\Fixture\Middlewares\InteropMiddleware'); + $this->dispatcher->push('Rougin\Slytherin\Middleware\FinalResponse'); - $this->assertCount(2, $this->dispatcher->stack()); + $expected = (integer) 2; + + $result = $this->dispatcher->stack(); + + $this->assertCount($expected, $result); } /** @@ -159,35 +174,18 @@ public function testStackMethod() * * @return \Psr\Http\Message\ResponseInterface */ - protected function response() + protected function process() { - $server = array(); + $server = array('REQUEST_METHOD' => 'GET'); - $server['REQUEST_METHOD'] = 'GET'; $server['REQUEST_URI'] = '/'; + $server['SERVER_NAME'] = 'localhost'; + $server['SERVER_PORT'] = '8000'; - $request = new \Rougin\Slytherin\Http\ServerRequest($server); + $request = new ServerRequest($server); return $this->dispatcher->process($request, new Delegate); } - - /** - * Verifies the specified dispatcher if it exists. - * - * @param string $dispatcher - * @return void - */ - protected function exists($dispatcher) - { - switch ($dispatcher) { - case 'Rougin\Slytherin\Middleware\StratigilityDispatcher': - if (class_exists('Zend\Stratigility\MiddlewarePipe') === false) { - $this->markTestSkipped('Zend Stratigility is not installed.'); - } - - break; - } - } } diff --git a/tests/Middleware/StratigilityDispatcherTest.php b/tests/Middleware/StratigilityDispatcherTest.php index 9b20c36a..fe7107b8 100644 --- a/tests/Middleware/StratigilityDispatcherTest.php +++ b/tests/Middleware/StratigilityDispatcherTest.php @@ -2,6 +2,8 @@ namespace Rougin\Slytherin\Middleware; +use Zend\Stratigility\MiddlewarePipe; + /** * Stratigility Dispatcher Test * @@ -11,18 +13,18 @@ class StratigilityDispatcherTest extends DispatcherTestCases { /** - * Sets up the middleware dispatcher. + * Sets up the middleware dispatcher instance. * * @return void */ public function setUp() { - if (! class_exists('Zend\Stratigility\MiddlewarePipe')) { - $this->markTestSkipped('Zend Stratigility is not installed.'); - } + $class = (string) 'Zend\Stratigility\MiddlewarePipe'; + + $message = 'Zend Stratigility is not installed'; - $pipe = new \Zend\Stratigility\MiddlewarePipe; + class_exists($class) || $this->markTestSkipped($message); - $this->dispatcher = new StratigilityDispatcher($pipe); + $this->dispatcher = new StratigilityDispatcher(new MiddlewarePipe); } } diff --git a/tests/Routing/DispatcherTest.php b/tests/Routing/DispatcherTest.php index 546e656f..861a9d41 100644 --- a/tests/Routing/DispatcherTest.php +++ b/tests/Routing/DispatcherTest.php @@ -2,6 +2,8 @@ namespace Rougin\Slytherin\Routing; +use Rougin\Slytherin\Fixture\Classes\NewClass; + /** * Dispatcher Test * @@ -21,8 +23,11 @@ public function setUp() $router = new Router($routes); - $router->get('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index'); - $router->post('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@store'); + $router->prefix('', 'Rougin\Slytherin\Fixture\Classes'); + + $router->get('/', 'NewClass@index'); + + $router->post('/', 'NewClass@store'); $router->get('/hi', function () { return 'Hi and this is a callback'; @@ -42,15 +47,21 @@ public function testDispatchMethodWithClassAndFastRouteRouter() $router = new FastRouteRouter; - $router->get('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index'); + $router->prefix('', 'Rougin\Slytherin\Fixture\Classes'); + + $router->get('/', 'NewClass@index'); $dispatcher = new Dispatcher($router); - $controller = new \Rougin\Slytherin\Fixture\Classes\NewClass; + $controller = new NewClass; list($function) = $dispatcher->dispatch('GET', '/'); - $this->assertEquals($controller->index(), $this->result($function)); + $expected = (string) $controller->index(); + + $result = $this->result($function); + + $this->assertEquals($expected, $result); } /** @@ -64,14 +75,20 @@ public function testDispatchMethodWithClassAndPhrouteRouter() $router = new PhrouteRouter; - $router->get('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index'); + $router->prefix('', 'Rougin\Slytherin\Fixture\Classes'); + + $router->get('/', 'NewClass@index'); $dispatcher = new Dispatcher($router); - $controller = new \Rougin\Slytherin\Fixture\Classes\NewClass; + $controller = new NewClass; list($function) = $dispatcher->dispatch('GET', '/'); - $this->assertEquals($controller->index(), $this->result($function)); + $expected = (string) $controller->index(); + + $result = $this->result($function); + + $this->assertEquals($expected, $result); } } diff --git a/tests/Routing/DispatcherTestCases.php b/tests/Routing/DispatcherTestCases.php index a4742220..163f99ee 100644 --- a/tests/Routing/DispatcherTestCases.php +++ b/tests/Routing/DispatcherTestCases.php @@ -2,6 +2,8 @@ namespace Rougin\Slytherin\Routing; +use Rougin\Slytherin\Fixture\Classes\NewClass; + /** * Dispatcher Test Cases * @@ -26,8 +28,11 @@ public function setUp() $router = new Router($routes); - $router->get('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index'); - $router->post('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@store'); + $router->prefix('', 'Rougin\Slytherin\Fixture\Classes'); + + $router->get('/', 'NewClass@index'); + + $router->post('/', 'NewClass@store'); $router->get('/hi', function () { return 'Hi and this is a callback'; @@ -45,11 +50,15 @@ public function testDispatchMethodWithClass() { $this->exists(get_class($this->dispatcher)); - $controller = new \Rougin\Slytherin\Fixture\Classes\NewClass; + $controller = new NewClass; list($function) = $this->dispatcher->dispatch('GET', '/'); - $this->assertEquals($controller->index(), $this->result($function)); + $expected = (string) $controller->index(); + + $result = $this->result($function); + + $this->assertEquals($expected, $result); } /** @@ -61,11 +70,15 @@ public function testDispatchMethodWithClassAndPostHttpMethod() { $this->exists(get_class($this->dispatcher)); - $controller = new \Rougin\Slytherin\Fixture\Classes\NewClass; + $controller = new NewClass; list($function) = $this->dispatcher->dispatch('POST', '/'); - $this->assertEquals($controller->store(), $this->result($function)); + $expected = (string) $controller->store(); + + $result = $this->result($function); + + $this->assertEquals($expected, $result); } /** @@ -79,7 +92,11 @@ public function testDispatchMethodWithCallback() list($function) = $this->dispatcher->dispatch('GET', '/hi'); - $this->assertEquals('Hi and this is a callback', $this->result($function)); + $expected = 'Hi and this is a callback'; + + $result = $this->result($function); + + $this->assertEquals($expected, $result); } /** @@ -89,11 +106,11 @@ public function testDispatchMethodWithCallback() */ public function testDispatchMethodWithError() { - $this->exists(get_class($this->dispatcher)); + $this->setExpectedException('UnexpectedValueException'); - $this->setExpectedException('UnexpectedValueException', 'Route "/test" not found'); + $this->exists(get_class($this->dispatcher)); - $this->dispatcher->dispatch('GET', '/test'); + $this->dispatcher->dispatch('GET', (string) '/test'); } /** @@ -103,11 +120,11 @@ public function testDispatchMethodWithError() */ public function testDispatchMethodWithInvalidMethod() { - $this->exists(get_class($this->dispatcher)); + $this->setExpectedException('UnexpectedValueException'); - $this->setExpectedException('UnexpectedValueException', 'Used method is not allowed'); + $this->exists(get_class($this->dispatcher)); - $this->dispatcher->dispatch('TEST', '/'); + $this->dispatcher->dispatch('TEST', (string) '/'); } /** @@ -121,7 +138,7 @@ protected function result($function) if (is_array($function) === true) { list($callback, $parameters) = $function; - if (is_array($callback)) { + if (is_array($callback) === true) { list($class, $method) = $callback; $callback = array(new $class, $method); @@ -141,19 +158,20 @@ protected function result($function) */ protected function exists($dispatcher) { - switch ($dispatcher) { - case 'Rougin\Slytherin\Routing\FastRouteDispatcher': - if (! interface_exists('FastRoute\Dispatcher')) { - $this->markTestSkipped('FastRoute is not installed.'); - } - - break; - case 'Rougin\Slytherin\Routing\PhrouteDispatcher': - if (! class_exists('Phroute\Phroute\Dispatcher')) { - $this->markTestSkipped('Phroute is not installed.'); - } - - break; + if ($dispatcher === 'Rougin\Slytherin\Routing\FastRouteDispatcher') { + $exists = interface_exists('FastRoute\Dispatcher'); + + $message = (string) 'FastRoute is not installed.'; + + $exists || $this->markTestSkipped((string) $message); + } + + if ($dispatcher === 'Rougin\Slytherin\Routing\PhrouteDispatcher') { + $exists = class_exists('Phroute\Phroute\Dispatcher'); + + $message = (string) 'Phroute is not installed.'; + + $exists || $this->markTestSkipped((string) $message); } } } diff --git a/tests/Routing/FastRouteDispatcherTest.php b/tests/Routing/FastRouteDispatcherTest.php index d0f25866..0d2cb403 100644 --- a/tests/Routing/FastRouteDispatcherTest.php +++ b/tests/Routing/FastRouteDispatcherTest.php @@ -2,6 +2,8 @@ namespace Rougin\Slytherin\Routing; +use Rougin\Slytherin\Fixture\Classes\NewClass; + /** * FastRoute Dispatcher Test * @@ -21,14 +23,17 @@ public function setUp() $router = new FastRouteRouter; - $router->get('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index'); - $router->post('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@store'); + $router->prefix('', 'Rougin\Slytherin\Fixture\Classes'); + + $router->get('/', 'NewClass@index'); + + $router->post('/', 'NewClass@store'); $router->get('/hi', function () { return 'Hi and this is a callback'; }); - $router->add('TEST', '/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index'); + $router->add('TEST', '/', 'NewClass@index'); $this->dispatcher = new FastRouteDispatcher($router); } @@ -44,15 +49,21 @@ public function testDispatchMethodWithClassAndSlytherinRouter() $router = new Router; - $router->get('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index'); + $router->prefix('', 'Rougin\Slytherin\Fixture\Classes'); + + $router->get('/', 'NewClass@index'); $dispatcher = new FastRouteDispatcher($router); - $controller = new \Rougin\Slytherin\Fixture\Classes\NewClass; + $controller = new NewClass; list($function) = $dispatcher->dispatch('GET', '/'); - $this->assertEquals($controller->index(), $this->result($function)); + $expected = (string) $controller->index(); + + $result = $this->result($function); + + $this->assertEquals($expected, $result); } /** @@ -63,18 +74,25 @@ public function testDispatchMethodWithClassAndSlytherinRouter() public function testDispatchMethodWithClassAndPhrouteRouter() { $this->exists('Rougin\Slytherin\Routing\FastRouteDispatcher'); + $this->exists('Rougin\Slytherin\Routing\PhrouteRouter'); $router = new PhrouteRouter; - $router->get('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index'); + $router->prefix('', 'Rougin\Slytherin\Fixture\Classes'); + + $router->get('/', 'NewClass@index'); $dispatcher = new FastRouteDispatcher($router); - $controller = new \Rougin\Slytherin\Fixture\Classes\NewClass; + $controller = new NewClass; list($function) = $dispatcher->dispatch('GET', '/'); - $this->assertEquals($controller->index(), $this->result($function)); + $expected = (string) $controller->index(); + + $result = $this->result($function); + + $this->assertEquals($expected, $result); } } diff --git a/tests/Routing/PhrouteDispatcherTest.php b/tests/Routing/PhrouteDispatcherTest.php index 11e671d5..ff10ccc3 100644 --- a/tests/Routing/PhrouteDispatcherTest.php +++ b/tests/Routing/PhrouteDispatcherTest.php @@ -2,6 +2,8 @@ namespace Rougin\Slytherin\Routing; +use Rougin\Slytherin\Fixture\Classes\NewClass; + /** * Phroute Dispatcher Test * @@ -21,14 +23,17 @@ public function setUp() $router = new PhrouteRouter; - $router->get('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index'); - $router->post('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@store'); + $router->prefix('', 'Rougin\Slytherin\Fixture\Classes'); + + $router->get('/', 'NewClass@index'); + + $router->post('/', 'NewClass@store'); $router->get('/hi', function () { return 'Hi and this is a callback'; }); - $router->add('TEST', '/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index'); + $router->add('TEST', '/', 'NewClass@index'); $this->dispatcher = new PhrouteDispatcher($router); } @@ -42,15 +47,21 @@ public function testDispatchMethodWithClassAndSlytherinRouter() { $router = new Router; - $router->get('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index'); + $router->prefix('', 'Rougin\Slytherin\Fixture\Classes'); + + $router->get('/', 'NewClass@index'); $dispatcher = new PhrouteDispatcher($router); - $controller = new \Rougin\Slytherin\Fixture\Classes\NewClass; + $controller = new NewClass; list($function) = $dispatcher->dispatch('GET', '/'); - $this->assertEquals($controller->index(), $this->result($function)); + $expected = (string) $controller->index(); + + $result = $this->result($function); + + $this->assertEquals($expected, $result); } /** @@ -64,14 +75,20 @@ public function testDispatchMethodWithClassAndFastRouteRouter() $router = new FastRouteRouter; - $router->get('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index'); + $router->prefix('', 'Rougin\Slytherin\Fixture\Classes'); + + $router->get('/', 'NewClass@index'); $dispatcher = new PhrouteDispatcher($router); - $controller = new \Rougin\Slytherin\Fixture\Classes\NewClass; + $controller = new NewClass; list($function) = $dispatcher->dispatch('GET', '/'); - $this->assertEquals($controller->index(), $this->result($function)); + $expected = (string) $controller->index(); + + $result = $this->result($function); + + $this->assertEquals($expected, $result); } } diff --git a/tests/Routing/RouterTestCases.php b/tests/Routing/RouterTestCases.php index 1c3eeba8..5adf8709 100644 --- a/tests/Routing/RouterTestCases.php +++ b/tests/Routing/RouterTestCases.php @@ -39,7 +39,9 @@ public function testAddMethod() { $this->exists(get_class($this->router)); - $this->router->add('POST', '/store', 'Rougin\Slytherin\Fixture\Classes\NewClass@store'); + $this->router->prefix('', 'Rougin\Slytherin\Fixture\Classes'); + + $this->router->add('POST', '/store', 'NewClass@store'); $this->assertTrue($this->router->has('POST', '/store')); } @@ -65,9 +67,9 @@ public function testCallMagicMethod() */ public function testCallMagicMethodWithException() { - $this->exists(get_class($this->router)); + $this->setExpectedException('BadMethodCallException'); - $this->setExpectedException('BadMethodCallException', '"TEST" is not a valid HTTP method'); + $this->exists(get_class($this->router)); $this->router->test('/test', 'PostsController@test'); } @@ -81,7 +83,9 @@ public function testHasMethod() { $this->exists(get_class($this->router)); - $this->assertTrue($this->router->has('GET', '/')); + $result = $this->router->has('GET', '/'); + + $this->assertTrue((boolean) $result); } /** @@ -97,7 +101,9 @@ public function testRetrieveMethod() $route = $this->router->retrieve('GET', '/'); - $this->assertEquals($expected, implode('@', $route[2])); + $result = implode('@', $route[2]); + + $this->assertEquals($expected, $result); } /** @@ -111,7 +117,7 @@ public function testRetrieveMethodWithNull() $route = $this->router->retrieve('GET', '/test'); - $this->assertEquals(null, $route); + $this->assertNull($route); } /** @@ -123,13 +129,15 @@ public function testRoutesMethod() { $this->exists(get_class($this->router)); - $routes = $this->routes; + $expected = $this->routes; + + $expected[0][2] = explode('@', $expected[0][2]); - $routes[0][2] = explode('@', $routes[0][2]); + $expected[0][] = array(); - $routes[0][] = array(); + $result = $this->router->routes(); - $this->assertEquals($routes, $this->router->routes()); + $this->assertEquals($expected, $result); } /** @@ -153,7 +161,9 @@ public function testRestfulMethod() $this->router->restful('posts', 'PostsController'); - $this->assertEquals($expected, $this->router->routes()); + $result = $this->router->routes(); + + $this->assertEquals($expected, $result); } /** @@ -165,11 +175,13 @@ public function testPrefixMethod() { $this->exists(get_class($this->router)); - $this->router->prefix('/v1/slytherin'); + $this->router->prefix((string) '/v1/slytherin'); + + $this->router->get('/hello', (string) 'HelloController@hello'); - $this->router->get('/hello', 'HelloController@hello'); + $exists = $this->router->has('GET', '/v1/slytherin/hello'); - $this->assertTrue($this->router->has('GET', '/v1/slytherin/hello')); + $this->assertTrue($exists); } /** @@ -188,20 +200,28 @@ public function testPrefixMethodWithMultiplePrefixes() $this->router->prefix('/v1/auth'); $this->router->post('/login', 'AuthController@login'); + $this->router->post('/logout', 'AuthController@logout'); $this->router->prefix('/v1/test'); $this->router->get('/hello', 'TestController@hello'); + $this->router->get('/test', 'TestController@test'); $home = $this->router->retrieve('GET', '/home'); + + $home = 'Acme\Http\Controllers\HomeController' === $home[2][0]; + $login = $this->router->retrieve('POST', '/v1/auth/login'); + + $login = 'Acme\Http\Controllers\AuthController' === $login[2][0]; + $hello = $this->router->retrieve('GET', '/v1/test/hello'); - $this->assertEquals('Acme\Http\Controllers\HomeController', $home[2][0]); - $this->assertEquals('Acme\Http\Controllers\AuthController', $login[2][0]); - $this->assertEquals('Acme\Http\Controllers\TestController', $hello[2][0]); + $hello = 'Acme\Http\Controllers\TestController' === $hello[2][0]; + + $this->assertTrue($home && $login && $hello); } /** @@ -219,7 +239,9 @@ public function testMergeMethod() $this->router->merge($router->routes()); - $this->assertTrue($this->router->has('GET', '/test')); + $exists = $this->router->has('GET', '/test'); + + $this->assertTrue($exists); } /** @@ -230,19 +252,20 @@ public function testMergeMethod() */ protected function exists($router) { - switch ($router) { - case 'Rougin\Slytherin\Routing\FastRouteRouter': - if (! class_exists('FastRoute\RouteCollector')) { - $this->markTestSkipped('FastRoute is not installed.'); - } - - break; - case 'Rougin\Slytherin\Routing\PhrouteRouter': - if (! class_exists('Phroute\Phroute\RouteCollector')) { - $this->markTestSkipped('Phroute is not installed.'); - } - - break; + if ($router === 'Rougin\Slytherin\Routing\FastRouteRouter') { + $exists = class_exists('FastRoute\RouteCollector'); + + $message = (string) 'FastRoute is not installed.'; + + $exists || $this->markTestSkipped((string) $message); + } + + if ($router === 'Rougin\Slytherin\Routing\PhrouteRouter') { + $exists = class_exists('Phroute\Phroute\RouteCollector'); + + $message = (string) 'Phroute is not installed.'; + + $exists || $this->markTestSkipped((string) $message); } } } diff --git a/tests/Template/RendererTest.php b/tests/Template/RendererTest.php new file mode 100644 index 00000000..941866a0 --- /dev/null +++ b/tests/Template/RendererTest.php @@ -0,0 +1,73 @@ + + */ +class RendererTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Rougin\Slytherin\Template\RendererInterface + */ + protected $renderer; + + /** + * Sets up the renderer instance. + * + * @return void + */ + public function setUp() + { + $root = str_replace('Template', 'Fixture', __DIR__); + + $directories = (string) $root . '/Templates'; + + $this->renderer = new Renderer($directories); + } + + /** + * Tests RendererInterface::render. + * + * @return void + */ + public function testRenderMethod() + { + $expected = 'This is a text from a template.'; + + $result = $this->renderer->render('test'); + + $this->assertEquals($expected, $result); + } + + /** + * Tests RendererInterface::render with data. + * + * @return void + */ + public function testRenderMethodWithData() + { + $expected = (string) 'This is a text from a template.'; + + $data = array('name' => 'template'); + + $result = $this->renderer->render('test-with-data', $data); + + $this->assertEquals($expected, $result); + } + + /** + * Tests if the specified template is not found. + * + * @return void + */ + public function testTemplateNotFound() + { + $this->setExpectedException('InvalidArgumentException'); + + $this->renderer->render('hello'); + } +}