Skip to content

Add setting subscription attributes to Sns and SnsQs #1281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@
"ext-mongo": "1.6.14",
"ext-sockets": "1"
},
"prefer-stable": true
"prefer-stable": true,
"allow-plugins": {
"php-http/discovery": false
}
}
}
12 changes: 6 additions & 6 deletions pkg/sns/SnsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public function unsubscribe(array $args): Result
return $this->callApi('unsubscribe', $args);
}

public function setSubscriptionAttributes(array $args): Result
{
return $this->callApi('setSubscriptionAttributes', $args);
}

public function listSubscriptionsByTopic(array $args): Result
{
return $this->callApi('ListSubscriptionsByTopic', $args);
Expand Down Expand Up @@ -135,11 +140,6 @@ private function resolveClient(): void
}
}

throw new \LogicException(sprintf(
'The input client must be an instance of "%s" or "%s" or a callable that returns one of those. Got "%s"',
AwsSnsClient::class,
MultiRegionClient::class,
is_object($client) ? get_class($client) : gettype($client)
));
throw new \LogicException(sprintf('The input client must be an instance of "%s" or "%s" or a callable that returns one of those. Got "%s"', AwsSnsClient::class, MultiRegionClient::class, is_object($client) ? get_class($client) : gettype($client)));
}
}
10 changes: 10 additions & 0 deletions pkg/sns/SnsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ public function getSubscriptions(SnsDestination $destination): array
return $subscriptions;
}

public function setSubscriptionAttributes(SnsSubscribe $subscribe): void
{
foreach ($this->getSubscriptions($subscribe->getTopic()) as $subscription) {
$this->client->setSubscriptionAttributes(array_merge(
$subscribe->getAttributes(),
['SubscriptionArn' => $subscription['SubscriptionArn']],
));
}
}

public function getTopicArn(SnsDestination $destination): string
{
if (false == array_key_exists($destination->getTopicName(), $this->topicArns)) {
Expand Down
31 changes: 30 additions & 1 deletion pkg/sns/Tests/Spec/SnsContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,49 @@

namespace Enqueue\Sns\Tests\Spec;

use Aws\Result;
use Enqueue\Sns\SnsClient;
use Enqueue\Sns\SnsContext;
use Enqueue\Sns\SnsDestination;
use Enqueue\Sns\SnsSubscribe;
use Interop\Queue\Spec\ContextSpec;

class SnsContextTest extends ContextSpec
{
public function testShouldCreateConsumerOnCreateConsumerMethodCall()
public function testShouldCreateConsumerOnCreateConsumerMethodCall(): void
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('SNS transport does not support consumption. You should consider using SQS instead.');

parent::testShouldCreateConsumerOnCreateConsumerMethodCall();
}

public function testSetsSubscriptionAttributes(): void
{
$client = $this->createMock(SnsClient::class);
$client->expects($this->once())
->method('listSubscriptionsByTopic')
->willReturn(new Result(['Subscriptions' => [
['SubscriptionArn' => 'arn1'],
['SubscriptionArn' => 'arn2'],
]]));
$client->expects($this->exactly(2))
->method('setSubscriptionAttributes')
->withConsecutive(
[$this->equalTo(['attr1' => 'value1', 'SubscriptionArn' => 'arn1'])],
[$this->equalTo(['attr1' => 'value1', 'SubscriptionArn' => 'arn2'])],
);

$context = new SnsContext($client, ['topic_arns' => ['topic1' => 'topicArn1']]);
$context->setSubscriptionAttributes(new SnsSubscribe(
new SnsDestination('topic1'),
'endpoint1',
'protocol1',
false,
['attr1' => 'value1'],
));
}

protected function createContext()
{
$client = $this->createMock(SnsClient::class);
Expand Down
11 changes: 11 additions & 0 deletions pkg/snsqs/SnsQsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ public function close(): void
$this->getSqsContext()->close();
}

public function setSubscriptionAttributes(SnsQsTopic $topic, SnsQsQueue $queue, array $attributes): void
{
$this->getSnsContext()->setSubscriptionAttributes(new SnsSubscribe(
$topic,
$this->getSqsContext()->getQueueArn($queue),
SnsSubscribe::PROTOCOL_SQS,
false,
$attributes,
));
}

private function getSnsContext(): SnsContext
{
if (null === $this->snsContext) {
Expand Down
34 changes: 34 additions & 0 deletions pkg/snsqs/Tests/Spec/SnsQsContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,47 @@
namespace Enqueue\SnsQs\Tests\Spec;

use Enqueue\Sns\SnsContext;
use Enqueue\Sns\SnsSubscribe;
use Enqueue\SnsQs\SnsQsContext;
use Enqueue\SnsQs\SnsQsQueue;
use Enqueue\SnsQs\SnsQsTopic;
use Enqueue\Sqs\SqsConsumer;
use Enqueue\Sqs\SqsContext;
use Interop\Queue\Spec\ContextSpec;

class SnsQsContextTest extends ContextSpec
{
public function testSetsSubscriptionAttributes(): void
{
$topic = new SnsQsTopic('topic1');

$snsContext = $this->createMock(SnsContext::class);
$snsContext->expects($this->once())
->method('setSubscriptionAttributes')
->with($this->equalTo(new SnsSubscribe(
$topic,
'queueArn1',
'sqs',
false,
['attr1' => 'value1'],
)));

$sqsContext = $this->createMock(SqsContext::class);
$sqsContext->expects($this->any())
->method('createConsumer')
->willReturn($this->createMock(SqsConsumer::class));
$sqsContext->expects($this->any())
->method('getQueueArn')
->willReturn('queueArn1');

$context = new SnsQsContext($snsContext, $sqsContext);
$context->setSubscriptionAttributes(
$topic,
new SnsQsQueue('queue1'),
['attr1' => 'value1'],
);
}

protected function createContext()
{
$sqsContext = $this->createMock(SqsContext::class);
Expand Down