summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorEwan Dennis <ewandennis@users.noreply.github.com>2017-07-10 16:13:28 +0100
committerGitHub <noreply@github.com>2017-07-10 16:13:28 +0100
commit342ecf62947cfe3b31f2253fb427b1b4a613907b (patch)
tree0a7ddf7a0dee31d895479ce8af589477b93d8bd4 /test
parent4de9c54c7a3554192e50fee9734fa96bf3e7bec3 (diff)
parent7beccdecb04d24cd055c91cf9b652ccc58eeaf60 (diff)
downloadphp-sparkpost-342ecf62947cfe3b31f2253fb427b1b4a613907b.zip
php-sparkpost-342ecf62947cfe3b31f2253fb427b1b4a613907b.tar.gz
php-sparkpost-342ecf62947cfe3b31f2253fb427b1b4a613907b.tar.bz2
Merge pull request #169 from SparkPost/issue-168
Issue-168: Optional automatic retry on 5xx
Diffstat (limited to 'test')
-rw-r--r--test/unit/SparkPostTest.php76
1 files changed, 76 insertions, 0 deletions
diff --git a/test/unit/SparkPostTest.php b/test/unit/SparkPostTest.php
index cb139f6..89e8e3a 100644
--- a/test/unit/SparkPostTest.php
+++ b/test/unit/SparkPostTest.php
@@ -60,6 +60,13 @@ class SparkPostTest extends \PHPUnit_Framework_TestCase
$this->responseMock->shouldReceive('getBody')->andReturn($responseBodyMock);
$responseBodyMock->shouldReceive('__toString')->andReturn(json_encode($this->responseBody));
+ $errorBodyMock = Mockery::mock();
+ $this->badResponseBody = ['errors' => []];
+ $this->badResponseMock = Mockery::mock('Psr\Http\Message\ResponseInterface');
+ $this->badResponseMock->shouldReceive('getStatusCode')->andReturn(503);
+ $this->badResponseMock->shouldReceive('getBody')->andReturn($errorBodyMock);
+ $errorBodyMock->shouldReceive('__toString')->andReturn(json_encode($this->badResponseBody));
+
// exception mock up
$exceptionResponseMock = Mockery::mock();
$this->exceptionBody = ['results' => 'failed'];
@@ -159,6 +166,35 @@ class SparkPostTest extends \PHPUnit_Framework_TestCase
}
}
+ public function testSuccessfulSyncRequestWithRetries()
+ {
+ $this->clientMock->shouldReceive('sendRequest')->
+ with(Mockery::type('GuzzleHttp\Psr7\Request'))->
+ andReturn($this->badResponseMock, $this->badResponseMock, $this->responseMock);
+
+ $this->resource->setOptions(['retries' => 2]);
+ $response = $this->resource->syncRequest('POST', 'transmissions', $this->postTransmissionPayload);
+
+ $this->assertEquals($this->responseBody, $response->getBody());
+ $this->assertEquals(200, $response->getStatusCode());
+ }
+
+ public function testUnsuccessfulSyncRequestWithRetries()
+ {
+ $this->clientMock->shouldReceive('sendRequest')->
+ once()->
+ with(Mockery::type('GuzzleHttp\Psr7\Request'))->
+ andThrow($this->exceptionMock);
+
+ $this->resource->setOptions(['retries' => 2]);
+ try {
+ $this->resource->syncRequest('POST', 'transmissions', $this->postTransmissionPayload);
+ } catch (\Exception $e) {
+ $this->assertEquals($this->exceptionBody, $e->getBody());
+ $this->assertEquals(500, $e->getCode());
+ }
+ }
+
public function testSuccessfulAsyncRequestWithWait()
{
$this->promiseMock->shouldReceive('wait')->andReturn($this->responseMock);
@@ -212,6 +248,46 @@ class SparkPostTest extends \PHPUnit_Framework_TestCase
})->wait();
}
+ public function testSuccessfulAsyncRequestWithRetries()
+ {
+ $testReq = $this->resource->buildRequest('POST', 'transmissions', $this->postTransmissionPayload, []);
+ $clientMock = Mockery::mock('Http\Adapter\Guzzle6\Client');
+ $clientMock->shouldReceive('sendAsyncRequest')->
+ with(Mockery::type('GuzzleHttp\Psr7\Request'))->
+ andReturn(
+ new GuzzleAdapterPromise(new GuzzleFulfilledPromise($this->badResponseMock), $testReq),
+ new GuzzleAdapterPromise(new GuzzleFulfilledPromise($this->badResponseMock), $testReq),
+ new GuzzleAdapterPromise(new GuzzleFulfilledPromise($this->responseMock), $testReq)
+ );
+
+ $resource = new SparkPost($clientMock, ['key' => 'SPARKPOST_API_KEY']);
+
+ $resource->setOptions(['async' => true, 'retries' => 2]);
+ $promise = $resource->asyncRequest('POST', 'transmissions', $this->postTransmissionPayload);
+ $promise->then(function($resp) {
+ $this->assertEquals(200, $resp->getStatusCode());
+ })->wait();
+ }
+
+ public function testUnsuccessfulAsyncRequestWithRetries()
+ {
+ $testReq = $this->resource->buildRequest('POST', 'transmissions', $this->postTransmissionPayload, []);
+ $rejectedPromise = new GuzzleRejectedPromise($this->exceptionMock);
+ $clientMock = Mockery::mock('Http\Adapter\Guzzle6\Client');
+ $clientMock->shouldReceive('sendAsyncRequest')->
+ with(Mockery::type('GuzzleHttp\Psr7\Request'))->
+ andReturn(new GuzzleAdapterPromise($rejectedPromise, $testReq));
+
+ $resource = new SparkPost($clientMock, ['key' => 'SPARKPOST_API_KEY']);
+
+ $resource->setOptions(['async' => true, 'retries' => 2]);
+ $promise = $resource->asyncRequest('POST', 'transmissions', $this->postTransmissionPayload);
+ $promise->then(null, function($exception) {
+ $this->assertEquals(500, $exception->getCode());
+ $this->assertEquals($this->exceptionBody, $exception->getBody());
+ })->wait();
+ }
+
public function testPromise()
{
$promise = $this->resource->asyncRequest('POST', 'transmissions', $this->postTransmissionPayload);