-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathRedisContextTest.php
228 lines (176 loc) · 6.69 KB
/
RedisContextTest.php
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
namespace Enqueue\Redis\Tests;
use Enqueue\Null\NullQueue;
use Enqueue\Null\NullTopic;
use Enqueue\Redis\Redis;
use Enqueue\Redis\RedisConsumer;
use Enqueue\Redis\RedisContext;
use Enqueue\Redis\RedisDestination;
use Enqueue\Redis\RedisMessage;
use Enqueue\Redis\RedisProducer;
use Enqueue\Redis\RedisSubscriptionConsumer;
use Enqueue\Test\ClassExtensionTrait;
use Interop\Queue\Context;
use Interop\Queue\Exception\InvalidDestinationException;
use Interop\Queue\Exception\TemporaryQueueNotSupportedException;
class RedisContextTest extends \PHPUnit\Framework\TestCase
{
use ClassExtensionTrait;
public function testShouldImplementContextInterface()
{
$this->assertClassImplements(Context::class, RedisContext::class);
}
public function testThrowIfNeitherRedisNorFactoryGiven()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The $redis argument must be either Enqueue\Redis\Redis or callable that returns Enqueue\Redis\Redis once called.');
new RedisContext(new \stdClass(), 300);
}
public function testShouldAllowCreateEmptyMessage()
{
$context = new RedisContext($this->createRedisMock(), 300);
$message = $context->createMessage();
$this->assertInstanceOf(RedisMessage::class, $message);
$this->assertSame('', $message->getBody());
$this->assertSame([], $message->getProperties());
$this->assertSame([], $message->getHeaders());
}
public function testShouldAllowCreateCustomMessage()
{
$context = new RedisContext($this->createRedisMock(), 300);
$message = $context->createMessage('theBody', ['aProp' => 'aPropVal'], ['aHeader' => 'aHeaderVal']);
$this->assertInstanceOf(RedisMessage::class, $message);
$this->assertSame('theBody', $message->getBody());
$this->assertSame(['aProp' => 'aPropVal'], $message->getProperties());
$this->assertSame(['aHeader' => 'aHeaderVal'], $message->getHeaders());
}
public function testShouldCreateQueue()
{
$context = new RedisContext($this->createRedisMock(), 300);
$queue = $context->createQueue('aQueue');
$this->assertInstanceOf(RedisDestination::class, $queue);
$this->assertSame('aQueue', $queue->getQueueName());
}
public function testShouldAllowCreateTopic()
{
$context = new RedisContext($this->createRedisMock(), 300);
$topic = $context->createTopic('aTopic');
$this->assertInstanceOf(RedisDestination::class, $topic);
$this->assertSame('aTopic', $topic->getTopicName());
}
public function testThrowNotImplementedOnCreateTmpQueueCall()
{
$context = new RedisContext($this->createRedisMock(), 300);
$this->expectException(TemporaryQueueNotSupportedException::class);
$context->createTemporaryQueue();
}
public function testShouldCreateProducer()
{
$context = new RedisContext($this->createRedisMock(), 300);
$producer = $context->createProducer();
$this->assertInstanceOf(RedisProducer::class, $producer);
}
public function testShouldThrowIfNotRedisDestinationGivenOnCreateConsumer()
{
$context = new RedisContext($this->createRedisMock(), 300);
$this->expectException(InvalidDestinationException::class);
$this->expectExceptionMessage('The destination must be an instance of Enqueue\Redis\RedisDestination but got Enqueue\Null\NullQueue.');
$consumer = $context->createConsumer(new NullQueue('aQueue'));
$this->assertInstanceOf(RedisConsumer::class, $consumer);
}
public function testShouldCreateConsumer()
{
$context = new RedisContext($this->createRedisMock(), 300);
$queue = $context->createQueue('aQueue');
$consumer = $context->createConsumer($queue);
$this->assertInstanceOf(RedisConsumer::class, $consumer);
}
public function testShouldCallRedisDisconnectOnClose()
{
$redisMock = $this->createRedisMock();
$redisMock
->expects($this->once())
->method('disconnect')
;
$context = new RedisContext($redisMock, 300);
$context->close();
}
public function testThrowIfNotRedisDestinationGivenOnDeleteQueue()
{
$redisMock = $this->createRedisMock();
$redisMock
->expects($this->never())
->method('del')
;
$context = new RedisContext($redisMock, 300);
$this->expectException(InvalidDestinationException::class);
$context->deleteQueue(new NullQueue('aQueue'));
}
public function testShouldAllowDeleteQueue()
{
$redisMock = $this->createRedisMock();
$redisMock
->expects($this->at(0))
->method('del')
->with('aQueueName')
;
$redisMock
->expects($this->at(1))
->method('del')
->with('aQueueName:delayed')
;
$redisMock
->expects($this->at(2))
->method('del')
->with('aQueueName:reserved')
;
$context = new RedisContext($redisMock, 300);
$queue = $context->createQueue('aQueueName');
$context->deleteQueue($queue);
}
public function testThrowIfNotRedisDestinationGivenOnDeleteTopic()
{
$redisMock = $this->createRedisMock();
$redisMock
->expects($this->never())
->method('del')
;
$context = new RedisContext($redisMock, 300);
$this->expectException(InvalidDestinationException::class);
$context->deleteTopic(new NullTopic('aTopic'));
}
public function testShouldAllowDeleteTopic()
{
$redisMock = $this->createRedisMock();
$redisMock
->expects($this->at(0))
->method('del')
->with('aTopicName')
;
$redisMock
->expects($this->at(1))
->method('del')
->with('aTopicName:delayed')
;
$redisMock
->expects($this->at(2))
->method('del')
->with('aTopicName:reserved')
;
$context = new RedisContext($redisMock, 300);
$topic = $context->createTopic('aTopicName');
$context->deleteQueue($topic);
}
public function testShouldReturnExpectedSubscriptionConsumerInstance()
{
$context = new RedisContext($this->createRedisMock(), 300);
$this->assertInstanceOf(RedisSubscriptionConsumer::class, $context->createSubscriptionConsumer());
}
/**
* @return \PHPUnit\Framework\MockObject\MockObject|Redis
*/
private function createRedisMock()
{
return $this->createMock(Redis::class);
}
}