From 1470055ee1a10f592eb508ed17a968f47927360a Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Wed, 24 Jan 2018 21:51:47 +0100 Subject: [PATCH 1/3] adds route key contract --- tests/SqsSnsJobTest.php | 56 ++++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/tests/SqsSnsJobTest.php b/tests/SqsSnsJobTest.php index 527618a..8bef652 100644 --- a/tests/SqsSnsJobTest.php +++ b/tests/SqsSnsJobTest.php @@ -12,7 +12,6 @@ class SqsSnsJobTest extends TestCase { private $sqsClient; private $container; - private $sqsSnsJob; protected function setUp() { @@ -21,8 +20,12 @@ protected function setUp() ->getMock(); $this->container = $this->createMock(Container::class); + } + private function createSqsSnsJob($routes = []) + { $body = [ + 'TopicArn' => 'TopicArn:123456', 'Subject' => 'Subject#action', 'Message' => 'The Message', ]; @@ -30,11 +33,7 @@ protected function setUp() 'Body' => json_encode($body), ]; - $routes = [ - 'Subject#action' => '\\stdClass', - ]; - - $this->sqsSnsJob = new SqsSnsJob( + return new SqsSnsJob( $this->container, $this->sqsClient, $payload, @@ -44,28 +43,67 @@ protected function setUp() ); } + private function getSqsSnsJobSubjectRoute() + { + return $this->createSqsSnsJob([ + 'Subject#action' => '\\stdClass', + ]); + } + + private function getSqsSnsJobTopicRoute() + { + return $this->createSqsSnsJob([ + 'TopicArn:123456' => '\\stdClass', + ]); + } + + public function testWillResolveSqsSubscriptionJob() { - $jobPayload = $this->sqsSnsJob->payload(); + $jobPayload = $this->getSqsSnsJobSubjectRoute()->payload(); $this->assertEquals('Illuminate\\Queue\\CallQueuedHandler@call', $jobPayload['job']); } public function testWillResolveSqsSubscriptionCommandName() { - $jobPayload = $this->sqsSnsJob->payload(); + $jobPayload = $this->getSqsSnsJobSubjectRoute()->payload(); $this->assertEquals('\\stdClass', $jobPayload['data']['commandName']); } public function testWillResolveSqsSubscriptionCommand() { - $jobPayload = $this->sqsSnsJob->payload(); + $jobPayload = $this->getSqsSnsJobSubjectRoute()->payload(); $expectedCommand = serialize(new \stdClass); $this->assertEquals($expectedCommand, $jobPayload['data']['command']); } + + public function testWillResolveSqsSubscriptionJobTopicRoute() + { + $jobPayload = $this->getSqsSnsJobTopicRoute()->payload(); + + $this->assertEquals('Illuminate\\Queue\\CallQueuedHandler@call', $jobPayload['job']); + } + + public function testWillResolveSqsSubscriptionCommandNameTopicRoute() + { + $jobPayload = $this->getSqsSnsJobTopicRoute()->payload(); + + $this->assertEquals('\\stdClass', $jobPayload['data']['commandName']); + } + + public function testWillResolveSqsSubscriptionCommandTopicRoute() + { + $jobPayload = $this->getSqsSnsJobTopicRoute()->payload(); + $expectedCommand = serialize(new \stdClass); + + $this->assertEquals($expectedCommand, $jobPayload['data']['command']); + } + + public function testWillLeaveDefaultSqsJobUntouched() { $body = [ From 93d9d3cec55350227c95325e784639dbd2a25cce Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Wed, 24 Jan 2018 21:52:21 +0100 Subject: [PATCH 2/3] implements route key TopicArn, Subject --- src/Queue/Jobs/SqsSnsJob.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Queue/Jobs/SqsSnsJob.php b/src/Queue/Jobs/SqsSnsJob.php index 36c9e6f..2d1b57a 100644 --- a/src/Queue/Jobs/SqsSnsJob.php +++ b/src/Queue/Jobs/SqsSnsJob.php @@ -37,10 +37,20 @@ private function resolveSnsSubscription(array $job, array $routes) { $body = json_decode($job['Body'], true); - if (isset($body['Subject']) && array_key_exists($body['Subject'], $routes)) { - // Find name of command in queue routes - $commandName = $routes[$body['Subject']]; + $commandName = null; + // available parameters to route your jobs by + $possibleRouteParams = ['TopicArn', 'Subject']; + + foreach ($possibleRouteParams as $param) { + if (isset($body[$param]) && array_key_exists($body[$param], $routes)) { + // Find name of command in queue routes using the param field + $commandName = $routes[$body[$param]]; + break; + } + } + + if ($commandName !== null) { // restructure job body $job['Body'] = json_encode([ 'job' => CallQueuedHandler::class . '@call', From ac860ea7f5d661f9315aa35287af913a5192e7c0 Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Wed, 24 Jan 2018 21:52:41 +0100 Subject: [PATCH 3/3] explains route configuration --- readme.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index ee0c508..48b73d3 100644 --- a/readme.md +++ b/readme.md @@ -29,8 +29,11 @@ You'll need to configure the queue connection in your config/queue.php 'queue' => env('QUEUE_URL', 'your-queue-url'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), 'routes' => [ - // specify routes to your queue jobs eg. + // you can use the "Subject" field 'Subject' => 'App\\Jobs\\YourJob', + // or the "TopicArn" of your SQS message + 'TopicArn:123' => 'App\\Jobs\\YourJob', + // to specify which job class should handle the job ], ], ], @@ -39,6 +42,7 @@ You'll need to configure the queue connection in your config/queue.php Once the sqs-sns queue connector is configured you can start using it by setting your queue driver to 'sqs-sns'. + ## Installation The best way to install laravel-sqs-sns-subscription is by using [Composer](http://getcomposer.org/).