From 7ae57f4497a68a07c666ac4f5b98bf27288a33c8 Mon Sep 17 00:00:00 2001 From: Daniel Badura Date: Wed, 5 Feb 2025 15:23:40 +0100 Subject: [PATCH] Add test --- src/DefaultWorker.php | 2 +- tests/Unit/DefaultWorkerTest.php | 50 +++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/DefaultWorker.php b/src/DefaultWorker.php index 70cfe3d..b2788ae 100644 --- a/src/DefaultWorker.php +++ b/src/DefaultWorker.php @@ -90,7 +90,7 @@ public static function create( Closure $job, array $options = [], LoggerInterface $logger = new NullLogger(), - EventDispatcherInterface|null $eventDispatcher, + EventDispatcherInterface|null $eventDispatcher = null, ): self { if ($eventDispatcher === null) { $eventDispatcher = new EventDispatcher(); diff --git a/tests/Unit/DefaultWorkerTest.php b/tests/Unit/DefaultWorkerTest.php index d3a3f88..4c81b3c 100644 --- a/tests/Unit/DefaultWorkerTest.php +++ b/tests/Unit/DefaultWorkerTest.php @@ -12,6 +12,7 @@ use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; use Psr\Log\LoggerInterface; +use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcherInterface; final class DefaultWorkerTest extends TestCase @@ -20,9 +21,9 @@ final class DefaultWorkerTest extends TestCase public function testRunWorker(): void { - $evenDispatcher = $this->prophesize(EventDispatcherInterface::class); - $evenDispatcher->dispatch(Argument::type(WorkerStartedEvent::class))->shouldBeCalledTimes(1); - $evenDispatcher->dispatch(Argument::type(WorkerRunningEvent::class))->shouldBeCalledTimes(1)->will( + $eventDispatcher = $this->prophesize(EventDispatcherInterface::class); + $eventDispatcher->dispatch(Argument::type(WorkerStartedEvent::class))->shouldBeCalledTimes(1); + $eventDispatcher->dispatch(Argument::type(WorkerRunningEvent::class))->shouldBeCalledTimes(1)->will( /** @param array{WorkerRunningEvent} $args */ static function (array $args) { $args[0]->worker->stop(); @@ -30,7 +31,7 @@ static function (array $args) { return $args[0]; }, ); - $evenDispatcher->dispatch(Argument::type(WorkerStoppedEvent::class))->shouldBeCalledTimes(1); + $eventDispatcher->dispatch(Argument::type(WorkerStoppedEvent::class))->shouldBeCalledTimes(1); $logger = $this->prophesize(LoggerInterface::class); $logger->debug('Worker starting')->shouldBeCalledTimes(1); @@ -40,16 +41,16 @@ static function (array $args) { $logger->debug('Worker stopped')->shouldBeCalledTimes(1); $logger->debug('Worker terminated')->shouldBeCalledTimes(1); - $worker = new DefaultWorker(static fn () => null, $evenDispatcher->reveal(), $logger->reveal()); + $worker = new DefaultWorker(static fn () => null, $eventDispatcher->reveal(), $logger->reveal()); $worker->run(200); } public function testJobStopWorker(): void { - $evenDispatcher = $this->prophesize(EventDispatcherInterface::class); - $evenDispatcher->dispatch(Argument::type(WorkerStartedEvent::class))->shouldBeCalledTimes(1); - $evenDispatcher->dispatch(Argument::type(WorkerRunningEvent::class))->shouldBeCalledTimes(1); - $evenDispatcher->dispatch(Argument::type(WorkerStoppedEvent::class))->shouldBeCalledTimes(1); + $eventDispatcher = $this->prophesize(EventDispatcherInterface::class); + $eventDispatcher->dispatch(Argument::type(WorkerStartedEvent::class))->shouldBeCalledTimes(1); + $eventDispatcher->dispatch(Argument::type(WorkerRunningEvent::class))->shouldBeCalledTimes(1); + $eventDispatcher->dispatch(Argument::type(WorkerStoppedEvent::class))->shouldBeCalledTimes(1); $logger = $this->prophesize(LoggerInterface::class); $logger->debug('Worker starting')->shouldBeCalledTimes(1); @@ -63,10 +64,39 @@ public function testJobStopWorker(): void static function ($stop): void { $stop(); }, - $evenDispatcher->reveal(), + $eventDispatcher->reveal(), $logger->reveal(), ); $worker->run(0); } + + public function testCustomEventDispatcher(): void + { + $listener = new class { + public int $called = 0; + + public function __invoke(WorkerStartedEvent $event): void + { + $this->called++; + } + }; + + $eventDispatcher = new EventDispatcher(); + $eventDispatcher->addListener(WorkerStartedEvent::class, $listener); + + $logger = $this->prophesize(LoggerInterface::class); + $worker = DefaultWorker::create( + static function ($stop): void { + $stop(); + }, + [], + $logger->reveal(), + $eventDispatcher, + ); + + $worker->run(0); + + self::assertEquals(1, $listener->called); + } }