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(), + ); + } +}