Completed
Pull Request — master (#47)
by Harry
03:26
created

testFailureToAcknowledgeForSomeMessages()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 24
nc 2
nop 0
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
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\Message\MessageFactoryInterface;
23
use Graze\Queue\Message\MessageInterface;
24
use Mockery as m;
25
use Mockery\MockInterface;
26
use PHPUnit_Framework_TestCase as TestCase;
27
28
class SqsAdapterTest extends TestCase
29
{
30
    /** @var MessageInterface|MockInterface */
31
    private $messageA;
32
    /** @var MessageInterface|MockInterface */
33
    private $messageB;
34
    /** @var MessageInterface|MockInterface */
35
    private $messageC;
36
    /** @var MessageInterface[]|MockInterface[] */
37
    private $messages;
38
    /** @var ResultInterface|MockInterface */
39
    private $model;
40
    /** @var MessageFactoryInterface|MockInterface */
41
    private $factory;
42
    /** @var SqsClient */
43
    private $client;
44
45
    public function setUp()
46
    {
47
        $this->client = m::mock(SqsClient::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like Mockery::mock(Aws\Sqs\SqsClient::class) of type Mockery\MockInterface is incompatible with the declared type Aws\Sqs\SqsClient of property $client.

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..

Loading history...
48
        $this->model = m::mock(ResultInterface::class);
49
        $this->factory = m::mock(MessageFactoryInterface::class);
50
51
        $this->messageA = $a = m::mock(MessageInterface::class);
52
        $this->messageB = $b = m::mock(MessageInterface::class);
53
        $this->messageC = $c = m::mock(MessageInterface::class);
54
        $this->messages = [$a, $b, $c];
55
    }
56
57
    /**
58
     * @param string $body
59
     * @param int    $id
60
     * @param string $handle
61
     */
62
    protected function stubCreateDequeueMessage($body, $id, $handle)
63
    {
64
        $this->factory->shouldReceive('createMessage')->once()->with(
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'createMessage'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        $this->factory->/** @scrutinizer ignore-call */ 
65
                        shouldReceive('createMessage')->once()->with(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
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 ignore-call  annotation

64
        $this->factory->/** @scrutinizer ignore-call */ 
65
                        shouldReceive('createMessage')->once()->with(

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.

Loading history...
65
            $body,
66
            m::on(function ($opts) use ($id, $handle) {
67
                $meta = ['Attributes' => [], 'MessageAttributes' => [], 'MessageId' => $id, 'ReceiptHandle' => $handle];
68
                $validator = isset($opts['validator']) && is_callable($opts['validator']);
69
70
                return isset($opts['metadata']) && $opts['metadata'] === $meta && $validator;
71
            })
72
        )->andReturn($this->messageA);
73
    }
74
75
    /**
76
     * @param string $name
77
     * @param array  $options
78
     *
79
     * @return string
80
     */
81
    protected function stubCreateQueue($name, array $options = [])
82
    {
83
        $url = 'foo://bar';
84
        $model = m::mock(ResultInterface::class);
85
        $model->shouldReceive('get')->once()->with('QueueUrl')->andReturn($url);
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'get'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
        $model->/** @scrutinizer ignore-call */ 
86
                shouldReceive('get')->once()->with('QueueUrl')->andReturn($url);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
86
87
        $this->client->shouldReceive('createQueue')->once()->with([
88
            'QueueName'  => $name,
89
            'Attributes' => $options,
90
        ])->andReturn($model);
91
92
        return $url;
93
    }
94
95
    /**
96
     * @param string $url
97
     *
98
     * @return int
99
     */
100
    protected function stubQueueVisibilityTimeout($url)
101
    {
102
        $timeout = 120;
103
        $model = m::mock(ResultInterface::class);
104
        $model->shouldReceive('get')->once()->with('Attributes')->andReturn(['VisibilityTimeout' => $timeout]);
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'get'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

104
        $model->/** @scrutinizer ignore-call */ 
105
                shouldReceive('get')->once()->with('Attributes')->andReturn(['VisibilityTimeout' => $timeout]);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
105
106
        $this->client->shouldReceive('getQueueAttributes')->once()->with([
107
            'QueueUrl'       => $url,
108
            'AttributeNames' => ['VisibilityTimeout'],
109
        ])->andReturn($model);
110
111
        return $timeout;
112
    }
113
114
    public function testInterface()
115
    {
116
        assertThat(new SqsAdapter($this->client, 'foo'), is(anInstanceOf('Graze\Queue\Adapter\AdapterInterface')));
117
    }
118
119
    public function testAcknowledge()
120
    {
121
        $adapter = new SqsAdapter($this->client, 'foo');
122
        $url = $this->stubCreateQueue('foo');
123
124
        $this->messageA->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('foo');
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'getMetadata->get'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

124
        $this->messageA->/** @scrutinizer ignore-call */ 
125
                         shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('foo');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
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 ignore-call  annotation

124
        $this->messageA->/** @scrutinizer ignore-call */ 
125
                         shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('foo');

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.

Loading history...
125
        $this->messageB->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('bar');
126
        $this->messageC->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('baz');
127
128
        $this->model->shouldReceive('get')->once()->with('Failed')->andReturn([]);
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

128
        $this->model->/** @scrutinizer ignore-call */ 
129
                      shouldReceive('get')->once()->with('Failed')->andReturn([]);

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.

Loading history...
129
130
        $this->client->shouldReceive('deleteMessageBatch')->once()->with([
131
            'QueueUrl' => $url,
132
            'Entries'  => [
133
                ['Id' => 0, 'ReceiptHandle' => 'foo'],
134
                ['Id' => 1, 'ReceiptHandle' => 'bar'],
135
                ['Id' => 2, 'ReceiptHandle' => 'baz'],
136
            ],
137
        ])->andReturn($this->model);
138
139
        $adapter->acknowledge($this->messages);
140
    }
141
142
    public function testFailureToAcknowledgeForSomeMessages()
143
    {
144
        $adapter = new SqsAdapter($this->client, 'foo');
145
        $url = $this->stubCreateQueue('foo');
146
147
        $this->messageA->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('foo');
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'getMetadata->get'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

147
        $this->messageA->/** @scrutinizer ignore-call */ 
148
                         shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('foo');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
148
        $this->messageB->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('bar');
149
        $this->messageC->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('baz');
150
151
        $this->model->shouldReceive('get')->once()->with('Failed')->andReturn([
152
            ['Id' => 2, 'Code' => 123, 'SenderFault' => true, 'Message' => 'baz is gone'],
153
        ]);
154
155
        $this->client->shouldReceive('deleteMessageBatch')->once()->with([
156
            'QueueUrl' => $url,
157
            'Entries'  => [
158
                ['Id' => 0, 'ReceiptHandle' => 'foo'],
159
                ['Id' => 1, 'ReceiptHandle' => 'bar'],
160
                ['Id' => 2, 'ReceiptHandle' => 'baz'],
161
            ],
162
        ])->andReturn($this->model);
163
164
        $errorThrown = false;
165
        try {
166
            $adapter->acknowledge($this->messages);
167
        } catch (FailedAcknowledgementException $e) {
168
            assertThat($e->getMessages(), is(anArray([$this->messageC])));
169
            assertThat(
170
                $e->getDebug(),
171
                is(anArray([['Id' => 2, 'Code' => 123, 'SenderFault' => true, 'Message' => 'baz is gone']]))
172
            );
173
            $errorThrown = true;
174
        }
175
176
        assertthat('an exception is thrown', $errorThrown);
177
    }
178
179
    public function testReject()
180
    {
181
        $adapter = new SqsAdapter($this->client, 'foo');
182
        $url = $this->stubCreateQueue('foo');
183
184
        $this->messageA->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('foo');
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'getMetadata->get'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

184
        $this->messageA->/** @scrutinizer ignore-call */ 
185
                         shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('foo');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
185
        $this->messageB->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('bar');
186
        $this->messageC->shouldReceive('getMetadata->get')->once()->with('ReceiptHandle')->andReturn('baz');
187
188
        $this->model->shouldReceive('get')->once()->with('Failed')->andReturn([]);
189
190
        $this->client->shouldReceive('changeMessageVisibilityBatch')->once()->with([
191
            'QueueUrl' => $url,
192
            'Entries'  => [
193
                ['Id' => 0, 'ReceiptHandle' => 'foo', 'VisibilityTimeout' => 0],
194
                ['Id' => 1, 'ReceiptHandle' => 'bar', 'VisibilityTimeout' => 0],
195
                ['Id' => 2, 'ReceiptHandle' => 'baz', 'VisibilityTimeout' => 0],
196
            ],
197
        ])->andReturn($this->model);
198
199
        $adapter->reject($this->messages);
200
    }
201
202
    public function testDequeue()
203
    {
204
        $adapter = new SqsAdapter($this->client, 'foo');
205
        $url = $this->stubCreateQueue('foo');
206
        $timeout = $this->stubQueueVisibilityTimeout($url);
207
208
        $this->stubCreateDequeueMessage('foo', 0, 'a');
209
        $this->stubCreateDequeueMessage('bar', 1, 'b');
210
        $this->stubCreateDequeueMessage('baz', 2, 'c');
211
212
        $this->model->shouldReceive('get')->once()->with('Messages')->andReturn([
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'get'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

212
        $this->model->/** @scrutinizer ignore-call */ 
213
                      shouldReceive('get')->once()->with('Messages')->andReturn([

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
213
            ['Body' => 'foo', 'Attributes' => [], 'MessageAttributes' => [], 'MessageId' => 0, 'ReceiptHandle' => 'a'],
214
            ['Body' => 'bar', 'Attributes' => [], 'MessageAttributes' => [], 'MessageId' => 1, 'ReceiptHandle' => 'b'],
215
            ['Body' => 'baz', 'Attributes' => [], 'MessageAttributes' => [], 'MessageId' => 2, 'ReceiptHandle' => 'c'],
216
        ]);
217
218
        $this->client->shouldReceive('receiveMessage')->once()->with([
219
            'QueueUrl'            => $url,
220
            'AttributeNames'      => ['All'],
221
            'MaxNumberOfMessages' => 3,
222
            'VisibilityTimeout'   => $timeout,
223
        ])->andReturn($this->model);
224
225
        $iterator = $adapter->dequeue($this->factory, 3);
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

225
        $iterator = $adapter->dequeue(/** @scrutinizer ignore-type */ $this->factory, 3);
Loading history...
226
227
        assertThat($iterator, is(anInstanceOf('Generator')));
228
        assertThat(iterator_to_array($iterator), is(equalTo($this->messages)));
229
    }
230
231
    public function testDequeueInBatches()
232
    {
233
        $adapter = new SqsAdapter($this->client, 'foo');
234
        $url = $this->stubCreateQueue('foo');
235
        $timeout = $this->stubQueueVisibilityTimeout($url);
236
237
        $limit = SqsAdapter::BATCHSIZE_RECEIVE;
238
239
        $return = [];
240
        $messages = [];
241
242
        for ($i = 0; $i < $limit; $i++) {
243
            $this->stubCreateDequeueMessage('tmp' . $i, $i, 'h' . $i);
244
            $return[] = [
245
                'Body'              => 'tmp' . $i,
246
                'Attributes'        => [],
247
                'MessageAttributes' => [],
248
                'MessageId'         => $i,
249
                'ReceiptHandle'     => 'h' . $i,
250
            ];
251
            $messages[] = $this->messageA;
252
        }
253
254
        $this->model->shouldReceive('get')->once()->with('Messages')->andReturn($return);
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'get'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

254
        $this->model->/** @scrutinizer ignore-call */ 
255
                      shouldReceive('get')->once()->with('Messages')->andReturn($return);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
255
256
        $this->client->shouldReceive('receiveMessage')->once()->with([
257
            'QueueUrl'            => $url,
258
            'AttributeNames'      => ['All'],
259
            'MaxNumberOfMessages' => $limit,
260
            'VisibilityTimeout'   => $timeout,
261
        ])->andReturn($this->model);
262
263
        $iterator = $adapter->dequeue($this->factory, $limit);
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

263
        $iterator = $adapter->dequeue(/** @scrutinizer ignore-type */ $this->factory, $limit);
Loading history...
264
265
        assertThat($iterator, is(anInstanceOf('Generator')));
266
        assertThat(iterator_to_array($iterator), is(equalTo($messages)));
267
    }
268
269
    public function testEnqueue()
270
    {
271
        $adapter = new SqsAdapter($this->client, 'foo');
272
        $url = $this->stubCreateQueue('foo');
273
274
        $metadata = m::mock(ContainerInterface::class);
275
        $metadata->shouldReceive('get')
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'get'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

275
        $metadata->/** @scrutinizer ignore-call */ 
276
                   shouldReceive('get')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
276
                 ->with('MessageAttributes')
277
                 ->times(3)
278
                 ->andReturn(null);
279
        $metadata->shouldReceive('get')
280
                 ->with('DelaySeconds')
281
                 ->andReturn(null);
282
283
        $this->messageA->shouldReceive('getBody')->once()->withNoArgs()->andReturn('foo');
284
        $this->messageB->shouldReceive('getBody')->once()->withNoArgs()->andReturn('bar');
285
        $this->messageC->shouldReceive('getBody')->once()->withNoArgs()->andReturn('baz');
286
        $this->messageA->shouldReceive('getMetadata')->andReturn($metadata);
287
        $this->messageB->shouldReceive('getMetadata')->andReturn($metadata);
288
        $this->messageC->shouldReceive('getMetadata')->andReturn($metadata);
289
290
        $this->model->shouldReceive('get')->once()->with('Failed')->andReturn([]);
291
292
        $this->client->shouldReceive('sendMessageBatch')->once()->with([
293
            'QueueUrl' => $url,
294
            'Entries'  => [
295
                ['Id' => 0, 'MessageBody' => 'foo', 'MessageAttributes' => []],
296
                ['Id' => 1, 'MessageBody' => 'bar', 'MessageAttributes' => []],
297
                ['Id' => 2, 'MessageBody' => 'baz', 'MessageAttributes' => []],
298
            ],
299
        ])->andReturn($this->model);
300
301
        $adapter->enqueue($this->messages);
302
    }
303
304
    public function testEnqueueWithDelaySecondsMetadata()
305
    {
306
        $adapter = new SqsAdapter($this->client, 'foo');
307
        $url = $this->stubCreateQueue('foo');
308
309
        $metadataA = m::mock(ContainerInterface::class);
310
        $metadataA->shouldReceive('get')->with('MessageAttributes')->once()->andReturn(null);
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'get'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

310
        $metadataA->/** @scrutinizer ignore-call */ 
311
                    shouldReceive('get')->with('MessageAttributes')->once()->andReturn(null);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
311
        $metadataA->shouldReceive('get')->with('DelaySeconds')->andReturn(1);
312
        $metadataB = m::mock(ContainerInterface::class);
313
        $metadataB->shouldReceive('get')->with('MessageAttributes')->once()->andReturn(null);
314
        $metadataB->shouldReceive('get')->with('DelaySeconds')->andReturn(2);
315
        $metadataC = m::mock(ContainerInterface::class);
316
        $metadataC->shouldReceive('get')->with('MessageAttributes')->once()->andReturn(null);
317
        $metadataC->shouldReceive('get')->with('DelaySeconds')->andReturn(3);
318
319
        $this->messageA->shouldReceive('getBody')->once()->withNoArgs()->andReturn('foo');
320
        $this->messageB->shouldReceive('getBody')->once()->withNoArgs()->andReturn('bar');
321
        $this->messageC->shouldReceive('getBody')->once()->withNoArgs()->andReturn('baz');
322
        $this->messageA->shouldReceive('getMetadata')->andReturn($metadataA);
323
        $this->messageB->shouldReceive('getMetadata')->andReturn($metadataB);
324
        $this->messageC->shouldReceive('getMetadata')->andReturn($metadataC);
325
326
        $this->model->shouldReceive('get')->once()->with('Failed')->andReturn([]);
327
328
        $this->client->shouldReceive('sendMessageBatch')->once()->with([
329
            'QueueUrl' => $url,
330
            'Entries'  => [
331
                ['Id' => 0, 'MessageBody' => 'foo', 'MessageAttributes' => [], 'DelaySeconds' => 1],
332
                ['Id' => 1, 'MessageBody' => 'bar', 'MessageAttributes' => [], 'DelaySeconds' => 2],
333
                ['Id' => 2, 'MessageBody' => 'baz', 'MessageAttributes' => [], 'DelaySeconds' => 3],
334
            ],
335
        ])->andReturn($this->model);
336
337
        $adapter->enqueue($this->messages);
338
    }
339
340
    public function testEnqueueWithDelaySecondsQueueConfiguration()
341
    {
342
        $options = ['DelaySeconds' => 10];
343
344
        $adapter = new SqsAdapter($this->client, 'foo', $options);
345
        $url = $this->stubCreateQueue('foo', $options);
346
347
        $metadataA = m::mock(ContainerInterface::class);
348
        $metadataA->shouldReceive('get')->with('MessageAttributes')->once()->andReturn(null);
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'get'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

348
        $metadataA->/** @scrutinizer ignore-call */ 
349
                    shouldReceive('get')->with('MessageAttributes')->once()->andReturn(null);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
349
        $metadataA->shouldReceive('get')->with('DelaySeconds')->andReturn(null);
350
        $metadataB = m::mock(ContainerInterface::class);
351
        $metadataB->shouldReceive('get')->with('MessageAttributes')->once()->andReturn(null);
352
        $metadataB->shouldReceive('get')->with('DelaySeconds')->andReturn(0);
353
        $metadataC = m::mock(ContainerInterface::class);
354
        $metadataC->shouldReceive('get')->with('MessageAttributes')->once()->andReturn(null);
355
        $metadataC->shouldReceive('get')->with('DelaySeconds')->andReturn(2);
356
357
        $this->messageA->shouldReceive('getBody')->once()->withNoArgs()->andReturn('foo');
358
        $this->messageB->shouldReceive('getBody')->once()->withNoArgs()->andReturn('bar');
359
        $this->messageC->shouldReceive('getBody')->once()->withNoArgs()->andReturn('baz');
360
        $this->messageA->shouldReceive('getMetadata')->andReturn($metadataA);
361
        $this->messageB->shouldReceive('getMetadata')->andReturn($metadataB);
362
        $this->messageC->shouldReceive('getMetadata')->andReturn($metadataC);
363
364
        $this->model->shouldReceive('get')->once()->with('Failed')->andReturn([]);
365
366
        $this->client->shouldReceive('sendMessageBatch')->once()->with([
367
            'QueueUrl' => $url,
368
            'Entries'  => [
369
                ['Id' => 0, 'MessageBody' => 'foo', 'MessageAttributes' => []],
370
                ['Id' => 1, 'MessageBody' => 'bar', 'MessageAttributes' => [], 'DelaySeconds' => 0],
371
                ['Id' => 2, 'MessageBody' => 'baz', 'MessageAttributes' => [], 'DelaySeconds' => 2],
372
            ],
373
        ])->andReturn($this->model);
374
375
        $adapter->enqueue($this->messages);
376
    }
377
378
    public function testReceiveMessageWaitTimeSecondsOption()
379
    {
380
        $options = ['ReceiveMessageWaitTimeSeconds' => 20];
381
382
        $adapter = new SqsAdapter($this->client, 'foo', $options);
383
        $url = $this->stubCreateQueue('foo', $options);
384
        $timeout = $this->stubQueueVisibilityTimeout($url);
385
386
        $this->stubCreateDequeueMessage('foo', 0, 'a');
387
        $this->stubCreateDequeueMessage('bar', 1, 'b');
388
        $this->stubCreateDequeueMessage('baz', 2, 'c');
389
390
        $this->model->shouldReceive('get')->once()->with('Messages')->andReturn([
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'get'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

390
        $this->model->/** @scrutinizer ignore-call */ 
391
                      shouldReceive('get')->once()->with('Messages')->andReturn([

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
391
            ['Body' => 'foo', 'Attributes' => [], 'MessageAttributes' => [], 'MessageId' => 0, 'ReceiptHandle' => 'a'],
392
            ['Body' => 'bar', 'Attributes' => [], 'MessageAttributes' => [], 'MessageId' => 1, 'ReceiptHandle' => 'b'],
393
            ['Body' => 'baz', 'Attributes' => [], 'MessageAttributes' => [], 'MessageId' => 2, 'ReceiptHandle' => 'c'],
394
        ]);
395
396
        $this->client->shouldReceive('receiveMessage')->once()->with([
397
            'QueueUrl'            => $url,
398
            'AttributeNames'      => ['All'],
399
            'MaxNumberOfMessages' => 3,
400
            'VisibilityTimeout'   => $timeout,
401
            'WaitTimeSeconds'     => 20,
402
        ])->andReturn($this->model);
403
404
        $iterator = $adapter->dequeue($this->factory, 3);
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

404
        $iterator = $adapter->dequeue(/** @scrutinizer ignore-type */ $this->factory, 3);
Loading history...
405
406
        assertThat($iterator, is(anInstanceOf('Generator')));
407
        assertThat(iterator_to_array($iterator), is(equalTo($this->messages)));
408
    }
409
410
    public function testPurge()
411
    {
412
        $adapter = new SqsAdapter($this->client, 'foo');
413
        $url = $this->stubCreateQueue('foo');
414
415
        $this->client->shouldReceive('purgeQueue')->once()->with([
416
            'QueueUrl' => $url,
417
        ])->andReturn($this->model);
418
419
        assertThat($adapter->purge(), is(nullValue()));
0 ignored issues
show
Bug introduced by
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 getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
420
    }
421
422
    public function testDelete()
423
    {
424
        $adapter = new SqsAdapter($this->client, 'foo');
425
        $url = $this->stubCreateQueue('foo');
426
427
        $this->client->shouldReceive('deleteQueue')->once()->with([
428
            'QueueUrl' => $url,
429
        ])->andReturn($this->model);
430
431
        assertThat($adapter->delete(), is(nullValue()));
0 ignored issues
show
Bug introduced by
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 getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
432
    }
433
}
434