From a7f272bd5b1b6a349e40025687bcd99d2794a34e Mon Sep 17 00:00:00 2001 From: Rougin Gutib Date: Tue, 26 Dec 2023 17:09:08 +0800 Subject: [PATCH] Improve type hinting in unit tests --- tests/Application/ApplicationTest.php | 35 ++++++------- tests/Application/ApplicationTestCases.php | 18 +++---- tests/Application/AurynContainerTest.php | 2 + .../Application/IntegrationInterfaceTest.php | 20 ++++---- tests/Component/CollectionTest.php | 4 ++ tests/Container/AurynContainerTest.php | 7 ++- tests/Container/LeagueContainerTest.php | 2 + tests/Debug/Whoops/DebuggerTest.php | 2 + .../Dispatching/FastRoute/DispatcherTest.php | 2 + tests/Dispatching/FastRoute/RouterTest.php | 2 + tests/Dispatching/Phroute/DispatcherTest.php | 2 + tests/Dispatching/Phroute/RouterTest.php | 2 + tests/Fixture/Classes/Container.php | 44 ---------------- .../Middlewares/BodyParametersMiddleware.php | 41 --------------- tests/Fixture/Middlewares/CorsMiddleware.php | 43 ---------------- tests/Fixture/Middlewares/EmptyMiddleware.php | 37 -------------- tests/IoC/Auryn/ContainerTest.php | 2 + tests/IoC/League/ContainerTest.php | 2 + tests/IoC/Vanilla/ContainerTest.php | 11 ++-- tests/Middleware/DispatcherTestCases.php | 26 +++++----- .../Stratigility/MiddlewareTest.php | 2 + .../Middleware/StratigilityDispatcherTest.php | 4 +- tests/Previous/PreviousTest.php | 2 + tests/Routing/DispatcherTestCases.php | 22 ++++---- tests/Routing/RouterTestCases.php | 22 ++++---- tests/Sample/Handlers/ToJson.php | 26 ---------- tests/Sample/Router.php | 2 + tests/Sample/Routes/Hello.php | 8 +++ tests/Sample/SampleTest.php | 51 ++++++++++++++++++- tests/Template/Twig/RendererTest.php | 16 +++--- tests/Template/Vanilla/RendererTest.php | 16 +++--- tests/Testcase.php | 1 + 32 files changed, 192 insertions(+), 284 deletions(-) delete mode 100644 tests/Fixture/Classes/Container.php delete mode 100644 tests/Fixture/Middlewares/BodyParametersMiddleware.php delete mode 100644 tests/Fixture/Middlewares/CorsMiddleware.php delete mode 100644 tests/Fixture/Middlewares/EmptyMiddleware.php delete mode 100644 tests/Sample/Handlers/ToJson.php diff --git a/tests/Application/ApplicationTest.php b/tests/Application/ApplicationTest.php index e1417bbb..4e661223 100644 --- a/tests/Application/ApplicationTest.php +++ b/tests/Application/ApplicationTest.php @@ -8,7 +8,6 @@ use Rougin\Slytherin\Dispatching\Phroute\Router as PhrouteRouter; use Rougin\Slytherin\Dispatching\Vanilla\Router; use Rougin\Slytherin\Http\Uri; -use Rougin\Slytherin\IoC\Vanilla\Container; use Rougin\Slytherin\Testcase; /** @@ -57,7 +56,7 @@ public function testRunMethod() { $this->expectOutputString('Hello'); - $this->runApplication('GET', '/')->run(); + $this->setUrl('GET', '/')->run(); } /** @@ -70,7 +69,7 @@ public function testRunMethodWithResponse() { $this->expectOutputString('Hello with response'); - $this->runApplication('GET', '/hello')->run(); + $this->setUrl('GET', '/hello')->run(); } /** @@ -83,7 +82,7 @@ public function testRunMethodWithParameter() { $this->expectOutputString('Hello'); - $this->runApplication('GET', '/parameter')->run(); + $this->setUrl('GET', '/parameter')->run(); } /** @@ -96,7 +95,7 @@ public function testRunMethodWithOptionalParameter() { $this->expectOutputString('Hello'); - $this->runApplication('GET', '/optional')->run(); + $this->setUrl('GET', '/optional')->run(); } /** @@ -109,7 +108,7 @@ public function testRunMethodWithCallback() { $this->expectOutputString('Hello'); - $this->runApplication('GET', '/callback')->run(); + $this->setUrl('GET', '/callback')->run(); } /** @@ -122,7 +121,7 @@ public function testRunMethodWithPutHttpMethod() { $this->expectOutputString('Hello from PUT HTTP method'); - $this->runApplication('PUT', '/hello', array('_method' => 'PUT'))->run(); + $this->setUrl('PUT', '/hello', array('_method' => 'PUT'))->run(); } /** @@ -133,10 +132,12 @@ public function testRunMethodWithPutHttpMethod() */ public function testRunMethodWithPhroute() { + // @codeCoverageIgnoreStart if (! class_exists('Phroute\Phroute\RouteCollector')) { $this->markTestSkipped('Phroute is not installed.'); } + // @codeCoverageIgnoreEnd $this->expectOutputString('Hello'); @@ -150,7 +151,7 @@ public function testRunMethodWithPhroute() $this->components->setDispatcher($dispatcher); - $this->runApplication('GET', '/')->run(); + $this->setUrl('GET', '/')->run(); } /** @@ -200,7 +201,7 @@ public function testRunMethodWithIntegrateMethod() * @param array $data * @return \Rougin\Slytherin\Application */ - private function runApplication($method, $uri, $data = array()) + private function setUrl($method, $uri, $data = array()) { $result = $this->components->getHttp(); @@ -214,18 +215,14 @@ private function runApplication($method, $uri, $data = array()) $request = $request->withMethod($method)->withUri($uri); - switch ($method) + if ($method === 'GET') { - case 'GET': - $request = $request->withQueryParams($data); - - break; - case 'POST': - case 'PUT': - case 'DELETE': - $request = $request->withParsedBody($data); + $request = $request->withQueryParams($data); + } - break; + if (in_array($method, array('POST', 'PUT', 'DELETE'))) + { + $request = $request->withParsedBody($data); } $this->components->setHttp($request, $response); diff --git a/tests/Application/ApplicationTestCases.php b/tests/Application/ApplicationTestCases.php index 4b79cdd5..de055d48 100644 --- a/tests/Application/ApplicationTestCases.php +++ b/tests/Application/ApplicationTestCases.php @@ -26,7 +26,9 @@ class ApplicationTestCases extends Testcase */ protected function doSetUp() { + // @codeCoverageIgnoreStart $this->markTestSkipped('No implementation style defined.'); + // @codeCoverageIgnoreEnd } /** @@ -242,18 +244,14 @@ protected function request($method, $uri, $data = array(), $server = array()) $request = new ServerRequest($server); - switch ($method) + if ($method === 'GET') { - case 'GET': - $request = $request->withQueryParams($data); - - break; - case 'POST': - case 'PUT': - case 'DELETE': - $request = $request->withParsedBody($data); + $request = $request->withQueryParams($data); + } - break; + if (in_array($method, array('POST', 'PUT', 'DELETE'))) + { + $request = $request->withParsedBody($data); } return $request; diff --git a/tests/Application/AurynContainerTest.php b/tests/Application/AurynContainerTest.php index ab3a1522..33949148 100644 --- a/tests/Application/AurynContainerTest.php +++ b/tests/Application/AurynContainerTest.php @@ -24,10 +24,12 @@ class AurynContainerTest extends ApplicationTestCases */ protected function doSetUp() { + // @codeCoverageIgnoreStart if (! class_exists('Auryn\Injector')) { $this->markTestSkipped('Auryn is not installed.'); } + // @codeCoverageIgnoreEnd $container = new AurynContainer(new Injector); diff --git a/tests/Application/IntegrationInterfaceTest.php b/tests/Application/IntegrationInterfaceTest.php index dc09557d..c2f183ec 100644 --- a/tests/Application/IntegrationInterfaceTest.php +++ b/tests/Application/IntegrationInterfaceTest.php @@ -19,23 +19,21 @@ class IntegrationInterfaceTest extends ApplicationTestCases */ protected function doSetUp() { - $integrations = array(); + $items = array(); - $integrations[] = 'Rougin\Slytherin\Debug\ErrorHandlerIntegration'; - $integrations[] = 'Rougin\Slytherin\Http\HttpIntegration'; - $integrations[] = 'Rougin\Slytherin\Routing\RoutingIntegration'; - $integrations[] = 'Rougin\Slytherin\Middleware\MiddlewareIntegration'; - $integrations[] = 'Rougin\Slytherin\Template\RendererIntegration'; - $integrations[] = 'Rougin\Slytherin\Integration\ConfigurationIntegration'; + $items[] = 'Rougin\Slytherin\Debug\ErrorHandlerIntegration'; + $items[] = 'Rougin\Slytherin\Http\HttpIntegration'; + $items[] = 'Rougin\Slytherin\Routing\RoutingIntegration'; + $items[] = 'Rougin\Slytherin\Middleware\MiddlewareIntegration'; + $items[] = 'Rougin\Slytherin\Template\RendererIntegration'; + $items[] = 'Rougin\Slytherin\Integration\ConfigurationIntegration'; $config = new Configuration; - $router = $this->router(); - - $config->set('app.router', $router); + $config->set('app.router', $this->router()); $app = new Application; - $this->application = $app->integrate($integrations, $config); + $this->application = $app->integrate($items, $config); } } diff --git a/tests/Component/CollectionTest.php b/tests/Component/CollectionTest.php index 7af72f9d..c46b4bb1 100644 --- a/tests/Component/CollectionTest.php +++ b/tests/Component/CollectionTest.php @@ -190,10 +190,12 @@ public function testSetHttpResponseMethod() */ public function testSetMiddlewareMethod() { + // @codeCoverageIgnoreStart if (! Interop::exists()) { $this->markTestSkipped('Interop middleware/s not yet installed'); } + // @codeCoverageIgnoreEnd $expected = new VanillaMiddleware; @@ -213,10 +215,12 @@ public function testSetTemplateMethod() { $twig = new TwigLoader; + // @codeCoverageIgnoreStart if (! $twig->exists()) { $this->markTestSkipped('Twig is not installed.'); } + // @codeCoverageIgnoreEnd /** @var string */ $path = realpath(__DIR__ . '/../../Fixture/Templates'); diff --git a/tests/Container/AurynContainerTest.php b/tests/Container/AurynContainerTest.php index ad55de83..3902dce0 100644 --- a/tests/Container/AurynContainerTest.php +++ b/tests/Container/AurynContainerTest.php @@ -22,7 +22,12 @@ class AurynContainerTest extends \Rougin\Slytherin\Testcase */ protected function doSetUp() { - class_exists('Auryn\Injector') || $this->markTestSkipped('Auryn is not installed.'); + // @codeCoverageIgnoreStart + if (! class_exists('Auryn\Injector')) + { + $this->markTestSkipped('Auryn is not installed.'); + } + // @codeCoverageIgnoreEnd $this->container = new \Rougin\Slytherin\Container\AurynContainer(new \Auryn\Injector); } diff --git a/tests/Container/LeagueContainerTest.php b/tests/Container/LeagueContainerTest.php index 088e4060..62740133 100644 --- a/tests/Container/LeagueContainerTest.php +++ b/tests/Container/LeagueContainerTest.php @@ -25,10 +25,12 @@ class LeagueContainerTest extends Testcase */ protected function doSetUp() { + // @codeCoverageIgnoreStart if (! class_exists('League\Container\Container')) { $this->markTestSkipped('League Container is not installed.'); } + // @codeCoverageIgnoreEnd $this->container = new LeagueContainer; } diff --git a/tests/Debug/Whoops/DebuggerTest.php b/tests/Debug/Whoops/DebuggerTest.php index 7898ce70..91d5821b 100644 --- a/tests/Debug/Whoops/DebuggerTest.php +++ b/tests/Debug/Whoops/DebuggerTest.php @@ -32,10 +32,12 @@ class DebuggerTest extends Testcase */ protected function doSetUp() { + // @codeCoverageIgnoreStart if (! class_exists('Whoops\Run')) { $this->markTestSkipped('Whoops is not installed.'); } + // @codeCoverageIgnoreEnd $this->debugger = new Debugger(new \Whoops\Run); } diff --git a/tests/Dispatching/FastRoute/DispatcherTest.php b/tests/Dispatching/FastRoute/DispatcherTest.php index 0715ec3a..6045f12f 100644 --- a/tests/Dispatching/FastRoute/DispatcherTest.php +++ b/tests/Dispatching/FastRoute/DispatcherTest.php @@ -27,10 +27,12 @@ class DispatcherTest extends Testcase */ protected function doSetUp() { + // @codeCoverageIgnoreStart if (! interface_exists('FastRoute\Dispatcher')) { $this->markTestSkipped('FastRoute is not installed.'); } + // @codeCoverageIgnoreEnd $middleware = 'Rougin\Slytherin\Fixture\Middlewares\LastMiddleware'; diff --git a/tests/Dispatching/FastRoute/RouterTest.php b/tests/Dispatching/FastRoute/RouterTest.php index 99712d70..bb871be3 100644 --- a/tests/Dispatching/FastRoute/RouterTest.php +++ b/tests/Dispatching/FastRoute/RouterTest.php @@ -33,10 +33,12 @@ class RouterTest extends Testcase */ protected function doSetUp() { + // @codeCoverageIgnoreStart if (! class_exists('FastRoute\RouteCollector')) { $this->markTestSkipped('FastRoute is not installed.'); } + // @codeCoverageIgnoreEnd // Generate a sample route for testing -------------- $class = 'Rougin\Slytherin\Fixture\Classes\NewClass'; diff --git a/tests/Dispatching/Phroute/DispatcherTest.php b/tests/Dispatching/Phroute/DispatcherTest.php index a159d3c6..7abd0702 100644 --- a/tests/Dispatching/Phroute/DispatcherTest.php +++ b/tests/Dispatching/Phroute/DispatcherTest.php @@ -31,10 +31,12 @@ class DispatcherTest extends Testcase */ protected function doSetUp() { + // @codeCoverageIgnoreStart if (! class_exists('Phroute\Phroute\Dispatcher')) { $this->markTestSkipped('Phroute is not installed.'); } + // @codeCoverageIgnoreEnd $routes = array(); $routes[] = array('GET', '/', array('Rougin\Slytherin\Fixture\Classes\NewClass', 'index')); diff --git a/tests/Dispatching/Phroute/RouterTest.php b/tests/Dispatching/Phroute/RouterTest.php index c168291c..1f0d84c1 100644 --- a/tests/Dispatching/Phroute/RouterTest.php +++ b/tests/Dispatching/Phroute/RouterTest.php @@ -31,10 +31,12 @@ class RouterTest extends Testcase */ protected function doSetUp() { + // @codeCoverageIgnoreStart if (! class_exists('Phroute\Phroute\RouteCollector')) { $this->markTestSkipped('Phroute is not installed.'); } + // @codeCoverageIgnoreEnd // Generate a sample route for testing -------------- $class = 'Rougin\Slytherin\Fixture\Classes\NewClass'; diff --git a/tests/Fixture/Classes/Container.php b/tests/Fixture/Classes/Container.php deleted file mode 100644 index ed94d25b..00000000 --- a/tests/Fixture/Classes/Container.php +++ /dev/null @@ -1,44 +0,0 @@ - - */ -class Container implements ContainerInterface -{ - /** - * @var array - */ - protected $instances = array(); - - /** - * Finds an entry of the container by its identifier and returns it. - * - * @throws \Psr\Container\NotFoundExceptionInterface - * @throws \Psr\Container\ContainerExceptionInterface - * - * @param string $id - * @return mixed - */ - public function get($id) - { - return $this->instances[$id]; - } - - /** - * Returns true if the container can return an entry for the given identifier. - * - * @param string $id - * @return boolean - */ - public function has($id) - { - return isset($this->instances[$id]); - } -} diff --git a/tests/Fixture/Middlewares/BodyParametersMiddleware.php b/tests/Fixture/Middlewares/BodyParametersMiddleware.php deleted file mode 100644 index 3983b326..00000000 --- a/tests/Fixture/Middlewares/BodyParametersMiddleware.php +++ /dev/null @@ -1,41 +0,0 @@ - - */ -class BodyParametersMiddleware implements MiddlewareInterface -{ - /** - * @var string[] - */ - protected $complex = array('PUT', 'DELETE'); - - /** - * @param \Psr\Http\Message\ServerRequestInterface $request - * @param \Rougin\Slytherin\Middleware\HandlerInterface $handler - * @return \Psr\Http\Message\ResponseInterface - */ - public function process(ServerRequestInterface $request, HandlerInterface $handler) - { - if (! in_array($request->getMethod(), $this->complex)) - { - return $handler->handle($request); - } - - /** @var string */ - $file = file_get_contents('php://input'); - - parse_str($file, $body); - - $request = $request->withParsedBody($body); - - return $handler->handle($request); - } -} diff --git a/tests/Fixture/Middlewares/CorsMiddleware.php b/tests/Fixture/Middlewares/CorsMiddleware.php deleted file mode 100644 index 7ee0e677..00000000 --- a/tests/Fixture/Middlewares/CorsMiddleware.php +++ /dev/null @@ -1,43 +0,0 @@ - - */ -class CorsMiddleware implements MiddlewareInterface -{ - /** - * @var string[] - */ - protected $allowed = array('*'); - - /** - * @var string[] - */ - protected $methods = array('GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'); - - /** - * @param \Psr\Http\Message\ServerRequestInterface $request - * @param \Rougin\Slytherin\Middleware\HandlerInterface $handler - * @return \Psr\Http\Message\ResponseInterface - */ - public function process(ServerRequestInterface $request, HandlerInterface $handler) - { - $isOptions = $request->getMethod() === 'OPTIONS'; - - $response = $isOptions ? new Response : $handler->handle($request); - - $response = $response->withHeader('Access-Control-Allow-Origin', $this->allowed); - - $response = $response->withHeader('Access-Control-Allow-Methods', $this->methods); - - return $response; - } -} diff --git a/tests/Fixture/Middlewares/EmptyMiddleware.php b/tests/Fixture/Middlewares/EmptyMiddleware.php deleted file mode 100644 index 33da8938..00000000 --- a/tests/Fixture/Middlewares/EmptyMiddleware.php +++ /dev/null @@ -1,37 +0,0 @@ - - */ -class EmptyMiddleware implements MiddlewareInterface -{ - /** - * @param \Psr\Http\Message\ServerRequestInterface $request - * @param \Psr\Http\Message\ResponseInterface $response - * @param callable|null $next - * @return \Psr\Http\Message\ResponseInterface - */ - public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next = null) - { - /** @phpstan-ignore-next-line */ - return $next($request, $response); - } - - /** - * @param \Psr\Http\Message\ServerRequestInterface $request - * @param \Rougin\Slytherin\Middleware\HandlerInterface $handler - * @return \Psr\Http\Message\ResponseInterface - */ - public function process(ServerRequestInterface $request, HandlerInterface $handler) - { - return $handler->handle($request); - } -} diff --git a/tests/IoC/Auryn/ContainerTest.php b/tests/IoC/Auryn/ContainerTest.php index 12395174..6c7fb02a 100644 --- a/tests/IoC/Auryn/ContainerTest.php +++ b/tests/IoC/Auryn/ContainerTest.php @@ -39,10 +39,12 @@ class ContainerTest extends Testcase */ protected function doSetUp() { + // @codeCoverageIgnoreStart if (! class_exists('Auryn\Injector')) { $this->markTestSkipped('Auryn is not installed.'); } + // @codeCoverageIgnoreEnd $this->container = new Container(new Injector); diff --git a/tests/IoC/League/ContainerTest.php b/tests/IoC/League/ContainerTest.php index 6a8e0986..247e107d 100644 --- a/tests/IoC/League/ContainerTest.php +++ b/tests/IoC/League/ContainerTest.php @@ -38,10 +38,12 @@ class ContainerTest extends Testcase */ protected function doSetUp() { + // @codeCoverageIgnoreStart if (! class_exists('League\Container\Container')) { $this->markTestSkipped('League Container is not installed.'); } + // @codeCoverageIgnoreEnd $this->container = new Container; diff --git a/tests/IoC/Vanilla/ContainerTest.php b/tests/IoC/Vanilla/ContainerTest.php index f562be5e..dbcdf28a 100644 --- a/tests/IoC/Vanilla/ContainerTest.php +++ b/tests/IoC/Vanilla/ContainerTest.php @@ -173,14 +173,15 @@ public function testGetMethodWithContainerException() */ public function testGetMethodWithInterface() { - $class = 'Rougin\Slytherin\Fixture\Classes\WithInterface'; - $classWithParameter = 'Rougin\Slytherin\Fixture\Classes\WithInterfaceParameter'; - $interface = 'Rougin\Slytherin\Fixture\Classes\NewInterface'; + $withParam = 'Rougin\Slytherin\Fixture\Classes\WithInterfaceParameter'; + + $interface = 'Rougin\Slytherin\Fixture\Classes\NewInterface'; $this->container->add($interface, new WithInterface); - $this->container->add($classWithParameter, $this->container->get($interface)); - $this->assertTrue($this->container->has($classWithParameter)); + $this->container->add($withParam, $this->container->get($interface)); + + $this->assertTrue($this->container->has($withParam)); } /** diff --git a/tests/Middleware/DispatcherTestCases.php b/tests/Middleware/DispatcherTestCases.php index 04cebf51..1e7a4616 100644 --- a/tests/Middleware/DispatcherTestCases.php +++ b/tests/Middleware/DispatcherTestCases.php @@ -17,7 +17,7 @@ class DispatcherTestCases extends Testcase { /** - * @var \Rougin\Slytherin\Middleware\DispatcherInterface + * @var \Rougin\Slytherin\Middleware\Dispatcher|\Rougin\Slytherin\Middleware\StratigilityDispatcher */ protected $dispatcher; @@ -58,10 +58,12 @@ public function testProcessMethodWithSinglePassCallback() /** @var \Rougin\Slytherin\Middleware\StratigilityDispatcher */ $zend = $this->dispatcher; + // @codeCoverageIgnoreStart if (! $zend->hasPsr() && ! $zend->hasFactory()) { $this->markTestSkipped('Current Stratigility version does not support single pass callbacks'); } + // @codeCoverageIgnoreEnd } $time = (integer) time(); @@ -96,10 +98,12 @@ public function testProcessMethodWithDelagateInterfaceCallback() /** @var \Rougin\Slytherin\Middleware\StratigilityDispatcher */ $zend = $this->dispatcher; + // @codeCoverageIgnoreStart if (! $zend->hasPsr() && ! $zend->hasFactory()) { $this->markTestSkipped('Current Stratigility version does not support single pass callbacks'); } + // @codeCoverageIgnoreEnd } $fn = function ($request, $next) @@ -125,10 +129,12 @@ public function testProcessMethodWithDelagateInterfaceCallback() */ public function testProcessMethodWithString() { + // @codeCoverageIgnoreStart if (! Interop::exists()) { - $this->markTestSkipped('Interop middleware/s not yet installed'); + $this->markTestSkipped('Interop middleware/s not installed.'); } + // @codeCoverageIgnoreEnd $interop = 'Rougin\Slytherin\Fixture\Middlewares\InteropMiddleware'; @@ -148,10 +154,12 @@ public function testProcessMethodWithString() */ public function testPushMethodWithArray() { + // @codeCoverageIgnoreStart if (! Interop::exists()) { - $this->markTestSkipped('Interop middleware/s not yet installed'); + $this->markTestSkipped('Interop middleware/s not installed.'); } + // @codeCoverageIgnoreEnd $interop = 'Rougin\Slytherin\Fixture\Middlewares\InteropMiddleware'; @@ -159,11 +167,7 @@ public function testPushMethodWithArray() $this->dispatcher->push(array($interop)); - // NOTE: Ignore for backward compatibility check --- - /** @disregard P1013 */ - /** @phpstan-ignore-next-line */ $actual = $this->dispatcher->stack(); - // ------------------------------------------------- $this->assertEquals($expected, $actual); } @@ -175,18 +179,16 @@ public function testPushMethodWithArray() */ public function testStackMethod() { + // @codeCoverageIgnoreStart if (! Interop::exists()) { - $this->markTestSkipped('Interop middleware/s not yet installed'); + $this->markTestSkipped('Interop middleware/s not installed.'); } + // @codeCoverageIgnoreEnd $this->dispatcher->push('Rougin\Slytherin\Fixture\Middlewares\InteropMiddleware'); - // NOTE: Ignore for backward compatibility check --- - /** @disregard P1013 */ - /** @phpstan-ignore-next-line */ $actual = $this->dispatcher->stack(); - // ------------------------------------------------- $this->assertCount(1, $actual); } diff --git a/tests/Middleware/Stratigility/MiddlewareTest.php b/tests/Middleware/Stratigility/MiddlewareTest.php index 3e41d0a1..9648167c 100644 --- a/tests/Middleware/Stratigility/MiddlewareTest.php +++ b/tests/Middleware/Stratigility/MiddlewareTest.php @@ -20,10 +20,12 @@ class MiddlewareTest extends Testcase */ public function testInvokeMethod() { + // @codeCoverageIgnoreStart if (! class_exists('Zend\Stratigility\MiddlewarePipe')) { $this->markTestSkipped('Zend Stratigility is not installed.'); } + // @codeCoverageIgnoreEnd $server = array(); $server['REQUEST_METHOD'] = 'GET'; diff --git a/tests/Middleware/StratigilityDispatcherTest.php b/tests/Middleware/StratigilityDispatcherTest.php index 56f41505..f165e730 100644 --- a/tests/Middleware/StratigilityDispatcherTest.php +++ b/tests/Middleware/StratigilityDispatcherTest.php @@ -17,10 +17,12 @@ class StratigilityDispatcherTest extends DispatcherTestCases */ protected function doSetUp() { + // @codeCoverageIgnoreStart if (! class_exists('Zend\Stratigility\MiddlewarePipe')) { - $this->markTestSkipped('Zend Stratigility is not installed'); + $this->markTestSkipped('Zend Stratigility is not installed.'); } + // @codeCoverageIgnoreEnd $pipe = new MiddlewarePipe; diff --git a/tests/Previous/PreviousTest.php b/tests/Previous/PreviousTest.php index 1525a665..1d6816c8 100644 --- a/tests/Previous/PreviousTest.php +++ b/tests/Previous/PreviousTest.php @@ -21,6 +21,7 @@ class PreviousTest extends Testcase */ protected function doSetUp() { + // @codeCoverageIgnoreStart if (! class_exists('Auryn\Injector')) { $this->markTestSkipped('Auryn is not installed.'); @@ -40,6 +41,7 @@ protected function doSetUp() { $this->markTestSkipped('Twig v1.0~v2.0 is not installed.'); } + // @codeCoverageIgnoreEnd $this->builder = new Builder; } diff --git a/tests/Routing/DispatcherTestCases.php b/tests/Routing/DispatcherTestCases.php index a193921f..710f3684 100644 --- a/tests/Routing/DispatcherTestCases.php +++ b/tests/Routing/DispatcherTestCases.php @@ -132,20 +132,22 @@ protected function exists($dispatcher) { if ($dispatcher === 'Rougin\Slytherin\Routing\FastRouteDispatcher') { - $exists = interface_exists('FastRoute\Dispatcher'); - - $message = (string) 'FastRoute is not installed.'; - - $exists || $this->markTestSkipped((string) $message); + // @codeCoverageIgnoreStart + if (! interface_exists('FastRoute\Dispatcher')) + { + $this->markTestSkipped('FastRoute is not installed.'); + } + // @codeCoverageIgnoreEnd } if ($dispatcher === 'Rougin\Slytherin\Routing\PhrouteDispatcher') { - $exists = class_exists('Phroute\Phroute\Dispatcher'); - - $message = (string) 'Phroute is not installed.'; - - $exists || $this->markTestSkipped((string) $message); + // @codeCoverageIgnoreStart + if (! class_exists('Phroute\Phroute\Dispatcher')) + { + $this->markTestSkipped('Phroute is not installed.'); + } + // @codeCoverageIgnoreEnd } } diff --git a/tests/Routing/RouterTestCases.php b/tests/Routing/RouterTestCases.php index 2f5ab546..bcaacb81 100644 --- a/tests/Routing/RouterTestCases.php +++ b/tests/Routing/RouterTestCases.php @@ -249,20 +249,22 @@ protected function exists($router) { if ($router === 'Rougin\Slytherin\Routing\FastRouteRouter') { - $exists = class_exists('FastRoute\RouteCollector'); - - $message = 'FastRoute is not installed.'; - - if (! $exists) $this->markTestSkipped($message); + // @codeCoverageIgnoreStart + if (! class_exists('FastRoute\RouteCollector')) + { + $this->markTestSkipped('FastRoute is not installed.'); + } + // @codeCoverageIgnoreEnd } if ($router === 'Rougin\Slytherin\Routing\PhrouteRouter') { - $exists = class_exists('Phroute\Phroute\RouteCollector'); - - $message = 'Phroute is not installed.'; - - if (! $exists) $this->markTestSkipped($message); + // @codeCoverageIgnoreStart + if (! class_exists('Phroute\Phroute\RouteCollector')) + { + $this->markTestSkipped('Phroute is not installed.'); + } + // @codeCoverageIgnoreEnd } } } diff --git a/tests/Sample/Handlers/ToJson.php b/tests/Sample/Handlers/ToJson.php deleted file mode 100644 index 512d6783..00000000 --- a/tests/Sample/Handlers/ToJson.php +++ /dev/null @@ -1,26 +0,0 @@ - - */ -class ToJson implements MiddlewareInterface -{ - /** - * @param \Psr\Http\Message\ServerRequestInterface $request - * @param \Rougin\Slytherin\Middleware\HandlerInterface $handler - * @return \Psr\Http\Message\ResponseInterface - */ - public function process(ServerRequestInterface $request, HandlerInterface $handler) - { - $response = $handler->handle($request); - - return $response->withHeader('Content-Type', 'application/json'); - } -} diff --git a/tests/Sample/Router.php b/tests/Sample/Router.php index 54a5b42b..0da065fa 100644 --- a/tests/Sample/Router.php +++ b/tests/Sample/Router.php @@ -75,6 +75,8 @@ public function routes() $this->get('interop', 'Hello@response', $interop); + $this->get('encoded', 'Hello@encoded'); + return parent::routes(); } } diff --git a/tests/Sample/Routes/Hello.php b/tests/Sample/Routes/Hello.php index 34df7429..8fd1362d 100644 --- a/tests/Sample/Routes/Hello.php +++ b/tests/Sample/Routes/Hello.php @@ -91,4 +91,12 @@ public function world() { return 'Hello string world!'; } + + /** + * @return \Psr\Http\Message\ResponseInterface + */ + public function encoded() + { + return $this->json('Encoded world!'); + } } diff --git a/tests/Sample/SampleTest.php b/tests/Sample/SampleTest.php index a6078a7d..2a2304fa 100644 --- a/tests/Sample/SampleTest.php +++ b/tests/Sample/SampleTest.php @@ -4,6 +4,7 @@ use Rougin\Slytherin\Middleware\Interop; use Rougin\Slytherin\Sample\Builder; +use Rougin\Slytherin\Sample\Handlers\Cors; use Rougin\Slytherin\Sample\Handlers\Parsed\Request; use Rougin\Slytherin\Sample\Handlers\Parsed\Response; use Rougin\Slytherin\Sample\Packages\MiddlewarePackage; @@ -27,6 +28,16 @@ class SampleTest extends Testcase protected function doSetUp() { $this->builder = new Builder; + + $this->builder->setCookies($_COOKIE); + + $this->builder->setFiles($_FILES); + + $this->builder->setQuery((array) $_GET); + + $this->builder->setParsed($_POST); + + $this->builder->setServer($_SERVER); } /** @@ -163,6 +174,20 @@ public function test_callable_as_the_route_with_params_and_string_only_as_the_ou $this->builder->make()->run(); } + /** + * @runInSeparateProcess + * + * @return void + */ + public function test_callable_as_the_route_with_multiple_params() + { + $this->builder->setUrl('GET', '/call/Slytherin/18'); + + $this->expectOutputString('Welcome Slytherin, 18!'); + + $this->builder->make()->run(); + } + /** * @runInSeparateProcess * @@ -170,6 +195,8 @@ public function test_callable_as_the_route_with_params_and_string_only_as_the_ou */ public function test_middleware_changing_the_request_constructor() { + $this->builder->addHandler(new Cors); + $this->builder->addPackage(new MiddlewarePackage); $this->builder->addHandler(new Request); @@ -188,6 +215,8 @@ public function test_middleware_changing_the_request_constructor() */ public function test_middleware_changing_the_request_parameter() { + $this->builder->addHandler(new Cors); + $this->builder->addPackage(new MiddlewarePackage); $this->builder->addHandler(new Request); @@ -206,6 +235,8 @@ public function test_middleware_changing_the_request_parameter() */ public function test_middleware_changing_the_response_parameter() { + $this->builder->addHandler(new Cors); + $this->builder->addPackage(new MiddlewarePackage); $this->builder->addHandler(new Response); @@ -242,10 +273,12 @@ public function test_interop_middleware_changing_the_response_parameter() { $this->builder->addPackage(new MiddlewarePackage); + // @codeCoverageIgnoreStart if (! Interop::exists()) { - $this->markTestSkipped('Interop middleware/s not yet installed'); + $this->markTestSkipped('Interop middleware/s not installed.'); } + // @codeCoverageIgnoreEnd $this->builder->setUrl('GET', '/interop'); @@ -290,4 +323,20 @@ public function test_uploaded_file_from_request() $this->builder->make()->run(); } + + /** + * @runInSeparateProcess + * + * @return void + */ + public function test_with_route_to_json() + { + $this->builder->addPackage(new SamplePackage); + + $this->builder->setUrl('GET', '/encoded'); + + $this->expectOutputString('"Encoded world!"'); + + $this->builder->make()->run(); + } } diff --git a/tests/Template/Twig/RendererTest.php b/tests/Template/Twig/RendererTest.php index 9ab8bec6..a5194df6 100644 --- a/tests/Template/Twig/RendererTest.php +++ b/tests/Template/Twig/RendererTest.php @@ -13,7 +13,7 @@ class RendererTest extends Testcase { /** - * @var \Rougin\Slytherin\Template\TwigRenderer + * @var \Rougin\Slytherin\Template\RendererInterface */ protected $renderer; @@ -31,10 +31,12 @@ protected function doSetUp() { $twig = new TwigLoader; + // @codeCoverageIgnoreStart if (! $twig->exists()) { $this->markTestSkipped('Twig is not installed.'); } + // @codeCoverageIgnoreEnd $path = realpath(__DIR__ . '/../../Fixture/Templates'); @@ -52,9 +54,9 @@ public function testRenderMethod() { $expected = 'This is a text from a template.'; - $rendered = $this->renderer->render('test', array(), 'php'); + $actual = $this->renderer->render('test', array(), 'php'); - $this->assertEquals($expected, $rendered); + $this->assertEquals($expected, $actual); } /** @@ -68,9 +70,9 @@ public function testRenderMethodWithData() $data = array('name' => 'template'); - $rendered = $this->renderer->render('test-with-twig-data', $data, 'php'); + $actual = $this->renderer->render('test-with-twig-data', $data, 'php'); - $this->assertEquals($expected, $rendered); + $this->assertEquals($expected, $actual); } /** @@ -88,8 +90,8 @@ public function testRenderMethodWithGlobals() $renderer->addGlobal('test', 'wew'); - $rendered = $renderer->render('test-with-twig-data', array(), 'php'); + $actual = $renderer->render('test-with-twig-data', array(), 'php'); - $this->assertEquals($expected, $rendered); + $this->assertEquals($expected, $actual); } } diff --git a/tests/Template/Vanilla/RendererTest.php b/tests/Template/Vanilla/RendererTest.php index 41d55e2e..5e4f91a7 100644 --- a/tests/Template/Vanilla/RendererTest.php +++ b/tests/Template/Vanilla/RendererTest.php @@ -2,11 +2,13 @@ namespace Rougin\Slytherin\Template\Vanilla; +use Rougin\Slytherin\Testcase; + /** * @package Slytherin * @author Rougin Gutib */ -class RendererTest extends \Rougin\Slytherin\Testcase +class RendererTest extends Testcase { /** * @var \Rougin\Slytherin\Template\RendererInterface @@ -20,9 +22,9 @@ class RendererTest extends \Rougin\Slytherin\Testcase */ protected function doSetUp() { - $directories = array(__DIR__ . '/../../Fixture/Templates'); + $paths = array(__DIR__ . '/../../Fixture/Templates'); - $this->renderer = new \Rougin\Slytherin\Template\Vanilla\Renderer($directories); + $this->renderer = new Renderer($paths); } /** @@ -34,7 +36,9 @@ public function testRenderMethod() { $result = 'This is a text from a template.'; - $this->assertEquals($result, $this->renderer->render('test')); + $actual = $this->renderer->render('test'); + + $this->assertEquals($result, $actual); } /** @@ -48,9 +52,9 @@ public function testRenderMethodWithData() $data = array('name' => 'template'); - $rendered = $this->renderer->render('test-with-data', $data); + $actual = $this->renderer->render('test-with-data', $data); - $this->assertEquals($expected, $rendered); + $this->assertEquals($expected, $actual); } /** diff --git a/tests/Testcase.php b/tests/Testcase.php index 133ef991..a07dcb57 100644 --- a/tests/Testcase.php +++ b/tests/Testcase.php @@ -7,6 +7,7 @@ /** * @package Slytherin * @author Rougin Gutib + * @codeCoverageIgnore */ class Testcase extends Legacy {