-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMessageHandlerTest.php
More file actions
108 lines (91 loc) · 3.01 KB
/
MessageHandlerTest.php
File metadata and controls
108 lines (91 loc) · 3.01 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
<?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 RuntimeException;
use Symfony\Component\Messenger\MessageBusInterface;
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);
}
public function testControlMessageDoesNotThrowAndDoesNotSetRetryCount(): void
{
$handler = $this->handler;
$message = new Message(['foo' => 'control']);
$handler($message);
$payload = $message->getPayload();
$this->assertArrayNotHasKey('retry_count', $payload);
}
public function testRetryMessageThrowsExceptionAndSetsRetryCount(): void
{
$handler = $this->handler;
$message = new Message(['foo' => 'retry']);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage("Intentional failure for testing retries");
try {
$handler($message);
} finally {
$payload = $message->getPayload();
$this->assertArrayHasKey('retry_count', $payload);
$this->assertEquals(1, $payload['retry_count']); // first retry
}
}
public function testRetryMessageWithExistingRetryCountIncrementsIt(): void
{
$handler = $this->handler;
$message = new Message([
'foo' => 'retry',
'retry_count' => 2,
]);
$this->expectException(RuntimeException::class);
try {
$handler($message);
} finally {
$payload = $message->getPayload();
$this->assertEquals(3, $payload['retry_count']); // incremented from 2 → 3
}
}
}