diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index fca4126..3c9a33e 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-versions: ['7.4', '8.0', '8.1', '8.2', '8.3'] + php-versions: ['8.3', '8.4'] steps: - name: Checkout uses: actions/checkout@v2 diff --git a/composer.json b/composer.json index 67c0afa..2d15493 100644 --- a/composer.json +++ b/composer.json @@ -3,17 +3,17 @@ "description": "Symfony with enhanced DI and a Silex-like interface", "type": "library", "require": { - "php": ">=7.2", + "php": ">=8.3", "ext-json": "*", "psr/container": "^1.0", - "symfony/http-kernel": "^4.4", - "symfony/http-foundation": "^4.4", - "symfony/routing": "^4.4", - "symfony/framework-bundle": "^4.4", - "symfony/dependency-injection": "^4.4" + "symfony/http-kernel": "^7.3", + "symfony/http-foundation": "^7.3", + "symfony/routing": "^7.3", + "symfony/framework-bundle": "^7.3", + "symfony/dependency-injection": "^7.3" }, "require-dev": { - "phpunit/phpunit": "^8" + "phpunit/phpunit": "^12" }, "license": "LGPL-3.0", "authors": [ diff --git a/src/ContainerProvider.php b/src/ContainerProvider.php index 8599c0a..4d5d7a1 100644 --- a/src/ContainerProvider.php +++ b/src/ContainerProvider.php @@ -1,4 +1,5 @@ fallbackContainer = $fallbackContainer; } - public function has( $id ) + #[\Override] + public function has( string $id ): bool { if ( parent::has( $id ) ) { return true; @@ -29,7 +31,8 @@ public function has( $id ) return $this->fallbackContainer->has( $id ); } - public function get( $id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE ) + #[\Override] + public function get( string $id, int $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE ): ?object { if ( !$this->fallbackContainer || parent::has( $id ) ) { return parent::get( $id ); diff --git a/src/Dilex.php b/src/Dilex.php index 8b2e865..7f1f70e 100644 --- a/src/Dilex.php +++ b/src/Dilex.php @@ -1,4 +1,7 @@ fallbackContainerInterface = $fallbackContainerInterface; + $this->routeRegistry = new RouteRegistry(); $this->containerProvider = new ContainerProvider(); $this->eventListenerRegistry = new EventListenerRegistry(); @@ -129,7 +80,8 @@ public function setLogDirectory( ?string $logDirectory ): void $this->logDirectory = $logDirectory === null ? null : ( '/' . trim( $logDirectory, '/' ) ); } - public function getProjectDir() + #[\Override] + public function getProjectDir(): string { if ( $this->projectDirectory === null ) { return parent::getProjectDir(); @@ -138,7 +90,8 @@ public function getProjectDir() return $this->projectDirectory; } - public function getCacheDir() + #[\Override] + public function getCacheDir(): string { if ( $this->cacheDirectory === null ) { return parent::getCacheDir(); @@ -147,7 +100,8 @@ public function getCacheDir() return $this->getProjectDir() . $this->cacheDirectory . $this->getEnvironment(); } - public function getLogDir() + #[\Override] + public function getLogDir(): string { if ( $this->logDirectory === null ) { return parent::getLogDir(); @@ -156,6 +110,7 @@ public function getLogDir() return $this->getProjectDir() . $this->logDirectory; } + #[\Override] public function registerBundles(): array { return [ @@ -168,19 +123,21 @@ protected function configureContainer( ContainerBuilder $container, LoaderInterf } - protected function configureRoutes( RouteCollectionBuilder $routes ): void + protected function configureRoutes( RoutingConfigurator $routes ): void { foreach ( $this->routeRegistry->getRoutes() as $route ) { - $routes->addRoute( $route ); + RouteApplier::applyRouteToSymfony($route, $routes); } } - protected function getContainerBaseClass() + #[\Override] + protected function getContainerBaseClass(): string { return ContainerWithFallback::class; } - protected function initializeContainer() + #[\Override] + protected function initializeContainer(): void { parent::initializeContainer(); @@ -228,41 +185,49 @@ private function initializeListeners(): void $this->eventListenerRegistry->registerEvents( $eventDispatcher ); } + #[\Override] public function match( string $pattern, string $endpoint ): Route { return $this->routeRegistry->addRoute( $pattern, $endpoint ); } + #[\Override] public function get( string $pattern, string $endpoint ): Route { return $this->routeRegistry->addRoute( $pattern, $endpoint, Request::METHOD_GET ); } + #[\Override] public function post( string $pattern, string $endpoint ): Route { return $this->routeRegistry->addRoute( $pattern, $endpoint, Request::METHOD_POST ); } + #[\Override] public function put( string $pattern, string $endpoint ): Route { return $this->routeRegistry->addRoute( $pattern, $endpoint, Request::METHOD_PUT ); } + #[\Override] public function delete( string $pattern, string $endpoint ): Route { return $this->routeRegistry->addRoute( $pattern, $endpoint, Request::METHOD_DELETE ); } + #[\Override] public function options( string $pattern, string $endpoint ): Route { return $this->routeRegistry->addRoute( $pattern, $endpoint, Request::METHOD_OPTIONS ); } + #[\Override] public function patch( string $pattern, string $endpoint ): Route { return $this->routeRegistry->addRoute( $pattern, $endpoint, Request::METHOD_PATCH ); } + #[\Override] public function before( $callback, int $priority = 0 ): void { $this->eventListenerRegistry->addEvent( @@ -274,6 +239,7 @@ public function before( $callback, int $priority = 0 ): void ); } + #[\Override] public function after( $callback, int $priority = 0 ): void { $this->eventListenerRegistry->addEvent( @@ -285,6 +251,7 @@ public function after( $callback, int $priority = 0 ): void ); } + #[\Override] public function finish( $callback, int $priority = 0 ): void { $this->eventListenerRegistry->addEvent( @@ -296,6 +263,7 @@ public function finish( $callback, int $priority = 0 ): void ); } + #[\Override] public function error( $callback, int $priority = -8 ): void { $this->eventListenerRegistry->addEvent( @@ -307,13 +275,7 @@ public function error( $callback, int $priority = -8 ): void ); } - /** - * Handles the request and delivers the response. - * - * @param Request|null $request - * @throws Exception - */ - public function run( Request $request = null ): void + public function run( ?Request $request = null ): void { if ( !$request ) { $request = Request::createFromGlobals(); diff --git a/src/Endpoint.php b/src/Endpoint.php index 04b3d8b..7dad945 100644 --- a/src/Endpoint.php +++ b/src/Endpoint.php @@ -1,5 +1,9 @@ containerProvider = $containerProvider; + public function __construct( + private readonly ContainerProvider $containerProvider + ) { $this->middlewareCallbackResolver = new MiddlewareCallbackResolver(); } @@ -43,7 +42,7 @@ public function resolve( $callback ): callable $callback[0] = $this->containerProvider->getContainer()->get( $callback[0] ); } else { - if ( strpos( $callback, '::' ) !== false ) { + if (str_contains($callback, '::')) { if ( !is_callable( $callback ) ) { throw new RuntimeException( 'Invalid callback.' ); } diff --git a/src/EventListener/CallbackWrapper/AfterWrapper.php b/src/EventListener/CallbackWrapper/AfterWrapper.php index 26b2a6b..0210167 100644 --- a/src/EventListener/CallbackWrapper/AfterWrapper.php +++ b/src/EventListener/CallbackWrapper/AfterWrapper.php @@ -1,4 +1,7 @@ containerProvider = $containerProvider; + public function __construct( + ContainerProvider $containerProvider + ) { $this->callbackResolver = new CallbackClassResolver( $containerProvider ); } + #[\Override] public function wrap( $callback ): callable { return function( ResponseEvent $event ) use ( $callback ) { - if ( !$event->isMasterRequest() ) { + if ( !$event->isMainRequest() ) { return; } diff --git a/src/EventListener/CallbackWrapper/BeforeWrapper.php b/src/EventListener/CallbackWrapper/BeforeWrapper.php index fa03465..a17f26b 100644 --- a/src/EventListener/CallbackWrapper/BeforeWrapper.php +++ b/src/EventListener/CallbackWrapper/BeforeWrapper.php @@ -1,4 +1,7 @@ containerProvider = $containerProvider; $this->callbackResolver = new CallbackClassResolver( $containerProvider ); } + #[\Override] public function wrap( $callback ): callable { return function( RequestEvent $event ) use ( $callback ) { - if ( !$event->isMasterRequest() ) { + if ( !$event->isMainRequest() ) { return; } diff --git a/src/EventListener/CallbackWrapper/CallbackWrapper.php b/src/EventListener/CallbackWrapper/CallbackWrapper.php index 3262517..4678d4a 100644 --- a/src/EventListener/CallbackWrapper/CallbackWrapper.php +++ b/src/EventListener/CallbackWrapper/CallbackWrapper.php @@ -1,4 +1,7 @@ containerProvider = $containerProvider; $this->callbackResolver = new CallbackClassResolver( $containerProvider ); } + #[\Override] public function wrap( $callback ): callable { return function( ExceptionEvent $event ) use ( $callback ) { diff --git a/src/EventListener/CallbackWrapper/FinishWrapper.php b/src/EventListener/CallbackWrapper/FinishWrapper.php index 1ae44c1..834ae54 100644 --- a/src/EventListener/CallbackWrapper/FinishWrapper.php +++ b/src/EventListener/CallbackWrapper/FinishWrapper.php @@ -1,28 +1,25 @@ containerProvider = $containerProvider; $this->callbackResolver = new CallbackClassResolver( $containerProvider ); } + #[\Override] public function wrap( $callback ): callable { return function( TerminateEvent $event ) use ( $callback ) { diff --git a/src/EventListener/EventListenerApplier.php b/src/EventListener/EventListenerApplier.php index 0634ebd..472e8f7 100644 --- a/src/EventListener/EventListenerApplier.php +++ b/src/EventListener/EventListenerApplier.php @@ -1,4 +1,7 @@ eventType = $eventType; + public function __construct( + private readonly string $eventType, + callable $callback, + private readonly int $priority + ) { $this->callback = $callback; - $this->priority = $priority; } public function getEventType(): string diff --git a/src/EventListener/EventListenerRegistry.php b/src/EventListener/EventListenerRegistry.php index ecd2f01..c2fae93 100644 --- a/src/EventListener/EventListenerRegistry.php +++ b/src/EventListener/EventListenerRegistry.php @@ -1,4 +1,5 @@ containerProvider = $containerProvider; + public function __construct( + private readonly ContainerProvider $containerProvider + ) { $this->callbackResolver = new CallbackClassResolver( $containerProvider ); } @@ -36,12 +33,16 @@ public function execute( ResponseEvent $event ): void $request = $event->getRequest(); $routeName = $request->attributes->get('_route'); + if ($routeName === null) { + return; + } + $route = $router->getRouteCollection()->get( $routeName ); if ( !$route ) { return; } - $callbacks = (array)$route->getOption( Route::OPTION_AFTER_CONTROLLER_LISTENERS ); + $callbacks = (array)$route->getOption( RouteApplier::OPTION_AFTER_CONTROLLER_LISTENERS ); foreach ( $callbacks as $callback ) { $result = call_user_func( $this->callbackResolver->resolve( $callback ), diff --git a/src/EventListener/ListenerRunner/BeforeControllerListenerRunner.php b/src/EventListener/ListenerRunner/BeforeControllerListenerRunner.php index 0c00a20..3feb206 100644 --- a/src/EventListener/ListenerRunner/BeforeControllerListenerRunner.php +++ b/src/EventListener/ListenerRunner/BeforeControllerListenerRunner.php @@ -1,29 +1,26 @@ containerProvider = $containerProvider; + public function __construct( + private readonly ContainerProvider $containerProvider + ) { $this->callbackResolver = new CallbackClassResolver( $containerProvider ); } @@ -36,12 +33,16 @@ public function execute( RequestEvent $event ): void $request = $event->getRequest(); $routeName = $request->attributes->get('_route'); + if ($routeName === null) { + return; + } + $route = $router->getRouteCollection()->get( $routeName ); if ( !$route ) { return; } - $callbacks = (array)$route->getOption( Route::OPTION_BEFORE_CONTROLLER_LISTENERS ); + $callbacks = (array)$route->getOption( RouteApplier::OPTION_BEFORE_CONTROLLER_LISTENERS ); foreach ( $callbacks as $callback ) { $result = call_user_func( $this->callbackResolver->resolve( $callback ), diff --git a/src/EventListener/StringToResponseListener.php b/src/EventListener/StringToResponseListener.php index 42c6994..dbcc420 100644 --- a/src/EventListener/StringToResponseListener.php +++ b/src/EventListener/StringToResponseListener.php @@ -1,4 +1,5 @@ addRequirements( [ $key => $regex ] ); + $this->requirements[$key] = $regex; + + return $this; } - private function addCallback( string $option, $callback ): void + public function before( string|array|Closure $callback ): self { - $callbacks = (array)$this->getOption( $option ); - $callbacks[] = $callback; - $this->setOption( $option, $callbacks ); + $this->beforeCallbacks[] = $callback; + + return $this; } - public function before( $callback ): self + public function after( string|array|Closure $callback ): self { - $this->addCallback( self::OPTION_BEFORE_CONTROLLER_LISTENERS, $callback ); + $this->afterCallbacks[] = $callback; + return $this; } - public function after( $callback ): self + public function getBeforeCallbacks(): array { - $this->addCallback( self::OPTION_AFTER_CONTROLLER_LISTENERS, $callback ); - return $this; + return $this->beforeCallbacks; + } + + public function getAfterCallbacks(): array + { + return $this->afterCallbacks; + } + + public function getPath(): string + { + return $this->path; + } + + public function getController(): array|Closure|string + { + return $this->controller; + } + + public function getRequirements(): array + { + return $this->requirements; + } + + public function getMethods(): array + { + return $this->methods; + } + + public function getName(): string + { + $methods = $this->methods; + + sort($methods); + + return implode('_', [...$methods, $this->getPath()]); } } diff --git a/src/RouteApplier.php b/src/RouteApplier.php new file mode 100644 index 0000000..800946c --- /dev/null +++ b/src/RouteApplier.php @@ -0,0 +1,25 @@ +add($route->getName(), $route->getPath()) + ->controller($route->getController()) + ->methods($route->getMethods()) + ->requirements($route->getRequirements()) + ->options([ + self::OPTION_BEFORE_CONTROLLER_LISTENERS => $route->getBeforeCallbacks(), + self::OPTION_AFTER_CONTROLLER_LISTENERS => $route->getAfterCallbacks(), + ]); + } +} diff --git a/src/RouteContainer.php b/src/RouteContainer.php index b5219a4..9d548ed 100644 --- a/src/RouteContainer.php +++ b/src/RouteContainer.php @@ -1,4 +1,7 @@ endpointCallbackResolver = new EndpointCallbackResolver(); } - private function createRoute( string $pattern, string $endpoint, string $method = null ): Route + private function createRoute( string $pattern, string $endpoint, ?string $method = null ): Route { - $route = new Route( $pattern ); - $route->setDefault( '_controller', $this->endpointCallbackResolver->resolve( $endpoint ) ); - if ( $method ) { - $route->setMethods( [ $method ] ); - } - return $route; + return new Route( + path: $pattern, + controller: $this->endpointCallbackResolver->resolve( $endpoint ), + methods: $method === null ? [] : [$method] + ); } - public function addRoute( string $pattern, string $endpoint, string $method = null ): Route + public function addRoute( string $pattern, string $endpoint, ?string $method = null ): Route { $route = $this->createRoute( $pattern, $endpoint, $method ); $this->routes[] = $route; diff --git a/test/ContainerProviderTest.php b/test/ContainerProviderTest.php index 1a77cee..7fb61bd 100644 --- a/test/ContainerProviderTest.php +++ b/test/ContainerProviderTest.php @@ -3,6 +3,7 @@ namespace Clearbooks\Dilex; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; class ContainerProviderTest extends TestCase @@ -18,17 +19,13 @@ public function setUp(): void $this->containerProvider = new ContainerProvider(); } - /** - * @test - */ + #[Test] public function GivenContainerNotSet_WhenGettingContainer_ExpectNull() { $this->assertNull($this->containerProvider->getContainer()); } - /** - * @test - */ + #[Test] public function WhenSettingContainer_ThenGettingContainer_ExpectContainerReturned() { $container = new MockContainer([]); diff --git a/test/ContainerWithFallbackTest.php b/test/ContainerWithFallbackTest.php index 99045f0..ac95d53 100644 --- a/test/ContainerWithFallbackTest.php +++ b/test/ContainerWithFallbackTest.php @@ -3,6 +3,7 @@ namespace Clearbooks\Dilex; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; class ContainerWithFallbackTest extends TestCase @@ -18,9 +19,7 @@ public function setUp(): void $this->containerWithFallback = new ContainerWithFallback(); } - /** - * @test - */ + #[Test] public function GivenHasFallbackContainer_WhenCallingHasAndFallbackContainerHasItem_ExpectTrue() { $fallbackContainer = new MockContainer([Endpoint::class => new EndpointDummy()]); @@ -28,9 +27,7 @@ public function GivenHasFallbackContainer_WhenCallingHasAndFallbackContainerHasI $this->assertTrue($this->containerWithFallback->has(Endpoint::class)); } - /** - * @test - */ + #[Test] public function GivenHasFallbackContainer_WhenCallingGetAndFallbackContainerHasItem_ExpectItemReturned() { $endpoint = new EndpointDummy(); diff --git a/test/DilexIntegrationTest.php b/test/DilexIntegrationTest.php index acbc9e5..1fe5e51 100644 --- a/test/DilexIntegrationTest.php +++ b/test/DilexIntegrationTest.php @@ -4,11 +4,13 @@ namespace Clearbooks\Dilex; use PHPUnit\Framework\Assert; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Throwable; +use function restore_exception_handler; class DilexIntegrationTest extends TestCase { @@ -57,6 +59,7 @@ public function tearDown(): void $this->app->shutdown(); $fileSystem = new Filesystem(); $fileSystem->remove( $this->app->getCacheDir() ); + restore_exception_handler(); } private function runAndGetResponse(Request $request): ?string @@ -66,9 +69,7 @@ private function runAndGetResponse(Request $request): ?string return ob_get_clean(); } - /** - * @test - */ + #[Test] public function GivenApplicationWithController_HandlesGetRequest(): void { $this->mockContainer->setMapping(EchoController::class, new EchoController()); @@ -83,9 +84,7 @@ public function GivenApplicationWithController_HandlesGetRequest(): void $this->assertNull($this->error); } - /** - * @test - */ + #[Test] public function GivenApplicationWithController_HandlesPostRequest(): void { $this->mockContainer->setMapping(EchoController::class, new EchoController()); @@ -100,9 +99,7 @@ public function GivenApplicationWithController_HandlesPostRequest(): void $this->assertNull($this->error); } - /** - * @test - */ + #[Test] public function GivenApplicationWithController_HandlesPutRequest(): void { $this->mockContainer->setMapping(EchoController::class, new EchoController()); @@ -117,9 +114,7 @@ public function GivenApplicationWithController_HandlesPutRequest(): void $this->assertNull($this->error); } - /** - * @test - */ + #[Test] public function GivenApplicationWithController_HandlesDeleteRequest(): void { $this->mockContainer->setMapping(EchoController::class, new EchoController()); @@ -134,9 +129,7 @@ public function GivenApplicationWithController_HandlesDeleteRequest(): void $this->assertNull($this->error); } - /** - * @test - */ + #[Test] public function GivenApplicationWithController_HandlesPatchRequest(): void { $this->mockContainer->setMapping(EchoController::class, new EchoController()); @@ -151,9 +144,7 @@ public function GivenApplicationWithController_HandlesPatchRequest(): void $this->assertNull($this->error); } - /** - * @test - */ + #[Test] public function GivenApplicationWithController_HandlesOptionsRequest(): void { $this->mockContainer->setMapping(EchoController::class, new EchoController()); @@ -168,9 +159,7 @@ public function GivenApplicationWithController_HandlesOptionsRequest(): void $this->assertNull($this->error); } - /** - * @test - */ + #[Test] public function GivenApplicationWithController_WhenNoMethodRestriction_HandlesAllKindOfRequests(): void { $this->mockContainer->setMapping(EchoController::class, new EchoController()); @@ -189,9 +178,7 @@ public function GivenApplicationWithController_WhenNoMethodRestriction_HandlesAl $this->assertNull($this->error); } - /** - * @test - */ + #[Test] public function GivenApplicationWithController_HandlesErrors(): void { $this->mockContainer->setMapping(ErrorThrowingController::class, new ErrorThrowingController()); @@ -206,9 +193,7 @@ public function GivenApplicationWithController_HandlesErrors(): void $this->assertEquals(new \RuntimeException('Test exception'), $this->error); } - /** - * @test - */ + #[Test] public function GivenApplicationWithController_HandlesFinish(): void { $this->mockContainer->setMapping(EchoController::class, new EchoController()); @@ -230,9 +215,7 @@ public function GivenApplicationWithController_HandlesFinish(): void $this->assertNotNull($responseSpy); } - /** - * @test - */ + #[Test] public function WhenBeforeCallbackIsSet_CallbackIsExecutedBeforeController(): void { $counter = new Counter(); @@ -251,9 +234,7 @@ public function WhenBeforeCallbackIsSet_CallbackIsExecutedBeforeController(): vo $this->assertEquals(2, $counter->get()); } - /** - * @test - */ + #[Test] public function WhenBeforeSpecificControllerCallbackIsSet_CallbackIsExecutedBeforeControllerAndAfterGlobalBeforeCallback(): void { $counter = new Counter(); @@ -276,9 +257,7 @@ public function WhenBeforeSpecificControllerCallbackIsSet_CallbackIsExecutedBefo $this->assertEquals(3, $counter->get()); } - /** - * @test - */ + #[Test] public function WhenAfterCallbackIsSet_CallbackIsExecutedAfterController(): void { $counter = new Counter(); @@ -297,9 +276,7 @@ public function WhenAfterCallbackIsSet_CallbackIsExecutedAfterController(): void $this->assertEquals(2, $counter->get()); } - /** - * @test - */ + #[Test] public function WhenAfterSpecificControllerCallbackIsSet_CallbackIsExecutedAfterControllerAndBeforeGlobalAfterCallback(): void { $counter = new Counter(); @@ -322,9 +299,7 @@ public function WhenAfterSpecificControllerCallbackIsSet_CallbackIsExecutedAfter $this->assertEquals(3, $counter->get()); } - /** - * @test - */ + #[Test] public function GivenMiddleWareCallback_WhenAfterCallbackIsSet_ExpectNoError(): void { $this->expectNotToPerformAssertions(); diff --git a/test/EndpointCallbackResolverTest.php b/test/EndpointCallbackResolverTest.php index a99856f..7f85bc4 100644 --- a/test/EndpointCallbackResolverTest.php +++ b/test/EndpointCallbackResolverTest.php @@ -4,6 +4,7 @@ namespace Clearbooks\Dilex; use Clearbooks\Dilex\EventListener\CallbackWrapper\AfterCallback; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; class EndpointCallbackResolverTest extends TestCase @@ -19,9 +20,7 @@ public function setUp(): void $this->endpointCallbackResolver = new EndpointCallbackResolver(); } - /** - * @test - */ + #[Test] public function WhenCallbackIsNotString_ReturnSameCallback() { $callback = new \stdClass(); @@ -29,9 +28,7 @@ public function WhenCallbackIsNotString_ReturnSameCallback() $this->assertSame($callback, $newCallback); } - /** - * @test - */ + #[Test] public function WhenCallbackIsStringButNotAClass_ReturnSameCallback() { $callback = 'hello'; @@ -39,9 +36,7 @@ public function WhenCallbackIsStringButNotAClass_ReturnSameCallback() $this->assertSame($callback, $newCallback); } - /** - * @test - */ + #[Test] public function WhenCallbackIsString_RefersToAClass_ButDoesNotImplementEndpoint_ReturnSameCallback() { $callback = AfterCallback::class; @@ -49,9 +44,7 @@ public function WhenCallbackIsString_RefersToAClass_ButDoesNotImplementEndpoint_ $this->assertSame($callback, $newCallback); } - /** - * @test - */ + #[Test] public function WhenCallbackIsString_RefersToAClass_AndImplementsEndpoint_ReturnArrayWithExecuteMethodSpecified() { $callback = EndpointDummy::class; diff --git a/test/EventListener/CallbackClassResolverTest.php b/test/EventListener/CallbackClassResolverTest.php index 675497c..2d35ff3 100644 --- a/test/EventListener/CallbackClassResolverTest.php +++ b/test/EventListener/CallbackClassResolverTest.php @@ -5,6 +5,7 @@ use Clearbooks\Dilex\ContainerProvider; use Clearbooks\Dilex\MockContainer; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use RuntimeException; @@ -29,9 +30,7 @@ public function setUp(): void $this->callbackClassResolver = new CallbackClassResolver($containerProvider); } - /** - * @test - */ + #[Test] public function WhenPassingNonCallableObject_ExpectException() { $this->expectException(RuntimeException::class); @@ -39,9 +38,7 @@ public function WhenPassingNonCallableObject_ExpectException() $this->callbackClassResolver->resolve(new \stdClass()); } - /** - * @test - */ + #[Test] public function WhenPassingCallableObject_ExpectSameObjectReturned() { $callable = new CallableDummy(); @@ -49,9 +46,7 @@ public function WhenPassingCallableObject_ExpectSameObjectReturned() $this->assertSame($callable, $result); } - /** - * @test - */ + #[Test] public function WhenPassingCallableArray_AndFirstArrayParameterIsObject_ExpectSameArrayReturned() { $callable = [new CallableDummy(), 'execute']; @@ -59,9 +54,7 @@ public function WhenPassingCallableArray_AndFirstArrayParameterIsObject_ExpectSa $this->assertSame($callable, $result); } - /** - * @test - */ + #[Test] public function WhenPassingCallableArray_AndFirstArrayParameterIsString_ExpectClassResolved() { $callable = [CallableDummy::class, 'execute']; @@ -71,9 +64,7 @@ public function WhenPassingCallableArray_AndFirstArrayParameterIsString_ExpectCl $this->assertSame([$callableDummyInstance, 'execute'], $result); } - /** - * @test - */ + #[Test] public function WhenPassingNonCallableStringWithDoubleColon_ExpectException() { $this->expectException(RuntimeException::class); @@ -82,9 +73,7 @@ public function WhenPassingNonCallableStringWithDoubleColon_ExpectException() $this->callbackClassResolver->resolve($callable); } - /** - * @test - */ + #[Test] public function WhenPassingCallableStringWithDoubleColon_ExpectCallableStringReturnedAsItIs() { $callable = CallableDummy::class . '::run'; @@ -92,9 +81,7 @@ public function WhenPassingCallableStringWithDoubleColon_ExpectCallableStringRet $this->assertSame($callable, $result); } - /** - * @test - */ + #[Test] public function WhenPassingStringWithoutDoubleColon_ExpectClassResolved() { $callable = CallableDummy::class; diff --git a/test/EventListener/CallbackWrapper/AfterWrapperTest.php b/test/EventListener/CallbackWrapper/AfterWrapperTest.php index 7309d53..cdd4658 100644 --- a/test/EventListener/CallbackWrapper/AfterWrapperTest.php +++ b/test/EventListener/CallbackWrapper/AfterWrapperTest.php @@ -5,6 +5,7 @@ use Clearbooks\Dilex\ContainerProvider; use Clearbooks\Dilex\MockContainer; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use RuntimeException; use Symfony\Component\HttpFoundation\Request; @@ -33,7 +34,7 @@ public function setUp(): void $this->afterWrapper = new AfterWrapper($containerProvider); } - private function createTestResponseEvent(int $requestType = HttpKernelInterface::MASTER_REQUEST): ResponseEvent + private function createTestResponseEvent(int $requestType = HttpKernelInterface::MAIN_REQUEST): ResponseEvent { return new ResponseEvent( $this->createMock(HttpKernelInterface::class), @@ -43,9 +44,7 @@ private function createTestResponseEvent(int $requestType = HttpKernelInterface: ); } - /** - * @test - */ + #[Test] public function WhenCallbackReturnsNotAResponseOrNull_ExpectException() { $this->expectException(RuntimeException::class); @@ -60,9 +59,7 @@ public function WhenCallbackReturnsNotAResponseOrNull_ExpectException() $callable($event); } - /** - * @test - */ + #[Test] public function WhenCallbackReturnsResponse_ExpectResponseSetOnEvent() { $event = $this->createTestResponseEvent(); @@ -76,9 +73,7 @@ public function WhenCallbackReturnsResponse_ExpectResponseSetOnEvent() $this->assertSame($response, $event->getResponse()); } - /** - * @test - */ + #[Test] public function WhenCalled_ExpectCallbackCalledWithCorrectParameters() { $event = $this->createTestResponseEvent(); @@ -90,9 +85,7 @@ public function WhenCalled_ExpectCallbackCalledWithCorrectParameters() $this->assertSame([[$event->getRequest(), $event->getResponse()]], $callbackInstance->getCallHistory()); } - /** - * @test - */ + #[Test] public function WhenCallbackReturnsNull_ExpectOriginalResponseNotChanged() { $event = $this->createTestResponseEvent(); @@ -105,9 +98,7 @@ public function WhenCallbackReturnsNull_ExpectOriginalResponseNotChanged() $this->assertSame($originalResponse, $event->getResponse()); } - /** - * @test - */ + #[Test] public function WhenEventIsNotMasterRequest_ExpectNothingCalledOrChanged() { $event = $this->createTestResponseEvent(HttpKernelInterface::SUB_REQUEST); diff --git a/test/EventListener/CallbackWrapper/BeforeWrapperTest.php b/test/EventListener/CallbackWrapper/BeforeWrapperTest.php index 8c1331d..172e673 100644 --- a/test/EventListener/CallbackWrapper/BeforeWrapperTest.php +++ b/test/EventListener/CallbackWrapper/BeforeWrapperTest.php @@ -5,12 +5,11 @@ use Clearbooks\Dilex\ContainerProvider; use Clearbooks\Dilex\MockContainer; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; -use RuntimeException; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\RequestEvent; -use Symfony\Component\HttpKernel\Event\ResponseEvent; use Symfony\Component\HttpKernel\HttpKernelInterface; class BeforeWrapperTest extends TestCase @@ -34,7 +33,7 @@ public function setUp(): void $this->beforeWrapper = new BeforeWrapper($containerProvider); } - private function createTestRequestEvent(int $requestType = HttpKernelInterface::MASTER_REQUEST): RequestEvent + private function createTestRequestEvent(int $requestType = HttpKernelInterface::MAIN_REQUEST): RequestEvent { return new RequestEvent( $this->createMock(HttpKernelInterface::class), @@ -43,9 +42,7 @@ private function createTestRequestEvent(int $requestType = HttpKernelInterface:: ); } - /** - * @test - */ + #[Test] public function WhenCallbackReturnsResponse_ExpectResponseSetOnEvent() { $event = $this->createTestRequestEvent(); @@ -59,9 +56,7 @@ public function WhenCallbackReturnsResponse_ExpectResponseSetOnEvent() $this->assertSame($response, $event->getResponse()); } - /** - * @test - */ + #[Test] public function WhenCalled_ExpectCallbackCalledWithCorrectParameters() { $event = $this->createTestRequestEvent(); @@ -73,9 +68,7 @@ public function WhenCalled_ExpectCallbackCalledWithCorrectParameters() $this->assertSame([$event->getRequest()], $callbackInstance->getCallHistory()); } - /** - * @test - */ + #[Test] public function WhenCallbackReturnsNull_ExpectOriginalResponseNotChanged() { $event = $this->createTestRequestEvent(); @@ -88,9 +81,7 @@ public function WhenCallbackReturnsNull_ExpectOriginalResponseNotChanged() $this->assertSame($originalResponse, $event->getResponse()); } - /** - * @test - */ + #[Test] public function WhenEventIsNotMasterRequest_ExpectNothingCalledOrChanged() { $event = $this->createTestRequestEvent(HttpKernelInterface::SUB_REQUEST); diff --git a/test/EventListener/CallbackWrapper/ErrorWrapperTest.php b/test/EventListener/CallbackWrapper/ErrorWrapperTest.php index f471586..d8fea8e 100644 --- a/test/EventListener/CallbackWrapper/ErrorWrapperTest.php +++ b/test/EventListener/CallbackWrapper/ErrorWrapperTest.php @@ -6,6 +6,7 @@ use Clearbooks\Dilex\ContainerProvider; use Clearbooks\Dilex\MockContainer; use Exception; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -35,19 +36,17 @@ public function setUp(): void $this->errorWrapper = new ErrorWrapper($containerProvider); } - private function createTestExceptionEvent(Throwable $exception = null): ExceptionEvent + private function createTestExceptionEvent(?Throwable $exception = null): ExceptionEvent { return new ExceptionEvent( $this->createMock(HttpKernelInterface::class), new Request(), - HttpKernelInterface::MASTER_REQUEST, + HttpKernelInterface::MAIN_REQUEST, $exception ?? new Exception() ); } - /** - * @test - */ + #[Test] public function WhenCallbackReturnsResponse_ExpectResponseSetOnEvent() { $event = $this->createTestExceptionEvent(); @@ -61,9 +60,7 @@ public function WhenCallbackReturnsResponse_ExpectResponseSetOnEvent() $this->assertSame($response, $event->getResponse()); } - /** - * @test - */ + #[Test] public function WhenCalled_ExpectCallbackCalledWithCorrectParameters() { $event = $this->createTestExceptionEvent(); @@ -75,9 +72,7 @@ public function WhenCalled_ExpectCallbackCalledWithCorrectParameters() $this->assertSame([[$event->getThrowable(), 500, $event->getRequest()]], $callbackInstance->getCallHistory()); } - /** - * @test - */ + #[Test] public function GivenHttpException_WhenCalled_ExpectCallbackCalledWithCorrectCode() { $exception = new HttpException(404); diff --git a/test/EventListener/CallbackWrapper/FinishWrapperTest.php b/test/EventListener/CallbackWrapper/FinishWrapperTest.php index 61b7bf5..4407feb 100644 --- a/test/EventListener/CallbackWrapper/FinishWrapperTest.php +++ b/test/EventListener/CallbackWrapper/FinishWrapperTest.php @@ -6,6 +6,7 @@ use Clearbooks\Dilex\ContainerProvider; use Clearbooks\Dilex\MockContainer; use Exception; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -45,9 +46,7 @@ private function createTestTerminateEvent(): TerminateEvent ); } - /** - * @test - */ + #[Test] public function WhenCalled_ExpectCallbackCalledWithCorrectParameters() { $event = $this->createTestTerminateEvent(); diff --git a/test/EventListener/EventListenerRegistryTest.php b/test/EventListener/EventListenerRegistryTest.php index b8d89c7..d2e89c7 100644 --- a/test/EventListener/EventListenerRegistryTest.php +++ b/test/EventListener/EventListenerRegistryTest.php @@ -3,6 +3,7 @@ namespace Clearbooks\Dilex\EventListener; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -27,18 +28,14 @@ public function setUp(): void $this->eventDispatcherInterfaceSpy = $this->createMock(EventDispatcherInterface::class); } - /** - * @test - */ + #[Test] public function GivenNoEventsAdded_WhenCallingRegisterEvents_ExpectEventDispatcherNotCalled() { $this->eventDispatcherInterfaceSpy->expects($this->never())->method('addListener'); $this->eventListenerRegistry->registerEvents($this->eventDispatcherInterfaceSpy); } - /** - * @test - */ + #[Test] public function GivenSomeEventsAdded_WhenCallingRegisterEvents_ExpectEventDispatcherCalledForEachEvent() { $event1 = new EventListenerRecord(KernelEvents::REQUEST, [$this, 'setUp'], 1); @@ -47,19 +44,27 @@ public function GivenSomeEventsAdded_WhenCallingRegisterEvents_ExpectEventDispat $event2 = new EventListenerRecord(KernelEvents::RESPONSE, [$this, 'count'], 2); $this->eventListenerRegistry->addEvent($event2); - $this->eventDispatcherInterfaceSpy->expects($this->exactly(2))->method('addListener')->withConsecutive( - [ + $this->eventDispatcherInterfaceSpy->expects($matcher = $this->exactly(2))->method('addListener')->willReturnCallback(function (...$x) use ($matcher, $event1, $event2) { + match ($matcher->numberOfInvocations()) { + 1 => self::assertEquals( + [ $event1->getEventType(), $event1->getCallback(), $event1->getPriority() - ], - - [ + ], + $x + ), + 2 => self::assertEquals( + [ $event2->getEventType(), $event2->getCallback(), $event2->getPriority() - ] - ); + ], + $x + ) + }; + }); + $this->eventListenerRegistry->registerEvents($this->eventDispatcherInterfaceSpy); } } diff --git a/test/EventListener/ListenerRunner/AfterControllerListenerRunnerTest.php b/test/EventListener/ListenerRunner/AfterControllerListenerRunnerTest.php index c75b894..0ade4fe 100644 --- a/test/EventListener/ListenerRunner/AfterControllerListenerRunnerTest.php +++ b/test/EventListener/ListenerRunner/AfterControllerListenerRunnerTest.php @@ -7,13 +7,18 @@ use Clearbooks\Dilex\EventListener\CallbackWrapper\AfterCallback; use Clearbooks\Dilex\MockContainer; use Clearbooks\Dilex\Route; +use Clearbooks\Dilex\RouteApplier; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use RuntimeException; +use Symfony\Component\Config\FileLocatorInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\ResponseEvent; use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; +use Symfony\Component\Routing\Loader\PhpFileLoader; use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouterInterface; @@ -39,13 +44,29 @@ class AfterControllerListenerRunnerTest extends TestCase */ private $afterControllerListenerRunner; + private RoutingConfigurator $routingConfigurator; + public function setUp(): void { parent::setUp(); $this->routeCollection = new RouteCollection(); + + $this->routingConfigurator = new RoutingConfigurator( + $this->routeCollection, + new PhpFileLoader(new class implements FileLocatorInterface { + + public function locate(string $name, ?string $currentPath = null, bool $first = true): string|array + { + return []; + } + }), + '', + '' + ); + $this->routerInterface = $this->createMock(RouterInterface::class); - $this->routerInterface->method('getRouteCollection')->willReturn($this->routeCollection); + $this->routerInterface->method('getRouteCollection')->willReturnCallback(fn () => $this->routeCollection); $this->mockContainer = new MockContainer(['router' => $this->routerInterface]); $containerProvider = new ContainerProvider(); @@ -60,14 +81,12 @@ private function createTestResponseEvent(string $route): ResponseEvent return new ResponseEvent( $this->createMock(HttpKernelInterface::class), $request, - HttpKernelInterface::MASTER_REQUEST, + HttpKernelInterface::MAIN_REQUEST, new Response() ); } - /** - * @test - */ + #[Test] public function GivenRouteDoesNotExist_ExpectNoError() { $this->expectNotToPerformAssertions(); @@ -76,42 +95,34 @@ public function GivenRouteDoesNotExist_ExpectNoError() $this->afterControllerListenerRunner->execute($event); } - /** - * @test - */ + #[Test] public function GivenRouteExist_ButNoAfterControllerListeners_ExpectNoError() { $this->expectNotToPerformAssertions(); - $routeName = '/test'; - $route = new Route($routeName); - $this->routeCollection->add($routeName, $route); - $event = $this->createTestResponseEvent($routeName); + $route = new Route('/test', ''); + RouteApplier::applyRouteToSymfony($route, $this->routingConfigurator); + $event = $this->createTestResponseEvent($route->getName()); $this->afterControllerListenerRunner->execute($event); } - /** - * @test - */ + #[Test] public function GivenRouteExistWithAfterControllerListener_ExpectListenerCalledWithCorrectParameters() { $callback = AfterCallback::class; $callbackInstance = new AfterCallback(); $this->mockContainer->setMapping($callback, $callbackInstance); - $routeName = '/test'; - $route = new Route($routeName); + $route = new Route('/test', ''); $route->after($callback); - $this->routeCollection->add($routeName, $route); - $event = $this->createTestResponseEvent($routeName); + RouteApplier::applyRouteToSymfony($route, $this->routingConfigurator); + $event = $this->createTestResponseEvent($route->getName()); $this->afterControllerListenerRunner->execute($event); $this->assertSame([[$event->getRequest(), $event->getResponse()]], $callbackInstance->getCallHistory()); } - /** - * @test - */ + #[Test] public function GivenRouteExistWithAfterControllerListener_WhenCallbackReturnsNotAResponseOrNull_ExpectException() { $this->expectException(RuntimeException::class); @@ -122,18 +133,15 @@ public function GivenRouteExistWithAfterControllerListener_WhenCallbackReturnsNo $callbackInstance->setResult(''); $this->mockContainer->setMapping($callback, $callbackInstance); - $routeName = '/test'; - $route = new Route($routeName); + $route = new Route('/test', ''); $route->after($callback); - $this->routeCollection->add($routeName, $route); - $event = $this->createTestResponseEvent($routeName); + RouteApplier::applyRouteToSymfony($route, $this->routingConfigurator); + $event = $this->createTestResponseEvent($route->getName()); $this->afterControllerListenerRunner->execute($event); } - /** - * @test - */ + #[Test] public function GivenRouteExistWithAfterControllerListener_WhenCallbackReturnsResponse_ExpectResponseSetOnEvent() { $callback = AfterCallback::class; @@ -142,34 +150,31 @@ public function GivenRouteExistWithAfterControllerListener_WhenCallbackReturnsRe $callbackInstance->setResult($response); $this->mockContainer->setMapping($callback, $callbackInstance); - $routeName = '/test'; - $route = new Route($routeName); + $route = new Route('/test', ''); $route->after($callback); - $this->routeCollection->add($routeName, $route); - $event = $this->createTestResponseEvent($routeName); + RouteApplier::applyRouteToSymfony($route, $this->routingConfigurator); + $event = $this->createTestResponseEvent($route->getName()); $this->afterControllerListenerRunner->execute($event); $this->assertSame($response, $event->getResponse()); } - /** - * @test - */ + #[Test] public function GivenRouteExistWithMultipleAfterControllerListeners_ExpectListenerCalledWithCorrectParametersForEachListener() { $callback = AfterCallback::class; $callbackInstance = new AfterCallback(); $this->mockContainer->setMapping($callback, $callbackInstance); - $routeName = '/test'; - $route = new Route($routeName); + $route = new Route('/test', ''); $route->after($callback); $route->after($callback); $route->after($callback); - $this->routeCollection->add($routeName, $route); - $event = $this->createTestResponseEvent($routeName); + RouteApplier::applyRouteToSymfony($route, $this->routingConfigurator); + + $event = $this->createTestResponseEvent($route->getName()); $this->afterControllerListenerRunner->execute($event); $this->assertSame( diff --git a/test/EventListener/ListenerRunner/BeforeControllerListenerRunnerTest.php b/test/EventListener/ListenerRunner/BeforeControllerListenerRunnerTest.php index 0cb67c5..f96c09f 100644 --- a/test/EventListener/ListenerRunner/BeforeControllerListenerRunnerTest.php +++ b/test/EventListener/ListenerRunner/BeforeControllerListenerRunnerTest.php @@ -7,13 +7,18 @@ use Clearbooks\Dilex\EventListener\CallbackWrapper\BeforeCallback; use Clearbooks\Dilex\MockContainer; use Clearbooks\Dilex\Route; +use Clearbooks\Dilex\RouteApplier; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use RuntimeException; +use Symfony\Component\Config\FileLocatorInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; +use Symfony\Component\Routing\Loader\PhpFileLoader; use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouterInterface; @@ -39,13 +44,29 @@ class BeforeControllerListenerRunnerTest extends TestCase */ private $beforeControllerListenerRunner; + private RoutingConfigurator $routingConfigurator; + public function setUp(): void { parent::setUp(); $this->routeCollection = new RouteCollection(); + + $this->routingConfigurator = new RoutingConfigurator( + $this->routeCollection, + new PhpFileLoader(new class implements FileLocatorInterface { + + public function locate(string $name, ?string $currentPath = null, bool $first = true): string|array + { + return []; + } + }), + '', + '' + ); + $this->routerInterface = $this->createMock(RouterInterface::class); - $this->routerInterface->method('getRouteCollection')->willReturn($this->routeCollection); + $this->routerInterface->method('getRouteCollection')->willReturnCallback(fn () => $this->routeCollection); $this->mockContainer = new MockContainer(['router' => $this->routerInterface]); $containerProvider = new ContainerProvider(); @@ -60,13 +81,11 @@ private function createTestRequestEvent(string $route): RequestEvent return new RequestEvent( $this->createMock(HttpKernelInterface::class), $request, - HttpKernelInterface::MASTER_REQUEST + HttpKernelInterface::MAIN_REQUEST ); } - /** - * @test - */ + #[Test] public function GivenRouteDoesNotExist_ExpectNoError() { $this->expectNotToPerformAssertions(); @@ -75,42 +94,35 @@ public function GivenRouteDoesNotExist_ExpectNoError() $this->beforeControllerListenerRunner->execute($event); } - /** - * @test - */ + #[Test] public function GivenRouteExist_ButNoBeforeControllerListeners_ExpectNoError() { $this->expectNotToPerformAssertions(); - $routeName = '/test'; - $route = new Route($routeName); - $this->routeCollection->add($routeName, $route); - $event = $this->createTestRequestEvent($routeName); + + $route = new Route('/test', ''); + RouteApplier::applyRouteToSymfony($route, $this->routingConfigurator); + $event = $this->createTestRequestEvent($route->getName()); $this->beforeControllerListenerRunner->execute($event); } - /** - * @test - */ + #[Test] public function GivenRouteExistWithBeforeControllerListener_ExpectListenerCalledWithCorrectParameters() { $callback = BeforeCallback::class; $callbackInstance = new BeforeCallback(); $this->mockContainer->setMapping($callback, $callbackInstance); - $routeName = '/test'; - $route = new Route($routeName); + $route = new Route('/test', ''); $route->before($callback); - $this->routeCollection->add($routeName, $route); - $event = $this->createTestRequestEvent($routeName); + RouteApplier::applyRouteToSymfony($route, $this->routingConfigurator); + $event = $this->createTestRequestEvent($route->getName()); $this->beforeControllerListenerRunner->execute($event); $this->assertSame([$event->getRequest()], $callbackInstance->getCallHistory()); } - /** - * @test - */ + #[Test] public function GivenRouteExistWithBeforeControllerListener_WhenCallbackReturnsNotAResponseOrNull_ExpectException() { $this->expectException(RuntimeException::class); @@ -121,18 +133,15 @@ public function GivenRouteExistWithBeforeControllerListener_WhenCallbackReturnsN $callbackInstance->setResult(''); $this->mockContainer->setMapping($callback, $callbackInstance); - $routeName = '/test'; - $route = new Route($routeName); + $route = new Route('/test', ''); $route->before($callback); - $this->routeCollection->add($routeName, $route); - $event = $this->createTestRequestEvent($routeName); + RouteApplier::applyRouteToSymfony($route, $this->routingConfigurator); + $event = $this->createTestRequestEvent($route->getName()); $this->beforeControllerListenerRunner->execute($event); } - /** - * @test - */ + #[Test] public function GivenRouteExistWithBeforeControllerListener_WhenCallbackReturnsResponse_ExpectResponseSetOnEvent() { $callback = BeforeCallback::class; @@ -141,34 +150,30 @@ public function GivenRouteExistWithBeforeControllerListener_WhenCallbackReturnsR $callbackInstance->setResult($response); $this->mockContainer->setMapping($callback, $callbackInstance); - $routeName = '/test'; - $route = new Route($routeName); + $route = new Route('/test', ''); $route->before($callback); - $this->routeCollection->add($routeName, $route); - $event = $this->createTestRequestEvent($routeName); + RouteApplier::applyRouteToSymfony($route, $this->routingConfigurator); + $event = $this->createTestRequestEvent($route->getName()); $this->beforeControllerListenerRunner->execute($event); $this->assertSame($response, $event->getResponse()); } - /** - * @test - */ + #[Test] public function GivenRouteExistWithMultipleBeforeControllerListeners_ExpectListenerCalledWithCorrectParametersForEachListener() { $callback = BeforeCallback::class; $callbackInstance = new BeforeCallback(); $this->mockContainer->setMapping($callback, $callbackInstance); - $routeName = '/test'; - $route = new Route($routeName); + $route = new Route('/test', ''); $route->before($callback); $route->before($callback); $route->before($callback); - $this->routeCollection->add($routeName, $route); - $event = $this->createTestRequestEvent($routeName); + RouteApplier::applyRouteToSymfony($route, $this->routingConfigurator); + $event = $this->createTestRequestEvent($route->getName()); $this->beforeControllerListenerRunner->execute($event); $this->assertSame( diff --git a/test/EventListener/StringToResponseListenerTest.php b/test/EventListener/StringToResponseListenerTest.php index 13b47ee..9bcd3a1 100644 --- a/test/EventListener/StringToResponseListenerTest.php +++ b/test/EventListener/StringToResponseListenerTest.php @@ -3,6 +3,7 @@ namespace Clearbooks\Dilex\EventListener; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; @@ -34,9 +35,7 @@ private function createViewEventWithControllerResult($result): ViewEvent ); } - /** - * @test - */ + #[Test] public function GivenNullControllerResult_ExpectResponseUnchanged() { $event = $this->createViewEventWithControllerResult(null); @@ -44,9 +43,7 @@ public function GivenNullControllerResult_ExpectResponseUnchanged() $this->assertFalse($event->hasResponse()); } - /** - * @test - */ + #[Test] public function GivenArrayControllerResult_ExpectResponseUnchanged() { $event = $this->createViewEventWithControllerResult([]); @@ -54,9 +51,7 @@ public function GivenArrayControllerResult_ExpectResponseUnchanged() $this->assertFalse($event->hasResponse()); } - /** - * @test - */ + #[Test] public function GivenResponseTypeControllerResult_ExpectResponseUnchanged() { $event = $this->createViewEventWithControllerResult(new Response()); @@ -64,9 +59,7 @@ public function GivenResponseTypeControllerResult_ExpectResponseUnchanged() $this->assertFalse($event->hasResponse()); } - /** - * @test - */ + #[Test] public function GivenObjectTypeControllerResult_WithoutToStringMethod_ExpectResponseUnchanged() { $event = $this->createViewEventWithControllerResult(new \stdClass()); @@ -74,9 +67,7 @@ public function GivenObjectTypeControllerResult_WithoutToStringMethod_ExpectResp $this->assertFalse($event->hasResponse()); } - /** - * @test - */ + #[Test] public function GivenStringControllerResult_ExpectResponseSet() { $result = "test"; @@ -85,9 +76,7 @@ public function GivenStringControllerResult_ExpectResponseSet() $this->assertEquals(new Response($result), $event->getResponse()); } - /** - * @test - */ + #[Test] public function GivenObjectTypeControllerResult_WithToStringMethod_ExpectResponseUnchanged() { $result = new TestResultWithToStringMethod(); diff --git a/test/MiddlewareCallbackResolverTest.php b/test/MiddlewareCallbackResolverTest.php index 2e1b845..19a682c 100644 --- a/test/MiddlewareCallbackResolverTest.php +++ b/test/MiddlewareCallbackResolverTest.php @@ -4,6 +4,7 @@ namespace Clearbooks\Dilex; use Clearbooks\Dilex\EventListener\CallbackWrapper\AfterCallback; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; class MiddlewareCallbackResolverTest extends TestCase @@ -19,9 +20,7 @@ public function setUp(): void $this->middlewareCallbackResolver = new MiddlewareCallbackResolver(); } - /** - * @test - */ + #[Test] public function WhenCallbackIsNotString_ReturnSameCallback() { $callback = new \stdClass(); @@ -29,9 +28,7 @@ public function WhenCallbackIsNotString_ReturnSameCallback() $this->assertSame($callback, $newCallback); } - /** - * @test - */ + #[Test] public function WhenCallbackIsStringButNotAClass_ReturnSameCallback() { $callback = 'hello'; @@ -39,9 +36,7 @@ public function WhenCallbackIsStringButNotAClass_ReturnSameCallback() $this->assertSame($callback, $newCallback); } - /** - * @test - */ + #[Test] public function WhenCallbackIsString_RefersToAClass_ButDoesNotImplementMiddleware_ReturnSameCallback() { $callback = AfterCallback::class; @@ -49,9 +44,7 @@ public function WhenCallbackIsString_RefersToAClass_ButDoesNotImplementMiddlewar $this->assertSame($callback, $newCallback); } - /** - * @test - */ + #[Test] public function WhenCallbackIsString_RefersToAClass_AndImplementsMiddleware_ReturnArrayWithExecuteMethodSpecified() { $callback = MiddlewareDummy::class; diff --git a/test/RouteRegistryTest.php b/test/RouteRegistryTest.php index e7b5fa9..c019b2b 100644 --- a/test/RouteRegistryTest.php +++ b/test/RouteRegistryTest.php @@ -3,7 +3,7 @@ namespace Clearbooks\Dilex; -use InvalidArgumentException; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; @@ -20,17 +20,13 @@ public function setUp(): void $this->routeRegistry = new RouteRegistry(); } - /** - * @test - */ + #[Test] public function GivenNoRoutes_WhenCallingGetRoutes_ExpectEmptyArray() { $this->assertEquals( [], $this->routeRegistry->getRoutes() ); } - /** - * @test - */ + #[Test] public function WhenAddingRoute_ExpectRouteCorrectlyConfigured() { $routePath = '/test'; @@ -38,13 +34,11 @@ public function WhenAddingRoute_ExpectRouteCorrectlyConfigured() $method = Request::METHOD_POST; $route = $this->routeRegistry->addRoute( $routePath, $controller, $method ); $this->assertEquals( $routePath, $route->getPath() ); - $this->assertEquals( [ $controller, 'execute' ], $route->getDefault('_controller') ); + $this->assertEquals( [ $controller, 'execute' ], $route->getController() ); $this->assertEquals( [ $method ], $route->getMethods() ); } - /** - * @test - */ + #[Test] public function GivenRouteAdded_WhenCallingGetRoutes_ExpectRouteReturned() { $routePath = '/test'; diff --git a/test/RouteTest.php b/test/RouteTest.php index 5a61601..24c926b 100644 --- a/test/RouteTest.php +++ b/test/RouteTest.php @@ -3,6 +3,7 @@ namespace Clearbooks\Dilex; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; class RouteTest extends TestCase @@ -15,55 +16,49 @@ class RouteTest extends TestCase public function setUp(): void { parent::setUp(); - $this->route = new Route('/test'); + $this->route = new Route('/test', ''); } - /** - * @test - */ + #[Test] public function WhenCallingAssert_ExpectRequirementAdded() { $this->route->assert('id', 'test'); - $this->assertEquals('test', $this->route->getRequirement('id')); + $this->assertEquals('test', $this->route->getRequirements()['id']); } - /** - * @test - */ + #[Test] public function WhenCallingBefore_ExpectBeforeControllerListenersAdded() { $testCallback = [self::class, 'setUp']; $this->route->before($testCallback); $this->assertEquals( [$testCallback], - $this->route->getOption(Route::OPTION_BEFORE_CONTROLLER_LISTENERS) + $this->route->getBeforeCallbacks() ); $testCallback2 = [self::class, 'tearDown']; $this->route->before($testCallback2); $this->assertEquals( [$testCallback, $testCallback2], - $this->route->getOption(Route::OPTION_BEFORE_CONTROLLER_LISTENERS) + $this->route->getBeforeCallbacks() ); } - /** - * @test - */ + #[Test] public function WhenCallingAfter_ExpectAfterControllerListenersAdded() { $testCallback = [self::class, 'setUp']; $this->route->after($testCallback); $this->assertEquals( [$testCallback], - $this->route->getOption(Route::OPTION_AFTER_CONTROLLER_LISTENERS) + $this->route->getAfterCallbacks() ); $testCallback2 = [self::class, 'tearDown']; $this->route->after($testCallback2); $this->assertEquals( [$testCallback, $testCallback2], - $this->route->getOption(Route::OPTION_AFTER_CONTROLLER_LISTENERS) + $this->route->getAfterCallbacks() ); } }