diff --git a/src/Traits/InteractsWithDispatcher.php b/src/Traits/InteractsWithDispatcher.php index 8a8cd2d..8250616 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,8 @@ public function assertDispatcherMissed(string $dispatcher): void */ public function getRegisteredDispatchers(): array { - return array_map(static function ($dispatcher): string { - return get_class($dispatcher); + return \array_map(static function ($dispatcher): string { + return \is_object($dispatcher) ? $dispatcher::class : $dispatcher; }, $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 */ 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(), + ); + } +}