Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Snowcap/Emarsys/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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);
}

Expand Down
42 changes: 24 additions & 18 deletions tests/Unit/Suites/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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',
Expand All @@ -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', '[email protected]');

Expand All @@ -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());

Expand All @@ -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' => '[email protected]',
Expand All @@ -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
*
Expand Down