Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 8b2d441

Browse files
committed
MAGETWO-80181: [2.2.x] - Remove Zend_Json from the Webapi #10333
1 parent ac3a840 commit 8b2d441

File tree

2 files changed

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

2 files changed

+47
-26
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Json implements \Magento\Framework\Webapi\Rest\Request\DeserializerInterfa
1414
{
1515
/**
1616
* @var \Magento\Framework\Json\Decoder
17+
* @deprecated
1718
*/
1819
protected $decoder;
1920

@@ -22,14 +23,26 @@ class Json implements \Magento\Framework\Webapi\Rest\Request\DeserializerInterfa
2223
*/
2324
protected $_appState;
2425

26+
/**
27+
* @var \Magento\Framework\Serialize\Serializer\Json
28+
*/
29+
private $serializer;
30+
2531
/**
2632
* @param \Magento\Framework\Json\Decoder $decoder
27-
* @param \Magento\Framework\App\State $appState
33+
* @param State $appState
34+
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
35+
* @throws \RuntimeException
2836
*/
29-
public function __construct(\Magento\Framework\Json\Decoder $decoder, State $appState)
30-
{
37+
public function __construct(
38+
\Magento\Framework\Json\Decoder $decoder,
39+
State $appState,
40+
\Magento\Framework\Serialize\Serializer\Json $serializer = null
41+
) {
3142
$this->decoder = $decoder;
3243
$this->_appState = $appState;
44+
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
45+
->get(\Magento\Framework\Serialize\Serializer\Json::class);
3346
}
3447

3548
/**
@@ -48,8 +61,8 @@ public function deserialize($encodedBody)
4861
);
4962
}
5063
try {
51-
$decodedBody = $this->decoder->decode($encodedBody);
52-
} catch (\Zend_Json_Exception $e) {
64+
$decodedBody = $this->serializer->unserialize($encodedBody);
65+
} catch (\InvalidArgumentException $e) {
5366
if ($this->_appState->getMode() !== State::MODE_DEVELOPER) {
5467
throw new \Magento\Framework\Webapi\Exception(new Phrase('Decoding error.'));
5568
} else {

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

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,26 @@ 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->createMock(\Magento\Framework\App\State::class);
34+
$this->_appStateMock = $this->createMock(
35+
\Magento\Framework\App\State::class
36+
);
37+
$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
38+
->getMock();
3239
/** Initialize SUT. */
3340
$this->_jsonDeserializer = new \Magento\Framework\Webapi\Rest\Request\Deserializer\Json(
3441
$this->decoderMock,
35-
$this->_appStateMock
42+
$this->_appStateMock,
43+
$this->serializerMock
3644
);
3745
parent::setUp();
3846
}
@@ -60,13 +68,13 @@ public function testDeserialize()
6068
'key2' => 'test2',
6169
'array' => ['test01' => 'some1', 'test02' => 'some2'],
6270
];
63-
$this->decoderMock->expects(
64-
$this->once()
65-
)->method(
66-
'decode'
67-
)->will(
68-
$this->returnValue($expectedDecodedJson)
69-
);
71+
$this->serializerMock->expects($this->any())
72+
->method('unserialize')
73+
->willReturnCallback(
74+
function ($serializedData) {
75+
return json_decode($serializedData, true);
76+
}
77+
);
7078
/** Initialize SUT. */
7179
$this->assertEquals(
7280
$expectedDecodedJson,
@@ -78,9 +86,10 @@ public function testDeserialize()
7886
public function testDeserializeInvalidEncodedBodyExceptionDeveloperModeOff()
7987
{
8088
/** Prepare mocks for SUT constructor. */
81-
$this->decoderMock->expects($this->once())
82-
->method('decode')
83-
->will($this->throwException(new \Zend_Json_Exception));
89+
$this->serializerMock
90+
->expects($this->once())
91+
->method('unserialize')
92+
->will($this->throwException(new \InvalidArgumentException));
8493
$this->_appStateMock->expects($this->once())
8594
->method('getMode')
8695
->will($this->returnValue('production'));
@@ -103,15 +112,14 @@ public function testDeserializeInvalidEncodedBodyExceptionDeveloperModeOff()
103112
public function testDeserializeInvalidEncodedBodyExceptionDeveloperModeOn()
104113
{
105114
/** 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-
);
115+
$this->serializerMock
116+
->expects($this->once())
117+
->method('unserialize')
118+
->will(
119+
$this->throwException(
120+
new \InvalidArgumentException('Unable to unserialize value.')
121+
)
122+
);
115123
$this->_appStateMock->expects($this->once())
116124
->method('getMode')
117125
->will($this->returnValue('developer'));

0 commit comments

Comments
 (0)