Skip to content

Commit 95bc6bb

Browse files
author
Oleksii Korshenko
committed
MAGETWO-71062: Remove zend json from json controller #10342
- Merge Pull Request #10342 from dmanners/magento2:remove-zend-json-from-json-controller - Merged commits: 1. baeb58c 2. db6f353 3. 4f78388 4. 7e46cea 5. ff0581b 6. 41e118b
2 parents c955c32 + 41e118b commit 95bc6bb

File tree

11 files changed

+140
-51
lines changed

11 files changed

+140
-51
lines changed

app/code/Magento/Checkout/view/frontend/layout/checkout_cart_item_renderers.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
<update handle="checkout_item_price_renderers"/>
1010
<body>
1111
<referenceBlock name="checkout.cart.item.renderers">
12-
<block class="Magento\Checkout\Block\Cart\Item\Renderer" as="default" template="Magento_Checkout::cart/item/default.phtml">
12+
<block class="Magento\Checkout\Block\Cart\Item\Renderer" name="checkout.cart.item.renderers.default" as="default" template="Magento_Checkout::cart/item/default.phtml">
1313
<block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions" name="checkout.cart.item.renderers.default.actions" as="actions">
1414
<block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions\Edit" name="checkout.cart.item.renderers.default.actions.edit" template="Magento_Checkout::cart/item/renderer/actions/edit.phtml"/>
1515
<block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions\Remove" name="checkout.cart.item.renderers.default.actions.remove" template="Magento_Checkout::cart/item/renderer/actions/remove.phtml"/>
1616
</block>
1717
</block>
18-
<block class="Magento\Checkout\Block\Cart\Item\Renderer" as="simple" template="Magento_Checkout::cart/item/default.phtml">
18+
<block class="Magento\Checkout\Block\Cart\Item\Renderer" name="checkout.cart.item.renderers.simple" as="simple" template="Magento_Checkout::cart/item/default.phtml">
1919
<block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions" name="checkout.cart.item.renderers.simple.actions" as="actions">
2020
<block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions\Edit" name="checkout.cart.item.renderers.simple.actions.edit" template="Magento_Checkout::cart/item/renderer/actions/edit.phtml"/>
2121
<block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions\Remove" name="checkout.cart.item.renderers.simple.actions.remove" template="Magento_Checkout::cart/item/renderer/actions/remove.phtml"/>

app/code/Magento/Tax/Controller/Adminhtml/Rate/AjaxLoad.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class AjaxLoad extends \Magento\Tax\Controller\Adminhtml\Rate
1414
/**
1515
* Json needed for the Ajax Edit Form
1616
*
17-
* @return void
17+
* @return \Magento\Framework\Controller\Result\Json
18+
* @throws \InvalidArgumentException
1819
*/
1920
public function execute()
2021
{

app/code/Magento/Tax/Controller/Adminhtml/Rate/AjaxSave.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class AjaxSave extends \Magento\Tax\Controller\Adminhtml\Rate
1414
* Save Tax Rate via AJAX
1515
*
1616
* @return \Magento\Framework\Controller\Result\Json
17+
* @throws \InvalidArgumentException
1718
*/
1819
public function execute()
1920
{

dev/tests/integration/framework/Magento/TestFramework/TestCase/AbstractController.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ abstract class AbstractController extends \PHPUnit_Framework_TestCase
2525
protected $_runOptions = [];
2626

2727
/**
28-
* @var \Magento\TestFramework\Request
28+
* @var \Magento\Framework\App\RequestInterface
2929
*/
3030
protected $_request;
3131

3232
/**
33-
* @var \Magento\TestFramework\Response
33+
* @var \Magento\Framework\App\ResponseInterface
3434
*/
3535
protected $_response;
3636

@@ -102,7 +102,7 @@ public function dispatch($uri)
102102
/**
103103
* Request getter
104104
*
105-
* @return \Magento\TestFramework\Request
105+
* @return \Magento\Framework\App\RequestInterface
106106
*/
107107
public function getRequest()
108108
{
@@ -115,7 +115,7 @@ public function getRequest()
115115
/**
116116
* Response getter
117117
*
118-
* @return \Magento\TestFramework\Response
118+
* @return \Magento\Framework\App\ResponseInterface
119119
*/
120120
public function getResponse()
121121
{
@@ -268,14 +268,21 @@ protected function getCookieMessages($messageType = null)
268268
{
269269
/** @var $cookieManager CookieManagerInterface */
270270
$cookieManager = $this->_objectManager->get(CookieManagerInterface::class);
271+
272+
/** @var $jsonSerializer \Magento\Framework\Serialize\Serializer\Json */
273+
$jsonSerializer = $this->_objectManager->get(\Magento\Framework\Serialize\Serializer\Json::class);
271274
try {
272-
$messages = \Zend_Json::decode(
273-
$cookieManager->getCookie(MessagePlugin::MESSAGES_COOKIES_NAME, \Zend_Json::encode([]))
275+
$messages = $jsonSerializer->unserialize(
276+
$cookieManager->getCookie(
277+
MessagePlugin::MESSAGES_COOKIES_NAME,
278+
$jsonSerializer->serialize([])
279+
)
274280
);
281+
275282
if (!is_array($messages)) {
276283
$messages = [];
277284
}
278-
} catch (\Zend_Json_Exception $e) {
285+
} catch (\InvalidArgumentException $e) {
279286
$messages = [];
280287
}
281288

dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/TestCase/ControllerAbstractTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,25 @@ class ControllerAbstractTest extends \Magento\TestFramework\TestCase\AbstractCon
2525
/** @var \PHPUnit_Framework_MockObject_MockObject | CookieManagerInterface */
2626
private $cookieManagerMock;
2727

28+
/**
29+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Serialize\Serializer\Json
30+
*/
31+
private $serializerMock;
32+
2833
protected function setUp()
2934
{
3035
$testObjectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
3136

3237
$this->messageManager = $this->getMock(\Magento\Framework\Message\Manager::class, [], [], '', false);
3338
$this->cookieManagerMock = $this->getMock(CookieManagerInterface::class, [], [], '', false);
39+
$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
40+
->disableOriginalConstructor()
41+
->getMock();
42+
$this->serializerMock->expects($this->any())->method('unserialize')->willReturnCallback(
43+
function ($serializedData) {
44+
return json_decode($serializedData, true);
45+
}
46+
);
3447
$this->interpretationStrategyMock = $this->getMock(InterpretationStrategyInterface::class, [], [], '', false);
3548
$this->interpretationStrategyMock->expects($this->any())
3649
->method('interpret')
@@ -58,6 +71,7 @@ function (MessageInterface $message) {
5871
[\Magento\Framework\App\ResponseInterface::class, $response],
5972
[\Magento\Framework\Message\Manager::class, $this->messageManager],
6073
[CookieManagerInterface::class, $this->cookieManagerMock],
74+
[\Magento\Framework\Serialize\Serializer\Json::class, $this->serializerMock],
6175
[InterpretationStrategyInterface::class, $this->interpretationStrategyMock],
6276
]
6377
)
@@ -244,6 +258,6 @@ private function addSessionMessages()
244258

245259
$this->cookieManagerMock->expects($this->any())
246260
->method('getCookie')
247-
->willReturn(\Zend_Json::encode($cookieMessages));
261+
->willReturn(json_encode($cookieMessages));
248262
}
249263
}

dev/tests/integration/testsuite/Magento/Email/Model/TemplateTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function testLoadDefault()
108108
$this->assertNotEmpty($this->model->getTemplateText());
109109
$this->assertNotEmpty($this->model->getTemplateSubject());
110110
$this->assertNotEmpty($this->model->getOrigTemplateVariables());
111-
$this->assertInternalType('array', \Zend_Json::decode($this->model->getOrigTemplateVariables()));
111+
$this->assertInternalType('array', json_decode($this->model->getOrigTemplateVariables(), true));
112112
}
113113

114114
/**

dev/tests/integration/testsuite/Magento/ImportExport/Block/Adminhtml/Import/Edit/BeforeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ protected function setUp()
9393
public function testGetEntityBehaviors()
9494
{
9595
$actualEntities = $this->_model->getEntityBehaviors();
96-
$expectedEntities = \Zend_Json::encode($this->_expectedEntities);
96+
$expectedEntities = json_encode($this->_expectedEntities);
9797
$this->assertEquals($expectedEntities, $actualEntities);
9898
}
9999

@@ -105,7 +105,7 @@ public function testGetEntityBehaviors()
105105
public function testGetUniqueBehaviors()
106106
{
107107
$actualBehaviors = $this->_model->getUniqueBehaviors();
108-
$expectedBehaviors = \Zend_Json::encode($this->_expectedBehaviors);
108+
$expectedBehaviors = json_encode($this->_expectedBehaviors);
109109
$this->assertEquals($expectedBehaviors, $actualBehaviors);
110110
}
111111
}

lib/internal/Magento/Framework/Controller/Result/Json.php

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,63 @@ class Json extends AbstractResult
2929
protected $json;
3030

3131
/**
32-
* @param \Magento\Framework\Translate\InlineInterface $translateInline
32+
* @var \Magento\Framework\Serialize\Serializer\Json
3333
*/
34-
public function __construct(InlineInterface $translateInline)
35-
{
34+
private $serializer;
35+
36+
/**
37+
* @param InlineInterface $translateInline
38+
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
39+
* @throws \RuntimeException
40+
*/
41+
public function __construct(
42+
InlineInterface $translateInline,
43+
\Magento\Framework\Serialize\Serializer\Json $serializer = null
44+
) {
3645
$this->translateInline = $translateInline;
46+
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
47+
->get(\Magento\Framework\Serialize\Serializer\Json::class);
3748
}
3849

3950
/**
4051
* Set json data
4152
*
42-
* @param mixed $data
43-
* @param boolean $cycleCheck Optional; whether or not to check for object recursion; off by default
44-
* @param array $options Additional options used during encoding
45-
* @return $this
53+
* @param array|string|\Magento\Framework\DataObject $data
54+
* @param bool $cycleCheck
55+
* @param array $options
56+
* @return Json
57+
* @throws \InvalidArgumentException
58+
* @throws \Magento\Framework\Exception\LocalizedException
59+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
60+
* @deprecated
4661
*/
4762
public function setData($data, $cycleCheck = false, $options = [])
4863
{
49-
$this->json = \Zend_Json::encode($data, $cycleCheck, $options);
64+
if ($data instanceof \Magento\Framework\DataObject) {
65+
return $this->setArrayData($data->toArray());
66+
}
67+
68+
if (is_array($data)) {
69+
return $this->setArrayData($data);
70+
}
71+
72+
if (is_string($data)) {
73+
return $this->setJsonData($data);
74+
}
75+
76+
throw new \Magento\Framework\Exception\LocalizedException(
77+
new \Magento\Framework\Phrase('Invalid argument type')
78+
);
79+
}
80+
81+
/**
82+
* @param array $data
83+
* @return $this
84+
* @throws \InvalidArgumentException
85+
*/
86+
public function setArrayData(array $data)
87+
{
88+
$this->setJsonData($this->serializer->serialize($data));
5089
return $this;
5190
}
5291

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)