Skip to content

Commit 5484c7a

Browse files
authored
Merge pull request #26 from patchlevel/add-possibility-to-add-subscribers
Enable injection of EventDispatcher
2 parents 7f3f7c7 + 7ae57f4 commit 5484c7a

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

src/DefaultWorker.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,12 @@ public static function create(
9090
Closure $job,
9191
array $options = [],
9292
LoggerInterface $logger = new NullLogger(),
93+
EventDispatcherInterface|null $eventDispatcher = null,
9394
): self {
94-
$eventDispatcher = new EventDispatcher();
95+
if ($eventDispatcher === null) {
96+
$eventDispatcher = new EventDispatcher();
97+
}
98+
9599
$eventDispatcher->addSubscriber(new StopWorkerOnSigtermSignalListener($logger));
96100

97101
if (isset($options['runLimit'])) {

tests/Unit/DefaultWorkerTest.php

+40-10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Prophecy\Argument;
1313
use Prophecy\PhpUnit\ProphecyTrait;
1414
use Psr\Log\LoggerInterface;
15+
use Symfony\Component\EventDispatcher\EventDispatcher;
1516
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1617

1718
final class DefaultWorkerTest extends TestCase
@@ -20,17 +21,17 @@ final class DefaultWorkerTest extends TestCase
2021

2122
public function testRunWorker(): void
2223
{
23-
$evenDispatcher = $this->prophesize(EventDispatcherInterface::class);
24-
$evenDispatcher->dispatch(Argument::type(WorkerStartedEvent::class))->shouldBeCalledTimes(1);
25-
$evenDispatcher->dispatch(Argument::type(WorkerRunningEvent::class))->shouldBeCalledTimes(1)->will(
24+
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
25+
$eventDispatcher->dispatch(Argument::type(WorkerStartedEvent::class))->shouldBeCalledTimes(1);
26+
$eventDispatcher->dispatch(Argument::type(WorkerRunningEvent::class))->shouldBeCalledTimes(1)->will(
2627
/** @param array{WorkerRunningEvent} $args */
2728
static function (array $args) {
2829
$args[0]->worker->stop();
2930

3031
return $args[0];
3132
},
3233
);
33-
$evenDispatcher->dispatch(Argument::type(WorkerStoppedEvent::class))->shouldBeCalledTimes(1);
34+
$eventDispatcher->dispatch(Argument::type(WorkerStoppedEvent::class))->shouldBeCalledTimes(1);
3435

3536
$logger = $this->prophesize(LoggerInterface::class);
3637
$logger->debug('Worker starting')->shouldBeCalledTimes(1);
@@ -40,16 +41,16 @@ static function (array $args) {
4041
$logger->debug('Worker stopped')->shouldBeCalledTimes(1);
4142
$logger->debug('Worker terminated')->shouldBeCalledTimes(1);
4243

43-
$worker = new DefaultWorker(static fn () => null, $evenDispatcher->reveal(), $logger->reveal());
44+
$worker = new DefaultWorker(static fn () => null, $eventDispatcher->reveal(), $logger->reveal());
4445
$worker->run(200);
4546
}
4647

4748
public function testJobStopWorker(): void
4849
{
49-
$evenDispatcher = $this->prophesize(EventDispatcherInterface::class);
50-
$evenDispatcher->dispatch(Argument::type(WorkerStartedEvent::class))->shouldBeCalledTimes(1);
51-
$evenDispatcher->dispatch(Argument::type(WorkerRunningEvent::class))->shouldBeCalledTimes(1);
52-
$evenDispatcher->dispatch(Argument::type(WorkerStoppedEvent::class))->shouldBeCalledTimes(1);
50+
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
51+
$eventDispatcher->dispatch(Argument::type(WorkerStartedEvent::class))->shouldBeCalledTimes(1);
52+
$eventDispatcher->dispatch(Argument::type(WorkerRunningEvent::class))->shouldBeCalledTimes(1);
53+
$eventDispatcher->dispatch(Argument::type(WorkerStoppedEvent::class))->shouldBeCalledTimes(1);
5354

5455
$logger = $this->prophesize(LoggerInterface::class);
5556
$logger->debug('Worker starting')->shouldBeCalledTimes(1);
@@ -63,10 +64,39 @@ public function testJobStopWorker(): void
6364
static function ($stop): void {
6465
$stop();
6566
},
66-
$evenDispatcher->reveal(),
67+
$eventDispatcher->reveal(),
6768
$logger->reveal(),
6869
);
6970

7071
$worker->run(0);
7172
}
73+
74+
public function testCustomEventDispatcher(): void
75+
{
76+
$listener = new class {
77+
public int $called = 0;
78+
79+
public function __invoke(WorkerStartedEvent $event): void
80+
{
81+
$this->called++;
82+
}
83+
};
84+
85+
$eventDispatcher = new EventDispatcher();
86+
$eventDispatcher->addListener(WorkerStartedEvent::class, $listener);
87+
88+
$logger = $this->prophesize(LoggerInterface::class);
89+
$worker = DefaultWorker::create(
90+
static function ($stop): void {
91+
$stop();
92+
},
93+
[],
94+
$logger->reveal(),
95+
$eventDispatcher,
96+
);
97+
98+
$worker->run(0);
99+
100+
self::assertEquals(1, $listener->called);
101+
}
72102
}

0 commit comments

Comments
 (0)