-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathInMemoryAdapterTest.php
More file actions
180 lines (142 loc) · 5.66 KB
/
Copy pathInMemoryAdapterTest.php
File metadata and controls
180 lines (142 loc) · 5.66 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
declare(strict_types=1);
namespace Yiisoft\Queue\Tests\Unit\Stubs;
use PHPUnit\Framework\TestCase;
use Yiisoft\Queue\Message\IdEnvelope;
use Yiisoft\Queue\Message\GenericMessage;
use Yiisoft\Queue\Message\MessageInterface;
use Yiisoft\Queue\MessageStatus;
use Yiisoft\Queue\Stubs\InMemoryAdapter;
final class InMemoryAdapterTest extends TestCase
{
public function testPush(): void
{
$adapter = new InMemoryAdapter();
$envelope1 = $adapter->push(new GenericMessage('test', 'a'));
$envelope2 = $adapter->push(new GenericMessage('test', 'b'));
$envelope3 = $adapter->push(new GenericMessage('test', 'c'));
$this->assertInstanceOf(IdEnvelope::class, $envelope1);
$this->assertInstanceOf(IdEnvelope::class, $envelope2);
$this->assertInstanceOf(IdEnvelope::class, $envelope3);
$this->assertSame(0, $envelope1->getId());
$this->assertSame('a', $envelope1->getMessage()->getData());
$this->assertSame(1, $envelope2->getId());
$this->assertSame('b', $envelope2->getMessage()->getData());
$this->assertSame(2, $envelope3->getId());
$this->assertSame('c', $envelope3->getMessage()->getData());
}
public function testHasStatusSupport(): void
{
$adapter = new InMemoryAdapter();
$this->assertTrue($adapter->hasStatusSupport());
}
public function testStatusWaitingForPushedMessage(): void
{
$adapter = new InMemoryAdapter();
$envelope = $adapter->push(new GenericMessage('test', null));
$this->assertInstanceOf(IdEnvelope::class, $envelope);
$this->assertSame(MessageStatus::WAITING, $adapter->status($envelope->getId()));
}
public function testStatusDoneAfterProcessing(): void
{
$adapter = new InMemoryAdapter();
$envelope = $adapter->push(new GenericMessage('test', null));
$adapter->runExisting(static fn() => true);
$this->assertInstanceOf(IdEnvelope::class, $envelope);
$this->assertSame(MessageStatus::DONE, $adapter->status($envelope->getId()));
}
public function testStatusNotFoundForNonExistentId(): void
{
$adapter = new InMemoryAdapter();
$this->assertSame(MessageStatus::NOT_FOUND, $adapter->status(99));
}
public function testStatusNotFoundForNegativeId(): void
{
$adapter = new InMemoryAdapter();
$this->assertSame(MessageStatus::NOT_FOUND, $adapter->status(-1));
}
public function testStatusAcceptsStringId(): void
{
$adapter = new InMemoryAdapter();
$envelope = $adapter->push(new GenericMessage('test', null));
$this->assertInstanceOf(IdEnvelope::class, $envelope);
$this->assertSame(MessageStatus::WAITING, $adapter->status((string) $envelope->getId()));
}
public function testRunExistingProcessesAllMessages(): void
{
$adapter = new InMemoryAdapter();
$adapter->push(new GenericMessage('test', 'a'));
$adapter->push(new GenericMessage('test', 'b'));
$adapter->push(new GenericMessage('test', 'c'));
$processed = [];
$adapter->runExisting(
static function (MessageInterface $message) use (&$processed): bool {
$processed[] = $message->getData();
return true;
},
);
$this->assertSame(['a', 'b', 'c'], $processed);
}
public function testRunExistingStopsWhenHandlerReturnsFalse(): void
{
$adapter = new InMemoryAdapter();
$adapter->push(new GenericMessage('test', 'a'));
$adapter->push(new GenericMessage('test', 'b'));
$adapter->push(new GenericMessage('test', 'c'));
$processed = [];
$adapter->runExisting(
static function (MessageInterface $message) use (&$processed): bool {
$processed[] = $message->getData();
return false;
},
);
$this->assertSame(['a'], $processed);
}
public function testRunExistingOnEmptyQueue(): void
{
$adapter = new InMemoryAdapter();
$called = false;
$adapter->runExisting(static function () use (&$called): bool {
$called = true;
return true;
});
$this->assertFalse($called);
}
public function testRunExistingDoesNotReprocessMessages(): void
{
$adapter = new InMemoryAdapter();
$adapter->push(new GenericMessage('test', 'x'));
$count = 0;
$handler = static function () use (&$count): bool {
$count++;
return true;
};
$adapter->runExisting($handler);
$adapter->runExisting($handler);
$this->assertSame(1, $count);
}
public function testIdContinuesAfterProcessing(): void
{
$adapter = new InMemoryAdapter();
$adapter->push(new GenericMessage('test', null));
$adapter->runExisting(static fn() => true);
$envelope = $adapter->push(new GenericMessage('test', null));
$this->assertInstanceOf(IdEnvelope::class, $envelope);
$this->assertSame(1, $envelope->getId());
$this->assertSame(MessageStatus::WAITING, $adapter->status($envelope->getId()));
}
public function testSubscribeProcessesExistingMessages(): void
{
$adapter = new InMemoryAdapter();
$adapter->push(new GenericMessage('test', 'a'));
$adapter->push(new GenericMessage('test', 'b'));
$processed = [];
$adapter->subscribe(
static function (MessageInterface $message) use (&$processed): bool {
$processed[] = $message->getData();
return true;
},
);
$this->assertSame(['a', 'b'], $processed);
}
}