-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds json parsing middleware for better response handling
- Loading branch information
1 parent
23c1aab
commit 3116b5c
Showing
4 changed files
with
99 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Shutterstock\Api; | ||
|
||
use GuzzleHttp\Psr7\StreamDecoratorTrait; | ||
use JsonSerializable; | ||
use Psr\Http\Message\StreamInterface; | ||
use RuntimeException; | ||
|
||
class JsonStream implements StreamInterface, JsonSerializable | ||
{ | ||
|
||
use StreamDecoratorTrait; | ||
|
||
public function jsonSerialize() | ||
{ | ||
$contents = (string) $this->getContents(); | ||
$decodedContents = json_decode($contents, true); | ||
if (json_last_error() != JSON_ERROR_NONE) { | ||
throw new RuntimeException('Error trying to decode response: ' . json_last_error_msg()); | ||
} | ||
|
||
return $decodedContents; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Shutterstock\Api; | ||
|
||
use GuzzleHttp\Psr7\Stream; | ||
use PHPUnit_Framework_TestCase; | ||
|
||
class JsonStreamTest extends PHPUnit_Framework_TestCase | ||
{ | ||
|
||
public function testIsInstanceOfJsonStream() | ||
{ | ||
$jsonStream = $this->getJsonStream(''); | ||
|
||
$this->assertInstanceOf('Shutterstock\Api\JsonStream', $jsonStream); | ||
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $jsonStream); | ||
} | ||
|
||
/** | ||
* @dataProvider dataJsonSerialize | ||
*/ | ||
public function testJsonSerialize($expectedArray, $string) | ||
{ | ||
$jsonStream = $this->getJsonStream($string); | ||
$array = $jsonStream->jsonSerialize(); | ||
|
||
$this->assertEquals($expectedArray, $array); | ||
} | ||
|
||
public function dataJsonSerialize() | ||
{ | ||
return [ | ||
[ | ||
'expectedArray' => ['key' => 'value'], | ||
'string' => '{"key":"value"}', | ||
], | ||
[ | ||
'expectedArray' => null, | ||
'string' => '', | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @expectedException RuntimeException | ||
*/ | ||
public function testJsonSerializeException() | ||
{ | ||
$jsonStream = $this->getJsonStream('words'); | ||
$jsonStream->jsonSerialize(); | ||
} | ||
|
||
protected function getJsonStream($string) | ||
{ | ||
$stream = fopen('php://temp', 'r+'); | ||
fwrite($stream, $string); | ||
fseek($stream, 0); | ||
$streamObject = new Stream($stream); | ||
|
||
return new JsonStream($streamObject); | ||
} | ||
} |