Skip to content

Commit

Permalink
improve tests structure and readability
Browse files Browse the repository at this point in the history
  • Loading branch information
IngeniozIT committed Feb 28, 2024
1 parent 782a55a commit 9db01df
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php

namespace IngeniozIT\Router\Tests\Features;
namespace IngeniozIT\Router\Tests;

use IngeniozIT\Router\Route;
use IngeniozIT\Router\RouteGroup;
use IngeniozIT\Router\Tests\RouterCase;
use IngeniozIT\Router\Tests\Utils\RouterCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

final class AdditionalAttributesTest extends RouterCase
{
public function testAddsRouteAttributesToRequest(): void
public function testRouteAttributesAreAddedToTheRequest(): void
{
$routeGroup = new RouteGroup(routes: [
Route::get(
Expand All @@ -28,7 +28,7 @@ public function testAddsRouteAttributesToRequest(): void
self::assertEquals('bar', (string)$response->getBody());
}

public function testAddsRouteGroupAttributesToRequest(): void
public function testRouteGroupAttributesAreAddedToTheRequest(): void
{
$routeGroup = new RouteGroup(
routes: [
Expand Down
10 changes: 5 additions & 5 deletions tests/Features/CallbackTest.php → tests/CallbackTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php

namespace IngeniozIT\Router\Tests\Features;
namespace IngeniozIT\Router\Tests;

use Closure;
use IngeniozIT\Http\Message\UriFactory;
use IngeniozIT\Router\Exception\InvalidRouteHandler;
use IngeniozIT\Router\Route;
use IngeniozIT\Router\RouteGroup;
use IngeniozIT\Router\Tests\Fakes\TestHandler;
use IngeniozIT\Router\Tests\Fakes\TestMiddleware;
use IngeniozIT\Router\Tests\RouterCase;
use IngeniozIT\Router\Tests\Utils\RouterCase;
use IngeniozIT\Router\Tests\Utils\TestHandler;
use IngeniozIT\Router\Tests\Utils\TestMiddleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
Expand Down Expand Up @@ -76,7 +76,7 @@ public static function providerInvalidHandlers(): array
{
return [
'not a handler' => [UriFactory::class],
'value that cannot be converted to a response' => [static fn(): array => ['foo' => 'bar']],
'handler that does not return a PSR response' => [static fn(): array => ['foo' => 'bar']],
];
}
}
12 changes: 6 additions & 6 deletions tests/Features/ConditionsTest.php → tests/ConditionsTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace IngeniozIT\Router\Tests\Features;
namespace IngeniozIT\Router\Tests;

use Closure;
use IngeniozIT\Http\Message\UriFactory;
use IngeniozIT\Router\Exception\InvalidRouteCondition;
use IngeniozIT\Router\Route;
use IngeniozIT\Router\RouteGroup;
use IngeniozIT\Router\Tests\RouterCase;
use IngeniozIT\Router\Tests\Utils\RouterCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

Expand All @@ -16,7 +16,7 @@ final class ConditionsTest extends RouterCase
/**
* @dataProvider providerConditions
*/
public function testRouteGroupCanHaveConditions(Closure $condition, string $expectedResponse): void
public function testRouteGroupsCanHaveConditions(Closure $condition, string $expectedResponse): void
{
$routeGroup = new RouteGroup(
routes: [
Expand Down Expand Up @@ -49,7 +49,7 @@ public static function providerConditions(): array
];
}

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

public function testConditionCanAddAttributesToARequest(): void
public function testConditionsCanAddAttributesToARequest(): void
{
$routeGroup = new RouteGroup(
routes: [
Expand All @@ -91,7 +91,7 @@ public function testConditionCanAddAttributesToARequest(): void
/**
* @dataProvider providerInvalidConditions
*/
public function testCannotExecuteInvalidConditions(mixed $condition): void
public function testRouterCannotExecuteInvalidConditions(mixed $condition): void
{
$routeGroup = new RouteGroup(
routes: [
Expand Down
10 changes: 5 additions & 5 deletions tests/Features/HttpMethodTest.php → tests/HttpMethodTest.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?php

namespace IngeniozIT\Router\Tests\Features;
namespace IngeniozIT\Router\Tests;

use IngeniozIT\Router\Route;
use IngeniozIT\Router\RouteElement;
use IngeniozIT\Router\RouteGroup;
use IngeniozIT\Router\Tests\RouterCase;
use IngeniozIT\Router\Tests\Utils\RouterCase;
use Psr\Http\Message\ResponseInterface;

final class HttpMethodTest extends RouterCase
{
/**
* @dataProvider providerMethodsAndRoutes
*/
public function testRouteMatchesRequestsBasedOnMethod(string $method, callable $routeCallable): void
public function testRoutesMatchRequestsBasedOnMethod(string $method, callable $routeCallable): void
{
/** @var RouteElement $route */
$route = $routeCallable('/', static fn(): ResponseInterface => self::response('OK'));
Expand Down Expand Up @@ -43,7 +43,7 @@ public static function providerMethodsAndRoutes(): array
/**
* @dataProvider providerRouteMethods
*/
public function testRouteCanMatchAnyMethod(string $method): void
public function testRoutesCanMatchAnyMethod(string $method): void
{
$route = Route::any('/', static fn(): ResponseInterface => self::response('OK'));
$request = self::serverRequest($method, '/');
Expand All @@ -69,7 +69,7 @@ public static function providerRouteMethods(): array
];
}

public function testCanMatchSomeMethods(): void
public function testRoutesCanMatchSomeMethods(): void
{
$routeGroup = new RouteGroup(
routes: [
Expand Down
12 changes: 6 additions & 6 deletions tests/Features/MiddlewaresTest.php → tests/MiddlewaresTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace IngeniozIT\Router\Tests\Features;
namespace IngeniozIT\Router\Tests;

use Exception;
use IngeniozIT\Http\Message\UriFactory;
use IngeniozIT\Router\Exception\InvalidRouteMiddleware;
use IngeniozIT\Router\Route;
use IngeniozIT\Router\RouteGroup;
use IngeniozIT\Router\Tests\Fakes\TestMiddleware;
use IngeniozIT\Router\Tests\RouterCase;
use IngeniozIT\Router\Tests\Utils\RouterCase;
use IngeniozIT\Router\Tests\Utils\TestMiddleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
Expand All @@ -20,7 +20,7 @@ final class MiddlewaresTest extends RouterCase
/**
* @dataProvider providerMiddlewares
*/
public function testRouteGroupCanHaveMiddlewares(mixed $middleware, string $expectedResponse): void
public function testRouteGroupsCanHaveMiddlewares(mixed $middleware, string $expectedResponse): void
{
$routeGroup = new RouteGroup(
routes: [
Expand Down Expand Up @@ -52,7 +52,7 @@ public static function providerMiddlewares(): array
];
}

public function testRouteGroupCanHaveMultipleMiddlewares(): void
public function testRouteGroupsCanHaveMultipleMiddlewares(): void
{
$routeGroup = new RouteGroup(
routes: [
Expand All @@ -74,7 +74,7 @@ public function testRouteGroupCanHaveMultipleMiddlewares(): void
/**
* @dataProvider providerInvalidMiddlewares
*/
public function testCannotExecuteInvalidMiddlewares(mixed $middleware): void
public function testRouterCannotExecuteInvalidMiddlewares(mixed $middleware): void
{
$routeGroup = new RouteGroup(
routes: [
Expand Down
6 changes: 3 additions & 3 deletions tests/Features/NameTest.php → tests/NameTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace IngeniozIT\Router\Tests\Features;
namespace IngeniozIT\Router\Tests;

use IngeniozIT\Router\Exception\InvalidRouteParameter;
use IngeniozIT\Router\Exception\MissingRouteParameters;
use IngeniozIT\Router\Exception\RouteNotFound;
use IngeniozIT\Router\Route;
use IngeniozIT\Router\RouteGroup;
use IngeniozIT\Router\Tests\RouterCase;
use IngeniozIT\Router\Tests\Utils\RouterCase;

final class NameTest extends RouterCase
{
Expand All @@ -27,7 +27,7 @@ public function testRouterCanFindARoutePathByName(): void
self::assertSame('/foo', $result);
}

public function testRouterCanFindARoutePathByNameWithParameters(): void
public function testRouterCanFindARouteWithParametersPathByName(): void
{
$routeGroup = new RouteGroup([
Route::get('/{foo:\d+}', 'foo', name: 'route_name'),
Expand Down
59 changes: 30 additions & 29 deletions tests/RouterTest.php → tests/RoutingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
use IngeniozIT\Router\Exception\EmptyRouteStack;
use IngeniozIT\Router\Route;
use IngeniozIT\Router\RouteGroup;
use IngeniozIT\Router\Tests\Utils\RouterCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

final class RouterTest extends RouterCase
final class RoutingTest extends RouterCase
{
public function testCanHandleAGroupOfRoutes(): void
public function testRouterHandlesARouteGroup(): void
{
$routeGroup = new RouteGroup(routes: [
Route::get(path: '/foo', callback: static fn(): ResponseInterface => self::response('OK')),
Expand All @@ -22,22 +23,7 @@ public function testCanHandleAGroupOfRoutes(): void
self::assertEquals('OK', (string)$response->getBody());
}

public function testRouteGroupCanHaveAPathPrefix(): void
{
$routeGroup = new RouteGroup(
routes: [
Route::get(path: '/bar', callback: static fn(): ResponseInterface => self::response('OK')),
],
path: '/foo'
);
$request = self::serverRequest('GET', '/foo/bar');

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

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

public function testFiltersOutNonMatchingPaths(): void
public function testRouterFiltersOutNonMatchingPaths(): void
{
$routeGroup = new RouteGroup(routes: [
Route::get(path: '/test2', callback: static fn(): ResponseInterface => self::response('KO')),
Expand All @@ -50,7 +36,7 @@ public function testFiltersOutNonMatchingPaths(): void
self::assertEquals('OK', (string)$response->getBody());
}

public function testCanHaveSubGroups(): void
public function testRouterCanHandleARouteAfterASubGroup(): void
{
$routeGroup = new RouteGroup(
routes: [
Expand All @@ -59,16 +45,17 @@ public function testCanHaveSubGroups(): void
Route::get(path: '/sub', callback: static fn(): ResponseInterface => self::response('TEST')),
],
),
Route::get(path: '/after-sub', callback: static fn(): ResponseInterface => self::response('TEST2')),
],
);
$request = self::serverRequest('GET', '/sub');
$request = self::serverRequest('GET', '/after-sub');

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

self::assertEquals('TEST', (string)$response->getBody());
self::assertEquals('TEST2', (string)$response->getBody());
}

public function testCanHandleARouteAfterASubGroup(): void
public function testRouteGroupsCanHaveSubGroups(): void
{
$routeGroup = new RouteGroup(
routes: [
Expand All @@ -77,17 +64,31 @@ public function testCanHandleARouteAfterASubGroup(): void
Route::get(path: '/sub', callback: static fn(): ResponseInterface => self::response('TEST')),
],
),
Route::get(path: '/after-sub', callback: static fn(): ResponseInterface => self::response('TEST2')),
],
);
$request = self::serverRequest('GET', '/after-sub');
$request = self::serverRequest('GET', '/sub');

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

self::assertEquals('TEST2', (string)$response->getBody());
self::assertEquals('TEST', (string)$response->getBody());
}

public function testRouteGroupsCanHaveAPathPrefix(): void
{
$routeGroup = new RouteGroup(
routes: [
Route::get(path: '/bar', callback: static fn(): ResponseInterface => self::response('OK')),
],
path: '/foo'
);
$request = self::serverRequest('GET', '/foo/bar');

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

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

public function testCanUsePathParameters(): void
public function testRoutesCanUsePathParameters(): void
{
$routeGroup = new RouteGroup(routes: [
Route::get(path: '/{foo}/{bar}', callback: static fn(ServerRequestInterface $request
Expand All @@ -103,7 +104,7 @@ public function testCanUsePathParameters(): void
/**
* @dataProvider providerRouteGroupsWithCustomParameters
*/
public function testCanUseCustomPathParameterPatterns(RouteGroup $routeGroup): void
public function testRoutesCanUseCustomPathParameters(RouteGroup $routeGroup): void
{
$matchingRequest = self::serverRequest('GET', '/123');
$nonMatchingRequest = self::serverRequest('GET', '/abc');
Expand Down Expand Up @@ -172,7 +173,7 @@ public static function providerRouteGroupsWithCustomParameters(): array
];
}

public function testMustFindARouteToProcess(): void
public function testRouterMustFindARouteToProcess(): void
{
$routeGroup = new RouteGroup(routes: [
Route::get(path: '/foo', callback: static fn(): ResponseInterface => self::response('TEST')),
Expand All @@ -184,7 +185,7 @@ public function testMustFindARouteToProcess(): void
$this->router($routeGroup)->handle($request);
}

public function testCanHaveAFallbackRoute(): void
public function testRouterCanHaveAFallbackRoute(): void
{
$routeGroup = new RouteGroup(routes: [
Route::get(path: '/foo', callback: static fn(): ResponseInterface => self::response('TEST')),
Expand Down
18 changes: 5 additions & 13 deletions tests/PsrTrait.php → tests/Utils/PsrTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,17 @@

declare(strict_types=1);

namespace IngeniozIT\Router\Tests;
namespace IngeniozIT\Router\Tests\Utils;

use Psr\Http\Message\{
ResponseFactoryInterface,
use IngeniozIT\Edict\Container;
use IngeniozIT\Http\Message\{ResponseFactory, ServerRequestFactory, StreamFactory, UploadedFileFactory, UriFactory,};
use Psr\Http\Message\{ResponseFactoryInterface,
ResponseInterface,
ServerRequestFactoryInterface,
ServerRequestInterface,
StreamFactoryInterface,
UploadedFileFactoryInterface,
UriFactoryInterface,
};
use IngeniozIT\Http\Message\{
ResponseFactory,
ServerRequestFactory,
StreamFactory,
UploadedFileFactory,
UriFactory,
};
use IngeniozIT\Edict\Container;
UriFactoryInterface,};

use function IngeniozIT\Edict\value;

Expand Down
2 changes: 1 addition & 1 deletion tests/RouterCase.php → tests/Utils/RouterCase.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace IngeniozIT\Router\Tests;
namespace IngeniozIT\Router\Tests\Utils;

use Closure;
use IngeniozIT\Router\RouteGroup;
Expand Down
Loading

0 comments on commit 9db01df

Please sign in to comment.