1 | <?php |
||||||
2 | |||||||
3 | /** |
||||||
4 | * This file is part of graze/queue. |
||||||
5 | * |
||||||
6 | * Copyright (c) 2015 Nature Delivered Ltd. <https://www.graze.com> |
||||||
7 | * |
||||||
8 | * For the full copyright and license information, please view the LICENSE |
||||||
9 | * file that was distributed with this source code. |
||||||
10 | * |
||||||
11 | * @license https://github.com/graze/queue/blob/master/LICENSE MIT |
||||||
12 | * |
||||||
13 | * @link https://github.com/graze/queue |
||||||
14 | */ |
||||||
15 | |||||||
16 | namespace Graze\Queue\Adapter; |
||||||
17 | |||||||
18 | use Aws\ResultInterface; |
||||||
19 | use Aws\Sqs\SqsClient; |
||||||
20 | use Graze\DataStructure\Container\ContainerInterface; |
||||||
21 | use Graze\Queue\Adapter\Exception\FailedAcknowledgementException; |
||||||
22 | use Graze\Queue\Adapter\Exception\FailedExtensionException; |
||||||
23 | use Graze\Queue\Adapter\Exception\FailedRejectionException; |
||||||
24 | use Graze\Queue\Message\MessageFactoryInterface; |
||||||
25 | use Graze\Queue\Message\MessageInterface; |
||||||
26 | use Mockery as m; |
||||||
27 | use Mockery\MockInterface; |
||||||
28 | use Graze\Queue\Test\TestCase; |
||||||
29 | |||||||
30 | class SqsAdapterTest extends TestCase |
||||||
31 | { |
||||||
32 | /** @var MessageInterface|MockInterface */ |
||||||
33 | private $messageA; |
||||||
34 | /** @var MessageInterface|MockInterface */ |
||||||
35 | private $messageB; |
||||||
36 | /** @var MessageInterface|MockInterface */ |
||||||
37 | private $messageC; |
||||||
38 | /** @var MessageInterface[]|MockInterface[] */ |
||||||
39 | private $messages; |
||||||
40 | /** @var ResultInterface|MockInterface */ |
||||||
41 | private $model; |
||||||
42 | /** @var MessageFactoryInterface|MockInterface */ |
||||||
43 | private $factory; |
||||||
44 | /** @var SqsClient */ |
||||||
45 | private $client; |
||||||
46 | |||||||
47 | public function setUp() |
||||||
48 | { |
||||||
49 | $this->client = m::mock(SqsClient::class); |
||||||
0 ignored issues
–
show
|
|||||||
50 | $this->model = m::mock(ResultInterface::class); |
||||||
51 | $this->factory = m::mock(MessageFactoryInterface::class); |
||||||
52 | |||||||
53 | $this->messageA = $a = m::mock(MessageInterface::class); |
||||||
54 | $this->messageB = $b = m::mock(MessageInterface::class); |
||||||
55 | $this->messageC = $c = m::mock(MessageInterface::class); |
||||||
56 | $this->messages = [$a, $b, $c]; |
||||||
57 | } |
||||||
58 | |||||||
59 | /** |
||||||
60 | * @param string $body |
||||||
61 | * @param int $id |
||||||
62 | * @param string $handle |
||||||
63 | */ |
||||||
64 | protected function stubCreateDequeueMessage($body, $id, $handle) |
||||||
65 | { |
||||||
66 | $this->factory->shouldReceive('createMessage')->once()->with( |
||||||
0 ignored issues
–
show
The method
shouldReceive() does not exist on Graze\Queue\Message\MessageFactoryInterface .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
67 | $body, |
||||||
68 | m::on(function ($opts) use ($id, $handle) { |
||||||
69 | $meta = ['Attributes' => [], 'MessageAttributes' => [], 'MessageId' => $id, 'ReceiptHandle' => $handle]; |
||||||
70 | $validator = isset($opts['validator']) && is_callable($opts['validator']); |
||||||
71 | |||||||
72 | return isset($opts['metadata']) && $opts['metadata'] === $meta && $validator; |
||||||
73 | }) |
||||||
74 | )->andReturn($this->messageA); |
||||||
75 | } |
||||||
76 | |||||||
77 | /** |
||||||
78 | * @param string $name |
||||||
79 | * @param array $options |
||||||
80 | * |
||||||
81 | * @return string |
||||||
82 | */ |
||||||
83 | protected function stubCreateQueue($name, array $options = []) |
||||||
84 | { |
||||||
85 | $url = 'foo://bar'; |
||||||
86 | $model = m::mock(ResultInterface::class); |
||||||
87 | $model->shouldReceive('get')->once()->with('QueueUrl')->andReturn($url); |
||||||
88 | |||||||
89 | $this->client->shouldReceive('createQueue')->once()->with([ |
||||||
90 | 'QueueName' => $name, |
||||||
91 | 'Attributes' => $options, |
||||||
92 | ])->andReturn($model); |
||||||
93 | |||||||
94 | return $url; |
||||||
95 | } |
||||||
96 | |||||||
97 | /** |
||||||
98 | * @param string $url |
||||||
99 | * |
||||||
100 | * @return int |
||||||
101 | */ |
||||||
102 | protected function stubQueueVisibilityTimeout($url) |
||||||
103 | { |
||||||
104 | $timeout = 120; |
||||||
105 | $model = m::mock(ResultInterface::class); |
||||||
106 | $model->shouldReceive('get')->once()->with('Attributes')->andReturn(['VisibilityTimeout' => $timeout]); |
||||||
107 | |||||||
108 | $this->client->shouldReceive('getQueueAttributes')->once()->with([ |
||||||
109 | 'QueueUrl' => $url, |
||||||
110 | 'AttributeNames' => ['VisibilityTimeout'], |
||||||
111 | ])->andReturn($model); |
||||||
112 | |||||||
113 | return $timeout; |
||||||
114 | } |
||||||
115 | |||||||
116 | public function testInterface() |
||||||
117 | { |
||||||
118 | assertThat(new SqsAdapter($this->client, 'foo'), is(anInstanceOf('Graze\Queue\Adapter\AdapterInterface'))); |
||||||
119 | } |
||||||
120 | |||||||
121 | public function testAcknowledge() |
||||||
122 | { |
||||||
123 | $adapter = new SqsAdapter($this->client, 'foo'); |
||||||
124 | $url = $this->stubCreateQueue('foo'); |
||||||
125 | |||||||
126 | $this->messageA->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('foo'); |
||||||
0 ignored issues
–
show
The method
shouldReceive() does not exist on Graze\Queue\Message\MessageInterface .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
127 | $this->messageB->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('bar'); |
||||||
128 | $this->messageC->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('baz'); |
||||||
129 | |||||||
130 | $this->model->shouldReceive('get')->once()->with('Failed')->andReturn([]); |
||||||
0 ignored issues
–
show
The method
shouldReceive() does not exist on Aws\ResultInterface .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
131 | |||||||
132 | $this->client->shouldReceive('deleteMessageBatch')->once()->with([ |
||||||
133 | 'QueueUrl' => $url, |
||||||
134 | 'Entries' => [ |
||||||
135 | ['Id' => 0, 'ReceiptHandle' => 'foo'], |
||||||
136 | ['Id' => 1, 'ReceiptHandle' => 'bar'], |
||||||
137 | ['Id' => 2, 'ReceiptHandle' => 'baz'], |
||||||
138 | ], |
||||||
139 | ])->andReturn($this->model); |
||||||
140 | |||||||
141 | $adapter->acknowledge($this->messages); |
||||||
142 | } |
||||||
143 | |||||||
144 | public function testFailureToAcknowledgeForSomeMessages() |
||||||
145 | { |
||||||
146 | $adapter = new SqsAdapter($this->client, 'foo'); |
||||||
147 | $url = $this->stubCreateQueue('foo'); |
||||||
148 | |||||||
149 | $this->messageA->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('foo'); |
||||||
150 | $this->messageB->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('bar'); |
||||||
151 | $this->messageC->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('baz'); |
||||||
152 | |||||||
153 | $this->model->shouldReceive('get')->once()->with('Failed')->andReturn([ |
||||||
154 | ['Id' => 2, 'Code' => 123, 'SenderFault' => true, 'Message' => 'baz is gone'], |
||||||
155 | ]); |
||||||
156 | |||||||
157 | $this->client->shouldReceive('deleteMessageBatch')->once()->with([ |
||||||
158 | 'QueueUrl' => $url, |
||||||
159 | 'Entries' => [ |
||||||
160 | ['Id' => 0, 'ReceiptHandle' => 'foo'], |
||||||
161 | ['Id' => 1, 'ReceiptHandle' => 'bar'], |
||||||
162 | ['Id' => 2, 'ReceiptHandle' => 'baz'], |
||||||
163 | ], |
||||||
164 | ])->andReturn($this->model); |
||||||
165 | |||||||
166 | $errorThrown = false; |
||||||
167 | try { |
||||||
168 | $adapter->acknowledge($this->messages); |
||||||
169 | } catch (FailedAcknowledgementException $e) { |
||||||
170 | assertThat($e->getMessages(), is(anArray([$this->messageC]))); |
||||||
171 | assertThat( |
||||||
172 | $e->getDebug(), |
||||||
173 | is(anArray([['Id' => 2, 'Code' => 123, 'SenderFault' => true, 'Message' => 'baz is gone']])) |
||||||
174 | ); |
||||||
175 | $errorThrown = true; |
||||||
176 | } |
||||||
177 | |||||||
178 | assertThat('an exception is thrown', $errorThrown); |
||||||
179 | } |
||||||
180 | |||||||
181 | public function testReject() |
||||||
182 | { |
||||||
183 | $adapter = new SqsAdapter($this->client, 'foo'); |
||||||
184 | $url = $this->stubCreateQueue('foo'); |
||||||
185 | |||||||
186 | $this->messageA->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('foo'); |
||||||
187 | $this->messageB->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('bar'); |
||||||
188 | $this->messageC->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('baz'); |
||||||
189 | |||||||
190 | $this->model->shouldReceive('get')->once()->with('Failed')->andReturn([]); |
||||||
191 | |||||||
192 | $this->client->shouldReceive('changeMessageVisibilityBatch')->once()->with([ |
||||||
193 | 'QueueUrl' => $url, |
||||||
194 | 'Entries' => [ |
||||||
195 | ['Id' => 0, 'ReceiptHandle' => 'foo', 'VisibilityTimeout' => 0], |
||||||
196 | ['Id' => 1, 'ReceiptHandle' => 'bar', 'VisibilityTimeout' => 0], |
||||||
197 | ['Id' => 2, 'ReceiptHandle' => 'baz', 'VisibilityTimeout' => 0], |
||||||
198 | ], |
||||||
199 | ])->andReturn($this->model); |
||||||
200 | |||||||
201 | $adapter->reject($this->messages); |
||||||
202 | } |
||||||
203 | |||||||
204 | public function testFailureToRejectForSomeMessages() |
||||||
205 | { |
||||||
206 | $adapter = new SqsAdapter($this->client, 'foo'); |
||||||
207 | $url = $this->stubCreateQueue('foo'); |
||||||
208 | |||||||
209 | $this->messageA->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('foo'); |
||||||
210 | $this->messageB->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('bar'); |
||||||
211 | $this->messageC->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('baz'); |
||||||
212 | |||||||
213 | $this->model->shouldReceive('get')->once()->with('Failed')->andReturn([ |
||||||
214 | ['Id' => 2, 'Code' => 123, 'SenderFault' => true, 'Message' => 'baz is gone'], |
||||||
215 | ]); |
||||||
216 | |||||||
217 | $this->client->shouldReceive('changeMessageVisibilityBatch')->once()->with([ |
||||||
218 | 'QueueUrl' => $url, |
||||||
219 | 'Entries' => [ |
||||||
220 | ['Id' => 0, 'ReceiptHandle' => 'foo', 'VisibilityTimeout' => 0], |
||||||
221 | ['Id' => 1, 'ReceiptHandle' => 'bar', 'VisibilityTimeout' => 0], |
||||||
222 | ['Id' => 2, 'ReceiptHandle' => 'baz', 'VisibilityTimeout' => 0], |
||||||
223 | ], |
||||||
224 | ])->andReturn($this->model); |
||||||
225 | |||||||
226 | $errorThrown = false; |
||||||
227 | try { |
||||||
228 | $adapter->reject($this->messages); |
||||||
229 | } catch (FailedRejectionException $e) { |
||||||
230 | assertThat($e->getMessages(), is(anArray([$this->messageC]))); |
||||||
231 | assertThat( |
||||||
232 | $e->getDebug(), |
||||||
233 | is(anArray([['Id' => 2, 'Code' => 123, 'SenderFault' => true, 'Message' => 'baz is gone']])) |
||||||
234 | ); |
||||||
235 | $errorThrown = true; |
||||||
236 | } |
||||||
237 | |||||||
238 | assertThat('an exception is thrown', $errorThrown); |
||||||
239 | } |
||||||
240 | |||||||
241 | public function testExtension() |
||||||
242 | { |
||||||
243 | $adapter = new SqsAdapter($this->client, 'foo'); |
||||||
244 | $url = $this->stubCreateQueue('foo'); |
||||||
245 | |||||||
246 | $this->messageA->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('foo'); |
||||||
247 | $this->messageB->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('bar'); |
||||||
248 | $this->messageC->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('baz'); |
||||||
249 | |||||||
250 | $this->model->shouldReceive('get')->once()->with('Failed')->andReturn([]); |
||||||
251 | |||||||
252 | $this->client->shouldReceive('changeMessageVisibilityBatch')->once()->with([ |
||||||
253 | 'QueueUrl' => $url, |
||||||
254 | 'Entries' => [ |
||||||
255 | ['Id' => 0, 'ReceiptHandle' => 'foo', 'VisibilityTimeout' => 1200], |
||||||
256 | ['Id' => 1, 'ReceiptHandle' => 'bar', 'VisibilityTimeout' => 1200], |
||||||
257 | ['Id' => 2, 'ReceiptHandle' => 'baz', 'VisibilityTimeout' => 1200], |
||||||
258 | ], |
||||||
259 | ])->andReturn($this->model); |
||||||
260 | |||||||
261 | $adapter->extend($this->messages, 1200); |
||||||
262 | } |
||||||
263 | |||||||
264 | public function testFailureToExtendForSomeMessages() |
||||||
265 | { |
||||||
266 | $adapter = new SqsAdapter($this->client, 'foo'); |
||||||
267 | $url = $this->stubCreateQueue('foo'); |
||||||
268 | |||||||
269 | $this->messageA->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('foo'); |
||||||
270 | $this->messageB->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('bar'); |
||||||
271 | $this->messageC->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('baz'); |
||||||
272 | |||||||
273 | $this->model->shouldReceive('get')->once()->with('Failed')->andReturn([ |
||||||
274 | ['Id' => 2, 'Code' => 123, 'SenderFault' => true, 'Message' => 'baz is gone'], |
||||||
275 | ]); |
||||||
276 | |||||||
277 | $this->client->shouldReceive('changeMessageVisibilityBatch')->once()->with([ |
||||||
278 | 'QueueUrl' => $url, |
||||||
279 | 'Entries' => [ |
||||||
280 | ['Id' => 0, 'ReceiptHandle' => 'foo', 'VisibilityTimeout' => 1200], |
||||||
281 | ['Id' => 1, 'ReceiptHandle' => 'bar', 'VisibilityTimeout' => 1200], |
||||||
282 | ['Id' => 2, 'ReceiptHandle' => 'baz', 'VisibilityTimeout' => 1200], |
||||||
283 | ], |
||||||
284 | ])->andReturn($this->model); |
||||||
285 | |||||||
286 | $errorThrown = false; |
||||||
287 | try { |
||||||
288 | $adapter->extend($this->messages, 1200); |
||||||
289 | } catch (FailedExtensionException $e) { |
||||||
290 | assertThat($e->getMessages(), is(anArray([$this->messageC]))); |
||||||
291 | assertThat( |
||||||
292 | $e->getDebug(), |
||||||
293 | is(anArray([['Id' => 2, 'Code' => 123, 'SenderFault' => true, 'Message' => 'baz is gone']])) |
||||||
294 | ); |
||||||
295 | $errorThrown = true; |
||||||
296 | } |
||||||
297 | |||||||
298 | assertThat('an exception is thrown', $errorThrown); |
||||||
299 | } |
||||||
300 | |||||||
301 | public function testDequeue() |
||||||
302 | { |
||||||
303 | $adapter = new SqsAdapter($this->client, 'foo'); |
||||||
304 | $url = $this->stubCreateQueue('foo'); |
||||||
305 | $timeout = $this->stubQueueVisibilityTimeout($url); |
||||||
306 | |||||||
307 | $this->stubCreateDequeueMessage('foo', 0, 'a'); |
||||||
308 | $this->stubCreateDequeueMessage('bar', 1, 'b'); |
||||||
309 | $this->stubCreateDequeueMessage('baz', 2, 'c'); |
||||||
310 | |||||||
311 | $this->model->shouldReceive('get')->once()->with('Messages')->andReturn([ |
||||||
312 | ['Body' => 'foo', 'Attributes' => [], 'MessageAttributes' => [], 'MessageId' => 0, 'ReceiptHandle' => 'a'], |
||||||
313 | ['Body' => 'bar', 'Attributes' => [], 'MessageAttributes' => [], 'MessageId' => 1, 'ReceiptHandle' => 'b'], |
||||||
314 | ['Body' => 'baz', 'Attributes' => [], 'MessageAttributes' => [], 'MessageId' => 2, 'ReceiptHandle' => 'c'], |
||||||
315 | ]); |
||||||
316 | |||||||
317 | $this->client->shouldReceive('receiveMessage')->once()->with([ |
||||||
318 | 'QueueUrl' => $url, |
||||||
319 | 'AttributeNames' => ['All'], |
||||||
320 | 'MaxNumberOfMessages' => 3, |
||||||
321 | 'VisibilityTimeout' => $timeout, |
||||||
322 | ])->andReturn($this->model); |
||||||
323 | |||||||
324 | $iterator = $adapter->dequeue($this->factory, 3); |
||||||
0 ignored issues
–
show
It seems like
$this->factory can also be of type Mockery\MockInterface ; however, parameter $factory of Graze\Queue\Adapter\SqsAdapter::dequeue() does only seem to accept Graze\Queue\Message\MessageFactoryInterface , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
325 | |||||||
326 | assertThat($iterator, is(anInstanceOf('Generator'))); |
||||||
327 | assertThat(iterator_to_array($iterator), is(equalTo($this->messages))); |
||||||
328 | } |
||||||
329 | |||||||
330 | public function testDequeueInBatches() |
||||||
331 | { |
||||||
332 | $adapter = new SqsAdapter($this->client, 'foo'); |
||||||
333 | $url = $this->stubCreateQueue('foo'); |
||||||
334 | $timeout = $this->stubQueueVisibilityTimeout($url); |
||||||
335 | |||||||
336 | $limit = SqsAdapter::BATCHSIZE_RECEIVE; |
||||||
337 | |||||||
338 | $return = []; |
||||||
339 | $messages = []; |
||||||
340 | |||||||
341 | for ($i = 0; $i < $limit; $i++) { |
||||||
342 | $this->stubCreateDequeueMessage('tmp' . $i, $i, 'h' . $i); |
||||||
343 | $return[] = [ |
||||||
344 | 'Body' => 'tmp' . $i, |
||||||
345 | 'Attributes' => [], |
||||||
346 | 'MessageAttributes' => [], |
||||||
347 | 'MessageId' => $i, |
||||||
348 | 'ReceiptHandle' => 'h' . $i, |
||||||
349 | ]; |
||||||
350 | $messages[] = $this->messageA; |
||||||
351 | } |
||||||
352 | |||||||
353 | $this->model->shouldReceive('get')->once()->with('Messages')->andReturn($return); |
||||||
354 | |||||||
355 | $this->client->shouldReceive('receiveMessage')->once()->with([ |
||||||
356 | 'QueueUrl' => $url, |
||||||
357 | 'AttributeNames' => ['All'], |
||||||
358 | 'MaxNumberOfMessages' => $limit, |
||||||
359 | 'VisibilityTimeout' => $timeout, |
||||||
360 | ])->andReturn($this->model); |
||||||
361 | |||||||
362 | $iterator = $adapter->dequeue($this->factory, $limit); |
||||||
0 ignored issues
–
show
It seems like
$this->factory can also be of type Mockery\MockInterface ; however, parameter $factory of Graze\Queue\Adapter\SqsAdapter::dequeue() does only seem to accept Graze\Queue\Message\MessageFactoryInterface , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
363 | |||||||
364 | assertThat($iterator, is(anInstanceOf('Generator'))); |
||||||
365 | assertThat(iterator_to_array($iterator), is(equalTo($messages))); |
||||||
366 | } |
||||||
367 | |||||||
368 | public function testEnqueue() |
||||||
369 | { |
||||||
370 | $adapter = new SqsAdapter($this->client, 'foo'); |
||||||
371 | $url = $this->stubCreateQueue('foo'); |
||||||
372 | |||||||
373 | $metadata = m::mock(ContainerInterface::class); |
||||||
374 | $metadata->shouldReceive('get') |
||||||
375 | ->with('MessageAttributes') |
||||||
376 | ->times(3) |
||||||
377 | ->andReturn(null); |
||||||
378 | $metadata->shouldReceive('get') |
||||||
379 | ->with('DelaySeconds') |
||||||
380 | ->andReturn(null); |
||||||
381 | |||||||
382 | $this->messageA->shouldReceive('getBody')->once()->withNoArgs()->andReturn('foo'); |
||||||
383 | $this->messageB->shouldReceive('getBody')->once()->withNoArgs()->andReturn('bar'); |
||||||
384 | $this->messageC->shouldReceive('getBody')->once()->withNoArgs()->andReturn('baz'); |
||||||
385 | $this->messageA->shouldReceive('getMetadata')->andReturn($metadata); |
||||||
386 | $this->messageB->shouldReceive('getMetadata')->andReturn($metadata); |
||||||
387 | $this->messageC->shouldReceive('getMetadata')->andReturn($metadata); |
||||||
388 | |||||||
389 | $this->model->shouldReceive('get')->once()->with('Failed')->andReturn([]); |
||||||
390 | |||||||
391 | $this->client->shouldReceive('sendMessageBatch')->once()->with([ |
||||||
392 | 'QueueUrl' => $url, |
||||||
393 | 'Entries' => [ |
||||||
394 | ['Id' => 0, 'MessageBody' => 'foo', 'MessageAttributes' => []], |
||||||
395 | ['Id' => 1, 'MessageBody' => 'bar', 'MessageAttributes' => []], |
||||||
396 | ['Id' => 2, 'MessageBody' => 'baz', 'MessageAttributes' => []], |
||||||
397 | ], |
||||||
398 | ])->andReturn($this->model); |
||||||
399 | |||||||
400 | $adapter->enqueue($this->messages); |
||||||
401 | } |
||||||
402 | |||||||
403 | public function testEnqueueWithDelaySecondsMetadata() |
||||||
404 | { |
||||||
405 | $adapter = new SqsAdapter($this->client, 'foo'); |
||||||
406 | $url = $this->stubCreateQueue('foo'); |
||||||
407 | |||||||
408 | $metadataA = m::mock(ContainerInterface::class); |
||||||
409 | $metadataA->shouldReceive('get')->with('MessageAttributes')->once()->andReturn(null); |
||||||
410 | $metadataA->shouldReceive('get')->with('DelaySeconds')->andReturn(1); |
||||||
411 | $metadataB = m::mock(ContainerInterface::class); |
||||||
412 | $metadataB->shouldReceive('get')->with('MessageAttributes')->once()->andReturn(null); |
||||||
413 | $metadataB->shouldReceive('get')->with('DelaySeconds')->andReturn(2); |
||||||
414 | $metadataC = m::mock(ContainerInterface::class); |
||||||
415 | $metadataC->shouldReceive('get')->with('MessageAttributes')->once()->andReturn(null); |
||||||
416 | $metadataC->shouldReceive('get')->with('DelaySeconds')->andReturn(3); |
||||||
417 | |||||||
418 | $this->messageA->shouldReceive('getBody')->once()->withNoArgs()->andReturn('foo'); |
||||||
419 | $this->messageB->shouldReceive('getBody')->once()->withNoArgs()->andReturn('bar'); |
||||||
420 | $this->messageC->shouldReceive('getBody')->once()->withNoArgs()->andReturn('baz'); |
||||||
421 | $this->messageA->shouldReceive('getMetadata')->andReturn($metadataA); |
||||||
422 | $this->messageB->shouldReceive('getMetadata')->andReturn($metadataB); |
||||||
423 | $this->messageC->shouldReceive('getMetadata')->andReturn($metadataC); |
||||||
424 | |||||||
425 | $this->model->shouldReceive('get')->once()->with('Failed')->andReturn([]); |
||||||
426 | |||||||
427 | $this->client->shouldReceive('sendMessageBatch')->once()->with([ |
||||||
428 | 'QueueUrl' => $url, |
||||||
429 | 'Entries' => [ |
||||||
430 | ['Id' => 0, 'MessageBody' => 'foo', 'MessageAttributes' => [], 'DelaySeconds' => 1], |
||||||
431 | ['Id' => 1, 'MessageBody' => 'bar', 'MessageAttributes' => [], 'DelaySeconds' => 2], |
||||||
432 | ['Id' => 2, 'MessageBody' => 'baz', 'MessageAttributes' => [], 'DelaySeconds' => 3], |
||||||
433 | ], |
||||||
434 | ])->andReturn($this->model); |
||||||
435 | |||||||
436 | $adapter->enqueue($this->messages); |
||||||
437 | } |
||||||
438 | |||||||
439 | public function testEnqueueWithDelaySecondsQueueConfiguration() |
||||||
440 | { |
||||||
441 | $options = ['DelaySeconds' => 10]; |
||||||
442 | |||||||
443 | $adapter = new SqsAdapter($this->client, 'foo', $options); |
||||||
444 | $url = $this->stubCreateQueue('foo', $options); |
||||||
445 | |||||||
446 | $metadataA = m::mock(ContainerInterface::class); |
||||||
447 | $metadataA->shouldReceive('get')->with('MessageAttributes')->once()->andReturn(null); |
||||||
448 | $metadataA->shouldReceive('get')->with('DelaySeconds')->andReturn(null); |
||||||
449 | $metadataB = m::mock(ContainerInterface::class); |
||||||
450 | $metadataB->shouldReceive('get')->with('MessageAttributes')->once()->andReturn(null); |
||||||
451 | $metadataB->shouldReceive('get')->with('DelaySeconds')->andReturn(0); |
||||||
452 | $metadataC = m::mock(ContainerInterface::class); |
||||||
453 | $metadataC->shouldReceive('get')->with('MessageAttributes')->once()->andReturn(null); |
||||||
454 | $metadataC->shouldReceive('get')->with('DelaySeconds')->andReturn(2); |
||||||
455 | |||||||
456 | $this->messageA->shouldReceive('getBody')->once()->withNoArgs()->andReturn('foo'); |
||||||
457 | $this->messageB->shouldReceive('getBody')->once()->withNoArgs()->andReturn('bar'); |
||||||
458 | $this->messageC->shouldReceive('getBody')->once()->withNoArgs()->andReturn('baz'); |
||||||
459 | $this->messageA->shouldReceive('getMetadata')->andReturn($metadataA); |
||||||
460 | $this->messageB->shouldReceive('getMetadata')->andReturn($metadataB); |
||||||
461 | $this->messageC->shouldReceive('getMetadata')->andReturn($metadataC); |
||||||
462 | |||||||
463 | $this->model->shouldReceive('get')->once()->with('Failed')->andReturn([]); |
||||||
464 | |||||||
465 | $this->client->shouldReceive('sendMessageBatch')->once()->with([ |
||||||
466 | 'QueueUrl' => $url, |
||||||
467 | 'Entries' => [ |
||||||
468 | ['Id' => 0, 'MessageBody' => 'foo', 'MessageAttributes' => []], |
||||||
469 | ['Id' => 1, 'MessageBody' => 'bar', 'MessageAttributes' => [], 'DelaySeconds' => 0], |
||||||
470 | ['Id' => 2, 'MessageBody' => 'baz', 'MessageAttributes' => [], 'DelaySeconds' => 2], |
||||||
471 | ], |
||||||
472 | ])->andReturn($this->model); |
||||||
473 | |||||||
474 | $adapter->enqueue($this->messages); |
||||||
475 | } |
||||||
476 | |||||||
477 | public function testReceiveMessageWaitTimeSecondsOption() |
||||||
478 | { |
||||||
479 | $options = ['ReceiveMessageWaitTimeSeconds' => 20]; |
||||||
480 | |||||||
481 | $adapter = new SqsAdapter($this->client, 'foo', $options); |
||||||
482 | $url = $this->stubCreateQueue('foo', $options); |
||||||
483 | $timeout = $this->stubQueueVisibilityTimeout($url); |
||||||
484 | |||||||
485 | $this->stubCreateDequeueMessage('foo', 0, 'a'); |
||||||
486 | $this->stubCreateDequeueMessage('bar', 1, 'b'); |
||||||
487 | $this->stubCreateDequeueMessage('baz', 2, 'c'); |
||||||
488 | |||||||
489 | $this->model->shouldReceive('get')->once()->with('Messages')->andReturn([ |
||||||
490 | ['Body' => 'foo', 'Attributes' => [], 'MessageAttributes' => [], 'MessageId' => 0, 'ReceiptHandle' => 'a'], |
||||||
491 | ['Body' => 'bar', 'Attributes' => [], 'MessageAttributes' => [], 'MessageId' => 1, 'ReceiptHandle' => 'b'], |
||||||
492 | ['Body' => 'baz', 'Attributes' => [], 'MessageAttributes' => [], 'MessageId' => 2, 'ReceiptHandle' => 'c'], |
||||||
493 | ]); |
||||||
494 | |||||||
495 | $this->client->shouldReceive('receiveMessage')->once()->with([ |
||||||
496 | 'QueueUrl' => $url, |
||||||
497 | 'AttributeNames' => ['All'], |
||||||
498 | 'MaxNumberOfMessages' => 3, |
||||||
499 | 'VisibilityTimeout' => $timeout, |
||||||
500 | 'WaitTimeSeconds' => 20, |
||||||
501 | ])->andReturn($this->model); |
||||||
502 | |||||||
503 | $iterator = $adapter->dequeue($this->factory, 3); |
||||||
0 ignored issues
–
show
It seems like
$this->factory can also be of type Mockery\MockInterface ; however, parameter $factory of Graze\Queue\Adapter\SqsAdapter::dequeue() does only seem to accept Graze\Queue\Message\MessageFactoryInterface , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
504 | |||||||
505 | assertThat($iterator, is(anInstanceOf('Generator'))); |
||||||
506 | assertThat(iterator_to_array($iterator), is(equalTo($this->messages))); |
||||||
507 | } |
||||||
508 | |||||||
509 | public function testPurge() |
||||||
510 | { |
||||||
511 | $adapter = new SqsAdapter($this->client, 'foo'); |
||||||
512 | $url = $this->stubCreateQueue('foo'); |
||||||
513 | |||||||
514 | $this->client->shouldReceive('purgeQueue')->once()->with([ |
||||||
515 | 'QueueUrl' => $url, |
||||||
516 | ])->andReturn($this->model); |
||||||
517 | |||||||
518 | assertThat($adapter->purge(), is(nullValue())); |
||||||
0 ignored issues
–
show
Are you sure the usage of
$adapter->purge() targeting Graze\Queue\Adapter\SqsAdapter::purge() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||||||
519 | } |
||||||
520 | |||||||
521 | public function testDelete() |
||||||
522 | { |
||||||
523 | $adapter = new SqsAdapter($this->client, 'foo'); |
||||||
524 | $url = $this->stubCreateQueue('foo'); |
||||||
525 | |||||||
526 | $this->client->shouldReceive('deleteQueue')->once()->with([ |
||||||
527 | 'QueueUrl' => $url, |
||||||
528 | ])->andReturn($this->model); |
||||||
529 | |||||||
530 | assertThat($adapter->delete(), is(nullValue())); |
||||||
0 ignored issues
–
show
Are you sure the usage of
$adapter->delete() targeting Graze\Queue\Adapter\SqsAdapter::delete() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||||||
531 | } |
||||||
532 | } |
||||||
533 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..