From 1f3f453fb043f21a4fbe97633bba102e0bef6322 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Wed, 17 Jan 2024 15:54:32 +0200 Subject: [PATCH 1/2] Add compatibility with SF 3.12 dispatchers --- src/Traits/InteractsWithDispatcher.php | 8 +++----- src/Traits/TestableKernel.php | 8 ++++++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Traits/InteractsWithDispatcher.php b/src/Traits/InteractsWithDispatcher.php index 8a8cd2d..e0f857c 100644 --- a/src/Traits/InteractsWithDispatcher.php +++ b/src/Traits/InteractsWithDispatcher.php @@ -15,7 +15,7 @@ trait InteractsWithDispatcher public function assertDispatcherCanBeServed(string $dispatcher): void { $this->assertTrue( - $this->getContainer()->get($dispatcher)->canServe(), + $this->getContainer()->invoke([$dispatcher, 'canServe']), \sprintf('Dispatcher [%s] can not be served.', $dispatcher) ); } @@ -26,7 +26,7 @@ public function assertDispatcherCanBeServed(string $dispatcher): void public function assertDispatcherCannotBeServed(string $dispatcher): void { $this->assertFalse( - $this->getContainer()->get($dispatcher)->canServe(), + $this->getContainer()->invoke([$dispatcher, 'canServe']), \sprintf('Dispatcher [%s] can be served.', $dispatcher) ); } @@ -77,8 +77,6 @@ public function assertDispatcherMissed(string $dispatcher): void */ public function getRegisteredDispatchers(): array { - return array_map(static function ($dispatcher): string { - return get_class($dispatcher); - }, $this->getContainer()->get(KernelInterface::class)->getRegisteredDispatchers()); + return $this->getContainer()->get(KernelInterface::class)->getRegisteredDispatchers(); } } diff --git a/src/Traits/TestableKernel.php b/src/Traits/TestableKernel.php index 8c396f4..0ec52f7 100644 --- a/src/Traits/TestableKernel.php +++ b/src/Traits/TestableKernel.php @@ -15,10 +15,14 @@ public function getContainer(): Container return $this->container; } - /** @return DispatcherInterface[] */ + /** @return array> */ public function getRegisteredDispatchers(): array { - return $this->dispatchers; + return \array_map(static fn (string|DispatcherInterface $dispatcher): string => \is_object($dispatcher) + ? $dispatcher::class + : $dispatcher, + $this->dispatchers + ); } /** @return array */ From 912871bf5d7d2b3c837db4641910b968a4f316af Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Wed, 17 Jan 2024 16:06:33 +0200 Subject: [PATCH 2/2] Add tests --- src/Traits/InteractsWithDispatcher.php | 4 +- .../Traits/InteractsWithDispatcherTest.php | 85 +++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 tests/src/Traits/InteractsWithDispatcherTest.php diff --git a/src/Traits/InteractsWithDispatcher.php b/src/Traits/InteractsWithDispatcher.php index e0f857c..8250616 100644 --- a/src/Traits/InteractsWithDispatcher.php +++ b/src/Traits/InteractsWithDispatcher.php @@ -77,6 +77,8 @@ public function assertDispatcherMissed(string $dispatcher): void */ public function getRegisteredDispatchers(): array { - return $this->getContainer()->get(KernelInterface::class)->getRegisteredDispatchers(); + return \array_map(static function ($dispatcher): string { + return \is_object($dispatcher) ? $dispatcher::class : $dispatcher; + }, $this->getContainer()->get(KernelInterface::class)->getRegisteredDispatchers()); } } diff --git a/tests/src/Traits/InteractsWithDispatcherTest.php b/tests/src/Traits/InteractsWithDispatcherTest.php new file mode 100644 index 0000000..97d1c96 --- /dev/null +++ b/tests/src/Traits/InteractsWithDispatcherTest.php @@ -0,0 +1,85 @@ +assertDispatcherCanBeServed($dispatcher::class); + } + + public function testAssertDispatcherCanBeServedStaticMethodWithEnv(): void + { + $dispatcher = new class { + public function canServe(EnvironmentInterface $env): bool + { + return $env->get('MODE') === 'http'; + } + }; + + $this->getContainer()->bindSingleton(EnvironmentInterface::class, new Environment(['MODE' => 'http']), true); + $this->assertDispatcherCanBeServed($dispatcher::class); + } + + public function testAssertDispatcherCannotBeServed(): void + { + $dispatcher = new class implements DispatcherInterface { + public function canServe(): bool + { + return false; + } + + public function serve(): void {} + }; + + $this->assertDispatcherCannotBeServed($dispatcher::class); + } + + public function testAssertDispatcherCannotBeServedStaticMethodWithEnv(): void + { + $dispatcher = new class { + public function canServe(EnvironmentInterface $env): bool + { + return $env->get('MODE') === 'http'; + } + }; + + $this->getContainer()->bindSingleton(EnvironmentInterface::class, new Environment(['MODE' => 'jobs']), true); + $this->assertDispatcherCannotBeServed($dispatcher::class); + } + + public function testGetRegisteredDispatchers(): void + { + $dispatcherA = $this->createMock(DispatcherInterface::class); + $dispatcherB = $this->createMock(DispatcherInterface::class); + + $ref = new \ReflectionProperty($this->getApp(), 'dispatchers'); + $ref->setValue($this->getApp(), [$dispatcherA, $dispatcherB::class]); + + $this->assertSame( + [$dispatcherA::class, $dispatcherB::class], + $this->getRegisteredDispatchers(), + ); + } +}