mirror of
https://github.com/halaxa/json-machine.git
synced 2025-03-15 17:09:39 +01:00
ErrorWrappingDecoder implementation
This commit is contained in:
parent
59051e1927
commit
5a16d75f38
@ -22,5 +22,8 @@
|
||||
"autoload" : {
|
||||
"psr-4": {"JsonMachine\\": "src/"},
|
||||
"files": ["src/functions.php"]
|
||||
},
|
||||
"autoload-dev" : {
|
||||
"psr-0": {"JsonMachineTest\\": "test/JsonMachineTest/"}
|
||||
}
|
||||
}
|
||||
|
38
src/JsonDecoder/DecodingError.php
Normal file
38
src/JsonDecoder/DecodingError.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace JsonMachine\JsonDecoder;
|
||||
|
||||
|
||||
class DecodingError
|
||||
{
|
||||
private $malformedJson;
|
||||
private $errorMessage;
|
||||
|
||||
/**
|
||||
* DecodingError constructor.
|
||||
* @param string $malformedJson
|
||||
* @param string $errorMessage
|
||||
*/
|
||||
public function __construct($malformedJson, $errorMessage)
|
||||
{
|
||||
$this->malformedJson = $malformedJson;
|
||||
$this->errorMessage = $errorMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMalformedJson()
|
||||
{
|
||||
return $this->malformedJson;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getErrorMessage()
|
||||
{
|
||||
return $this->errorMessage;
|
||||
}
|
||||
}
|
36
src/JsonDecoder/ErrorWrappingDecoder.php
Normal file
36
src/JsonDecoder/ErrorWrappingDecoder.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace JsonMachine\JsonDecoder;
|
||||
|
||||
|
||||
class ErrorWrappingDecoder implements Decoder
|
||||
{
|
||||
/**
|
||||
* @var Decoder
|
||||
*/
|
||||
private $innerDecoder;
|
||||
|
||||
public function __construct(Decoder $innerDecoder)
|
||||
{
|
||||
$this->innerDecoder = $innerDecoder;
|
||||
}
|
||||
|
||||
public function decodeKey($jsonScalarKey)
|
||||
{
|
||||
$result = $this->innerDecoder->decodeKey($jsonScalarKey);
|
||||
if ( ! $result->isOk()) {
|
||||
return new DecodingResult(true, new DecodingError($jsonScalarKey, $result->getErrorMessage()));
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function decodeValue($jsonValue)
|
||||
{
|
||||
$result = $this->innerDecoder->decodeValue($jsonValue);
|
||||
if ( ! $result->isOk()) {
|
||||
return new DecodingResult(true, new DecodingError($jsonValue, $result->getErrorMessage()));
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace JsonMachineTest\JsonDecoder;
|
||||
|
||||
use JsonMachine\JsonDecoder\Decoder;
|
||||
use JsonMachine\JsonDecoder\DecodingError;
|
||||
use JsonMachine\JsonDecoder\DecodingResult;
|
||||
use JsonMachine\JsonDecoder\ErrorWrappingDecoder;
|
||||
use PHPUnit_Framework_TestCase;
|
||||
|
||||
class ErrorWrappingDecoderTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider data_testTrueFalseMatrix
|
||||
* @param array $mockMethods
|
||||
*/
|
||||
public function testTrueFalseMatrix(array $mockMethods)
|
||||
{
|
||||
$innerDecoderMock = $this->createMock(Decoder::class);
|
||||
$innerDecoderMock->method('decodeValue')->willReturn($mockMethods['decodeValue']);
|
||||
$innerDecoderMock->method('decodeKey')->willReturn($mockMethods['decodeKey']);
|
||||
|
||||
$decoder = new ErrorWrappingDecoder($innerDecoderMock);
|
||||
|
||||
$keyResult = $decoder->decodeKey('"json"');
|
||||
$valueResult = $decoder->decodeValue('"json"');
|
||||
|
||||
$this->assertTrue($keyResult->isOk());
|
||||
$this->assertTrue($valueResult->isOk());
|
||||
$this->assertEquals($mockMethods['wrappedDecodeValue'], $valueResult);
|
||||
$this->assertEquals($mockMethods['wrappedDecodeKey'], $keyResult);
|
||||
}
|
||||
|
||||
public function data_testTrueFalseMatrix()
|
||||
{
|
||||
$notOkResult = new DecodingResult(false, null, 'Error happened.');
|
||||
$okResult = new DecodingResult(true, 'json');
|
||||
$wrappedNotOkResult = new DecodingResult(true, new DecodingError('"json"', 'Error happened.'));
|
||||
$wrappedOkResult = $okResult;
|
||||
|
||||
return [
|
||||
[
|
||||
[
|
||||
'decodeValue' => $notOkResult,
|
||||
'decodeKey' => $notOkResult,
|
||||
'wrappedDecodeValue' => $wrappedNotOkResult,
|
||||
'wrappedDecodeKey' => $wrappedNotOkResult,
|
||||
]
|
||||
],
|
||||
[
|
||||
[
|
||||
'decodeValue' => $notOkResult,
|
||||
'decodeKey' => $okResult,
|
||||
'wrappedDecodeValue' => $wrappedNotOkResult,
|
||||
'wrappedDecodeKey' => $wrappedOkResult,
|
||||
]
|
||||
],
|
||||
[
|
||||
[
|
||||
'decodeValue' => $okResult,
|
||||
'decodeKey' => $notOkResult,
|
||||
'wrappedDecodeValue' => $wrappedOkResult,
|
||||
'wrappedDecodeKey' => $wrappedNotOkResult,
|
||||
]
|
||||
],
|
||||
[
|
||||
[
|
||||
'decodeValue' => $okResult,
|
||||
'decodeKey' => $okResult,
|
||||
'wrappedDecodeValue' => $wrappedOkResult,
|
||||
'wrappedDecodeKey' => $wrappedOkResult,
|
||||
]
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@ use JsonMachine\JsonDecoder\ExtJsonDecoder;
|
||||
use JsonMachine\JsonDecoder\PassThruDecoder;
|
||||
use PHPUnit_Framework_TestCase;
|
||||
|
||||
class DecodersTest extends PHPUnit_Framework_TestCase
|
||||
class ExtJsonDecodersTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider dataPassesOptionsToJsonDecode
|
Loading…
x
Reference in New Issue
Block a user