Completed
Pull Request — master (#40)
by Harry
10:25
created

SqsIntegrationTest::testSend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 13
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 9
nc 1
nop 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;
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 PHPUnit_Framework_TestCase as 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'));
39
    }
40
41
    /**
42
     * Create a queue
43
     *
44
     * @return string
45
     */
46 View Code Duplication
    protected function stubCreateQueue()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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([
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Aws\Sqs\SqsClient.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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 View Code Duplication
    protected function stubQueueVisibilityTimeout($url)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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([
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Aws\Sqs\SqsClient.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
72
            'QueueUrl'       => $url,
73
            'AttributeNames' => ['VisibilityTimeout'],
74
        ])->andReturn($model);
75
76
        return $timeout;
77
    }
78
79 View Code Duplication
    public function testReceive()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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([
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Aws\Sqs\SqsClient.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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
$timeout is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
114
115
        $receiveModel = m::mock(ResultInterface::class);
116
        $receiveModel->shouldReceive('get')->with('Messages')->andReturn(
117
            [
118
                ['Body'              => 'foo',
119
                 'Attributes'        => [],
120
                 'MessageAttributes' => [],
121
                 'MessageId'         => 0,
122
                 'ReceiptHandle'     => 'a',
123
                ],
124
                ['Body'              => 'foo',
125
                 'Attributes'        => [],
126
                 'MessageAttributes' => [],
127
                 'MessageId'         => 0,
128
                 'ReceiptHandle'     => 'a',
129
                ],
130
                ['Body'              => 'foo',
131
                 'Attributes'        => [],
132
                 'MessageAttributes' => [],
133
                 'MessageId'         => 0,
134
                 'ReceiptHandle'     => 'a',
135
                ],
136
                ['Body'              => 'foo',
137
                 'Attributes'        => [],
138
                 'MessageAttributes' => [],
139
                 'MessageId'         => 0,
140
                 'ReceiptHandle'     => 'a',
141
                ],
142
                ['Body'              => 'foo',
143
                 'Attributes'        => [],
144
                 'MessageAttributes' => [],
145
                 'MessageId'         => 0,
146
                 'ReceiptHandle'     => 'a',
147
                ],
148
                ['Body'              => 'foo',
149
                 'Attributes'        => [],
150
                 'MessageAttributes' => [],
151
                 'MessageId'         => 0,
152
                 'ReceiptHandle'     => 'a',
153
                ],
154
                ['Body'              => 'foo',
155
                 'Attributes'        => [],
156
                 'MessageAttributes' => [],
157
                 'MessageId'         => 0,
158
                 'ReceiptHandle'     => 'a',
159
                ],
160
                ['Body'              => 'foo',
161
                 'Attributes'        => [],
162
                 'MessageAttributes' => [],
163
                 'MessageId'         => 0,
164
                 'ReceiptHandle'     => 'a',
165
                ],
166
                ['Body'              => 'foo',
167
                 'Attributes'        => [],
168
                 'MessageAttributes' => [],
169
                 'MessageId'         => 0,
170
                 'ReceiptHandle'     => 'a',
171
                ],
172
            ],
173
            [
174
                ['Body'              => 'foo',
175
                 'Attributes'        => [],
176
                 'MessageAttributes' => [],
177
                 'MessageId'         => 0,
178
                 'ReceiptHandle'     => 'a',
179
                ],
180
                ['Body'              => 'foo',
181
                 'Attributes'        => [],
182
                 'MessageAttributes' => [],
183
                 'MessageId'         => 0,
184
                 'ReceiptHandle'     => 'a',
185
                ],
186
            ],
187
            null
188
        );
189
190
        $this->sqsClient->shouldReceive('receiveMessage')->andReturn($receiveModel);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Aws\Sqs\SqsClient.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
191
192
        $deleteModel = m::mock(ResultInterface::class);
193
        $deleteModel->shouldReceive('get')->twice()->with('Failed')->andReturn([]);
194
        $this->sqsClient->shouldReceive('deleteMessageBatch')->with(m::type('array'))->andReturn($deleteModel);
195
196
        $msgs = [];
197
        $this->client->receive(function ($msg) use (&$msgs) {
198
            $msgs[] = $msg;
199
        }, 11);
200
201
        assertThat($msgs, is(arrayWithSize(11)));
202
    }
203
204 View Code Duplication
    public function testReceiveWithLimit()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
205
    {
206
        $url = $this->stubCreateQueue();
207
        $timeout = $this->stubQueueVisibilityTimeout($url);
208
209
        $receiveModel = m::mock(ResultInterface::class);
210
        $receiveModel->shouldReceive('get')->once()->with('Messages')->andReturn([
211
            ['Body' => 'foo', 'Attributes' => [], 'MessageAttributes' => [], 'MessageId' => 0, 'ReceiptHandle' => 'a'],
212
        ]);
213
        $this->sqsClient->shouldReceive('receiveMessage')->once()->with([
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Aws\Sqs\SqsClient.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
214
            'QueueUrl'            => $url,
215
            'AttributeNames'      => ['All'],
216
            'MaxNumberOfMessages' => SqsAdapter::BATCHSIZE_RECEIVE,
217
            'VisibilityTimeout'   => $timeout,
218
        ])->andReturn($receiveModel);
219
220
        $deleteModel = m::mock(ResultInterface::class);
221
        $deleteModel->shouldReceive('get')->once()->with('Failed')->andReturn([]);
222
        $this->sqsClient->shouldReceive('deleteMessageBatch')->once()->with([
223
            'QueueUrl' => $url,
224
            'Entries'  => [['Id' => 0, 'ReceiptHandle' => 'a']],
225
        ])->andReturn($deleteModel);
226
227
        $msgs = [];
228
        $this->client->receive(function ($msg, $done) use (&$msgs) {
229
            $msgs[] = $msg;
230
            $done();
231
        }, 100);
232
233
        assertThat($msgs, is(arrayWithSize(1)));
234
    }
235
236 View Code Duplication
    public function testReceiveWithPolling()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
237
    {
238
        $url = $this->stubCreateQueue();
239
        $timeout = $this->stubQueueVisibilityTimeout($url);
240
241
        $receiveModel = m::mock(ResultInterface::class);
242
        $receiveModel->shouldReceive('get')->once()->with('Messages')->andReturn([
243
            ['Body' => 'foo', 'Attributes' => [], 'MessageAttributes' => [], 'MessageId' => 0, 'ReceiptHandle' => 'a'],
244
        ]);
245
        $this->sqsClient->shouldReceive('receiveMessage')->once()->with([
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Aws\Sqs\SqsClient.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
246
            'QueueUrl'            => $url,
247
            'AttributeNames'      => ['All'],
248
            'MaxNumberOfMessages' => SqsAdapter::BATCHSIZE_RECEIVE,
249
            'VisibilityTimeout'   => $timeout,
250
        ])->andReturn($receiveModel);
251
252
        $deleteModel = m::mock(ResultInterface::class);
253
        $deleteModel->shouldReceive('get')->once()->with('Failed')->andReturn([]);
254
        $this->sqsClient->shouldReceive('deleteMessageBatch')->once()->with([
255
            'QueueUrl' => $url,
256
            'Entries'  => [['Id' => 0, 'ReceiptHandle' => 'a']],
257
        ])->andReturn($deleteModel);
258
259
        $msgs = [];
260
        $this->client->receive(function ($msg, $done) use (&$msgs) {
261
            $msgs[] = $msg;
262
            $done();
263
        }, null);
264
265
        assertThat($msgs, is(arrayWithSize(1)));
266
    }
267
268
    public function testSend()
269
    {
270
        $url = $this->stubCreateQueue();
271
        $model = m::mock(ResultInterface::class);
272
        $model->shouldReceive('get')->once()->with('Failed')->andReturn([]);
273
274
        $this->sqsClient->shouldReceive('sendMessageBatch')->once()->with([
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Aws\Sqs\SqsClient.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
275
            'QueueUrl' => $url,
276
            'Entries'  => [['Id' => 0, 'MessageBody' => 'foo', 'MessageAttributes' => []]],
277
        ])->andReturn($model);
278
279
        $this->client->send([$this->client->create('foo')]);
280
    }
281
282
    public function testPurge()
283
    {
284
        $url = $this->stubCreateQueue();
285
        $timeout = $this->stubQueueVisibilityTimeout($url);
286
287
        $receiveModel = m::mock(ResultInterface::class);
288
        $receiveModel->shouldReceive('get')->once()->with('Messages')->andReturn([]);
289
        $this->sqsClient->shouldReceive('receiveMessage')->once()->with([
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Aws\Sqs\SqsClient.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
290
            'QueueUrl'            => $url,
291
            'AttributeNames'      => ['All'],
292
            'MaxNumberOfMessages' => 1,
293
            'VisibilityTimeout'   => $timeout,
294
        ])->andReturn($receiveModel);
295
296
        $purgeModel = m::mock(ResultInterface::class);
297
        $this->sqsClient->shouldReceive('purgeQueue')->once()->with([
298
            'QueueUrl' => $url,
299
        ])->andReturn($purgeModel);
300
301
        $this->client->purge();
302
303
        $msgs = [];
304
        $this->client->receive(function ($msg) use (&$msgs) {
305
            $msgs[] = $msg;
306
        });
307
308
        assertThat($msgs, is(emptyArray()));
309
    }
310
311
    public function testDelete()
312
    {
313
        $url = $this->stubCreateQueue();
314
315
        $deleteModel = m::mock(ResultInterface::class);
316
        $this->sqsClient->shouldReceive('deleteQueue')->once()->with([
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Aws\Sqs\SqsClient.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
317
            'QueueUrl' => $url,
318
        ])->andReturn($deleteModel);
319
320
        $this->client->delete();
321
    }
322
}
323