@@ -60,6 +60,13 @@ public function setUp()
60
60
$ this ->responseMock ->shouldReceive ('getBody ' )->andReturn ($ responseBodyMock );
61
61
$ responseBodyMock ->shouldReceive ('__toString ' )->andReturn (json_encode ($ this ->responseBody ));
62
62
63
+ $ errorBodyMock = Mockery::mock ();
64
+ $ this ->badResponseBody = ['errors ' => []];
65
+ $ this ->badResponseMock = Mockery::mock ('Psr\Http\Message\ResponseInterface ' );
66
+ $ this ->badResponseMock ->shouldReceive ('getStatusCode ' )->andReturn (503 );
67
+ $ this ->badResponseMock ->shouldReceive ('getBody ' )->andReturn ($ errorBodyMock );
68
+ $ errorBodyMock ->shouldReceive ('__toString ' )->andReturn (json_encode ($ this ->badResponseBody ));
69
+
63
70
// exception mock up
64
71
$ exceptionResponseMock = Mockery::mock ();
65
72
$ this ->exceptionBody = ['results ' => 'failed ' ];
@@ -159,6 +166,35 @@ public function testUnsuccessfulSyncRequest()
159
166
}
160
167
}
161
168
169
+ public function testSuccessfulSyncRequestWithRetries ()
170
+ {
171
+ $ this ->clientMock ->shouldReceive ('sendRequest ' )->
172
+ with (Mockery::type ('GuzzleHttp\Psr7\Request ' ))->
173
+ andReturn ($ this ->badResponseMock , $ this ->badResponseMock , $ this ->responseMock );
174
+
175
+ $ this ->resource ->setOptions (['retries ' => 2 ]);
176
+ $ response = $ this ->resource ->syncRequest ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload );
177
+
178
+ $ this ->assertEquals ($ this ->responseBody , $ response ->getBody ());
179
+ $ this ->assertEquals (200 , $ response ->getStatusCode ());
180
+ }
181
+
182
+ public function testUnsuccessfulSyncRequestWithRetries ()
183
+ {
184
+ $ this ->clientMock ->shouldReceive ('sendRequest ' )->
185
+ once ()->
186
+ with (Mockery::type ('GuzzleHttp\Psr7\Request ' ))->
187
+ andThrow ($ this ->exceptionMock );
188
+
189
+ $ this ->resource ->setOptions (['retries ' => 2 ]);
190
+ try {
191
+ $ this ->resource ->syncRequest ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload );
192
+ } catch (\Exception $ e ) {
193
+ $ this ->assertEquals ($ this ->exceptionBody , $ e ->getBody ());
194
+ $ this ->assertEquals (500 , $ e ->getCode ());
195
+ }
196
+ }
197
+
162
198
public function testSuccessfulAsyncRequestWithWait ()
163
199
{
164
200
$ this ->promiseMock ->shouldReceive ('wait ' )->andReturn ($ this ->responseMock );
@@ -212,6 +248,46 @@ public function testUnsuccessfulAsyncRequestWithThen()
212
248
})->wait ();
213
249
}
214
250
251
+ public function testSuccessfulAsyncRequestWithRetries ()
252
+ {
253
+ $ testReq = $ this ->resource ->buildRequest ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload , []);
254
+ $ clientMock = Mockery::mock ('Http\Adapter\Guzzle6\Client ' );
255
+ $ clientMock ->shouldReceive ('sendAsyncRequest ' )->
256
+ with (Mockery::type ('GuzzleHttp\Psr7\Request ' ))->
257
+ andReturn (
258
+ new GuzzleAdapterPromise (new GuzzleFulfilledPromise ($ this ->badResponseMock ), $ testReq ),
259
+ new GuzzleAdapterPromise (new GuzzleFulfilledPromise ($ this ->badResponseMock ), $ testReq ),
260
+ new GuzzleAdapterPromise (new GuzzleFulfilledPromise ($ this ->responseMock ), $ testReq )
261
+ );
262
+
263
+ $ resource = new SparkPost ($ clientMock , ['key ' => 'SPARKPOST_API_KEY ' ]);
264
+
265
+ $ resource ->setOptions (['async ' => true , 'retries ' => 2 ]);
266
+ $ promise = $ resource ->asyncRequest ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload );
267
+ $ promise ->then (function ($ resp ) {
268
+ $ this ->assertEquals (200 , $ resp ->getStatusCode ());
269
+ })->wait ();
270
+ }
271
+
272
+ public function testUnsuccessfulAsyncRequestWithRetries ()
273
+ {
274
+ $ testReq = $ this ->resource ->buildRequest ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload , []);
275
+ $ rejectedPromise = new GuzzleRejectedPromise ($ this ->exceptionMock );
276
+ $ clientMock = Mockery::mock ('Http\Adapter\Guzzle6\Client ' );
277
+ $ clientMock ->shouldReceive ('sendAsyncRequest ' )->
278
+ with (Mockery::type ('GuzzleHttp\Psr7\Request ' ))->
279
+ andReturn (new GuzzleAdapterPromise ($ rejectedPromise , $ testReq ));
280
+
281
+ $ resource = new SparkPost ($ clientMock , ['key ' => 'SPARKPOST_API_KEY ' ]);
282
+
283
+ $ resource ->setOptions (['async ' => true , 'retries ' => 2 ]);
284
+ $ promise = $ resource ->asyncRequest ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload );
285
+ $ promise ->then (null , function ($ exception ) {
286
+ $ this ->assertEquals (500 , $ exception ->getCode ());
287
+ $ this ->assertEquals ($ this ->exceptionBody , $ exception ->getBody ());
288
+ })->wait ();
289
+ }
290
+
215
291
public function testPromise ()
216
292
{
217
293
$ promise = $ this ->resource ->asyncRequest ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload );
0 commit comments