diff --git a/src/Snowcap/Emarsys/Client.php b/src/Snowcap/Emarsys/Client.php index 3d62e58..bcd784a 100644 --- a/src/Snowcap/Emarsys/Client.php +++ b/src/Snowcap/Emarsys/Client.php @@ -795,6 +795,7 @@ public function addBlacklistEntries(array $emails = array(), array $domains = ar * @param string $uri * @param array $body * @return Response + * @throws ClientException * @throws ServerException */ protected function send($method = 'GET', $uri, array $body = array()) @@ -810,6 +811,15 @@ protected function send($method = 'GET', $uri, array $body = array()) $responseArray = json_decode($responseJson, true); + if ($responseArray === null) { + switch (json_last_error()) { + case JSON_ERROR_DEPTH: + throw new ClientException('JSON response could not be decoded, maximum depth reached.'); + default: + throw new ServerException("JSON response could not be decoded:\n" . json_last_error_msg()); + } + } + return new Response($responseArray); } diff --git a/tests/Unit/Suites/ClientTest.php b/tests/Unit/Suites/ClientTest.php index 67a60de..02664fc 100644 --- a/tests/Unit/Suites/ClientTest.php +++ b/tests/Unit/Suites/ClientTest.php @@ -16,12 +16,12 @@ class ClientTest extends \PHPUnit_Framework_TestCase /** * @var \PHPUnit_Framework_MockObject_MockObject|HttpClient */ - private $httpClient; + private $stubHttpClient; protected function setUp() { - $this->httpClient = $this->getMock('\Snowcap\Emarsys\HttpClient'); - $this->client = new Client($this->httpClient, 'dummy-api-username', 'dummy-api-secret'); + $this->stubHttpClient = $this->getMock('\Snowcap\Emarsys\HttpClient'); + $this->client = new Client($this->stubHttpClient, 'dummy-api-username', 'dummy-api-secret'); } public function testItAddsFieldsMapping() @@ -138,9 +138,7 @@ public function testItReturnsChoiceIdIfChoiceNameIsNotFound() public function testGetEmails() { $expectedResponse = $this->createExpectedResponse('emails'); - $this->httpClient->expects($this->any()) - ->method('send') - ->willReturn($expectedResponse); + $this->stubHttpClient->method('send')->willReturn($expectedResponse); $response = $this->client->getEmails(); @@ -170,9 +168,7 @@ public function testGetEmails() public function testCreateEmail() { $expectedResponse = $this->createExpectedResponse('createContact'); - $this->httpClient->expects($this->any()) - ->method('send') - ->willReturn($expectedResponse); + $this->stubHttpClient->method('send')->willReturn($expectedResponse); $data = array( 'language' => 'en', @@ -198,9 +194,7 @@ public function testCreateEmail() public function testGetContactIdSuccess() { $expectedResponse = $this->createExpectedResponse('getContactId'); - $this->httpClient->expects($this->any()) - ->method('send') - ->willReturn($expectedResponse); + $this->stubHttpClient->method('send')->willReturn($expectedResponse); $response = $this->client->getContactId('3', 'sender@example.com'); @@ -211,9 +205,7 @@ public function testGetContactIdSuccess() public function testItReturnsContactData() { $expectedResponse = $this->createExpectedResponse('getContactData'); - $this->httpClient->expects($this->once()) - ->method('send') - ->willReturn($expectedResponse); + $this->stubHttpClient->method('send')->willReturn($expectedResponse); $response = $this->client->getContactData(array()); @@ -223,9 +215,7 @@ public function testItReturnsContactData() public function testItCreatesContact() { $expectedResponse = $this->createExpectedResponse('createContact'); - $this->httpClient->expects($this->once()) - ->method('send') - ->willReturn($expectedResponse); + $this->stubHttpClient->method('send')->willReturn($expectedResponse); $data = array( '3' => 'recipient@example.com', @@ -236,6 +226,22 @@ public function testItCreatesContact() $this->assertInstanceOf('\Snowcap\Emarsys\Response', $response); } + /** + * @expectedException \Snowcap\Emarsys\Exception\ClientException + * @expectedExceptionMessage JSON response could not be decoded, maximum depth reached. + */ + public function testThrowsExceptionIfJsonDepthExceedsLimit() + { + $nestedStructure = array(); + for ($i=0; $i<511; $i++) { + $nestedStructure = array($nestedStructure); + } + + $this->stubHttpClient->method('send')->willReturn(json_encode($nestedStructure)); + + $this->client->createContact(array()); + } + /** * Get a json test data and decode it *