12
12
namespace Symfony \Component \Notifier \Bridge \OneSignal \Tests ;
13
13
14
14
use Symfony \Component \HttpClient \MockHttpClient ;
15
+ use Symfony \Component \HttpClient \Response \JsonMockResponse ;
15
16
use Symfony \Component \Notifier \Bridge \OneSignal \OneSignalOptions ;
16
17
use Symfony \Component \Notifier \Bridge \OneSignal \OneSignalTransport ;
17
18
use Symfony \Component \Notifier \Exception \LogicException ;
@@ -84,15 +85,7 @@ public function testSendThrowsWithoutRecipient()
84
85
85
86
public function testSendWithErrorResponseThrows ()
86
87
{
87
- $ response = $ this ->createMock (ResponseInterface::class);
88
- $ response ->expects ($ this ->exactly (2 ))
89
- ->method ('getStatusCode ' )
90
- ->willReturn (400 );
91
- $ response ->expects ($ this ->once ())
92
- ->method ('getContent ' )
93
- ->willReturn (json_encode (['errors ' => ['Message Notifications must have English language content ' ]]));
94
-
95
- $ client = new MockHttpClient (static fn (): ResponseInterface => $ response );
88
+ $ client = new MockHttpClient (new JsonMockResponse (['errors ' => ['Message Notifications must have English language content ' ]],['http_code ' => 400 ]));
96
89
97
90
$ transport = self ::createTransport ($ client , 'ea345989-d273-4f21-a33b-0c006efc5edb ' );
98
91
@@ -104,15 +97,7 @@ public function testSendWithErrorResponseThrows()
104
97
105
98
public function testSendWithErrorResponseThrowsWhenAllUnsubscribed ()
106
99
{
107
- $ response = $ this ->createMock (ResponseInterface::class);
108
- $ response ->expects ($ this ->exactly (2 ))
109
- ->method ('getStatusCode ' )
110
- ->willReturn (200 );
111
- $ response ->expects ($ this ->once ())
112
- ->method ('getContent ' )
113
- ->willReturn (json_encode (['id ' => '' , 'recipients ' => 0 , 'errors ' => ['All included players are not subscribed ' ]]));
114
-
115
- $ client = new MockHttpClient (static fn (): ResponseInterface => $ response );
100
+ $ client = new MockHttpClient (new JsonMockResponse (['id ' => '' , 'recipients ' => 0 , 'errors ' => ['All included players are not subscribed ' ]]));
116
101
117
102
$ transport = self ::createTransport ($ client , 'ea345989-d273-4f21-a33b-0c006efc5edb ' );
118
103
@@ -124,20 +109,12 @@ public function testSendWithErrorResponseThrowsWhenAllUnsubscribed()
124
109
125
110
public function testSend ()
126
111
{
127
- $ response = $ this ->createMock (ResponseInterface::class);
128
- $ response ->expects ($ this ->exactly (2 ))
129
- ->method ('getStatusCode ' )
130
- ->willReturn (200 );
131
- $ response ->expects ($ this ->once ())
132
- ->method ('getContent ' )
133
- ->willReturn (json_encode (['id ' => 'b98881cc-1e94-4366-bbd9-db8f3429292b ' , 'recipients ' => 1 , 'external_id ' => null ]));
112
+ $ expectedBody = json_encode (['app_id ' => '9fb175f0-0b32-4e99-ae97-bd228b9eb246 ' , 'headings ' => ['en ' => 'Hello ' ], 'contents ' => ['en ' => 'World ' ], 'include_subscription_ids ' => ['ea345989-d273-4f21-a33b-0c006efc5edb ' ]]);
134
113
135
- $ expectedBody = json_encode (['app_id ' => '9fb175f0-0b32-4e99-ae97-bd228b9eb246 ' , 'headings ' => ['en ' => 'Hello ' ], 'contents ' => ['en ' => 'World ' ], 'include_player_ids ' => ['ea345989-d273-4f21-a33b-0c006efc5edb ' ]]);
136
-
137
- $ client = new MockHttpClient (function (string $ method , string $ url , array $ options = []) use ($ response , $ expectedBody ): ResponseInterface {
114
+ $ client = new MockHttpClient (function (string $ method , string $ url , array $ options = []) use ($ expectedBody ): ResponseInterface {
138
115
$ this ->assertJsonStringEqualsJsonString ($ expectedBody , $ options ['body ' ]);
139
116
140
- return $ response ;
117
+ return new JsonMockResponse ([ ' id ' => ' b98881cc-1e94-4366-bbd9-db8f3429292b ' , ' recipients ' => 1 , ' external_id ' => null ]) ;
141
118
});
142
119
143
120
$ transport = self ::createTransport ($ client , 'ea345989-d273-4f21-a33b-0c006efc5edb ' );
@@ -146,4 +123,24 @@ public function testSend()
146
123
147
124
$ this ->assertSame ('b98881cc-1e94-4366-bbd9-db8f3429292b ' , $ sentMessage ->getMessageId ());
148
125
}
126
+
127
+ public function testSendExternalIds ()
128
+ {
129
+ $ expectedBody = json_encode (['app_id ' => '9fb175f0-0b32-4e99-ae97-bd228b9eb246 ' , 'headings ' => ['en ' => 'Hello ' ], 'contents ' => ['en ' => 'World ' ], 'include_aliases ' => ['external_id ' => ['ea345989-d273-4f21-a33b-0c006efc5edb ' ]], 'target_channel ' => 'push ' ]);
130
+
131
+ $ client = new MockHttpClient (function (string $ method , string $ url , array $ options = []) use ($ expectedBody ): ResponseInterface {
132
+ $ this ->assertJsonStringEqualsJsonString ($ expectedBody , $ options ['body ' ]);
133
+
134
+ return new JsonMockResponse (['id ' => 'b98881cc-1e94-4366-bbd9-db8f3429292b ' , 'recipients ' => 1 , 'external_id ' => null ]);
135
+ });
136
+
137
+ $ transport = self ::createTransport ($ client , 'ea345989-d273-4f21-a33b-0c006efc5edb ' );
138
+
139
+ $ options = new OneSignalOptions ();
140
+ $ options ->isExternalUserId ();
141
+
142
+ $ sentMessage = $ transport ->send (new PushMessage ('Hello ' , 'World ' , $ options ));
143
+
144
+ $ this ->assertSame ('b98881cc-1e94-4366-bbd9-db8f3429292b ' , $ sentMessage ->getMessageId ());
145
+ }
149
146
}
0 commit comments