1212namespace Symfony \Component \Notifier \Bridge \OneSignal \Tests ;
1313
1414use Symfony \Component \HttpClient \MockHttpClient ;
15+ use Symfony \Component \HttpClient \Response \JsonMockResponse ;
1516use Symfony \Component \Notifier \Bridge \OneSignal \OneSignalOptions ;
1617use Symfony \Component \Notifier \Bridge \OneSignal \OneSignalTransport ;
1718use Symfony \Component \Notifier \Exception \LogicException ;
@@ -84,15 +85,7 @@ public function testSendThrowsWithoutRecipient()
8485
8586public function testSendWithErrorResponseThrows ()
8687 {
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 ]));
9689
9790$ transport =self ::createTransport ($ client ,'ea345989-d273-4f21-a33b-0c006efc5edb ' );
9891
@@ -104,15 +97,7 @@ public function testSendWithErrorResponseThrows()
10497
10598public function testSendWithErrorResponseThrowsWhenAllUnsubscribed ()
10699 {
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 ' ]]));
116101
117102$ transport =self ::createTransport ($ client ,'ea345989-d273-4f21-a33b-0c006efc5edb ' );
118103
@@ -124,20 +109,12 @@ public function testSendWithErrorResponseThrowsWhenAllUnsubscribed()
124109
125110public function testSend ()
126111 {
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 ' ]]);
134113
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 {
138115$ this ->assertJsonStringEqualsJsonString ($ expectedBody ,$ options ['body ' ]);
139116
140- return $ response ;
117+ return new JsonMockResponse ([ ' id ' => ' b98881cc-1e94-4366-bbd9-db8f3429292b ' , ' recipients ' => 1 , ' external_id ' => null ]) ;
141118 });
142119
143120$ transport =self ::createTransport ($ client ,'ea345989-d273-4f21-a33b-0c006efc5edb ' );
@@ -146,4 +123,24 @@ public function testSend()
146123
147124$ this ->assertSame ('b98881cc-1e94-4366-bbd9-db8f3429292b ' ,$ sentMessage ->getMessageId ());
148125 }
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+ }
149146}