Skip to content

Commit

Permalink
Adding service providers for the laravel packages (#572)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Nyholm authored May 9, 2020
1 parent fb0179f commit af10009
Show file tree
Hide file tree
Showing 11 changed files with 501 additions and 12 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,12 @@ jobs:
uses: shivammathur/[email protected]
with:
php-version: ${{ matrix.php }}
coverage: xdebug
ini-values: xdebug.overload_var_dump=1
tools: prestissimo
coverage: none
tools: flex

- name: Checkout code
uses: actions/checkout@v2

- name: Initialize tests
run: make initialize

- name: Download dependencies
run: |
composer config minimum-stability dev
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ composer require async-aws/illuminate-queue

## Documentation

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

## Contribute

Expand Down
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@
"license": "MIT",
"require": {
"php": "^7.2.5",
"async-aws/sqs": "^1.0",
"illuminate/queue": "^6.18.11 || ^7.10"
"async-aws/sqs": "^1.1",
"illuminate/queue": "^6.18.11 || ^7.10",
"illuminate/support": "^6.18.13 || ^7.10"
},
"extra": {
"branch-alias": {
"dev-master": "0.1-dev"
},
"laravel": {
"providers": [
"AsyncAws\\Illuminate\\Queue\\ServiceProvider"
]
}
},
"autoload": {
Expand Down
5 changes: 3 additions & 2 deletions src/AsyncAwsSqsQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AsyncAws\Illuminate\Queue;

use AsyncAws\Illuminate\Queue\Job\AsyncAwsSqsJob;
use AsyncAws\Sqs\Enum\MessageSystemAttributeName;
use AsyncAws\Sqs\Enum\QueueAttributeName;
use AsyncAws\Sqs\SqsClient;
use Illuminate\Contracts\Queue\Job;
Expand Down Expand Up @@ -138,7 +139,7 @@ public function pop($queue = null)
{
$response = $this->sqs->receiveMessage([
'QueueUrl' => $queue = $this->getQueue($queue),
'AttributeNames' => ['ApproximateReceiveCount'],
'AttributeNames' => [MessageSystemAttributeName::APPROXIMATE_RECEIVE_COUNT],
]);

foreach ($response->getMessages() as $message) {
Expand All @@ -161,7 +162,7 @@ public function pop($queue = null)
*
* @return string
*/
public function getQueue($queue)
public function getQueue($queue = null)
{
$queue = $queue ?: $this->default;

Expand Down
6 changes: 5 additions & 1 deletion src/Connector/AsyncAwsSqsConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ class AsyncAwsSqsConnector implements ConnectorInterface
public function connect(array $config)
{
$clientConfig = [];
if ($config['key'] && $config['secret']) {
if (isset($config['key']) && isset($config['secret'])) {
$clientConfig['accessKeyId'] = $config['key'] ?? null;
$clientConfig['accessKeySecret'] = $config['secret'] ?? null;
$clientConfig['sessionToken'] = $config['token'] ?? null;
}

if (!empty($config['region'])) {
$clientConfig['region'] = $config['region'];
}

return new AsyncAwsSqsQueue(
new SqsClient($clientConfig, null, HttpClient::create(['timeout' => 30])),
$config['queue'],
Expand Down
25 changes: 25 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace AsyncAws\Illuminate\Queue;

use AsyncAws\Illuminate\Queue\Connector\AsyncAwsSqsConnector;
use Illuminate\Queue\QueueManager;
use Illuminate\Support\ServiceProvider as AbstractServiceProvider;

class ServiceProvider extends AbstractServiceProvider
{
public function boot()
{
/** @var QueueManager $manager */
$manager = $this->app['queue'];

$manager->addConnector('async-aws-sqs', \Closure::fromCallable([$this, 'createConnector']));
}

public function createConnector(): AsyncAwsSqsConnector
{
return new AsyncAwsSqsConnector();
}
}
34 changes: 34 additions & 0 deletions tests/Resource/CreateUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace AsyncAws\Illuminate\Queue\Tests\Resource;

class CreateUser
{
/**
* @var string
*/
private $name;

/**
* @var string
*/
private $email;

public function __construct(string $name, string $email)
{
$this->name = $name;
$this->email = $email;
}

public function getName(): string
{
return $this->name;
}

public function getEmail(): string
{
return $this->email;
}
}
218 changes: 218 additions & 0 deletions tests/Unit/AsyncAwsSqsQueueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
<?php

declare(strict_types=1);

namespace AsyncAws\Illuminate\Queue\Tests\Unit;

use AsyncAws\Core\Test\ResultMockFactory;
use AsyncAws\Illuminate\Queue\AsyncAwsSqsQueue;
use AsyncAws\Illuminate\Queue\Job\AsyncAwsSqsJob;
use AsyncAws\Illuminate\Queue\Tests\Resource\CreateUser;
use AsyncAws\Sqs\Enum\MessageSystemAttributeName;
use AsyncAws\Sqs\Enum\QueueAttributeName;
use AsyncAws\Sqs\Result\GetQueueAttributesResult;
use AsyncAws\Sqs\Result\ReceiveMessageResult;
use AsyncAws\Sqs\Result\SendMessageResult;
use AsyncAws\Sqs\SqsClient;
use AsyncAws\Sqs\ValueObject\Message;
use Illuminate\Container\Container;
use PHPUnit\Framework\TestCase;

class AsyncAwsSqsQueueTest extends TestCase
{
private $queue = 'https://sqs.us-east-2.amazonaws.com/123456789012/invoice';

public function testSize()
{
$result = ResultMockFactory::create(GetQueueAttributesResult::class, [
'Attributes' => [QueueAttributeName::APPROXIMATE_NUMBER_OF_MESSAGES => 4],
]);

$sqs = $this->getMockBuilder(SqsClient::class)
->disableOriginalConstructor()
->onlyMethods(['getQueueAttributes'])
->getMock();

$sqs->expects(self::once())
->method('getQueueAttributes')
->with(self::callback(function (array $input) {
if ($input['QueueUrl'] !== $this->queue) {
return false;
}

if (!isset($input['AttributeNames'])) {
return false;
}

if ([QueueAttributeName::APPROXIMATE_NUMBER_OF_MESSAGES] !== $input['AttributeNames']) {
return false;
}

return true;
}))
->willReturn($result);

$queue = new AsyncAwsSqsQueue($sqs, $this->queue);
self::assertEquals(4, $queue->size());
}

public function testPush()
{
$result = ResultMockFactory::create(SendMessageResult::class, [
'MessageId' => 'my-id',
]);

$sqs = $this->getMockBuilder(SqsClient::class)
->disableOriginalConstructor()
->onlyMethods(['sendMessage'])
->getMock();

$sqs->expects(self::once())
->method('sendMessage')
->with(self::callback(function (array $input) {
if ($input['QueueUrl'] !== $this->queue) {
return false;
}

if (!isset($input['MessageBody'])) {
return false;
}

// verify that body is serialized.
$body = json_decode($input['MessageBody'], true);
$this->assertArrayHasKey('displayName', $body);
$this->assertArrayHasKey('job', $body);
$this->assertArrayHasKey('data', $body);

if (CreateUser::class !== $body['displayName']) {
return false;
}

return true;
}))
->willReturn($result);

$queue = new AsyncAwsSqsQueue($sqs, $this->queue);
$output = $queue->push(new CreateUser('Alic', '[email protected]'));
self::assertEquals('my-id', $output);
}

public function testPushRaw()
{
$result = ResultMockFactory::create(SendMessageResult::class, [
'MessageId' => 'my-id',
]);

$sqs = $this->getMockBuilder(SqsClient::class)
->disableOriginalConstructor()
->onlyMethods(['sendMessage'])
->getMock();

$sqs->expects(self::once())
->method('sendMessage')
->with(self::callback(function (array $input) {
if ($input['QueueUrl'] !== $this->queue) {
return false;
}

if (!isset($input['MessageBody'])) {
return false;
}

if ('payload' !== $input['MessageBody']) {
return false;
}

return true;
}))
->willReturn($result);

$queue = new AsyncAwsSqsQueue($sqs, $this->queue);
self::assertEquals('my-id', $queue->pushRaw('payload'));
}

public function testLater()
{
$result = ResultMockFactory::create(SendMessageResult::class, [
'MessageId' => 'my-id',
]);

$sqs = $this->getMockBuilder(SqsClient::class)
->disableOriginalConstructor()
->onlyMethods(['sendMessage'])
->getMock();

$sqs->expects(self::once())
->method('sendMessage')
->with(self::callback(function (array $input) {
if ($input['QueueUrl'] !== $this->queue) {
return false;
}

if (47 !== $input['DelaySeconds']) {
return false;
}

if (!isset($input['MessageBody'])) {
return false;
}

if (false === strpos($input['MessageBody'], 'my-custom-payload')) {
return false;
}

return true;
}))
->willReturn($result);

$queue = new AsyncAwsSqsQueue($sqs, $this->queue);
self::assertEquals('my-id', $queue->later(47, 'my-custom-payload'));
}

public function testPop()
{
$data = [
'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]\";}"}}',
'MessageId' => 'message-id-example',
'Attributes' => ['ApproximateReceiveCount' => '3'],
'ReceiptHandle' => '123',
];
$message = new Message($data);

$result = ResultMockFactory::create(ReceiveMessageResult::class, [
'Messages' => [$message],
]);

$sqs = $this->getMockBuilder(SqsClient::class)
->disableOriginalConstructor()
->onlyMethods(['receiveMessage'])
->getMock();

$sqs->expects(self::once())
->method('receiveMessage')
->with(self::callback(function (array $input) {
if ($input['QueueUrl'] !== $this->queue) {
return false;
}

if (!isset($input['AttributeNames'])) {
return false;
}

if ([MessageSystemAttributeName::APPROXIMATE_RECEIVE_COUNT] !== $input['AttributeNames']) {
return false;
}

return true;
}))
->willReturn($result);

$queue = new AsyncAwsSqsQueue($sqs, $this->queue);
$queue->setContainer(new Container());
$queue->setConnectionName('connection-name');
$job = $queue->pop();

self::assertInstanceOf(AsyncAwsSqsJob::class, $job);
self::assertEquals($data, $job->getSqsJob());
}
}
Loading

0 comments on commit af10009

Please sign in to comment.