-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultWorkerTest.php
More file actions
102 lines (84 loc) · 3.75 KB
/
DefaultWorkerTest.php
File metadata and controls
102 lines (84 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
declare(strict_types=1);
namespace Patchlevel\Worker\Tests\Unit;
use Patchlevel\Worker\DefaultWorker;
use Patchlevel\Worker\Event\WorkerRunningEvent;
use Patchlevel\Worker\Event\WorkerStartedEvent;
use Patchlevel\Worker\Event\WorkerStoppedEvent;
use PHPUnit\Framework\TestCase;
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
{
use ProphecyTrait;
public function testRunWorker(): void
{
$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];
},
);
$eventDispatcher->dispatch(Argument::type(WorkerStoppedEvent::class))->shouldBeCalledTimes(1);
$logger = $this->prophesize(LoggerInterface::class);
$logger->debug('Worker starting')->shouldBeCalledTimes(1);
$logger->debug('Worker starting job run')->shouldBeCalledTimes(1);
$logger->debug('Worker finished job run ({ranTime}ms)', Argument::any())->shouldBeCalledTimes(1);
$logger->debug('Worker received stop signal')->shouldBeCalledTimes(1);
$logger->debug('Worker stopped')->shouldBeCalledTimes(1);
$logger->debug('Worker terminated')->shouldBeCalledTimes(1);
$worker = new DefaultWorker(static fn () => null, $eventDispatcher->reveal(), $logger->reveal());
$worker->run(200);
}
public function testJobStopWorker(): void
{
$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);
$logger->debug('Worker starting job run')->shouldBeCalledTimes(1);
$logger->debug('Worker finished job run ({ranTime}ms)', Argument::any())->shouldBeCalledTimes(1);
$logger->debug('Worker received stop signal')->shouldBeCalledTimes(1);
$logger->debug('Worker stopped')->shouldBeCalledTimes(1);
$logger->debug('Worker terminated')->shouldBeCalledTimes(1);
$worker = new DefaultWorker(
static function ($stop): void {
$stop();
},
$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);
}
}