Skip to content

Commit 9324a7f

Browse files
authored
Merge pull request #1281 from andrewmy/add-sns-subscription-attrs
Add setting subscription attributes to Sns and SnsQs
2 parents a2b6082 + 6545ef7 commit 9324a7f

File tree

6 files changed

+95
-8
lines changed

6 files changed

+95
-8
lines changed

composer.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@
130130
"ext-mongo": "1.6.14",
131131
"ext-sockets": "1"
132132
},
133-
"prefer-stable": true
133+
"prefer-stable": true,
134+
"allow-plugins": {
135+
"php-http/discovery": false
136+
}
134137
}
135138
}

pkg/sns/SnsClient.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public function unsubscribe(array $args): Result
6060
return $this->callApi('unsubscribe', $args);
6161
}
6262

63+
public function setSubscriptionAttributes(array $args): Result
64+
{
65+
return $this->callApi('setSubscriptionAttributes', $args);
66+
}
67+
6368
public function listSubscriptionsByTopic(array $args): Result
6469
{
6570
return $this->callApi('ListSubscriptionsByTopic', $args);
@@ -135,11 +140,6 @@ private function resolveClient(): void
135140
}
136141
}
137142

138-
throw new \LogicException(sprintf(
139-
'The input client must be an instance of "%s" or "%s" or a callable that returns one of those. Got "%s"',
140-
AwsSnsClient::class,
141-
MultiRegionClient::class,
142-
is_object($client) ? get_class($client) : gettype($client)
143-
));
143+
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)));
144144
}
145145
}

pkg/sns/SnsContext.php

+10
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ public function getSubscriptions(SnsDestination $destination): array
145145
return $subscriptions;
146146
}
147147

148+
public function setSubscriptionAttributes(SnsSubscribe $subscribe): void
149+
{
150+
foreach ($this->getSubscriptions($subscribe->getTopic()) as $subscription) {
151+
$this->client->setSubscriptionAttributes(array_merge(
152+
$subscribe->getAttributes(),
153+
['SubscriptionArn' => $subscription['SubscriptionArn']],
154+
));
155+
}
156+
}
157+
148158
public function getTopicArn(SnsDestination $destination): string
149159
{
150160
if (false == array_key_exists($destination->getTopicName(), $this->topicArns)) {

pkg/sns/Tests/Spec/SnsContextTest.php

+30-1
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,49 @@
22

33
namespace Enqueue\Sns\Tests\Spec;
44

5+
use Aws\Result;
56
use Enqueue\Sns\SnsClient;
67
use Enqueue\Sns\SnsContext;
8+
use Enqueue\Sns\SnsDestination;
9+
use Enqueue\Sns\SnsSubscribe;
710
use Interop\Queue\Spec\ContextSpec;
811

912
class SnsContextTest extends ContextSpec
1013
{
11-
public function testShouldCreateConsumerOnCreateConsumerMethodCall()
14+
public function testShouldCreateConsumerOnCreateConsumerMethodCall(): void
1215
{
1316
$this->expectException(\LogicException::class);
1417
$this->expectExceptionMessage('SNS transport does not support consumption. You should consider using SQS instead.');
1518

1619
parent::testShouldCreateConsumerOnCreateConsumerMethodCall();
1720
}
1821

22+
public function testSetsSubscriptionAttributes(): void
23+
{
24+
$client = $this->createMock(SnsClient::class);
25+
$client->expects($this->once())
26+
->method('listSubscriptionsByTopic')
27+
->willReturn(new Result(['Subscriptions' => [
28+
['SubscriptionArn' => 'arn1'],
29+
['SubscriptionArn' => 'arn2'],
30+
]]));
31+
$client->expects($this->exactly(2))
32+
->method('setSubscriptionAttributes')
33+
->withConsecutive(
34+
[$this->equalTo(['attr1' => 'value1', 'SubscriptionArn' => 'arn1'])],
35+
[$this->equalTo(['attr1' => 'value1', 'SubscriptionArn' => 'arn2'])],
36+
);
37+
38+
$context = new SnsContext($client, ['topic_arns' => ['topic1' => 'topicArn1']]);
39+
$context->setSubscriptionAttributes(new SnsSubscribe(
40+
new SnsDestination('topic1'),
41+
'endpoint1',
42+
'protocol1',
43+
false,
44+
['attr1' => 'value1'],
45+
));
46+
}
47+
1948
protected function createContext()
2049
{
2150
$client = $this->createMock(SnsClient::class);

pkg/snsqs/SnsQsContext.php

+11
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,17 @@ public function close(): void
173173
$this->getSqsContext()->close();
174174
}
175175

176+
public function setSubscriptionAttributes(SnsQsTopic $topic, SnsQsQueue $queue, array $attributes): void
177+
{
178+
$this->getSnsContext()->setSubscriptionAttributes(new SnsSubscribe(
179+
$topic,
180+
$this->getSqsContext()->getQueueArn($queue),
181+
SnsSubscribe::PROTOCOL_SQS,
182+
false,
183+
$attributes,
184+
));
185+
}
186+
176187
private function getSnsContext(): SnsContext
177188
{
178189
if (null === $this->snsContext) {

pkg/snsqs/Tests/Spec/SnsQsContextTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,47 @@
33
namespace Enqueue\SnsQs\Tests\Spec;
44

55
use Enqueue\Sns\SnsContext;
6+
use Enqueue\Sns\SnsSubscribe;
67
use Enqueue\SnsQs\SnsQsContext;
8+
use Enqueue\SnsQs\SnsQsQueue;
9+
use Enqueue\SnsQs\SnsQsTopic;
710
use Enqueue\Sqs\SqsConsumer;
811
use Enqueue\Sqs\SqsContext;
912
use Interop\Queue\Spec\ContextSpec;
1013

1114
class SnsQsContextTest extends ContextSpec
1215
{
16+
public function testSetsSubscriptionAttributes(): void
17+
{
18+
$topic = new SnsQsTopic('topic1');
19+
20+
$snsContext = $this->createMock(SnsContext::class);
21+
$snsContext->expects($this->once())
22+
->method('setSubscriptionAttributes')
23+
->with($this->equalTo(new SnsSubscribe(
24+
$topic,
25+
'queueArn1',
26+
'sqs',
27+
false,
28+
['attr1' => 'value1'],
29+
)));
30+
31+
$sqsContext = $this->createMock(SqsContext::class);
32+
$sqsContext->expects($this->any())
33+
->method('createConsumer')
34+
->willReturn($this->createMock(SqsConsumer::class));
35+
$sqsContext->expects($this->any())
36+
->method('getQueueArn')
37+
->willReturn('queueArn1');
38+
39+
$context = new SnsQsContext($snsContext, $sqsContext);
40+
$context->setSubscriptionAttributes(
41+
$topic,
42+
new SnsQsQueue('queue1'),
43+
['attr1' => 'value1'],
44+
);
45+
}
46+
1347
protected function createContext()
1448
{
1549
$sqsContext = $this->createMock(SqsContext::class);

0 commit comments

Comments
 (0)