Skip to content

Commit

Permalink
improve tests readability
Browse files Browse the repository at this point in the history
  • Loading branch information
IngeniozIT committed Feb 25, 2024
1 parent 1931e1e commit d9d7a5f
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 115 deletions.
16 changes: 10 additions & 6 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ final class Router implements RequestHandlerInterface
public function __construct(
private readonly RouteGroup $routeGroup,
private readonly ContainerInterface $container,
private readonly ResponseFactoryInterface $responseFactory,
private readonly StreamFactoryInterface $streamFactory,
private readonly mixed $fallback = null,
) {
}
Expand Down Expand Up @@ -98,7 +100,13 @@ private function executeRoutables(ServerRequestInterface $request): ResponseInte
$route = $this->routeGroup->routes[$this->routeIndex++];

if ($route instanceof RouteGroup) {
$newRouter = new Router($route, $this->container, $this->handle(...));
$newRouter = new Router(
$route,
$this->container,
$this->responseFactory,
$this->streamFactory,
$this->handle(...)
);
return $newRouter->handle($request);
}

Expand Down Expand Up @@ -152,11 +160,7 @@ private function fallback(ServerRequestInterface $request): ResponseInterface
private function processResponse(mixed $response): ResponseInterface
{
if (is_string($response)) {
/** @var StreamFactoryInterface $streamFactory */
$streamFactory = $this->container->get(StreamFactoryInterface::class);
/** @var ResponseFactoryInterface $responseFactory */
$responseFactory = $this->container->get(ResponseFactoryInterface::class);
$response = $responseFactory->createResponse()->withBody($streamFactory->createStream($response));
$response = $this->responseFactory->createResponse()->withBody($this->streamFactory->createStream($response));
}

if (!$response instanceof ResponseInterface) {
Expand Down
50 changes: 25 additions & 25 deletions tests/RouteGroupConditionTest.php → tests/ConditionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
final class RouteGroupConditionTest extends TestCase
final class ConditionTest extends TestCase
{
use PsrTrait;

private function router(RouteGroup $routeGroup, ?Closure $fallback = null): Router
{
return new Router($routeGroup, self::container(), $fallback);
return new Router($routeGroup, self::container(), self::responseFactory(), self::streamFactory(), $fallback);
}

/**
* @dataProvider providerConditions
*/
public function testCanHaveConditions(Closure $condition, string $expectedResponse): void
public function testRouteGroupCanHaveConditions(Closure $condition, string $expectedResponse): void
{
$routeGroup = new RouteGroup(
routes: [
Expand Down Expand Up @@ -58,7 +58,7 @@ public static function providerConditions(): array
];
}

public function testCanHaveMultipleConditions(): void
public function testRouteGroupCanHaveMultipleConditions(): void
{
$routeGroup = new RouteGroup(
routes: [
Expand All @@ -77,6 +77,26 @@ public function testCanHaveMultipleConditions(): void
self::assertEquals('TEST', (string)$response->getBody());
}

public function testConditionCanAddAttributesToARequest(): void
{
$routeGroup = new RouteGroup(
routes: [
Route::get(
path: '/',
callback: static fn(ServerRequestInterface $request): ResponseInterface => self::response(var_export($request->getAttribute('foo'), true))
),
],
conditions: [
static fn(): array => ['foo' => 'bar'],
],
);
$request = self::serverRequest('GET', '/');

$response = $this->router($routeGroup)->handle($request);

self::assertEquals("'bar'", (string)$response->getBody());
}

/**
* @dataProvider providerInvalidConditions
*/
Expand All @@ -101,27 +121,7 @@ public static function providerInvalidConditions(): array
{
return [
'not a callable' => [UriFactory::class],
'callable that does not return array or false' => [static fn(): bool => true],
'callable that does not return bool or array' => [static fn(): int => 42],
];
}

public function testAddsConditionParametersToRequest(): void
{
$routeGroup = new RouteGroup(
routes: [
Route::get(
path: '/',
callback: static fn(ServerRequestInterface $request): ResponseInterface => self::response(var_export($request->getAttribute('foo'), true))
),
],
conditions: [
static fn(): array => ['foo' => 'bar'],
],
);
$request = self::serverRequest('GET', '/');

$response = $this->router($routeGroup)->handle($request);

self::assertEquals("'bar'", (string)$response->getBody());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@
/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
final class RouteGroupMiddlewareTest extends TestCase
final class MiddlewareTest extends TestCase
{
use PsrTrait;

private function router(RouteGroup $routeGroup, ?Closure $fallback = null): Router
{
return new Router($routeGroup, self::container(), $fallback);
return new Router($routeGroup, self::container(), self::responseFactory(), self::streamFactory(), $fallback);
}

/**
* @dataProvider providerMiddlewares
*/
public function testCanHaveMiddlewares(mixed $middleware, string $expectedResponse): void
public function testRouteGroupCanHaveMiddlewares(mixed $middleware, string $expectedResponse): void
{
$routeGroup = new RouteGroup(
routes: [
Expand Down Expand Up @@ -69,7 +69,7 @@ public static function providerMiddlewares(): array
];
}

public function testCanHaveMultipleMiddlewares(): void
public function testRouteGroupCanHaveMultipleMiddlewares(): void
{
$routeGroup = new RouteGroup(
routes: [
Expand Down
101 changes: 101 additions & 0 deletions tests/RouteHttpMethodTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

declare(strict_types=1);

namespace IngeniozIT\Router\Tests;

use IngeniozIT\Router\Route;
use PHPUnit\Framework\TestCase;

/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
class RouteHttpMethodTest extends TestCase
{
use PsrTrait;

/**
* @dataProvider providerMethodsAndRoutes
*/
public function testRouteMatchesRequestsBasedOnMethod(string $method, callable $routeCallable): void
{
/** @var Route $route */
$route = $routeCallable('/', 'foo');
$request = self::serverRequest($method, '/');

$result = $route->match($request);

self::assertSame([], $result);
}

/**
* @return array<string, array{0: string, 1: callable}>
*/
public static function providerMethodsAndRoutes(): array
{
return [
'GET' => ['GET', Route::get(...)],
'POST' => ['POST', Route::post(...)],
'PUT' => ['PUT', Route::put(...)],
'PATCH' => ['PATCH', Route::patch(...)],
'DELETE' => ['DELETE', Route::delete(...)],
'HEAD' => ['HEAD', Route::head(...)],
'OPTIONS' => ['OPTIONS', Route::options(...)],
];
}

/**
* @dataProvider providerRouteMethods
*/
public function testRouteCanMatchAnyMethod(string $method): void
{
$route = Route::any('/', 'foo');
$request = self::serverRequest($method, '/');

$result = $route->match($request);

self::assertSame([], $result);
}

/**
* @return array<string, array{0: string}>
*/
public static function providerRouteMethods(): array
{
return [
'GET' => ['GET'],
'POST' => ['POST'],
'PUT' => ['PUT'],
'PATCH' => ['PATCH'],
'DELETE' => ['DELETE'],
'HEAD' => ['HEAD'],
'OPTIONS' => ['OPTIONS'],
];
}

public function testCanMatchSomeMethods(): void
{
$route = Route::some(['GET', 'POST'], '/', 'foo');
$getRequest = self::serverRequest('GET', '/');
$postRequest = self::serverRequest('POST', '/');
$putRequest = self::serverRequest('PUT', '/');

$getResult = $route->match($getRequest);
$postResult = $route->match($postRequest);
$putResult = $route->match($putRequest);

self::assertSame([], $getResult);
self::assertSame([], $postResult);
self::assertSame(false, $putResult);
}

public function testMethodNameCanBeLowercase(): void
{
$route = Route::some(['delete'], '/', 'foo');
$deleteRequest = self::serverRequest('DELETE', '/');

$deleteResult = $route->match($deleteRequest);

self::assertSame([], $deleteResult);
}
}
78 changes: 0 additions & 78 deletions tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,84 +15,6 @@ final class RouteTest extends TestCase
{
use PsrTrait;

/**
* @dataProvider providerMethodsAndRoutes
*/
public function testMatchesRequestsBasedOnMethod(string $method, callable $routeCallable): void
{
/** @var Route $route */
$route = $routeCallable('/', 'foo');
$request = self::serverRequest($method, '/');

$result = $route->match($request);

self::assertSame([], $result);
}

/**
* @return array<string, array{0: string, 1: callable}>
*/
public static function providerMethodsAndRoutes(): array
{
return [
'GET' => ['GET', Route::get(...)],
'POST' => ['POST', Route::post(...)],
'PUT' => ['PUT', Route::put(...)],
'PATCH' => ['PATCH', Route::patch(...)],
'DELETE' => ['DELETE', Route::delete(...)],
'HEAD' => ['HEAD', Route::head(...)],
'OPTIONS' => ['OPTIONS', Route::options(...)],
];
}

/**
* @dataProvider providerRouteMethods
*/
public function testCanMatchAnyMethod(string $method): void
{
$route = Route::any('/', 'foo');
$request = self::serverRequest($method, '/');

$result = $route->match($request);

self::assertSame([], $result);
}

/**
* @return array<string, array{0: string}>
*/
public static function providerRouteMethods(): array
{
return [
'GET' => ['GET'],
'POST' => ['POST'],
'PUT' => ['PUT'],
'PATCH' => ['PATCH'],
'DELETE' => ['DELETE'],
'HEAD' => ['HEAD'],
'OPTIONS' => ['OPTIONS'],
];
}

public function testCanMatchSomeMethods(): void
{
$route = Route::some(['GET', 'POST', 'delete'], '/', 'foo');
$getRequest = self::serverRequest('GET', '/');
$postRequest = self::serverRequest('POST', '/');
$putRequest = self::serverRequest('PUT', '/');
$deleteRequest = self::serverRequest('DELETE', '/');

$getResult = $route->match($getRequest);
$postResult = $route->match($postRequest);
$putResult = $route->match($putRequest);
$deleteResult = $route->match($deleteRequest);

self::assertSame([], $getResult);
self::assertSame([], $postResult);
self::assertSame(false, $putResult);
self::assertSame([], $deleteResult);
}

public function testMatchesRequestsBasedOnPath(): void
{
$route = Route::get('/foo', 'foo');
Expand Down
2 changes: 1 addition & 1 deletion tests/RouterRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class RouterRouteTest extends TestCase

private function router(RouteGroup $routeGroup, ?Closure $fallback = null): Router
{
return new Router($routeGroup, self::container(), $fallback);
return new Router($routeGroup, self::container(), self::responseFactory(), self::streamFactory(), $fallback);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/SubGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class SubGroupTest extends TestCase

private function router(RouteGroup $routeGroup, ?Closure $fallback = null): Router
{
return new Router($routeGroup, self::container(), $fallback);
return new Router($routeGroup, self::container(), self::responseFactory(), self::streamFactory(), $fallback);
}

public function testCanHaveSubGroups(): void
Expand Down

0 comments on commit d9d7a5f

Please sign in to comment.