Skip to content
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

Get the eventSourceArn from $snsRecord->getTopicArn instead of getEve… #87

Merged
merged 1 commit into from
Jun 17, 2024
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
6 changes: 3 additions & 3 deletions src/Service/Sns/SnsTransportNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public function __construct(
/** @throws InvalidArgumentException */
public function __invoke(SnsRecord $snsRecord): string
{
if (!array_key_exists('EventSubscriptionArn', $snsRecord->toArray())) {
throw new InvalidArgumentException('EventSubscriptionArn is missing in sns record.');
if (!array_key_exists('TopicArn', $snsRecord->toArray()['Sns'])) {
throw new InvalidArgumentException('TopicArn is missing in sns record.');
}

$eventSourceArn = $snsRecord->getEventSubscriptionArn();
$eventSourceArn = $snsRecord->getTopicArn();

return $this->configurationProvider->provideTransportFromEventSource(self::TRANSPORT_PROTOCOL . $eventSourceArn);
}
Expand Down
35 changes: 33 additions & 2 deletions tests/Unit/Service/Sns/SnsTransportNameResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Bref\Event\Sns\SnsEvent;
use Bref\Symfony\Messenger\Service\MessengerTransportConfiguration;
use Bref\Symfony\Messenger\Service\Sns\SnsTransportNameResolver;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
Expand All @@ -26,9 +27,7 @@ public function test_event_source_can_resolved_as_expected(): void
$event = new SnsEvent([
'Records' => [
[

'EventSource'=>'aws:sns',
'EventSubscriptionArn' => 'arn:aws:sns:us-east-1:1234567890:async',
'Sns' => [
'Message' => 'Test message.',
'MessageAttributes' => [
Expand All @@ -37,11 +36,43 @@ public function test_event_source_can_resolved_as_expected(): void
'Value'=> ['Content-Type' => 'application/json'],
],
],
'TopicArn' => 'arn:aws:sns:us-east-1:1234567890:async',
],
],
],
]);

self::assertSame('async', ($transportNameResolver)($event->getRecords()[0]));
}

public function test_throws_exception_if_topic_arn_deos_not_exist(): void
{
$messengerTransportConfiguration = $this->prophesize(MessengerTransportConfiguration::class);
/** @phpstan-ignore-next-line */
$messengerTransportConfiguration
->provideTransportFromEventSource(Argument::cetera())
->willReturn('async');

$transportNameResolver = new SnsTransportNameResolver($messengerTransportConfiguration->reveal());

$eventWithMissingTopicArn = new SnsEvent([
'Records' => [
[
'EventSource'=>'aws:sns',
'Sns' => [
'Message' => 'Test message.',
'MessageAttributes' => [
'Headers' => [
'Type'=> 'String',
'Value'=> ['Content-Type' => 'application/json'],
],
],
],
],
],
]);

$this->expectException(InvalidArgumentException::class);
($transportNameResolver)($eventWithMissingTopicArn->getRecords()[0]);
}
}