Completed
Push — master ( 964908...9eae13 )
by Antonio
02:35
created

SqsQueueStoreAdapterTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
namespace Da\tests\Queue\Database;
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
        $getQueueAttributesResult1 = new Collection([
33
            'MessageId' => 'getQueueAttributesResult1Id',
34
            'Attributes' => [
35
                'ApproximateNumberOfMessages' => 1,
36
            ],
37
        ]);
38
        $getQueueAttributesResult2 = new Collection([
39
            'MessageId' => 'getQueueAttributesResult2Id',
40
            'Attributes' => [
41
                'ApproximateNumberOfMessages' => 0,
42
            ],
43
        ]);
44
45
        $receiveMessageResult1 = new Collection([
46
            'Messages' => [
47
                [
48
                    'MessageId' => 'receiveMessageResult1Id',
49
                    'ReceiptHandle' => 'receiveMessageResult1Handle',
50
                    'Body' => json_encode(FixtureHelper::getMailMessage()),
51
                ],
52
            ],
53
        ]);
54
        $receiveMessageResult2 = new Collection([
55
            // no message(s) returned by Amazon SQS
56
        ]);
57
        // prepare sqs response collections - end
58
59
        // ------------------------------------------------------------
60
61
        // prepare queue store 1 - begin
62
        /** @var SqsClient $sqsClient1 */
63
        $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...
64
            ->shouldReceive('createQueue')
65
                ->with(Mockery::mustBe([
66
                    'QueueName' => 'testing_queue_1',
67
                ]))
68
                ->andReturn($createQueueResult)
69
            ->shouldReceive('sendMessage')
70
                ->with(Mockery::mustBe([
71
                    'QueueUrl' => 'http://queue.url/path/',
72
                    'MessageBody' => json_encode(FixtureHelper::getMailMessage()),
73
                    'DelaySeconds' => null,
74
                ]))
75
                ->andReturn($sendMessageResult)
76
            ->shouldReceive('getQueueAttributes')
77
                ->with(Mockery::mustBe([
78
                    'QueueUrl' => 'http://queue.url/path/',
79
                    'AttributeNames' => ['ApproximateNumberOfMessages'],
80
                ]))
81
                ->andReturn($getQueueAttributesResult1, $getQueueAttributesResult2)
82
            ->shouldReceive('receiveMessage')
83
                ->with(Mockery::mustBe([
84
                    'QueueUrl' => 'http://queue.url/path/',
85
                ]))
86
                ->andReturn($receiveMessageResult1, $receiveMessageResult2)
87
            ->shouldReceive('deleteMessage')
88
                ->with(Mockery::mustBe([
89
                    'QueueUrl' => 'http://queue.url/path/',
90
                    'ReceiptHandle' => 'receiveMessageResult1Handle',
91
                ]))
92
            ->getMock();
93
94
        /** @var SqsQueueStoreConnection $sqsQueueStoreConnection1 */
95
        $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...
96
            ->shouldReceive('connect')
97
                ->andReturnSelf()
98
            ->shouldReceive('getInstance')
99
                ->andReturn($sqsClient1)
100
            ->getMock();
101
102
        $this->sqsQueueStore1 = new SqsQueueStoreAdapter($sqsQueueStoreConnection1, 'testing_queue_1');
103
        // prepare queue store 1 - end
104
105
        // ------------------------------------------------------------
106
107
        // prepare queue store 2 - begin
108
        /** @var SqsClient $sqsClient1 */
109
        $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...
110
            ->shouldReceive('createQueue')
111
                ->with(Mockery::mustBe([
112
                    'QueueName' => 'testing_queue_2',
113
                ]))
114
                ->andReturn($createQueueResult)
115
            ->shouldReceive('sendMessage')
116
                ->with(Mockery::mustBe([
117
                    'QueueUrl' => 'http://queue.url/path/',
118
                    'MessageBody' => json_encode(FixtureHelper::getMailMessage()),
119
                    'DelaySeconds' => null,
120
                ]))
121
                ->andReturn($sendMessageResult)
122
            ->shouldReceive('getQueueAttributes')
123
                ->with(Mockery::mustBe([
124
                    'QueueUrl' => 'http://queue.url/path/',
125
                    'AttributeNames' => ['ApproximateNumberOfMessages'],
126
                ]))
127
                ->andReturn($getQueueAttributesResult1, $getQueueAttributesResult2, $getQueueAttributesResult1)
128
            ->shouldReceive('receiveMessage')
129
                ->with(Mockery::mustBe([
130
                    'QueueUrl' => 'http://queue.url/path/',
131
                ]))
132
                ->andReturn($receiveMessageResult1, $receiveMessageResult2)
133
            ->shouldReceive('changeMessageVisibility')
134
                ->with(Mockery::mustBe([
135
                    'QueueUrl' => 'http://queue.url/path/',
136
                    'ReceiptHandle' => 'receiveMessageResult1Handle',
137
                    'VisibilityTimeout' => 5,
138
                ]))
139
            ->getMock();
140
141
        /** @var SqsQueueStoreConnection $sqsQueueStoreConnection1 */
142
        $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...
143
            ->shouldReceive('connect')
144
                ->andReturnSelf()
145
            ->shouldReceive('getInstance')
146
                ->andReturn($sqsClient2)
147
            ->getMock();
148
149
        $this->sqsQueueStore2 = new SqsQueueStoreAdapter($sqsQueueStoreConnection1, 'testing_queue_2');
150
        // prepare queue store 2 - end
151
    }
152
153
    public function tearDown()
154
    {
155
        Mockery::close();
156
157
        parent::tearDown();
158
    }
159
160
    public function testEnqueueDequeueAndAcknowledge()
161
    {
162
        $mailJob = FixtureHelper::getSqsMailJob();
163
164
        $this->assertSame($this->sqsQueueStore1, $this->sqsQueueStore1->init());
165
166
        $this->assertTrue($this->sqsQueueStore1->enqueue($mailJob));
167
168
        $this->assertFalse($this->sqsQueueStore1->isEmpty());
169
170
        $mailJob = $this->sqsQueueStore1->dequeue();
171
        $this->assertNull($this->sqsQueueStore1->dequeue());
172
        $this->assertSame('receiveMessageResult1Id', $mailJob->getId());
173
174
        $this->assertTrue($this->sqsQueueStore1->isEmpty());
175
176
        $this->assertTrue(!empty($mailJob->getMessage()));
177
178
        $dequeuedMailMessage = MailMessage::fromArray(json_decode($mailJob->getMessage(), true));
179
180
        $this->assertEquals(FixtureHelper::getMailMessage(), $dequeuedMailMessage);
181
182
        $mailJob->setDeleted(true);
183
        $this->sqsQueueStore1->ack($mailJob);
0 ignored issues
show
Bug introduced by
It seems like $mailJob defined by $this->sqsQueueStore1->dequeue() on line 170 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...
184
185
        $this->assertNull($this->sqsQueueStore1->dequeue());
186
    }
187
188
    public function testAcknowledgementToUpdateMailJobs()
189
    {
190
        $mailJob = FixtureHelper::getSqsMailJob();
191
192
        $this->sqsQueueStore2->enqueue($mailJob);
193
        $this->assertFalse($this->sqsQueueStore2->isEmpty());
194
        $dequedMailJob = $this->sqsQueueStore2->dequeue();
195
        $this->assertNull($this->sqsQueueStore2->dequeue());
196
        $this->assertTrue($this->sqsQueueStore2->isEmpty());
197
        // set visibility timeout to five seconds
198
        $dequedMailJob->setVisibilityTimeout(5);
199
        $this->assertEquals(5, $dequedMailJob->getVisibilityTimeout());
200
        $this->sqsQueueStore2->ack($dequedMailJob);
0 ignored issues
show
Bug introduced by
It seems like $dequedMailJob defined by $this->sqsQueueStore2->dequeue() on line 194 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...
201
        $this->assertFalse($this->sqsQueueStore2->isEmpty());
202
    }
203
204
    public function testDoNothingWithMailJob()
205
    {
206
        $mailJob = FixtureHelper::getSqsMailJob();
207
208
        $this->sqsQueueStore2->enqueue($mailJob);
209
        $this->assertFalse($this->sqsQueueStore2->isEmpty());
210
        $dequedMailJob = $this->sqsQueueStore2->dequeue();
211
        $this->assertNull($this->sqsQueueStore2->dequeue());
212
        $this->assertTrue($this->sqsQueueStore2->isEmpty());
213
        $this->assertFalse($this->sqsQueueStore2->ack($dequedMailJob));
0 ignored issues
show
Bug introduced by
It seems like $dequedMailJob defined by $this->sqsQueueStore2->dequeue() on line 210 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...
214
        $this->assertFalse($this->sqsQueueStore2->isEmpty());
215
    }
216
217
    /**
218
     * @expectedException \BadMethodCallException
219
     */
220
    public function testBadMethodCallExceptionOnAck()
221
    {
222
        $mailJob = FixtureHelper::getPdoMailJob();
223
        $this->sqsQueueStore1->ack($mailJob);
224
    }
225
226
    /**
227
     * @expectedException \BadMethodCallException
228
     */
229
    public function testBadMethodCallExceptionOnSetDelaySeconds()
230
    {
231
        $mailJob = FixtureHelper::getSqsMailJob();
232
        $mailJob->setDelaySeconds(900);
233
        $this->assertEquals(900, $mailJob->getDelaySeconds());
234
        $mailJob->setDelaySeconds(901);
235
    }
236
}
237