Skip to content

Commit

Permalink
Improve type hinting in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Dec 26, 2023
1 parent a3d68dd commit b33f5aa
Show file tree
Hide file tree
Showing 24 changed files with 198 additions and 131 deletions.
2 changes: 1 addition & 1 deletion src/Routing/FastRouteRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class FastRouteRouter extends Router
/**
* Initializes the router instance.
*
* @param array<int, array<int, \Rougin\Slytherin\Middleware\MiddlewareInterface[]|string[]|string>> $routes
* @param array<int, array<int, \Rougin\Slytherin\Middleware\MiddlewareInterface[]|callable|string|string[]>> $routes
*/
public function __construct(array $routes = array())
{
Expand Down
2 changes: 1 addition & 1 deletion src/Routing/PhrouteRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PhrouteRouter extends Router
/**
* Initializes the router instance.
*
* @param array<int, array<int, \Rougin\Slytherin\Middleware\MiddlewareInterface[]|string[]|string>> $routes
* @param array<int, array<int, \Rougin\Slytherin\Middleware\MiddlewareInterface[]|callable|string|string[]>> $routes
*/
public function __construct(array $routes = array())
{
Expand Down
2 changes: 1 addition & 1 deletion src/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Router implements RouterInterface
/**
* Initializes the router instance.
*
* @param array<int, array<int, \Rougin\Slytherin\Middleware\MiddlewareInterface[]|string[]|string>> $routes
* @param array<int, array<int, \Rougin\Slytherin\Middleware\MiddlewareInterface[]|callable|string|string[]>> $routes
*/
public function __construct(array $routes = array())
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Application/ApplicationTestCases.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class ApplicationTestCases extends Testcase
{
/**
* @var \Rougin\Slytherin\Application
* @var \Rougin\Slytherin\System
*/
protected $application;

Expand Down
1 change: 1 addition & 0 deletions tests/Component/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ public function testSetTemplateMethod()
$this->markTestSkipped('Twig is not installed.');
}

/** @var string */
$path = realpath(__DIR__ . '/../../Fixture/Templates');

$environment = $twig->load($path);
Expand Down
2 changes: 2 additions & 0 deletions tests/Component/Server.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

// @codeCoverageIgnoreStart
use Rougin\Slytherin\Application;
use Rougin\Slytherin\Component\Collector;

Expand All @@ -18,3 +19,4 @@
$app = new Application($components);

$app->run();
// @codeCoverageIgnoreEnd
3 changes: 2 additions & 1 deletion tests/Container/ReflectionContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class ReflectionContainerTest extends Testcase
{
/**
* @var \Rougin\Slytherin\Container\ContainerInterface
* @var \Psr\Container\ContainerInterface
*/
protected $container;

Expand Down Expand Up @@ -66,6 +66,7 @@ public function testGetMethodWithMultipleParameters()

$expected = 'With multiple parameters';

/** @var \Rougin\Slytherin\Fixture\Classes\ParameterClass */
$object = $this->container->get($class);

$this->assertEquals($expected, $object->index());
Expand Down
65 changes: 34 additions & 31 deletions tests/Dispatching/FastRoute/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Rougin\Slytherin\Dispatching\FastRoute;

use Rougin\Slytherin\Dispatching\Vanilla\Router as Vanilla;
use Rougin\Slytherin\Fixture\Classes\NewClass;
use Rougin\Slytherin\Routing\Route;
use Rougin\Slytherin\Testcase;
Expand All @@ -15,7 +16,7 @@
class DispatcherTest extends Testcase
{
/**
* @var \Rougin\Slytherin\Dispatching\DispatcherInterface
* @var \Rougin\Slytherin\Routing\DispatcherInterface
*/
protected $dispatcher;

Expand All @@ -26,30 +27,21 @@ class DispatcherTest extends Testcase
*/
protected function doSetUp()
{
if (! interface_exists('FastRoute\Dispatcher')) {
if (! interface_exists('FastRoute\Dispatcher'))
{
$this->markTestSkipped('FastRoute is not installed.');
}

$routes = array(
array(
'GET',
'/',
array('Rougin\Slytherin\Fixture\Classes\NewClass', 'index'),
'Rougin\Slytherin\Fixture\Middlewares\LastMiddleware'
),
array('GET', '/hi', function () {
return 'Hi';
}),
array('TEST', '/hello', function () {
return 'It must not go through here';
}),
);

$router = new \Rougin\Slytherin\Dispatching\FastRoute\Router($routes);

$dispatcher = new \Rougin\Slytherin\Dispatching\FastRoute\Dispatcher($router);

$this->dispatcher = $dispatcher;
$middleware = 'Rougin\Slytherin\Fixture\Middlewares\LastMiddleware';

$routes = array();
$routes[] = array('GET', '/', array('Rougin\Slytherin\Fixture\Classes\NewClass', 'index'), $middleware);
$routes[] = array('GET', '/hi', function () { return 'Hi'; });
$routes[] = array('TEST', '/hello', function () { return 'It must not go through here'; });

$router = new Router($routes);

$this->dispatcher = new Dispatcher($router);
}

/**
Expand All @@ -65,11 +57,17 @@ public function testDispatchMethod()

$route = $this->dispatcher->dispatch('GET', '/');

list($class, $method) = $route->getHandler();
/** @var string */
$handler = $route->getHandler();

$class = $handler[0]; $method = $handler[1];

$params = (array) $route->getParams();

/** @var callable */
$object = array(new $class, $method);

$actual = call_user_func_array($object, $route->getParams());
$actual = call_user_func_array($object, $params);

$this->assertEquals($expected, $actual);
}
Expand All @@ -83,11 +81,12 @@ public function testDispatchMethodWithClosure()
{
$route = $this->dispatcher->dispatch('GET', '/hi');

/** @var callable */
$callback = $route->getHandler();

$parameters = $route->getParams();
$params = $route->getParams();

$actual = call_user_func($callback, $parameters);
$actual = call_user_func($callback, $params);

$this->assertEquals('Hi', $actual);
}
Expand Down Expand Up @@ -147,14 +146,18 @@ public function testDispatcherInterface()
*/
public function testDispatchMethodWithDifferentRouter()
{
$routes = array(array('GET', '/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index', 'Rougin\Slytherin\Fixture\Middlewares\LastMiddleware'));
$middleware = 'Rougin\Slytherin\Fixture\Middlewares\LastMiddleware';

$routes = array(array('GET', '/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index', $middleware));

$router = new \Rougin\Slytherin\Dispatching\Vanilla\Router($routes);
$router = new Vanilla($routes);

$dispatcher = new \Rougin\Slytherin\Dispatching\FastRoute\Dispatcher($router);
$dispatcher = new Dispatcher($router);

$expected = new Route('GET', '/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index', 'Rougin\Slytherin\Fixture\Middlewares\LastMiddleware');
$expected = new Route('GET', '/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index', $middleware);

$this->assertEquals($expected, $dispatcher->dispatch('GET', '/'));
$actual = $dispatcher->dispatch('GET', '/');

$this->assertEquals($expected, $actual);
}
}
50 changes: 28 additions & 22 deletions tests/Dispatching/Phroute/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace Rougin\Slytherin\Dispatching\Phroute;

use Rougin\Slytherin\Container\Container;
use Rougin\Slytherin\Dispatching\Vanilla\Router as Vanilla;
use Rougin\Slytherin\Fixture\Classes\NewClass;
use Rougin\Slytherin\Routing\PhrouteResolver;
use Rougin\Slytherin\Testcase;

/**
Expand All @@ -17,12 +15,12 @@
class DispatcherTest extends Testcase
{
/**
* @var \Rougin\Slytherin\Dispatching\DispatcherInterface
* @var \Rougin\Slytherin\Routing\DispatcherInterface
*/
protected $dispatcher;

/**
* @var array
* @var array<int, array<int, \Rougin\Slytherin\Middleware\MiddlewareInterface[]|callable|string|string[]>>
*/
protected $routes = array();

Expand All @@ -38,21 +36,16 @@ protected function doSetUp()
$this->markTestSkipped('Phroute is not installed.');
}

$this->routes = array(
array('GET', '/', array('Rougin\Slytherin\Fixture\Classes\NewClass', 'index')),
array('GET', '/hi', function () {
return 'Hi';
}),
array('TEST', '/hello', function () {
return 'It must not go through here';
}),
);
$routes = array();
$routes[] = array('GET', '/', array('Rougin\Slytherin\Fixture\Classes\NewClass', 'index'));
$routes[] = array('GET', '/hi', function () { return 'Hi'; });
$routes[] = array('TEST', '/hello', function () { return 'It must not go through here'; });

$router = new Router($this->routes);
$this->routes = $routes;

$dispatcher = new Dispatcher($router);
$router = new Router($routes);

$this->dispatcher = $dispatcher;
$this->dispatcher = new Dispatcher($router);
}

/**
Expand All @@ -68,11 +61,17 @@ public function testDispatchMethod()

$route = $this->dispatcher->dispatch('GET', '/');

list($class, $method) = $route->getHandler();
/** @var string */
$handler = $route->getHandler();

$class = $handler[0]; $method = $handler[1];

$params = (array) $route->getParams();

/** @var callable */
$object = array(new $class, $method);

$actual = call_user_func_array($object, $route->getParams());
$actual = call_user_func_array($object, $params);

$this->assertEquals($expected, $actual);
}
Expand All @@ -86,11 +85,12 @@ public function testDispatchMethodWithClosure()
{
$route = $this->dispatcher->dispatch('GET', '/hi');

/** @var callable */
$callback = $route->getHandler();

$parameters = $route->getParams();
$params = $route->getParams();

$actual = call_user_func($callback, $parameters);
$actual = call_user_func($callback, $params);

$this->assertEquals('Hi', $actual);
}
Expand Down Expand Up @@ -148,11 +148,17 @@ public function testDispatchMethodWithDifferentRouter()

$route = $dispatcher->dispatch('GET', '/');

list($class, $method) = $route->getHandler();
/** @var string */
$handler = $route->getHandler();

$class = $handler[0]; $method = $handler[1];

$params = (array) $route->getParams();

/** @var callable */
$object = array(new $class, $method);

$actual = call_user_func_array($object, $route->getParams());
$actual = call_user_func_array($object, $params);

$this->assertEquals($expected, $actual);
}
Expand Down
43 changes: 26 additions & 17 deletions tests/Dispatching/Vanilla/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class DispatcherTest extends Testcase
{
/**
* @var \Rougin\Slytherin\Dispatching\DispatcherInterface
* @var \Rougin\Slytherin\Routing\DispatcherInterface
*/
protected $dispatcher;

Expand All @@ -25,16 +25,12 @@ class DispatcherTest extends Testcase
*/
protected function doSetUp()
{
$routes = array(
array('GET', '/', array('Rougin\Slytherin\Fixture\Classes\NewClass', 'index')),
array('POST', '/', array('Rougin\Slytherin\Fixture\Classes\NewClass', 'store')),
array('GET', '/hi', function () {
return 'Hi';
}),
array('TEST', '/hello', function () {
return 'It must not go through here';
}),
);
$routes = array();

$routes[] = array('GET', '/', array('Rougin\Slytherin\Fixture\Classes\NewClass', 'index'));
$routes[] = array('POST', '/', array('Rougin\Slytherin\Fixture\Classes\NewClass', 'store'));
$routes[] = array('GET', '/hi', function () { return 'Hi'; });
$routes[] = array('TEST', '/hello', function () { return 'It must not go through here'; });

$router = new Router($routes);

Expand All @@ -56,11 +52,17 @@ public function testDispatchMethodWithClass()

$route = $this->dispatcher->dispatch('GET', '/');

list($class, $method) = $route->getHandler();
/** @var string */
$handler = $route->getHandler();

$class = $handler[0]; $method = $handler[1];

$params = (array) $route->getParams();

/** @var callable */
$object = array(new $class, $method);

$actual = call_user_func_array($object, $route->getParams());
$actual = call_user_func_array($object, $params);

$this->assertEquals($expected, $actual);
}
Expand All @@ -78,11 +80,17 @@ public function testDispatchMethodWithClassAndPostMethod()

$route = $this->dispatcher->dispatch('POST', '/');

list($class, $method) = $route->getHandler();
/** @var string */
$handler = $route->getHandler();

$class = $handler[0]; $method = $handler[1];

$params = (array) $route->getParams();

/** @var callable */
$object = array(new $class, $method);

$actual = call_user_func_array($object, $route->getParams());
$actual = call_user_func_array($object, $params);

$this->assertEquals($expected, $actual);
}
Expand All @@ -96,11 +104,12 @@ public function testDispatchMethodWithClosure()
{
$route = $this->dispatcher->dispatch('GET', '/hi');

/** @var callable */
$callback = $route->getHandler();

$parameters = $route->getParams();
$params = $route->getParams();

$actual = call_user_func($callback, $parameters);
$actual = call_user_func($callback, $params);

$this->assertEquals('Hi', $actual);
}
Expand Down
Loading

0 comments on commit b33f5aa

Please sign in to comment.