-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGetQueuedMessagesCommandTest.php
More file actions
161 lines (131 loc) · 5.17 KB
/
GetQueuedMessagesCommandTest.php
File metadata and controls
161 lines (131 loc) · 5.17 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
154
155
156
157
158
159
160
161
<?php
declare(strict_types=1);
namespace QueueTest\Swoole\Command;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Queue\App\Message\Message;
use Queue\Swoole\Command\GetQueuedMessagesCommand;
use Redis;
use RedisException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\ExceptionInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Stamp\RedeliveryStamp;
use function addslashes;
use function array_keys;
use function count;
use function json_encode;
use function serialize;
class GetQueuedMessagesCommandTest extends TestCase
{
private Redis|MockObject $redisMock;
/**
* @throws Exception
*/
protected function setUp(): void
{
$this->redisMock = $this->createMock(Redis::class);
}
/**
* @throws ExceptionInterface
*/
public function testExecuteWithNoMessages(): void
{
$this->redisMock
->expects($this->once())
->method('xRange')
->with('messages', '-', '+')
->willReturn([]);
$command = new GetQueuedMessagesCommand($this->redisMock);
$input = new ArrayInput(['--stream' => 'messages']);
$output = new BufferedOutput();
$exitCode = $command->run($input, $output);
$outputText = $output->fetch();
$this->assertEquals(Command::SUCCESS, $exitCode);
$this->assertStringContainsString('No messages queued found', $outputText);
}
/**
* @throws ExceptionInterface
*/
public function testExecuteWithSimpleMessages(): void
{
$fakeMessages = [
'1691000000000-0' => ['type' => 'testEmail', 'payload' => '{"to":"test@example.com"}'],
'1691000000001-0' => ['type' => 'testEmail2', 'payload' => '{"to":"test@example2.com"}'],
];
$this->redisMock
->expects($this->once())
->method('xRange')
->with('messages', '-', '+')
->willReturn($fakeMessages);
$command = new GetQueuedMessagesCommand($this->redisMock);
$input = new ArrayInput(['--stream' => 'messages']);
$output = new BufferedOutput();
$exitCode = $command->run($input, $output);
$outputText = $output->fetch();
$this->assertEquals(Command::SUCCESS, $exitCode);
foreach (array_keys($fakeMessages) as $id) {
$this->assertStringContainsString('Message ID:', $outputText);
$this->assertStringContainsString($id, $outputText);
}
$this->assertStringContainsString('Total queued messages in stream', $outputText);
$this->assertStringContainsString((string) count($fakeMessages), $outputText);
}
/**
* @throws ExceptionInterface
*/
public function testRedisThrowsException(): void
{
$this->redisMock
->expects($this->once())
->method('xRange')
->willThrowException(new RedisException('Redis unavailable'));
$command = new GetQueuedMessagesCommand($this->redisMock);
$input = new ArrayInput(['--stream' => 'messages']);
$output = new BufferedOutput();
$this->expectException(RedisException::class);
$command->run($input, $output);
}
public function testExecuteWithInvalidBody(): void
{
$invalidBodyJson = json_encode(['body' => 'not-a-serialized-envelope']);
$this->redisMock
->expects($this->once())
->method('xRange')
->willReturn([
'2-0' => ['body' => $invalidBodyJson],
]);
$command = new GetQueuedMessagesCommand($this->redisMock);
$input = new ArrayInput(['--stream' => 'messages']);
$output = new BufferedOutput();
$exitCode = $command->run($input, $output);
$outputText = $output->fetch();
$this->assertEquals(Command::SUCCESS, $exitCode);
$this->assertStringContainsString('failed to unserialize envelope', $outputText);
}
public function testExecuteWithValidEnvelope(): void
{
$message = new Message(['foo' => 'bar']);
$envelope = new Envelope($message, [new RedeliveryStamp(1)]);
$serializedEnvelope = serialize($envelope);
$jsonBody = json_encode(['body' => addslashes($serializedEnvelope)]);
$this->redisMock
->expects($this->once())
->method('xRange')
->willReturn([
'100-0' => ['body' => $jsonBody],
]);
$command = new GetQueuedMessagesCommand($this->redisMock);
$input = new ArrayInput(['--stream' => 'messages']);
$output = new BufferedOutput();
$exitCode = $command->run($input, $output);
$outputText = $output->fetch();
$this->assertEquals(Command::SUCCESS, $exitCode);
$this->assertStringContainsString('Message Class', $outputText);
$this->assertStringContainsString('Payload', $outputText);
$this->assertStringContainsString('Timestamps', $outputText);
}
}