Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 876e9b7

Browse files
committed
Added standard unit tests for SqsQueue and SqsJob which are compatible with 4.1+
1 parent f857ae3 commit 876e9b7

2 files changed

Lines changed: 178 additions & 0 deletions

File tree

tests/Queue/QueueSqsJobTest.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
use Mockery as m;
4+
5+
use Aws\Sqs\SqsClient;
6+
use Aws\Common\Credentials\Credentials;
7+
use Aws\Common\Credentials\CredentialsInterface;
8+
use Aws\Common\Signature\SignatureInterface;
9+
use Aws\Common\Signature\SignatureV4;
10+
11+
use Guzzle\Common\Collection;
12+
use Guzzle\Service\Resource\Model;
13+
14+
class QueueSqsJobTest extends PHPUnit_Framework_TestCase {
15+
16+
public function setUp() {
17+
18+
$this->key = 'AMAZONSQSKEY';
19+
$this->secret = 'AmAz0n+SqSsEcReT+aLpHaNuM3R1CsTr1nG';
20+
$this->service = 'sqs';
21+
$this->region = 'someregion';
22+
$this->account = '1234567891011';
23+
$this->queueName = 'emails';
24+
$this->baseUrl = 'https://sqs.someregion.amazonaws.com';
25+
26+
// The Aws\Common\AbstractClient needs these three constructor parameters
27+
$this->credentials = new Credentials( $this->key, $this->secret );
28+
$this->signature = new SignatureV4( $this->service, $this->region );
29+
$this->config = new Collection();
30+
31+
// This is how the modified getQueue builds the queueUrl
32+
$this->queueUrl = $this->baseUrl . '/' . $this->account . '/' . $this->queueName;
33+
34+
// Get a mock of the SqsClient
35+
$this->mockedSqsClient = $this->getMock('Aws\Sqs\SqsClient', array('deleteMessage'), array($this->credentials, $this->signature, $this->config));
36+
37+
// Use Mockery to mock the IoC Container
38+
$this->mockedContainer = m::mock('Illuminate\Container\Container');
39+
40+
$this->mockedJob = 'foo';
41+
$this->mockedData = array('data');
42+
$this->mockedPayload = json_encode(array('job' => $this->mockedJob, 'data' => $this->mockedData, 'attempts' => 1));
43+
$this->mockedMessageId = 'e3cd03ee-59a3-4ad8-b0aa-ee2e3808ac81';
44+
$this->mockedReceiptHandle = '0NNAq8PwvXuWv5gMtS9DJ8qEdyiUwbAjpp45w2m6M4SJ1Y+PxCh7R930NRB8ylSacEmoSnW18bgd4nK\/O6ctE+VFVul4eD23mA07vVoSnPI4F\/voI1eNCp6Iax0ktGmhlNVzBwaZHEr91BRtqTRM3QKd2ASF8u+IQaSwyl\/DGK+P1+dqUOodvOVtExJwdyDLy1glZVgm85Yw9Jf5yZEEErqRwzYz\/qSigdvW4sm2l7e4phRol\/+IjMtovOyH\/ukueYdlVbQ4OshQLENhUKe7RNN5i6bE\/e5x9bnPhfj2gbM';
45+
46+
$this->mockedJobData = array('Body' => $this->mockedPayload,
47+
'MD5OfBody' => md5($this->mockedPayload),
48+
'ReceiptHandle' => $this->mockedReceiptHandle,
49+
'MessageId' => $this->mockedMessageId,
50+
'Attributes' => array('ApproximateReceiveCount' => 1));
51+
52+
}
53+
54+
public function tearDown()
55+
{
56+
m::close();
57+
}
58+
59+
60+
public function testFireProperlyCallsTheJobHandler()
61+
{
62+
$job = $this->getJob();
63+
$job->getContainer()->shouldReceive('make')->once()->with('foo')->andReturn($handler = m::mock('StdClass'));
64+
$handler->shouldReceive('fire')->once()->with($job, array('data'));
65+
$job->fire();
66+
}
67+
68+
69+
public function testDeleteRemovesTheJobFromSqs()
70+
{
71+
$this->mockedSqsClient = $this->getMock('Aws\Sqs\SqsClient', array('deleteMessage'), array($this->credentials, $this->signature, $this->config));
72+
$queue = $this->getMock('Illuminate\Queue\SqsQueue', array('getQueue'), array($this->mockedSqsClient, $this->queueName, $this->account));
73+
$queue->setContainer($this->mockedContainer);
74+
$job = $this->getJob();
75+
$job->getSqs()->expects($this->once())->method('deleteMessage')->with(array('QueueUrl' => $this->queueUrl, 'ReceiptHandle' => $this->mockedReceiptHandle));
76+
$job->delete();
77+
}
78+
79+
protected function getJob()
80+
{
81+
return new Illuminate\Queue\Jobs\SqsJob(
82+
$this->mockedContainer,
83+
$this->mockedSqsClient,
84+
$this->queueUrl,
85+
$this->mockedJobData
86+
);
87+
}
88+
89+
}

tests/Queue/QueueSqsQueueTest.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
use Mockery as m;
4+
5+
use Aws\Sqs\SqsClient;
6+
use Guzzle\Service\Resource\Model;
7+
8+
class QueueSqsQueueTest extends PHPUnit_Framework_TestCase {
9+
10+
public function tearDown()
11+
{
12+
m::close();
13+
}
14+
15+
public function setUp() {
16+
17+
// Use Mockery to mock the SqsClient
18+
$this->sqs = m::mock('Aws\Sqs\SqsClient');
19+
20+
$this->account = '1234567891011';
21+
$this->queueName = 'emails';
22+
$this->baseUrl = 'https://sqs.someregion.amazonaws.com';
23+
24+
// This is how the modified getQueue builds the queueUrl
25+
$this->queueUrl = $this->baseUrl . '/' . $this->account . '/' . $this->queueName;
26+
27+
$this->mockedJob = 'foo';
28+
$this->mockedData = array('data');
29+
$this->mockedPayload = json_encode(array('job' => $this->mockedJob, 'data' => $this->mockedData));
30+
$this->mockedDelay = 10;
31+
$this->mockedDateTime = m::mock('DateTime');
32+
$this->mockedMessageId = 'e3cd03ee-59a3-4ad8-b0aa-ee2e3808ac81';
33+
$this->mockedReceiptHandle = '0NNAq8PwvXuWv5gMtS9DJ8qEdyiUwbAjpp45w2m6M4SJ1Y+PxCh7R930NRB8ylSacEmoSnW18bgd4nK\/O6ctE+VFVul4eD23mA07vVoSnPI4F\/voI1eNCp6Iax0ktGmhlNVzBwaZHEr91BRtqTRM3QKd2ASF8u+IQaSwyl\/DGK+P1+dqUOodvOVtExJwdyDLy1glZVgm85Yw9Jf5yZEEErqRwzYz\/qSigdvW4sm2l7e4phRol\/+IjMtovOyH\/ukueYdlVbQ4OshQLENhUKe7RNN5i6bE\/e5x9bnPhfj2gbM';
34+
35+
$this->mockedSendMessageResponseModel = new Model(array('Body' => $this->mockedPayload,
36+
'MD5OfBody' => md5($this->mockedPayload),
37+
'ReceiptHandle' => $this->mockedReceiptHandle,
38+
'MessageId' => $this->mockedMessageId,
39+
'Attributes' => array('ApproximateReceiveCount' => 1)));
40+
41+
$this->mockedReceiveMessageResponseModel = new Model(array('Messages' => array( 0 => array(
42+
'Body' => $this->mockedPayload,
43+
'MD5OfBody' => md5($this->mockedPayload),
44+
'ReceiptHandle' => $this->mockedReceiptHandle,
45+
'MessageId' => $this->mockedMessageId))));
46+
}
47+
48+
public function testPopProperlyPopsJobOffOfSqs()
49+
{
50+
$queue = $this->getMock('Illuminate\Queue\SqsQueue', array('getQueue'), array($this->sqs, $this->queueName, $this->account));
51+
$queue->setContainer(m::mock('Illuminate\Container\Container'));
52+
$queue->expects($this->once())->method('getQueue')->with($this->queueName)->will($this->returnValue($this->queueUrl));
53+
$this->sqs->shouldReceive('receiveMessage')->once()->with(array('QueueUrl' => $this->queueUrl, 'AttributeNames' => array('ApproximateReceiveCount')))->andReturn($this->mockedReceiveMessageResponseModel);
54+
$result = $queue->pop($this->queueName);
55+
$this->assertInstanceOf('Illuminate\Queue\Jobs\SqsJob', $result);
56+
}
57+
58+
public function testDelayedPushWithDateTimeProperlyPushesJobOntoSqs()
59+
{
60+
$queue = $this->getMock('Illuminate\Queue\SqsQueue', array('createPayload', 'getSeconds', 'getQueue'), array($this->sqs, $this->queueName, $this->account));
61+
$queue->expects($this->once())->method('createPayload')->with($this->mockedJob, $this->mockedData)->will($this->returnValue($this->mockedPayload));
62+
$queue->expects($this->once())->method('getSeconds')->with($this->mockedDateTime)->will($this->returnValue($this->mockedDateTime));
63+
$queue->expects($this->once())->method('getQueue')->with($this->queueName)->will($this->returnValue($this->queueUrl));
64+
$this->sqs->shouldReceive('sendMessage')->once()->with(array('QueueUrl' => $this->queueUrl, 'MessageBody' => $this->mockedPayload, 'DelaySeconds' => $this->mockedDateTime))->andReturn($this->mockedSendMessageResponseModel);
65+
$id = $queue->later($this->mockedDateTime, $this->mockedJob, $this->mockedData, $this->queueName);
66+
$this->assertEquals($this->mockedMessageId, $id);
67+
}
68+
69+
public function testDelayedPushProperlyPushesJobOntoSqs()
70+
{
71+
$queue = $this->getMock('Illuminate\Queue\SqsQueue', array('createPayload', 'getSeconds', 'getQueue'), array($this->sqs, $this->queueName, $this->account));
72+
$queue->expects($this->once())->method('createPayload')->with($this->mockedJob, $this->mockedData)->will($this->returnValue($this->mockedPayload));
73+
$queue->expects($this->once())->method('getSeconds')->with($this->mockedDelay)->will($this->returnValue($this->mockedDelay));
74+
$queue->expects($this->once())->method('getQueue')->with($this->queueName)->will($this->returnValue($this->queueUrl));
75+
$this->sqs->shouldReceive('sendMessage')->once()->with(array('QueueUrl' => $this->queueUrl, 'MessageBody' => $this->mockedPayload, 'DelaySeconds' => $this->mockedDelay))->andReturn($this->mockedSendMessageResponseModel);
76+
$id = $queue->later($this->mockedDelay, $this->mockedJob, $this->mockedData, $this->queueName);
77+
$this->assertEquals($this->mockedMessageId, $id);
78+
}
79+
80+
public function testPushProperlyPushesJobOntoSqs()
81+
{
82+
$queue = $this->getMock('Illuminate\Queue\SqsQueue', array('createPayload', 'getQueue'), array($this->sqs, $this->queueName, $this->account));
83+
$queue->expects($this->once())->method('createPayload')->with($this->mockedJob, $this->mockedData)->will($this->returnValue($this->mockedPayload));
84+
$queue->expects($this->once())->method('getQueue')->with($this->queueName)->will($this->returnValue($this->queueUrl));
85+
$this->sqs->shouldReceive('sendMessage')->once()->with(array('QueueUrl' => $this->queueUrl, 'MessageBody' => $this->mockedPayload))->andReturn($this->mockedSendMessageResponseModel);
86+
$id = $queue->push($this->mockedJob, $this->mockedData, $this->queueName);
87+
$this->assertEquals($this->mockedMessageId, $id);
88+
}
89+
}

0 commit comments

Comments
 (0)