Skip to content

Commit 21abedf

Browse files
author
Oleksii Korshenko
committed
MAGETWO-71059: Remove Zend_Json from setup #10339
- Merge Pull Request #10339 from dmanners/magento2:remove-zend-json-from-setup - Merged commits: 1. acc5353 2. 097fe08
2 parents c955c32 + 097fe08 commit 21abedf

File tree

10 files changed

+124
-47
lines changed

10 files changed

+124
-47
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"/>

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/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'));

setup/src/Magento/Setup/Model/PackagesAuth.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,29 @@ class PackagesAuth
4949
*/
5050
private $filesystem;
5151

52+
/**
53+
* @var \Magento\Framework\Serialize\Serializer\Json
54+
*/
55+
private $serializer;
56+
5257
/**
5358
* @param \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator
5459
* @param \Magento\Framework\HTTP\Client\Curl $curl
5560
* @param \Magento\Framework\Filesystem $filesystem
61+
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
62+
* @throws \RuntimeException
5663
*/
5764
public function __construct(
5865
\Zend\ServiceManager\ServiceLocatorInterface $serviceLocator,
5966
\Magento\Framework\HTTP\Client\Curl $curl,
60-
\Magento\Framework\Filesystem $filesystem
67+
\Magento\Framework\Filesystem $filesystem,
68+
\Magento\Framework\Serialize\Serializer\Json $serializer = null
6169
) {
6270
$this->serviceLocator = $serviceLocator;
6371
$this->curlClient = $curl;
6472
$this->filesystem = $filesystem;
73+
$this->serializer = $serializer?: \Magento\Framework\App\ObjectManager::getInstance()
74+
->get(\Magento\Framework\Serialize\Serializer\Json::class);
6575
}
6676

6777
/**
@@ -85,9 +95,11 @@ public function getCredentialBaseUrl()
8595
* @param string $token
8696
* @param string $secretKey
8797
* @return string
98+
* @throws \InvalidArgumentException
8899
*/
89100
public function checkCredentials($token, $secretKey)
90101
{
102+
$response = ['success' => true];
91103
$serviceUrl = $this->getPackagesJsonUrl();
92104
$this->curlClient->setCredentials($token, $secretKey);
93105
try {
@@ -96,13 +108,13 @@ public function checkCredentials($token, $secretKey)
96108
$packagesInfo = $this->curlClient->getBody();
97109
$directory = $this->filesystem->getDirectoryWrite(DirectoryList::COMPOSER_HOME);
98110
$directory->writeFile(self::PATH_TO_PACKAGES_FILE, $packagesInfo);
99-
return \Zend_Json::encode(['success' => true]);
100111
} else {
101-
return \Zend_Json::encode(['success' => false, 'message' => 'Bad credentials']);
112+
$response = ['success' => false, 'message' => 'Bad credentials'];
102113
}
103114
} catch (\Exception $e) {
104-
return \Zend_Json::encode(['success' => false, 'message' => $e->getMessage()]);
115+
$response = ['success' => false, 'message' => $e->getMessage()];
105116
}
117+
return $this->serializer->serialize($response);
106118
}
107119

108120
/**

setup/src/Magento/Setup/Test/Unit/Controller/MarketplaceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function testSaveAuthJsonAction()
3939
$this->packagesAuth
4040
->expects($this->once())
4141
->method('checkCredentials')
42-
->will($this->returnValue(\Zend_Json::encode(['success' => true])));
42+
->will($this->returnValue(json_encode(['success' => true])));
4343
$this->packagesAuth
4444
->expects($this->once())
4545
->method('saveAuthJson')
@@ -75,7 +75,7 @@ public function testCheckAuthAction()
7575
$this->packagesAuth
7676
->expects($this->once())
7777
->method('checkCredentials')
78-
->will($this->returnValue(\Zend_Json::encode(['success' => true])));
78+
->will($this->returnValue(json_encode(['success' => true])));
7979
$jsonModel = $this->controller->checkAuthAction();
8080
$this->assertInstanceOf(\Zend\View\Model\ViewModel::class, $jsonModel);
8181
$variables = $jsonModel->getVariables();

setup/src/Magento/Setup/Test/Unit/Model/PackagesAuthTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class PackagesAuthTest extends \PHPUnit_Framework_TestCase
2828
*/
2929
private $packagesAuth;
3030

31+
/** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
32+
private $serializerMock;
33+
3134
public function setUp()
3235
{
3336
$zendServiceLocator = $this->getMock(\Zend\ServiceManager\ServiceLocatorInterface::class, [], [], '', false);
@@ -42,7 +45,21 @@ public function setUp()
4245
]);
4346
$this->curl = $this->getMock(\Magento\Framework\HTTP\Client\Curl::class, [], [], '', false);
4447
$this->filesystem = $this->getMock(\Magento\Framework\Filesystem::class, [], [], '', false);
45-
$this->packagesAuth = new PackagesAuth($zendServiceLocator, $this->curl, $this->filesystem);
48+
$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
49+
->getMock();
50+
$this->serializerMock->expects($this->any())
51+
->method('serialize')
52+
->willReturnCallback(
53+
function ($serializedData) {
54+
return json_encode($serializedData);
55+
}
56+
);
57+
$this->packagesAuth = new PackagesAuth(
58+
$zendServiceLocator,
59+
$this->curl,
60+
$this->filesystem,
61+
$this->serializerMock
62+
);
4663
}
4764

4865
public function testCheckCredentialsActionBadCredentials()

0 commit comments

Comments
 (0)