Skip to content

Commit 9fac4f9

Browse files
committed
Move ARN mapping to configuration
1 parent 47de3ec commit 9fac4f9

File tree

5 files changed

+95
-32
lines changed

5 files changed

+95
-32
lines changed

config/sns-handler.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22

33
return [
44
'validate-sns-messages' => env('VALIDATE_SNS_MESSAGES', true),
5-
'sns-class-map' => []
5+
'message-events' => [
6+
Nipwaayoni\SnsHandler\Events\SnsMessageReceived::class => ['*']
7+
],
68
];

src/SnsBroker.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Aws\Sns\Message;
77
use Aws\Sns\MessageValidator;
8+
use Illuminate\Config\Repository as Config;
89
use Illuminate\Support\Facades\Log;
910
use Nipwaayoni\SnsHandler\Events\SnsConfirmationRequestReceived;
1011
use Nipwaayoni\SnsHandler\Events\SnsMessageReceived;
@@ -21,10 +22,15 @@ class SnsBroker
2122
* @var Log
2223
*/
2324
private $log;
25+
/**
26+
* @var Config
27+
*/
28+
private $config;
2429

25-
public function __construct(MessageValidator $validator)
30+
public function __construct(MessageValidator $validator, Config $config)
2631
{
2732
$this->validator = $validator;
33+
$this->config = $config;
2834
}
2935

3036
/**
@@ -68,23 +74,35 @@ public function handleRequest(SnsHttpRequest $request): void
6874
throw new SnsException(sprintf('Unknown message type: %s', $message->type()));
6975
}
7076

71-
private function getSubscriptionEvent(string $arn)
77+
/**
78+
* @param string $arn
79+
* @return string
80+
* @throws SnsUnknownTopicArnException
81+
*/
82+
private function getSubscriptionEvent(string $arn): string
7283
{
7384
$map = [SnsConfirmationRequestReceived::class => ['*']];
7485
return $this->arnMap($arn, $map);
7586
}
7687

77-
private function getNotificationEvent(string $arn)
88+
/**
89+
* @param string $arn
90+
* @return string
91+
* @throws SnsUnknownTopicArnException
92+
*/
93+
private function getNotificationEvent(string $arn): string
7894
{
79-
$map = [
80-
SnsMessageAlphaReceived::class => ['arn:aws:sns:us-west-2:123456789012:AlphaTopic'],
81-
SnsMessageBetaReceived::class => ['arn:aws:sns:us-west-2:123456789012:AlphaTopic'],
82-
SnsMessageReceived::class => ['*'],
83-
];
95+
$map = $this->config->get('sns-handler.message-events', []);
8496
return $this->arnMap($arn, $map);
8597
}
8698

87-
private function arnMap(string $arn, array $map)
99+
/**
100+
* @param string $arn
101+
* @param array $map
102+
* @return string
103+
* @throws SnsUnknownTopicArnException
104+
*/
105+
private function arnMap(string $arn, array $map): string
88106
{
89107
$default = null;
90108
foreach ($map as $className => $arnList) {
@@ -96,6 +114,12 @@ private function arnMap(string $arn, array $map)
96114
}
97115
}
98116

117+
if (null === $default) {
118+
throw new SnsUnknownTopicArnException(sprintf('Unmappable TopicArn: %s', $arn));
119+
}
120+
121+
// TODO ensure class is dispatchable
122+
99123
return $default;
100124
}
101125
}

src/SnsUnknownTopicArnException.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33

44
namespace Nipwaayoni\SnsHandler;
55

6-
use Throwable;
7-
86
class SnsUnknownTopicArnException extends SnsException
97
{
10-
public function __construct(string $topicArn = '', $code = 0, Throwable $previous = null)
11-
{
12-
parent::__construct(sprintf('No handler registered for TopicArn %s', $topicArn), $code, $previous);
13-
}
148
}

tests/TestCase.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@
33

44
namespace Nipwaayoni\Tests\SnsHandler;
55

6+
use Illuminate\Config\Repository;
7+
use Nipwaayoni\SnsHandler\Events\SnsMessageReceived;
8+
use PHPUnit\Framework\MockObject\MockObject;
9+
610
class TestCase extends \Orchestra\Testbench\TestCase
711
{
12+
/** @var Repository|mixed|MockObject */
13+
protected $config;
14+
15+
protected $configValues = [
16+
'validate-sns-messages' => true,
17+
'message-events' => [
18+
SnsMessageReceived::class => ['*']
19+
],
20+
];
21+
22+
public function setUp(): void
23+
{
24+
parent::setUp();
25+
26+
$this->config = $this->createMock(Repository::class);
27+
$this->config->method('get')
28+
->willReturnCallback(function (string $key) {
29+
$parts = explode('.', $key);
30+
return $this->configValues[$parts[1]] ?? null;
31+
});
32+
}
833
}

tests/Unit/SnsBrokerTest.php

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,15 @@ class SnsBrokerTest extends TestCase
2727
/** @var MessageValidator|MockObject */
2828
private $validator;
2929

30-
3130
public function setUp(): void
3231
{
3332
parent::setUp();
3433

3534
Event::fake();
3635

37-
3836
$this->validator = $this->createMock(MessageValidator::class);
3937

40-
$this->broker = new SnsBroker($this->validator);
38+
$this->broker = new SnsBroker($this->validator, $this->config);
4139
}
4240

4341
public function testMakesSnsMessageFromHttpRequest(): void
@@ -51,19 +49,6 @@ public function testMakesSnsMessageFromHttpRequest(): void
5149
$this->assertEquals(SnsMessage::NOTIFICATION_TYPE, $message->type());
5250
}
5351

54-
public function testRejectsMessageWithUnknownTopicArn(): void
55-
{
56-
$this->markTestSkipped("Doesn't work with event faking and we think this test will go away after refactoring.");
57-
$request = $this->createMock(SnsHttpRequest::class);
58-
$request->expects($this->once())->method('jsonContent')
59-
->willReturn($this->makeSnsMessageJson(['TopicArn' => 'arn:aws:sns:us-west-2:123456789012:Unknown']));
60-
61-
$this->expectException(SnsUnknownTopicArnException::class);
62-
$this->expectExceptionMessage('No handler registered for TopicArn arn:aws:sns:us-west-2:123456789012:Unknown');
63-
64-
$this->broker->handleRequest($request);
65-
}
66-
6752
public function testThrowsExceptionForUnknownMessageType(): void
6853
{
6954
$request = $this->createMock(SnsHttpRequest::class);
@@ -117,6 +102,12 @@ public function testValidatesSnsMessage(): void
117102

118103
public function testDispatchMappedNotificationMessage(): void
119104
{
105+
$this->configValues['message-events'] = [
106+
SnsMessageAlphaReceived::class => ['arn:aws:sns:us-west-2:123456789012:AlphaTopic'],
107+
SnsMessageBetaReceived::class => ['arn:aws:sns:us-west-2:123456789012:AlphaTopic'],
108+
SnsMessageReceived::class => ['*'],
109+
];
110+
120111
$request = $this->createMock(SnsHttpRequest::class);
121112
$request->expects($this->once())->method('jsonContent')
122113
->willReturn($this->makeSnsMessageJson([
@@ -131,6 +122,12 @@ public function testDispatchMappedNotificationMessage(): void
131122

132123
public function testDispatchFirstMappedNotificationMessage(): void
133124
{
125+
$this->configValues['message-events'] = [
126+
SnsMessageAlphaReceived::class => ['arn:aws:sns:us-west-2:123456789012:AlphaTopic'],
127+
SnsMessageBetaReceived::class => ['arn:aws:sns:us-west-2:123456789012:AlphaTopic'],
128+
SnsMessageReceived::class => ['*'],
129+
];
130+
134131
$request = $this->createMock(SnsHttpRequest::class);
135132
$request->expects($this->once())->method('jsonContent')
136133
->willReturn($this->makeSnsMessageJson([
@@ -143,4 +140,25 @@ public function testDispatchFirstMappedNotificationMessage(): void
143140
Event::assertNotDispatched(SnsMessageBetaReceived::class);
144141
Event::assertNotDispatched(SnsMessageReceived::class);
145142
}
143+
144+
public function testRejectsMessageWithUnhandledTopicArn(): void
145+
{
146+
$this->configValues['message-events'] = [
147+
SnsMessageBetaReceived::class => ['arn:aws:sns:us-west-2:123456789012:BetaTopic'],
148+
];
149+
150+
$request = $this->createMock(SnsHttpRequest::class);
151+
$request->expects($this->once())->method('jsonContent')
152+
->willReturn($this->makeSnsMessageJson([
153+
'MessageId' => 'abc123',
154+
'TopicArn' => 'arn:aws:sns:us-west-2:123456789012:AlphaTopic'
155+
]));
156+
157+
$this->expectException(SnsUnknownTopicArnException::class);
158+
$this->expectExceptionMessage('Unmappable TopicArn: arn:aws:sns:us-west-2:123456789012:AlphaTopic');
159+
160+
$this->broker->handleRequest($request);
161+
162+
Event::assertNotDispatched(SnsMessageReceived::class);
163+
}
146164
}

0 commit comments

Comments
 (0)