-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMessageHandlerTest.php
More file actions
153 lines (135 loc) · 4.69 KB
/
MessageHandlerTest.php
File metadata and controls
153 lines (135 loc) · 4.69 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
declare(strict_types=1);
namespace QueueTest\App\Message;
use Dot\Log\Logger;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerExceptionInterface;
use Queue\App\Message\Message;
use Queue\App\Message\MessageHandler;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\DelayStamp;
class MessageHandlerTest extends TestCase
{
private MessageBusInterface|MockObject $bus;
private Logger $logger;
private array $config;
private MessageHandler $handler;
/**
* @throws Exception
* @throws ContainerExceptionInterface
*/
protected function setUp(): void
{
$this->bus = $this->createMock(MessageBusInterface::class);
$this->logger = new Logger([
'writers' => [
'FileWriter' => [
'name' => 'null',
'level' => Logger::ALERT,
],
],
]);
$this->config = [
'fail-safe' => [
'first_retry' => 1000,
'second_retry' => 2000,
'third_retry' => 3000,
],
'notification' => [
'server' => [
'protocol' => 'tcp',
'host' => 'localhost',
'port' => '8556',
'eof' => "\n",
],
],
'application' => [
'name' => 'dotkernel',
],
];
$this->handler = new MessageHandler($this->bus, $this->logger, $this->config);
}
/**
* @throws Exception
*/
public function testInvokeSuccessfulProcessing(): void
{
$payload = ['foo' => 'control'];
$message = $this->createMock(Message::class);
$message->method('getPayload')->willReturn($payload);
$this->handler->__invoke($message);
$this->expectNotToPerformAssertions();
}
/**
* @throws Exception
*/
public function testInvokeFailureTriggersFirstRetry(): void
{
$payload = ['foo' => 'fail'];
$message = $this->createMock(Message::class);
$message->method('getPayload')->willReturn($payload);
$this->bus->expects($this->once())
->method('dispatch')
->with(
$this->callback(function ($msg) {
return $msg instanceof Message
&& $msg->getPayload()['foo'] === 'fail'
&& $msg->getPayload()['retry'] === 1;
}),
$this->callback(function ($stamps) {
return isset($stamps[0]) && $stamps[0] instanceof DelayStamp
&& $stamps[0]->getDelay() === 1000;
})
)
->willReturn(new Envelope($message));
$this->handler->__invoke($message);
}
/**
* @throws ExceptionInterface
*/
public function testRetrySecondTime(): void
{
$payload = ['foo' => 'retry_test', 'retry' => 1];
$this->bus->expects($this->once())
->method('dispatch')
->with(
$this->callback(function ($msg) {
return $msg instanceof Message
&& $msg->getPayload()['retry'] === 2
&& $msg->getPayload()['foo'] === 'retry_test';
}),
$this->callback(function ($stamps) {
return isset($stamps[0]) && $stamps[0] instanceof DelayStamp
&& $stamps[0]->getDelay() === 2000;
})
)
->willReturn(new Envelope(new Message($payload)));
$this->handler->retry($payload);
}
/**
* @throws ExceptionInterface
*/
public function testRetryThirdTime(): void
{
$payload = ['foo' => 'retry_test', 'retry' => 2];
$this->bus->expects($this->once())
->method('dispatch')
->with(
$this->callback(function ($msg) {
return $msg instanceof Message
&& $msg->getPayload()['retry'] === 3
&& $msg->getPayload()['foo'] === 'retry_test';
}),
$this->callback(function ($stamps) {
return isset($stamps[0]) && $stamps[0] instanceof DelayStamp
&& $stamps[0]->getDelay() === 3000;
})
)
->willReturn(new Envelope(new Message($payload)));
$this->handler->retry($payload);
}
}