Skip to content

Commit af10009

Browse files
authored
Adding service providers for the laravel packages (#572)
* Adding service providers for the laravel packages * Fix for DynamoDb * Updated baseline * minor * Fixes * Added cache docs * cs * baseline * Added support for more config * Added link in readle * Bugfix * wording * CI fix * Added links on README * Added test to serialize * Remove laravel 7 feature * added docs * More doxs
1 parent fb0179f commit af10009

11 files changed

+501
-12
lines changed

.github/workflows/ci.yml

+2-6
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,12 @@ jobs:
2020
uses: shivammathur/[email protected]
2121
with:
2222
php-version: ${{ matrix.php }}
23-
coverage: xdebug
24-
ini-values: xdebug.overload_var_dump=1
25-
tools: prestissimo
23+
coverage: none
24+
tools: flex
2625

2726
- name: Checkout code
2827
uses: actions/checkout@v2
2928

30-
- name: Initialize tests
31-
run: make initialize
32-
3329
- name: Download dependencies
3430
run: |
3531
composer config minimum-stability dev

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ composer require async-aws/illuminate-queue
1111

1212
## Documentation
1313

14-
See https://async-aws.com/ for documentation.
14+
See https://async-aws.com/integration/laravel.html for documentation.
1515

1616
## Contribute
1717

composer.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,18 @@
1313
"license": "MIT",
1414
"require": {
1515
"php": "^7.2.5",
16-
"async-aws/sqs": "^1.0",
17-
"illuminate/queue": "^6.18.11 || ^7.10"
16+
"async-aws/sqs": "^1.1",
17+
"illuminate/queue": "^6.18.11 || ^7.10",
18+
"illuminate/support": "^6.18.13 || ^7.10"
1819
},
1920
"extra": {
2021
"branch-alias": {
2122
"dev-master": "0.1-dev"
23+
},
24+
"laravel": {
25+
"providers": [
26+
"AsyncAws\\Illuminate\\Queue\\ServiceProvider"
27+
]
2228
}
2329
},
2430
"autoload": {

src/AsyncAwsSqsQueue.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace AsyncAws\Illuminate\Queue;
44

55
use AsyncAws\Illuminate\Queue\Job\AsyncAwsSqsJob;
6+
use AsyncAws\Sqs\Enum\MessageSystemAttributeName;
67
use AsyncAws\Sqs\Enum\QueueAttributeName;
78
use AsyncAws\Sqs\SqsClient;
89
use Illuminate\Contracts\Queue\Job;
@@ -138,7 +139,7 @@ public function pop($queue = null)
138139
{
139140
$response = $this->sqs->receiveMessage([
140141
'QueueUrl' => $queue = $this->getQueue($queue),
141-
'AttributeNames' => ['ApproximateReceiveCount'],
142+
'AttributeNames' => [MessageSystemAttributeName::APPROXIMATE_RECEIVE_COUNT],
142143
]);
143144

144145
foreach ($response->getMessages() as $message) {
@@ -161,7 +162,7 @@ public function pop($queue = null)
161162
*
162163
* @return string
163164
*/
164-
public function getQueue($queue)
165+
public function getQueue($queue = null)
165166
{
166167
$queue = $queue ?: $this->default;
167168

src/Connector/AsyncAwsSqsConnector.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ class AsyncAwsSqsConnector implements ConnectorInterface
1818
public function connect(array $config)
1919
{
2020
$clientConfig = [];
21-
if ($config['key'] && $config['secret']) {
21+
if (isset($config['key']) && isset($config['secret'])) {
2222
$clientConfig['accessKeyId'] = $config['key'] ?? null;
2323
$clientConfig['accessKeySecret'] = $config['secret'] ?? null;
2424
$clientConfig['sessionToken'] = $config['token'] ?? null;
2525
}
2626

27+
if (!empty($config['region'])) {
28+
$clientConfig['region'] = $config['region'];
29+
}
30+
2731
return new AsyncAwsSqsQueue(
2832
new SqsClient($clientConfig, null, HttpClient::create(['timeout' => 30])),
2933
$config['queue'],

src/ServiceProvider.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AsyncAws\Illuminate\Queue;
6+
7+
use AsyncAws\Illuminate\Queue\Connector\AsyncAwsSqsConnector;
8+
use Illuminate\Queue\QueueManager;
9+
use Illuminate\Support\ServiceProvider as AbstractServiceProvider;
10+
11+
class ServiceProvider extends AbstractServiceProvider
12+
{
13+
public function boot()
14+
{
15+
/** @var QueueManager $manager */
16+
$manager = $this->app['queue'];
17+
18+
$manager->addConnector('async-aws-sqs', \Closure::fromCallable([$this, 'createConnector']));
19+
}
20+
21+
public function createConnector(): AsyncAwsSqsConnector
22+
{
23+
return new AsyncAwsSqsConnector();
24+
}
25+
}

tests/Resource/CreateUser.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AsyncAws\Illuminate\Queue\Tests\Resource;
6+
7+
class CreateUser
8+
{
9+
/**
10+
* @var string
11+
*/
12+
private $name;
13+
14+
/**
15+
* @var string
16+
*/
17+
private $email;
18+
19+
public function __construct(string $name, string $email)
20+
{
21+
$this->name = $name;
22+
$this->email = $email;
23+
}
24+
25+
public function getName(): string
26+
{
27+
return $this->name;
28+
}
29+
30+
public function getEmail(): string
31+
{
32+
return $this->email;
33+
}
34+
}

tests/Unit/AsyncAwsSqsQueueTest.php

+218
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AsyncAws\Illuminate\Queue\Tests\Unit;
6+
7+
use AsyncAws\Core\Test\ResultMockFactory;
8+
use AsyncAws\Illuminate\Queue\AsyncAwsSqsQueue;
9+
use AsyncAws\Illuminate\Queue\Job\AsyncAwsSqsJob;
10+
use AsyncAws\Illuminate\Queue\Tests\Resource\CreateUser;
11+
use AsyncAws\Sqs\Enum\MessageSystemAttributeName;
12+
use AsyncAws\Sqs\Enum\QueueAttributeName;
13+
use AsyncAws\Sqs\Result\GetQueueAttributesResult;
14+
use AsyncAws\Sqs\Result\ReceiveMessageResult;
15+
use AsyncAws\Sqs\Result\SendMessageResult;
16+
use AsyncAws\Sqs\SqsClient;
17+
use AsyncAws\Sqs\ValueObject\Message;
18+
use Illuminate\Container\Container;
19+
use PHPUnit\Framework\TestCase;
20+
21+
class AsyncAwsSqsQueueTest extends TestCase
22+
{
23+
private $queue = 'https://sqs.us-east-2.amazonaws.com/123456789012/invoice';
24+
25+
public function testSize()
26+
{
27+
$result = ResultMockFactory::create(GetQueueAttributesResult::class, [
28+
'Attributes' => [QueueAttributeName::APPROXIMATE_NUMBER_OF_MESSAGES => 4],
29+
]);
30+
31+
$sqs = $this->getMockBuilder(SqsClient::class)
32+
->disableOriginalConstructor()
33+
->onlyMethods(['getQueueAttributes'])
34+
->getMock();
35+
36+
$sqs->expects(self::once())
37+
->method('getQueueAttributes')
38+
->with(self::callback(function (array $input) {
39+
if ($input['QueueUrl'] !== $this->queue) {
40+
return false;
41+
}
42+
43+
if (!isset($input['AttributeNames'])) {
44+
return false;
45+
}
46+
47+
if ([QueueAttributeName::APPROXIMATE_NUMBER_OF_MESSAGES] !== $input['AttributeNames']) {
48+
return false;
49+
}
50+
51+
return true;
52+
}))
53+
->willReturn($result);
54+
55+
$queue = new AsyncAwsSqsQueue($sqs, $this->queue);
56+
self::assertEquals(4, $queue->size());
57+
}
58+
59+
public function testPush()
60+
{
61+
$result = ResultMockFactory::create(SendMessageResult::class, [
62+
'MessageId' => 'my-id',
63+
]);
64+
65+
$sqs = $this->getMockBuilder(SqsClient::class)
66+
->disableOriginalConstructor()
67+
->onlyMethods(['sendMessage'])
68+
->getMock();
69+
70+
$sqs->expects(self::once())
71+
->method('sendMessage')
72+
->with(self::callback(function (array $input) {
73+
if ($input['QueueUrl'] !== $this->queue) {
74+
return false;
75+
}
76+
77+
if (!isset($input['MessageBody'])) {
78+
return false;
79+
}
80+
81+
// verify that body is serialized.
82+
$body = json_decode($input['MessageBody'], true);
83+
$this->assertArrayHasKey('displayName', $body);
84+
$this->assertArrayHasKey('job', $body);
85+
$this->assertArrayHasKey('data', $body);
86+
87+
if (CreateUser::class !== $body['displayName']) {
88+
return false;
89+
}
90+
91+
return true;
92+
}))
93+
->willReturn($result);
94+
95+
$queue = new AsyncAwsSqsQueue($sqs, $this->queue);
96+
$output = $queue->push(new CreateUser('Alic', '[email protected]'));
97+
self::assertEquals('my-id', $output);
98+
}
99+
100+
public function testPushRaw()
101+
{
102+
$result = ResultMockFactory::create(SendMessageResult::class, [
103+
'MessageId' => 'my-id',
104+
]);
105+
106+
$sqs = $this->getMockBuilder(SqsClient::class)
107+
->disableOriginalConstructor()
108+
->onlyMethods(['sendMessage'])
109+
->getMock();
110+
111+
$sqs->expects(self::once())
112+
->method('sendMessage')
113+
->with(self::callback(function (array $input) {
114+
if ($input['QueueUrl'] !== $this->queue) {
115+
return false;
116+
}
117+
118+
if (!isset($input['MessageBody'])) {
119+
return false;
120+
}
121+
122+
if ('payload' !== $input['MessageBody']) {
123+
return false;
124+
}
125+
126+
return true;
127+
}))
128+
->willReturn($result);
129+
130+
$queue = new AsyncAwsSqsQueue($sqs, $this->queue);
131+
self::assertEquals('my-id', $queue->pushRaw('payload'));
132+
}
133+
134+
public function testLater()
135+
{
136+
$result = ResultMockFactory::create(SendMessageResult::class, [
137+
'MessageId' => 'my-id',
138+
]);
139+
140+
$sqs = $this->getMockBuilder(SqsClient::class)
141+
->disableOriginalConstructor()
142+
->onlyMethods(['sendMessage'])
143+
->getMock();
144+
145+
$sqs->expects(self::once())
146+
->method('sendMessage')
147+
->with(self::callback(function (array $input) {
148+
if ($input['QueueUrl'] !== $this->queue) {
149+
return false;
150+
}
151+
152+
if (47 !== $input['DelaySeconds']) {
153+
return false;
154+
}
155+
156+
if (!isset($input['MessageBody'])) {
157+
return false;
158+
}
159+
160+
if (false === strpos($input['MessageBody'], 'my-custom-payload')) {
161+
return false;
162+
}
163+
164+
return true;
165+
}))
166+
->willReturn($result);
167+
168+
$queue = new AsyncAwsSqsQueue($sqs, $this->queue);
169+
self::assertEquals('my-id', $queue->later(47, 'my-custom-payload'));
170+
}
171+
172+
public function testPop()
173+
{
174+
$data = [
175+
'Body' => '{"uuid":"401eddde-fd9e-4dcd-83f3-d1ffe2ec4259","displayName":"AsyncAws\\\Illuminate\\\Queue\\\Tests\\\Resource\\\CreateUser","job":"Illuminate\\\Queue\\\CallQueuedHandler@call","maxTries":null,"maxExceptions":null,"delay":null,"timeout":null,"timeoutAt":null,"data":{"commandName":"AsyncAws\\\Illuminate\\\Queue\\\Tests\\\Resource\\\CreateUser","command":"O:51:\"AsyncAws\\\Illuminate\\\Queue\\\Tests\\\Resource\\\CreateUser\":2:{s:57:\"\u0000AsyncAws\\\Illuminate\\\Queue\\\Tests\\\Resource\\\CreateUser\u0000name\";s:4:\"Alic\";s:58:\"\u0000AsyncAws\\\Illuminate\\\Queue\\\Tests\\\Resource\\\CreateUser\u0000email\";s:17:\"[email protected]\";}"}}',
176+
'MessageId' => 'message-id-example',
177+
'Attributes' => ['ApproximateReceiveCount' => '3'],
178+
'ReceiptHandle' => '123',
179+
];
180+
$message = new Message($data);
181+
182+
$result = ResultMockFactory::create(ReceiveMessageResult::class, [
183+
'Messages' => [$message],
184+
]);
185+
186+
$sqs = $this->getMockBuilder(SqsClient::class)
187+
->disableOriginalConstructor()
188+
->onlyMethods(['receiveMessage'])
189+
->getMock();
190+
191+
$sqs->expects(self::once())
192+
->method('receiveMessage')
193+
->with(self::callback(function (array $input) {
194+
if ($input['QueueUrl'] !== $this->queue) {
195+
return false;
196+
}
197+
198+
if (!isset($input['AttributeNames'])) {
199+
return false;
200+
}
201+
202+
if ([MessageSystemAttributeName::APPROXIMATE_RECEIVE_COUNT] !== $input['AttributeNames']) {
203+
return false;
204+
}
205+
206+
return true;
207+
}))
208+
->willReturn($result);
209+
210+
$queue = new AsyncAwsSqsQueue($sqs, $this->queue);
211+
$queue->setContainer(new Container());
212+
$queue->setConnectionName('connection-name');
213+
$job = $queue->pop();
214+
215+
self::assertInstanceOf(AsyncAwsSqsJob::class, $job);
216+
self::assertEquals($data, $job->getSqsJob());
217+
}
218+
}

0 commit comments

Comments
 (0)