Skip to content

Commit 78de6f8

Browse files
authored
Merge pull request #87 from SinaFetrat/fix-no-transport-found-error-for-sns-event-source
Get the eventSourceArn from $snsRecord->getTopicArn instead of getEve…
2 parents cb1b6be + 2716b27 commit 78de6f8

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

src/Service/Sns/SnsTransportNameResolver.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ public function __construct(
1919
/** @throws InvalidArgumentException */
2020
public function __invoke(SnsRecord $snsRecord): string
2121
{
22-
if (!array_key_exists('EventSubscriptionArn', $snsRecord->toArray())) {
23-
throw new InvalidArgumentException('EventSubscriptionArn is missing in sns record.');
22+
if (!array_key_exists('TopicArn', $snsRecord->toArray()['Sns'])) {
23+
throw new InvalidArgumentException('TopicArn is missing in sns record.');
2424
}
2525

26-
$eventSourceArn = $snsRecord->getEventSubscriptionArn();
26+
$eventSourceArn = $snsRecord->getTopicArn();
2727

2828
return $this->configurationProvider->provideTransportFromEventSource(self::TRANSPORT_PROTOCOL . $eventSourceArn);
2929
}

tests/Unit/Service/Sns/SnsTransportNameResolverTest.php

+33-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Bref\Event\Sns\SnsEvent;
66
use Bref\Symfony\Messenger\Service\MessengerTransportConfiguration;
77
use Bref\Symfony\Messenger\Service\Sns\SnsTransportNameResolver;
8+
use InvalidArgumentException;
89
use PHPUnit\Framework\TestCase;
910
use Prophecy\Argument;
1011
use Prophecy\PhpUnit\ProphecyTrait;
@@ -26,9 +27,7 @@ public function test_event_source_can_resolved_as_expected(): void
2627
$event = new SnsEvent([
2728
'Records' => [
2829
[
29-
3030
'EventSource'=>'aws:sns',
31-
'EventSubscriptionArn' => 'arn:aws:sns:us-east-1:1234567890:async',
3231
'Sns' => [
3332
'Message' => 'Test message.',
3433
'MessageAttributes' => [
@@ -37,11 +36,43 @@ public function test_event_source_can_resolved_as_expected(): void
3736
'Value'=> ['Content-Type' => 'application/json'],
3837
],
3938
],
39+
'TopicArn' => 'arn:aws:sns:us-east-1:1234567890:async',
4040
],
4141
],
4242
],
4343
]);
4444

4545
self::assertSame('async', ($transportNameResolver)($event->getRecords()[0]));
4646
}
47+
48+
public function test_throws_exception_if_topic_arn_deos_not_exist(): void
49+
{
50+
$messengerTransportConfiguration = $this->prophesize(MessengerTransportConfiguration::class);
51+
/** @phpstan-ignore-next-line */
52+
$messengerTransportConfiguration
53+
->provideTransportFromEventSource(Argument::cetera())
54+
->willReturn('async');
55+
56+
$transportNameResolver = new SnsTransportNameResolver($messengerTransportConfiguration->reveal());
57+
58+
$eventWithMissingTopicArn = new SnsEvent([
59+
'Records' => [
60+
[
61+
'EventSource'=>'aws:sns',
62+
'Sns' => [
63+
'Message' => 'Test message.',
64+
'MessageAttributes' => [
65+
'Headers' => [
66+
'Type'=> 'String',
67+
'Value'=> ['Content-Type' => 'application/json'],
68+
],
69+
],
70+
],
71+
],
72+
],
73+
]);
74+
75+
$this->expectException(InvalidArgumentException::class);
76+
($transportNameResolver)($eventWithMissingTopicArn->getRecords()[0]);
77+
}
4778
}

0 commit comments

Comments
 (0)