1111use Nipwaayoni \SnsHandler \SnsHttpRequest ;
1212use Nipwaayoni \SnsHandler \SnsMessage ;
1313use Nipwaayoni \SnsHandler \SnsUnknownTopicArnException ;
14+ use Nipwaayoni \Tests \SnsHandler \Events \SnsConfirmationRequestAlphaReceived ;
15+ use Nipwaayoni \Tests \SnsHandler \Events \SnsConfirmationRequestBetaReceived ;
1416use Nipwaayoni \Tests \SnsHandler \Events \SnsMessageAlphaReceived ;
1517use Nipwaayoni \Tests \SnsHandler \Events \SnsMessageBetaReceived ;
1618use PHPUnit \Framework \MockObject \MockObject ;
@@ -62,7 +64,20 @@ public function testThrowsExceptionForUnknownMessageType(): void
6264 Event::assertNotDispatched (SnsMessageReceived::class);
6365 }
6466
65- public function testDispatchesSnsConfirmationRequestEvent (): void
67+ public function testValidatesSnsMessage (): void
68+ {
69+ $ request = $ this ->createMock (SnsHttpRequest::class);
70+ $ request ->expects ($ this ->once ())->method ('jsonContent ' )
71+ ->willReturn ($ this ->makeSnsMessageJson ([
72+ 'MessageId ' => 'abc123 ' ,
73+ ]));
74+
75+ $ this ->validator ->expects ($ this ->once ())->method ('validate ' );
76+
77+ $ this ->broker ->handleRequest ($ request );
78+ }
79+
80+ public function testDispatchesDefaultConfirmationRequestEvent (): void
6681 {
6782 $ request = $ this ->createMock (SnsHttpRequest::class);
6883 $ request ->expects ($ this ->once ())->method ('jsonContent ' )
@@ -75,32 +90,84 @@ public function testDispatchesSnsConfirmationRequestEvent(): void
7590 Event::assertDispatched (SnsConfirmationRequestReceived::class);
7691 }
7792
78- public function testDispatchesDefaultNotificationMessage (): void
93+ public function testDispatchesMappedConfirmationRequestEvent (): void
7994 {
95+ $ this ->configValues ['confirmation-events ' ] = [
96+ SnsConfirmationRequestAlphaReceived::class => ['arn:aws:sns:us-west-2:123456789012:AlphaTopic ' ],
97+ SnsConfirmationRequestBetaReceived::class => ['arn:aws:sns:us-west-2:123456789012:AlphaTopic ' ],
98+ SnsConfirmationRequestReceived::class => ['* ' ],
99+ ];
100+
80101 $ request = $ this ->createMock (SnsHttpRequest::class);
81102 $ request ->expects ($ this ->once ())->method ('jsonContent ' )
82103 ->willReturn ($ this ->makeSnsMessageJson ([
83- 'MessageId ' => 'abc123 ' ,
104+ 'Type ' => SnsMessage::SUBSCRIBE_TYPE ,
105+ 'SubscribeURL ' => 'https://aws.amazon.com/subscribe/123 ' ,
106+ 'TopicArn ' => 'arn:aws:sns:us-west-2:123456789012:AlphaTopic '
84107 ]));
85108
86109 $ this ->broker ->handleRequest ($ request );
87- Event::assertDispatched (SnsMessageReceived::class);
110+ Event::assertDispatched (SnsConfirmationRequestAlphaReceived::class);
111+ Event::assertNotDispatched (SnsConfirmationRequestReceived::class);
88112 }
89113
90- public function testValidatesSnsMessage (): void
114+ public function testDispatchFirstMappedConfirmationEvent (): void
91115 {
116+ $ this ->configValues ['confirmation-events ' ] = [
117+ SnsConfirmationRequestAlphaReceived::class => ['arn:aws:sns:us-west-2:123456789012:AlphaTopic ' ],
118+ SnsConfirmationRequestBetaReceived::class => ['arn:aws:sns:us-west-2:123456789012:AlphaTopic ' ],
119+ SnsConfirmationRequestReceived::class => ['* ' ],
120+ ];
121+
92122 $ request = $ this ->createMock (SnsHttpRequest::class);
93123 $ request ->expects ($ this ->once ())->method ('jsonContent ' )
94124 ->willReturn ($ this ->makeSnsMessageJson ([
95- 'MessageId ' => 'abc123 ' ,
125+ 'Type ' => SnsMessage::SUBSCRIBE_TYPE ,
126+ 'SubscribeURL ' => 'https://aws.amazon.com/subscribe/123 ' ,
127+ 'TopicArn ' => 'arn:aws:sns:us-west-2:123456789012:AlphaTopic '
96128 ]));
97129
98- $ this ->validator ->expects ($ this ->once ())->method ('validate ' );
130+ $ this ->broker ->handleRequest ($ request );
131+ Event::assertDispatched (SnsConfirmationRequestAlphaReceived::class);
132+ Event::assertNotDispatched (SnsConfirmationRequestBetaReceived::class);
133+ Event::assertNotDispatched (SnsConfirmationRequestReceived::class);
134+ }
135+
136+ public function testRejectsWithUnhandledTopicArnOnConfirmation (): void
137+ {
138+ $ this ->configValues ['confirmation-events ' ] = [
139+ SnsConfirmationRequestBetaReceived::class => ['arn:aws:sns:us-west-2:123456789012:BetaTopic ' ],
140+ ];
141+
142+ $ request = $ this ->createMock (SnsHttpRequest::class);
143+ $ request ->expects ($ this ->once ())->method ('jsonContent ' )
144+ ->willReturn ($ this ->makeSnsMessageJson ([
145+ 'Type ' => SnsMessage::SUBSCRIBE_TYPE ,
146+ 'SubscribeURL ' => 'https://aws.amazon.com/subscribe/123 ' ,
147+ 'TopicArn ' => 'arn:aws:sns:us-west-2:123456789012:AlphaTopic '
148+ ]));
149+
150+ $ this ->expectException (SnsUnknownTopicArnException::class);
151+ $ this ->expectExceptionMessage ('Unmappable TopicArn: arn:aws:sns:us-west-2:123456789012:AlphaTopic ' );
152+
153+ $ this ->broker ->handleRequest ($ request );
154+
155+ Event::assertNotDispatched (SnsConfirmationRequestReceived::class);
156+ }
157+
158+ public function testDispatchesDefaultNotificationEvent (): void
159+ {
160+ $ request = $ this ->createMock (SnsHttpRequest::class);
161+ $ request ->expects ($ this ->once ())->method ('jsonContent ' )
162+ ->willReturn ($ this ->makeSnsMessageJson ([
163+ 'MessageId ' => 'abc123 ' ,
164+ ]));
99165
100166 $ this ->broker ->handleRequest ($ request );
167+ Event::assertDispatched (SnsMessageReceived::class);
101168 }
102169
103- public function testDispatchMappedNotificationMessage (): void
170+ public function testDispatchMappedNotificationEvent (): void
104171 {
105172 $ this ->configValues ['message-events ' ] = [
106173 SnsMessageAlphaReceived::class => ['arn:aws:sns:us-west-2:123456789012:AlphaTopic ' ],
@@ -120,7 +187,7 @@ public function testDispatchMappedNotificationMessage(): void
120187 Event::assertNotDispatched (SnsMessageReceived::class);
121188 }
122189
123- public function testDispatchFirstMappedNotificationMessage (): void
190+ public function testDispatchFirstMappedNotificationEvent (): void
124191 {
125192 $ this ->configValues ['message-events ' ] = [
126193 SnsMessageAlphaReceived::class => ['arn:aws:sns:us-west-2:123456789012:AlphaTopic ' ],
@@ -141,7 +208,7 @@ public function testDispatchFirstMappedNotificationMessage(): void
141208 Event::assertNotDispatched (SnsMessageReceived::class);
142209 }
143210
144- public function testRejectsMessageWithUnhandledTopicArn (): void
211+ public function testRejectsWithUnhandledTopicArnOnMessage (): void
145212 {
146213 $ this ->configValues ['message-events ' ] = [
147214 SnsMessageBetaReceived::class => ['arn:aws:sns:us-west-2:123456789012:BetaTopic ' ],
0 commit comments