SqsIntegrationTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 307
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 307
rs 10
c 0
b 0
f 0
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testSend() 0 12 1
A setUp() 0 5 1
B testReceiveWithLimit() 0 30 1
A testDelete() 0 10 1
B testReceiveWithReceiveMessageReturningLessThanMaxNumberOfMessages() 0 103 1
A stubQueueVisibilityTimeout() 0 12 1
B testReceiveWithPolling() 0 30 1
B testReceive() 0 29 1
A stubCreateQueue() 0 12 1
B testPurge() 0 27 1
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;
17
18
use Aws\ResultInterface;
19
use Aws\Sqs\SqsClient;
20
use Graze\Queue\Adapter\SqsAdapter;
21
use Mockery as m;
22
use Mockery\MockInterface;
23
use Graze\Queue\Test\TestCase;
24
25
class SqsIntegrationTest extends TestCase
26
{
27
    /** @var string */
28
    private $queueName;
29
    /** @var SqsClient|MockInterface */
30
    private $sqsClient;
31
    /** @var Client */
32
    private $client;
33
34
    public function setUp()
35
    {
36
        $this->queueName = 'queue_foo';
37
        $this->sqsClient = m::mock(SqsClient::class);
38
        $this->client = new Client(new SqsAdapter($this->sqsClient, 'queue_foo'));
0 ignored issues
show
Bug introduced by
$this->sqsClient of type Mockery\MockInterface is incompatible with the type Aws\Sqs\SqsClient expected by parameter $client of Graze\Queue\Adapter\SqsAdapter::__construct(). ( Ignorable by Annotation )

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

38
        $this->client = new Client(new SqsAdapter(/** @scrutinizer ignore-type */ $this->sqsClient, 'queue_foo'));
Loading history...
39
    }
40
41
    /**
42
     * Create a queue
43
     *
44
     * @return string
45
     */
46
    protected function stubCreateQueue()
47
    {
48
        $url = 'queue://foo';
49
        $model = m::mock(ResultInterface::class);
50
        $model->shouldReceive('get')->once()->with('QueueUrl')->andReturn($url);
51
52
        $this->sqsClient->shouldReceive('createQueue')->once()->with([
53
            'QueueName'  => $this->queueName,
54
            'Attributes' => [],
55
        ])->andReturn($model);
56
57
        return $url;
58
    }
59
60
    /**
61
     * @param string $url
62
     *
63
     * @return int
64
     */
65
    protected function stubQueueVisibilityTimeout($url)
66
    {
67
        $timeout = 120;
68
        $model = m::mock(ResultInterface::class);
69
        $model->shouldReceive('get')->once()->with('Attributes')->andReturn(['VisibilityTimeout' => $timeout]);
70
71
        $this->sqsClient->shouldReceive('getQueueAttributes')->once()->with([
72
            'QueueUrl'       => $url,
73
            'AttributeNames' => ['VisibilityTimeout'],
74
        ])->andReturn($model);
75
76
        return $timeout;
77
    }
78
79
    public function testReceive()
80
    {
81
        $url = $this->stubCreateQueue();
82
        $timeout = $this->stubQueueVisibilityTimeout($url);
83
84
        $receiveModel = m::mock(ResultInterface::class);
85
        $receiveModel->shouldReceive('get')->once()->with('Messages')->andReturn([
86
            ['Body' => 'foo', 'Attributes' => [], 'MessageAttributes' => [], 'MessageId' => 0, 'ReceiptHandle' => 'a'],
87
        ]);
88
        $this->sqsClient->shouldReceive('receiveMessage')->once()->with([
89
            'QueueUrl'            => $url,
90
            'AttributeNames'      => ['All'],
91
            'MaxNumberOfMessages' => 1,
92
            'VisibilityTimeout'   => $timeout,
93
        ])->andReturn($receiveModel);
94
95
        $deleteModel = m::mock(ResultInterface::class);
96
        $deleteModel->shouldReceive('get')->once()->with('Failed')->andReturn([]);
97
        $this->sqsClient->shouldReceive('deleteMessageBatch')->once()->with([
98
            'QueueUrl' => $url,
99
            'Entries'  => [['Id' => 0, 'ReceiptHandle' => 'a']],
100
        ])->andReturn($deleteModel);
101
102
        $msgs = [];
103
        $this->client->receive(function ($msg) use (&$msgs) {
104
            $msgs[] = $msg;
105
        });
106
107
        assertThat($msgs, is(arrayWithSize(1)));
108
    }
109
110
    public function testReceiveWithReceiveMessageReturningLessThanMaxNumberOfMessages()
111
    {
112
        $url = $this->stubCreateQueue();
113
        $timeout = $this->stubQueueVisibilityTimeout($url);
0 ignored issues
show
Unused Code introduced by
The assignment to $timeout is dead and can be removed.
Loading history...
114
115
        $receiveModel = m::mock(ResultInterface::class);
116
        $receiveModel->shouldReceive('get')->with('Messages')->andReturn(
117
            [
118
                [
119
                    'Body'              => 'foo',
120
                    'Attributes'        => [],
121
                    'MessageAttributes' => [],
122
                    'MessageId'         => 0,
123
                    'ReceiptHandle'     => 'a',
124
                ],
125
                [
126
                    'Body'              => 'foo',
127
                    'Attributes'        => [],
128
                    'MessageAttributes' => [],
129
                    'MessageId'         => 0,
130
                    'ReceiptHandle'     => 'a',
131
                ],
132
                [
133
                    'Body'              => 'foo',
134
                    'Attributes'        => [],
135
                    'MessageAttributes' => [],
136
                    'MessageId'         => 0,
137
                    'ReceiptHandle'     => 'a',
138
                ],
139
                [
140
                    'Body'              => 'foo',
141
                    'Attributes'        => [],
142
                    'MessageAttributes' => [],
143
                    'MessageId'         => 0,
144
                    'ReceiptHandle'     => 'a',
145
                ],
146
                [
147
                    'Body'              => 'foo',
148
                    'Attributes'        => [],
149
                    'MessageAttributes' => [],
150
                    'MessageId'         => 0,
151
                    'ReceiptHandle'     => 'a',
152
                ],
153
                [
154
                    'Body'              => 'foo',
155
                    'Attributes'        => [],
156
                    'MessageAttributes' => [],
157
                    'MessageId'         => 0,
158
                    'ReceiptHandle'     => 'a',
159
                ],
160
                [
161
                    'Body'              => 'foo',
162
                    'Attributes'        => [],
163
                    'MessageAttributes' => [],
164
                    'MessageId'         => 0,
165
                    'ReceiptHandle'     => 'a',
166
                ],
167
                [
168
                    'Body'              => 'foo',
169
                    'Attributes'        => [],
170
                    'MessageAttributes' => [],
171
                    'MessageId'         => 0,
172
                    'ReceiptHandle'     => 'a',
173
                ],
174
                [
175
                    'Body'              => 'foo',
176
                    'Attributes'        => [],
177
                    'MessageAttributes' => [],
178
                    'MessageId'         => 0,
179
                    'ReceiptHandle'     => 'a',
180
                ],
181
            ],
182
            [
183
                [
184
                    'Body'              => 'foo',
185
                    'Attributes'        => [],
186
                    'MessageAttributes' => [],
187
                    'MessageId'         => 0,
188
                    'ReceiptHandle'     => 'a',
189
                ],
190
                [
191
                    'Body'              => 'foo',
192
                    'Attributes'        => [],
193
                    'MessageAttributes' => [],
194
                    'MessageId'         => 0,
195
                    'ReceiptHandle'     => 'a',
196
                ],
197
            ],
198
            null
199
        );
200
201
        $this->sqsClient->shouldReceive('receiveMessage')->andReturn($receiveModel);
202
203
        $deleteModel = m::mock(ResultInterface::class);
204
        $deleteModel->shouldReceive('get')->twice()->with('Failed')->andReturn([]);
205
        $this->sqsClient->shouldReceive('deleteMessageBatch')->with(m::type('array'))->andReturn($deleteModel);
206
207
        $msgs = [];
208
        $this->client->receive(function ($msg) use (&$msgs) {
209
            $msgs[] = $msg;
210
        }, 11);
211
212
        assertThat($msgs, is(arrayWithSize(11)));
213
    }
214
215
    public function testReceiveWithLimit()
216
    {
217
        $url = $this->stubCreateQueue();
218
        $timeout = $this->stubQueueVisibilityTimeout($url);
219
220
        $receiveModel = m::mock(ResultInterface::class);
221
        $receiveModel->shouldReceive('get')->once()->with('Messages')->andReturn([
222
            ['Body' => 'foo', 'Attributes' => [], 'MessageAttributes' => [], 'MessageId' => 0, 'ReceiptHandle' => 'a'],
223
        ]);
224
        $this->sqsClient->shouldReceive('receiveMessage')->once()->with([
225
            'QueueUrl'            => $url,
226
            'AttributeNames'      => ['All'],
227
            'MaxNumberOfMessages' => SqsAdapter::BATCHSIZE_RECEIVE,
228
            'VisibilityTimeout'   => $timeout,
229
        ])->andReturn($receiveModel);
230
231
        $deleteModel = m::mock(ResultInterface::class);
232
        $deleteModel->shouldReceive('get')->once()->with('Failed')->andReturn([]);
233
        $this->sqsClient->shouldReceive('deleteMessageBatch')->once()->with([
234
            'QueueUrl' => $url,
235
            'Entries'  => [['Id' => 0, 'ReceiptHandle' => 'a']],
236
        ])->andReturn($deleteModel);
237
238
        $msgs = [];
239
        $this->client->receive(function ($msg, $done) use (&$msgs) {
240
            $msgs[] = $msg;
241
            $done();
242
        }, 100);
243
244
        assertThat($msgs, is(arrayWithSize(1)));
245
    }
246
247
    public function testReceiveWithPolling()
248
    {
249
        $url = $this->stubCreateQueue();
250
        $timeout = $this->stubQueueVisibilityTimeout($url);
251
252
        $receiveModel = m::mock(ResultInterface::class);
253
        $receiveModel->shouldReceive('get')->once()->with('Messages')->andReturn([
254
            ['Body' => 'foo', 'Attributes' => [], 'MessageAttributes' => [], 'MessageId' => 0, 'ReceiptHandle' => 'a'],
255
        ]);
256
        $this->sqsClient->shouldReceive('receiveMessage')->once()->with([
257
            'QueueUrl'            => $url,
258
            'AttributeNames'      => ['All'],
259
            'MaxNumberOfMessages' => SqsAdapter::BATCHSIZE_RECEIVE,
260
            'VisibilityTimeout'   => $timeout,
261
        ])->andReturn($receiveModel);
262
263
        $deleteModel = m::mock(ResultInterface::class);
264
        $deleteModel->shouldReceive('get')->once()->with('Failed')->andReturn([]);
265
        $this->sqsClient->shouldReceive('deleteMessageBatch')->once()->with([
266
            'QueueUrl' => $url,
267
            'Entries'  => [['Id' => 0, 'ReceiptHandle' => 'a']],
268
        ])->andReturn($deleteModel);
269
270
        $msgs = [];
271
        $this->client->receive(function ($msg, $done) use (&$msgs) {
272
            $msgs[] = $msg;
273
            $done();
274
        }, null);
275
276
        assertThat($msgs, is(arrayWithSize(1)));
277
    }
278
279
    public function testSend()
280
    {
281
        $url = $this->stubCreateQueue();
282
        $model = m::mock(ResultInterface::class);
283
        $model->shouldReceive('get')->once()->with('Failed')->andReturn([]);
284
285
        $this->sqsClient->shouldReceive('sendMessageBatch')->once()->with([
286
            'QueueUrl' => $url,
287
            'Entries'  => [['Id' => 0, 'MessageBody' => 'foo', 'MessageAttributes' => []]],
288
        ])->andReturn($model);
289
290
        $this->client->send([$this->client->create('foo')]);
291
    }
292
293
    public function testPurge()
294
    {
295
        $url = $this->stubCreateQueue();
296
        $timeout = $this->stubQueueVisibilityTimeout($url);
297
298
        $receiveModel = m::mock(ResultInterface::class);
299
        $receiveModel->shouldReceive('get')->once()->with('Messages')->andReturn([]);
300
        $this->sqsClient->shouldReceive('receiveMessage')->once()->with([
301
            'QueueUrl'            => $url,
302
            'AttributeNames'      => ['All'],
303
            'MaxNumberOfMessages' => 1,
304
            'VisibilityTimeout'   => $timeout,
305
        ])->andReturn($receiveModel);
306
307
        $purgeModel = m::mock(ResultInterface::class);
308
        $this->sqsClient->shouldReceive('purgeQueue')->once()->with([
309
            'QueueUrl' => $url,
310
        ])->andReturn($purgeModel);
311
312
        $this->client->purge();
313
314
        $msgs = [];
315
        $this->client->receive(function ($msg) use (&$msgs) {
316
            $msgs[] = $msg;
317
        });
318
319
        assertThat($msgs, is(emptyArray()));
320
    }
321
322
    public function testDelete()
323
    {
324
        $url = $this->stubCreateQueue();
325
326
        $deleteModel = m::mock(ResultInterface::class);
327
        $this->sqsClient->shouldReceive('deleteQueue')->once()->with([
328
            'QueueUrl' => $url,
329
        ])->andReturn($deleteModel);
330
331
        $this->client->delete();
332
    }
333
}
334