Completed
Push — master ( 9eae13...e56856 )
by Antonio
04:26
created

SqsQueueStoreAdapterTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 124
Code Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 124
rs 8.2857
cc 1
eloc 78
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Da\tests\Queue\Backend\Sqs;
3
4
use Da\Mailer\Test\Fixture\FixtureHelper;
5
use Da\Mailer\Model\MailMessage;
6
use Da\Mailer\Queue\Backend\Sqs\SqsQueueStoreAdapter;
7
use Da\Mailer\Queue\Backend\Sqs\SqsQueueStoreConnection;
8
use Aws\Sqs\SqsClient;
9
use Guzzle\Common\Collection;
10
use PHPUnit_Framework_TestCase;
11
use Mockery;
12
13
class SqsQueueStoreAdapterTest extends PHPUnit_Framework_TestCase
14
{
15
    /**
16
     * @var SqsQueueStoreAdapter
17
     */
18
    private $sqsQueueStore1, $sqsQueueStore2;
19
20
    protected function setUp()
21
    {
22
        // prepare sqs response collections - begin
23
        $createQueueResult = new Collection([
24
            'MessageId' => 'createQueueResultId',
25
            'QueueUrl' => 'http://queue.url/path/',
26
        ]);
27
28
        $sendMessageResult = new Collection([
29
            'MessageId' => 'sendMessageResultId',
30
31
        ]);
32
33
        $getQueueAttributesResult1 = new Collection([
34
            'MessageId' => 'getQueueAttributesResult1Id',
35
            'Attributes' => [
36
                'ApproximateNumberOfMessages' => 1,
37
            ],
38
        ]);
39
        $getQueueAttributesResult2 = new Collection([
40
            'MessageId' => 'getQueueAttributesResult2Id',
41
            'Attributes' => [
42
                'ApproximateNumberOfMessages' => 0,
43
            ],
44
        ]);
45
46
        $receiveMessageResult1 = new Collection([
47
            'Messages' => [
48
                [
49
                    'MessageId' => 'receiveMessageResult1Id',
50
                    'ReceiptHandle' => 'receiveMessageResult1Handle',
51
                    'Body' => json_encode(FixtureHelper::getMailMessage()),
52
                    'Attempt' => 1
53
                ],
54
            ],
55
        ]);
56
        $receiveMessageResult2 = new Collection([
57
            // no message(s) returned by Amazon SQS
58
        ]);
59
        // prepare sqs response collections - end
60
61
        // ------------------------------------------------------------
62
63
        // prepare queue store 1 - begin
64
        /** @var SqsClient $sqsClient1 */
65
        $sqsClient1 = Mockery::mock('\Aws\Sqs\SqsClient')
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not seem to exist on object<Mockery\Expectation>.

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...
66
            ->shouldReceive('createQueue')
67
                ->with(Mockery::mustBe([
68
                    'QueueName' => 'testing_queue_1',
69
                ]))
70
                ->andReturn($createQueueResult)
71
            ->shouldReceive('sendMessage')
72
                ->andReturn($sendMessageResult)
73
            ->shouldReceive('getQueueAttributes')
74
                ->with(Mockery::mustBe([
75
                    'QueueUrl' => 'http://queue.url/path/',
76
                    'AttributeNames' => ['ApproximateNumberOfMessages'],
77
                ]))
78
                ->andReturn($getQueueAttributesResult1, $getQueueAttributesResult2)
79
            ->shouldReceive('receiveMessage')
80
                ->with(Mockery::mustBe([
81
                    'QueueUrl' => 'http://queue.url/path/',
82
                ]))
83
                ->andReturn($receiveMessageResult1, $receiveMessageResult2)
84
            ->shouldReceive('deleteMessage')
85
                ->with(Mockery::mustBe([
86
                    'QueueUrl' => 'http://queue.url/path/',
87
                    'ReceiptHandle' => 'receiveMessageResult1Handle',
88
                ]))
89
            ->getMock();
90
91
        /** @var SqsQueueStoreConnection $sqsQueueStoreConnection1 */
92
        $sqsQueueStoreConnection1 = Mockery::mock('\Da\Mailer\Queue\Backend\Sqs\SqsQueueStoreConnection')
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not seem to exist on object<Mockery\Expectation>.

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...
93
            ->shouldReceive('connect')
94
                ->andReturnSelf()
95
            ->shouldReceive('getInstance')
96
                ->andReturn($sqsClient1)
97
            ->getMock();
98
99
        $this->sqsQueueStore1 = new SqsQueueStoreAdapter($sqsQueueStoreConnection1, 'testing_queue_1');
100
        // prepare queue store 1 - end
101
102
        // ------------------------------------------------------------
103
104
        // prepare queue store 2 - begin
105
        /** @var SqsClient $sqsClient1 */
106
        $sqsClient2 = Mockery::mock('\Aws\Sqs\SqsClient')
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not seem to exist on object<Mockery\Expectation>.

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...
107
            ->shouldReceive('createQueue')
108
                ->with(Mockery::mustBe([
109
                    'QueueName' => 'testing_queue_2',
110
                ]))
111
                ->andReturn($createQueueResult)
112
            ->shouldReceive('sendMessage')
113
                ->andReturn($sendMessageResult)
114
            ->shouldReceive('getQueueAttributes')
115
                ->with(Mockery::mustBe([
116
                    'QueueUrl' => 'http://queue.url/path/',
117
                    'AttributeNames' => ['ApproximateNumberOfMessages'],
118
                ]))
119
                ->andReturn($getQueueAttributesResult1, $getQueueAttributesResult2, $getQueueAttributesResult1)
120
            ->shouldReceive('receiveMessage')
121
                ->with(Mockery::mustBe([
122
                    'QueueUrl' => 'http://queue.url/path/',
123
                ]))
124
                ->andReturn($receiveMessageResult1, $receiveMessageResult2)
125
            ->shouldReceive('changeMessageVisibility')
126
                ->with(Mockery::mustBe([
127
                    'QueueUrl' => 'http://queue.url/path/',
128
                    'ReceiptHandle' => 'receiveMessageResult1Handle',
129
                    'VisibilityTimeout' => 5,
130
                ]))
131
            ->getMock();
132
133
        /** @var SqsQueueStoreConnection $sqsQueueStoreConnection1 */
134
        $sqsQueueStoreConnection1 = Mockery::mock('\Da\Mailer\Queue\Backend\Sqs\SqsQueueStoreConnection')
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not seem to exist on object<Mockery\Expectation>.

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...
135
            ->shouldReceive('connect')
136
                ->andReturnSelf()
137
            ->shouldReceive('getInstance')
138
                ->andReturn($sqsClient2)
139
            ->getMock();
140
141
        $this->sqsQueueStore2 = new SqsQueueStoreAdapter($sqsQueueStoreConnection1, 'testing_queue_2');
142
        // prepare queue store 2 - end
143
    }
144
145
    public function tearDown()
146
    {
147
        Mockery::close();
148
149
        parent::tearDown();
150
    }
151
152
    public function testEnqueueDequeueAndAcknowledge()
153
    {
154
        $mailJob = FixtureHelper::getSqsMailJob();
155
156
        $this->assertSame($this->sqsQueueStore1, $this->sqsQueueStore1->init());
157
158
        $this->assertTrue($this->sqsQueueStore1->enqueue($mailJob));
159
160
        $this->assertFalse($this->sqsQueueStore1->isEmpty());
161
162
        $mailJob = $this->sqsQueueStore1->dequeue();
163
        $this->assertNull($this->sqsQueueStore1->dequeue());
164
        $this->assertSame('receiveMessageResult1Id', $mailJob->getId());
165
166
        $this->assertTrue($this->sqsQueueStore1->isEmpty());
167
168
        $this->assertTrue(!empty($mailJob->getMessage()));
169
170
        $dequeuedMailMessage = MailMessage::fromArray(json_decode($mailJob->getMessage(), true));
171
172
        $this->assertEquals(FixtureHelper::getMailMessage(), $dequeuedMailMessage);
173
174
        $mailJob->setDeleted(true);
175
        $this->sqsQueueStore1->ack($mailJob);
0 ignored issues
show
Bug introduced by
It seems like $mailJob defined by $this->sqsQueueStore1->dequeue() on line 162 can be null; however, Da\Mailer\Queue\Backend\...ueueStoreAdapter::ack() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
176
177
        $this->assertNull($this->sqsQueueStore1->dequeue());
178
    }
179
180
    public function testAcknowledgementToUpdateMailJobs()
181
    {
182
        $mailJob = FixtureHelper::getSqsMailJob();
183
184
        $this->sqsQueueStore2->enqueue($mailJob);
185
        $this->assertFalse($this->sqsQueueStore2->isEmpty());
186
        $dequedMailJob = $this->sqsQueueStore2->dequeue();
187
        $this->assertNull($this->sqsQueueStore2->dequeue());
188
        $this->assertTrue($this->sqsQueueStore2->isEmpty());
189
        // set visibility timeout to five seconds
190
        $dequedMailJob->setVisibilityTimeout(5);
191
        $this->assertEquals(5, $dequedMailJob->getVisibilityTimeout());
192
        $this->sqsQueueStore2->ack($dequedMailJob);
0 ignored issues
show
Bug introduced by
It seems like $dequedMailJob defined by $this->sqsQueueStore2->dequeue() on line 186 can be null; however, Da\Mailer\Queue\Backend\...ueueStoreAdapter::ack() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
193
        $this->assertFalse($this->sqsQueueStore2->isEmpty());
194
    }
195
196
    public function testDoNothingWithMailJob()
197
    {
198
        $mailJob = FixtureHelper::getSqsMailJob();
199
200
        $this->sqsQueueStore2->enqueue($mailJob);
201
        $this->assertFalse($this->sqsQueueStore2->isEmpty());
202
        $dequedMailJob = $this->sqsQueueStore2->dequeue();
203
        $this->assertNull($this->sqsQueueStore2->dequeue());
204
        $this->assertTrue($this->sqsQueueStore2->isEmpty());
205
        $this->assertFalse($this->sqsQueueStore2->ack($dequedMailJob));
0 ignored issues
show
Bug introduced by
It seems like $dequedMailJob defined by $this->sqsQueueStore2->dequeue() on line 202 can be null; however, Da\Mailer\Queue\Backend\...ueueStoreAdapter::ack() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
206
        $this->assertFalse($this->sqsQueueStore2->isEmpty());
207
    }
208
209
    /**
210
     * @expectedException \BadMethodCallException
211
     */
212
    public function testBadMethodCallExceptionOnAck()
213
    {
214
        $mailJob = FixtureHelper::getPdoMailJob();
215
        $this->sqsQueueStore1->ack($mailJob);
216
    }
217
218
    /**
219
     * @expectedException \BadMethodCallException
220
     */
221
    public function testBadMethodCallExceptionOnSetDelaySeconds()
222
    {
223
        $mailJob = FixtureHelper::getSqsMailJob();
224
        $mailJob->setDelaySeconds(900);
225
        $this->assertEquals(900, $mailJob->getDelaySeconds());
226
        $mailJob->setDelaySeconds(901);
227
    }
228
}
229