Skip to content

Commit d2e2251

Browse files
MAGETWO-71040: Remove Zend_Json from the Webapi #10333
2 parents e12a17a + f9fb7a4 commit d2e2251

File tree

2 files changed

+54
-27
lines changed
  • lib/internal/Magento/Framework/Webapi

2 files changed

+54
-27
lines changed

lib/internal/Magento/Framework/Webapi/Rest/Request/Deserializer/Json.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,37 @@
1212

1313
class Json implements \Magento\Framework\Webapi\Rest\Request\DeserializerInterface
1414
{
15-
/** @var \Magento\Framework\Json\Decoder */
15+
/**
16+
* @var \Magento\Framework\Json\Decoder
17+
* @deprecated
18+
*/
1619
protected $decoder;
1720

1821
/**
1922
* @var State
2023
*/
2124
protected $_appState;
2225

26+
/**
27+
* @var \Magento\Framework\Serialize\Serializer\Json
28+
*/
29+
private $serializer;
30+
2331
/**
2432
* @param \Magento\Framework\Json\Decoder $decoder
25-
* @param \Magento\Framework\App\State $appState
33+
* @param State $appState
34+
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
35+
* @throws \RuntimeException
2636
*/
27-
public function __construct(\Magento\Framework\Json\Decoder $decoder, State $appState)
28-
{
37+
public function __construct(
38+
\Magento\Framework\Json\Decoder $decoder,
39+
State $appState,
40+
\Magento\Framework\Serialize\Serializer\Json $serializer = null
41+
) {
2942
$this->decoder = $decoder;
3043
$this->_appState = $appState;
44+
$this->serializer = $serializer?: \Magento\Framework\App\ObjectManager::getInstance()
45+
->get(\Magento\Framework\Serialize\Serializer\Json::class);
3146
}
3247

3348
/**
@@ -46,8 +61,8 @@ public function deserialize($encodedBody)
4661
);
4762
}
4863
try {
49-
$decodedBody = $this->decoder->decode($encodedBody);
50-
} catch (\Zend_Json_Exception $e) {
64+
$decodedBody = $this->serializer->unserialize($encodedBody);
65+
} catch (\InvalidArgumentException $e) {
5166
if ($this->_appState->getMode() !== State::MODE_DEVELOPER) {
5267
throw new \Magento\Framework\Webapi\Exception(new Phrase('Decoding error.'));
5368
} else {

lib/internal/Magento/Framework/Webapi/Test/Unit/Rest/Request/Deserializer/JsonTest.php

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,30 @@ class JsonTest extends \PHPUnit_Framework_TestCase
2121
/** @var \PHPUnit_Framework_MockObject_MockObject */
2222
protected $_appStateMock;
2323

24+
/** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
25+
private $serializerMock;
26+
2427
protected function setUp()
2528
{
2629
/** Prepare mocks for SUT constructor. */
2730
$this->decoderMock = $this->getMockBuilder(\Magento\Framework\Json\Decoder::class)
2831
->disableOriginalConstructor()
2932
->setMethods(['decode'])
3033
->getMock();
31-
$this->_appStateMock = $this->getMock(\Magento\Framework\App\State::class, [], [], '', false);
34+
$this->_appStateMock = $this->getMock(
35+
\Magento\Framework\App\State::class,
36+
[],
37+
[],
38+
'',
39+
false
40+
);
41+
$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
42+
->getMock();
3243
/** Initialize SUT. */
3344
$this->_jsonDeserializer = new \Magento\Framework\Webapi\Rest\Request\Deserializer\Json(
3445
$this->decoderMock,
35-
$this->_appStateMock
46+
$this->_appStateMock,
47+
$this->serializerMock
3648
);
3749
parent::setUp();
3850
}
@@ -60,13 +72,13 @@ public function testDeserialize()
6072
'key2' => 'test2',
6173
'array' => ['test01' => 'some1', 'test02' => 'some2'],
6274
];
63-
$this->decoderMock->expects(
64-
$this->once()
65-
)->method(
66-
'decode'
67-
)->will(
68-
$this->returnValue($expectedDecodedJson)
69-
);
75+
$this->serializerMock->expects($this->any())
76+
->method('unserialize')
77+
->willReturnCallback(
78+
function ($serializedData) {
79+
return json_decode($serializedData, true);
80+
}
81+
);
7082
/** Initialize SUT. */
7183
$this->assertEquals(
7284
$expectedDecodedJson,
@@ -78,9 +90,10 @@ public function testDeserialize()
7890
public function testDeserializeInvalidEncodedBodyExceptionDeveloperModeOff()
7991
{
8092
/** Prepare mocks for SUT constructor. */
81-
$this->decoderMock->expects($this->once())
82-
->method('decode')
83-
->will($this->throwException(new \Zend_Json_Exception));
93+
$this->serializerMock
94+
->expects($this->once())
95+
->method('unserialize')
96+
->will($this->throwException(new \InvalidArgumentException));
8497
$this->_appStateMock->expects($this->once())
8598
->method('getMode')
8699
->will($this->returnValue('production'));
@@ -103,15 +116,14 @@ public function testDeserializeInvalidEncodedBodyExceptionDeveloperModeOff()
103116
public function testDeserializeInvalidEncodedBodyExceptionDeveloperModeOn()
104117
{
105118
/** Prepare mocks for SUT constructor. */
106-
$this->decoderMock->expects(
107-
$this->once()
108-
)->method(
109-
'decode'
110-
)->will(
111-
$this->throwException(
112-
new \Zend_Json_Exception('Decoding error:' . PHP_EOL . 'Decoding failed: Syntax error')
113-
)
114-
);
119+
$this->serializerMock
120+
->expects($this->once())
121+
->method('unserialize')
122+
->will(
123+
$this->throwException(
124+
new \InvalidArgumentException('Unable to unserialize value.')
125+
)
126+
);
115127
$this->_appStateMock->expects($this->once())
116128
->method('getMode')
117129
->will($this->returnValue('developer'));

0 commit comments

Comments
 (0)