Skip to content

Commit

Permalink
Merge pull request #26 from patchlevel/add-possibility-to-add-subscri…
Browse files Browse the repository at this point in the history
…bers

Enable injection of EventDispatcher
  • Loading branch information
DanielBadura authored Feb 5, 2025
2 parents 7f3f7c7 + 7ae57f4 commit 5484c7a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
6 changes: 5 additions & 1 deletion src/DefaultWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,12 @@ public static function create(
Closure $job,
array $options = [],
LoggerInterface $logger = new NullLogger(),
EventDispatcherInterface|null $eventDispatcher = null,
): self {
$eventDispatcher = new EventDispatcher();
if ($eventDispatcher === null) {
$eventDispatcher = new EventDispatcher();
}

$eventDispatcher->addSubscriber(new StopWorkerOnSigtermSignalListener($logger));

if (isset($options['runLimit'])) {
Expand Down
50 changes: 40 additions & 10 deletions tests/Unit/DefaultWorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,17 +21,17 @@ 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();

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);
Expand All @@ -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);
Expand All @@ -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);
}
}

0 comments on commit 5484c7a

Please sign in to comment.