Skip to content

Commit 1fa737e

Browse files
committed
Remove Zend_Json from the Webapi
- inject the new \Magento\Framework\Serialize\Serializer\Json - call unserialize, - Update the catch to \InvalidArgumentException
1 parent e12a17a commit 1fa737e

File tree

2 files changed

+50
-26
lines changed
  • lib/internal/Magento/Framework/Webapi

2 files changed

+50
-26
lines changed

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,26 @@ class Json implements \Magento\Framework\Webapi\Rest\Request\DeserializerInterfa
2020
*/
2121
protected $_appState;
2222

23+
/**
24+
* @var \Magento\Framework\Serialize\Serializer\Json
25+
*/
26+
private $serializer;
27+
2328
/**
2429
* @param \Magento\Framework\Json\Decoder $decoder
25-
* @param \Magento\Framework\App\State $appState
30+
* @param State $appState
31+
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
32+
* @throws \RuntimeException
2633
*/
27-
public function __construct(\Magento\Framework\Json\Decoder $decoder, State $appState)
28-
{
34+
public function __construct(
35+
\Magento\Framework\Json\Decoder $decoder,
36+
State $appState,
37+
\Magento\Framework\Serialize\Serializer\Json $serializer = null
38+
) {
2939
$this->decoder = $decoder;
3040
$this->_appState = $appState;
41+
$this->serializer = $serializer?: \Magento\Framework\App\ObjectManager::getInstance()
42+
->get(\Magento\Framework\Serialize\Serializer\Json::class);
3143
}
3244

3345
/**
@@ -46,8 +58,8 @@ public function deserialize($encodedBody)
4658
);
4759
}
4860
try {
49-
$decodedBody = $this->decoder->decode($encodedBody);
50-
} catch (\Zend_Json_Exception $e) {
61+
$decodedBody = $this->serializer->unserialize($encodedBody);
62+
} catch (\InvalidArgumentException $e) {
5163
if ($this->_appState->getMode() !== State::MODE_DEVELOPER) {
5264
throw new \Magento\Framework\Webapi\Exception(new Phrase('Decoding error.'));
5365
} 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)