Skip to content

Commit

Permalink
Merge pull request #5 from joblocal/feature-variable-route-key
Browse files Browse the repository at this point in the history
Variable route key (Subject, TopicArn)
  • Loading branch information
jayhof authored Jan 24, 2018
2 parents f5d7ed9 + ac860ea commit a354082
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 13 deletions.
6 changes: 5 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
],
],
],
Expand All @@ -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/).
Expand Down
16 changes: 13 additions & 3 deletions src/Queue/Jobs/SqsSnsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
56 changes: 47 additions & 9 deletions tests/SqsSnsJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class SqsSnsJobTest extends TestCase
{
private $sqsClient;
private $container;
private $sqsSnsJob;

protected function setUp()
{
Expand All @@ -21,20 +20,20 @@ protected function setUp()
->getMock();

$this->container = $this->createMock(Container::class);
}

private function createSqsSnsJob($routes = [])
{
$body = [
'TopicArn' => 'TopicArn:123456',
'Subject' => 'Subject#action',
'Message' => 'The Message',
];
$payload = [
'Body' => json_encode($body),
];

$routes = [
'Subject#action' => '\\stdClass',
];

$this->sqsSnsJob = new SqsSnsJob(
return new SqsSnsJob(
$this->container,
$this->sqsClient,
$payload,
Expand All @@ -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 = [
Expand Down

0 comments on commit a354082

Please sign in to comment.