diff --git a/tests/unit/Route/RouteCollectionTest.php b/tests/unit/Route/RouteCollectionTest.php index 64be414..ad26dad 100644 --- a/tests/unit/Route/RouteCollectionTest.php +++ b/tests/unit/Route/RouteCollectionTest.php @@ -34,15 +34,24 @@ use Archict\Router\Method; use Archict\Router\RequestHandler; use Archict\Router\ResponseFactory; +use CuyZ\Valinor\Mapper\TreeMapper; +use CuyZ\Valinor\MapperBuilder; use PHPUnit\Framework\TestCase; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; final class RouteCollectionTest extends TestCase { + private TreeMapper $mapper; + + protected function setUp(): void + { + $this->mapper = (new MapperBuilder())->enableFlexibleCasting()->allowSuperfluousKeys()->allowPermissiveTypes()->mapper(); + } + public function testItCanAddRouteWithRequestHandler(): void { - $collection = new RouteCollection(new ServiceManager()); + $collection = new RouteCollection(new ServiceManager($this->mapper)); self::assertTrue( $collection->addRoute( Method::GET, @@ -59,7 +68,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface // ph public function testItCanAddRouteWithCallable(): void { - $collection = new RouteCollection(new ServiceManager()); + $collection = new RouteCollection(new ServiceManager($this->mapper)); self::assertTrue( $collection->addRoute(Method::GET, 'route', static fn(ServerRequestInterface $request) => ResponseFactory::build()->get()) // phpcs:ignore ); @@ -67,7 +76,7 @@ public function testItCanAddRouteWithCallable(): void public function testItCanAddRouteWithClassname(): void { - $collection = new RouteCollection(new ServiceManager()); + $collection = new RouteCollection(new ServiceManager($this->mapper)); $handler = new class implements RequestHandler { public function handle(ServerRequestInterface $request): ResponseInterface // phpcs:ignore { @@ -79,7 +88,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface // ph public function testItNotAcceptIfAddTwiceSameRoute(): void { - $collection = new RouteCollection(new ServiceManager()); + $collection = new RouteCollection(new ServiceManager($this->mapper)); self::assertTrue( $collection->addRoute(Method::GET, 'route', static fn(ServerRequestInterface $request) => ResponseFactory::build()->get()) // phpcs:ignore ); @@ -90,7 +99,7 @@ public function testItNotAcceptIfAddTwiceSameRoute(): void public function testItAcceptSameRouteDifferentMethod(): void { - $collection = new RouteCollection(new ServiceManager()); + $collection = new RouteCollection(new ServiceManager($this->mapper)); self::assertTrue( $collection->addRoute(Method::GET, 'route', static fn(ServerRequestInterface $request) => ResponseFactory::build()->get()) // phpcs:ignore ); @@ -101,7 +110,7 @@ public function testItAcceptSameRouteDifferentMethod(): void public function testItNotAcceptSameRouteWithMethodAll(): void { - $collection = new RouteCollection(new ServiceManager()); + $collection = new RouteCollection(new ServiceManager($this->mapper)); self::assertTrue( $collection->addRoute(Method::GET, 'route', static fn(ServerRequestInterface $request) => ResponseFactory::build()->get()) // phpcs:ignore ); @@ -112,7 +121,7 @@ public function testItNotAcceptSameRouteWithMethodAll(): void public function testItNotAcceptSameRouteIfFirstIsMethodAll(): void { - $collection = new RouteCollection(new ServiceManager()); + $collection = new RouteCollection(new ServiceManager($this->mapper)); self::assertTrue( $collection->addRoute(Method::ALL, 'route', static fn(ServerRequestInterface $request) => ResponseFactory::build()->get()) // phpcs:ignore ); @@ -123,7 +132,7 @@ public function testItNotAcceptSameRouteIfFirstIsMethodAll(): void public function testItAcceptSeveralRoute(): void { - $collection = new RouteCollection(new ServiceManager()); + $collection = new RouteCollection(new ServiceManager($this->mapper)); self::assertTrue( $collection->addRoute(Method::GET, 'route1', static fn(ServerRequestInterface $request) => ResponseFactory::build()->get()) // phpcs:ignore ); @@ -137,7 +146,7 @@ public function testItAcceptSeveralRoute(): void public function testItAcceptSameRouteWithDifferentGroup(): void { - $collection = new RouteCollection(new ServiceManager()); + $collection = new RouteCollection(new ServiceManager($this->mapper)); self::assertTrue( $collection->addRoute(Method::GET, '{group:\d+}', static fn(ServerRequestInterface $request) => ResponseFactory::build()->get()) // phpcs:ignore ); @@ -148,14 +157,14 @@ public function testItAcceptSameRouteWithDifferentGroup(): void public function testItThrowIfRouteNotFound(): void { - $collection = new RouteCollection(new ServiceManager()); + $collection = new RouteCollection(new ServiceManager($this->mapper)); self::expectException(NotFoundException::class); $collection->getMatchingRoute('', 'GET'); } public function testItThrowIfMethodNotAllowed(): void { - $collection = new RouteCollection(new ServiceManager()); + $collection = new RouteCollection(new ServiceManager($this->mapper)); $collection->addRoute(Method::GET, 'route', static fn(ServerRequestInterface $request) => ResponseFactory::build()->get()); // phpcs:ignore self::expectException(MethodNotAllowedException::class); $collection->getMatchingRoute('route', 'POST'); @@ -163,7 +172,7 @@ public function testItThrowIfMethodNotAllowed(): void public function testItCanFindMatchingRouteSimple(): void { - $collection = new RouteCollection(new ServiceManager()); + $collection = new RouteCollection(new ServiceManager($this->mapper)); $collection->addRoute(Method::ALL, 'route', static fn(ServerRequestInterface $request) => ResponseFactory::build()->get()); // phpcs:ignore $route = $collection->getMatchingRoute('route', 'PATCH'); @@ -173,7 +182,7 @@ public function testItCanFindMatchingRouteSimple(): void public function testItCanFindMatchingRouteWithGroup(): void { - $collection = new RouteCollection(new ServiceManager()); + $collection = new RouteCollection(new ServiceManager($this->mapper)); $collection->addRoute(Method::GET, 'article/{id:\d+}', static fn(ServerRequestInterface $request) => ResponseFactory::build()->get()); // phpcs:ignore $route = $collection->getMatchingRoute('article/5', 'GET'); @@ -183,28 +192,28 @@ public function testItCanFindMatchingRouteWithGroup(): void public function testItCanAddMiddlewareWithCallable(): void { - $collection = new RouteCollection(new ServiceManager()); + $collection = new RouteCollection(new ServiceManager($this->mapper)); self::expectNotToPerformAssertions(); $collection->addMiddleware(Method::GET, 'route', static fn(ServerRequestInterface $request) => $request); } public function testItCanAddMiddlewareWithMiddleware(): void { - $collection = new RouteCollection(new ServiceManager()); + $collection = new RouteCollection(new ServiceManager($this->mapper)); self::expectNotToPerformAssertions(); $collection->addMiddleware(Method::GET, 'route', new IdentityMiddlewareStub()); } public function testItCanAddMiddlewareWithClassname(): void { - $collection = new RouteCollection(new ServiceManager()); + $collection = new RouteCollection(new ServiceManager($this->mapper)); self::expectNotToPerformAssertions(); $collection->addMiddleware(Method::GET, 'route', IdentityMiddlewareStub::class); } public function testItAcceptMultipleMiddlewareOnSameRoute(): void { - $collection = new RouteCollection(new ServiceManager()); + $collection = new RouteCollection(new ServiceManager($this->mapper)); self::expectNotToPerformAssertions(); $collection->addMiddleware(Method::GET, 'route', new IdentityMiddlewareStub()); $collection->addMiddleware(Method::GET, 'route', new IdentityMiddlewareStub());