Skip to content

Commit 4ac9508

Browse files
committed
Replace Zend_json in the ThemePackageInfo class and use the new serializer instead
1 parent ab91230 commit 4ac9508

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

app/code/Magento/Theme/Model/Theme/ThemePackageInfo.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class ThemePackageInfo
2828
*/
2929
private $packageNameToFullPathMap = [];
3030

31+
/**
32+
* @var \Magento\Framework\Serialize\Serializer\Json
33+
*/
34+
private $serializer;
35+
3136
/**
3237
* Constructor
3338
*
@@ -36,18 +41,20 @@ class ThemePackageInfo
3641
*/
3742
public function __construct(
3843
ComponentRegistrar $componentRegistrar,
39-
ReadFactory $readDirFactory
44+
ReadFactory $readDirFactory,
45+
\Magento\Framework\Serialize\Serializer\Json $serializer = null
4046
) {
4147
$this->componentRegistrar = $componentRegistrar;
4248
$this->readDirFactory = $readDirFactory;
49+
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
50+
->get(\Magento\Framework\Serialize\Serializer\Json::class);
4351
}
4452

4553
/**
4654
* Get package name of a theme by its full theme path
4755
*
4856
* @param string $themePath
4957
* @return string
50-
* @throws \Zend_Json_Exception
5158
*/
5259
public function getPackageName($themePath)
5360
{
@@ -57,7 +64,7 @@ public function getPackageName($themePath)
5764
$rawData = [];
5865
$themeFile = $themeDir->readFile('composer.json');
5966
if ($themeFile) {
60-
$rawData = \Zend_Json::decode($themeFile);
67+
$rawData = $this->serializer->unserialize($themeFile);
6168
}
6269
return isset($rawData['name']) ? $rawData['name'] : '';
6370
}
@@ -83,7 +90,6 @@ public function getFullThemePath($packageName)
8390
* Initialize package name to full theme path map
8491
*
8592
* @return void
86-
* @throws \Zend_Json_Exception
8793
*/
8894
private function initializeMap()
8995
{
@@ -92,7 +98,7 @@ private function initializeMap()
9298
foreach ($themePaths as $fullThemePath => $themeDir) {
9399
$themeDirRead = $this->readDirFactory->create($themeDir);
94100
if ($themeDirRead->isExist('composer.json')) {
95-
$rawData = \Zend_Json::decode($themeDirRead->readFile('composer.json'));
101+
$rawData = $this->serializer->unserialize($themeDirRead->readFile('composer.json'));
96102
if (isset($rawData['name'])) {
97103
$this->packageNameToFullPathMap[$rawData['name']] = $fullThemePath;
98104
}

app/code/Magento/Theme/Test/Unit/Controller/Result/MessagePluginTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function testAfterRenderResult()
112112
->method('setPublicCookie')
113113
->with(
114114
MessagePlugin::MESSAGES_COOKIES_NAME,
115-
\Zend_Json::encode($messages),
115+
json_encode($messages),
116116
$cookieMetadataMock
117117
);
118118
$this->cookieManagerMock->expects($this->once())
@@ -411,7 +411,7 @@ public function testAfterRenderResultWithWrongArray()
411411
->method('setPublicCookie')
412412
->with(
413413
MessagePlugin::MESSAGES_COOKIES_NAME,
414-
\Zend_Json::encode($messages),
414+
json_encode($messages),
415415
$cookieMetadataMock
416416
);
417417
$this->cookieManagerMock->expects($this->once())

app/code/Magento/Theme/Test/Unit/Model/Theme/ThemePackageInfoTest.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class ThemePackageInfoTest extends \PHPUnit_Framework_TestCase
2929
*/
3030
private $dirReadFactory;
3131

32+
/** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
33+
private $serializerMock;
34+
3235
protected function setUp()
3336
{
3437
$this->componentRegistrar = $this->getMock(
@@ -47,20 +50,27 @@ protected function setUp()
4750
false
4851
);
4952
$this->dirReadFactory->expects($this->any())->method('create')->willReturn($this->dirRead);
53+
$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
54+
->getMock();
5055
$this->themePackageInfo = new ThemePackageInfo(
5156
$this->componentRegistrar,
52-
$this->dirReadFactory
57+
$this->dirReadFactory,
58+
$this->serializerMock
5359
);
5460
}
5561

5662
public function testGetPackageName()
5763
{
64+
$themeFileContents = '{"name": "package"}';
5865
$this->componentRegistrar->expects($this->once())->method('getPath')->willReturn('path/to/A');
5966
$this->dirRead->expects($this->once())->method('isExist')->with('composer.json')->willReturn(true);
6067
$this->dirRead->expects($this->once())
6168
->method('readFile')
6269
->with('composer.json')
63-
->willReturn('{"name": "package"}');
70+
->willReturn($themeFileContents);
71+
$this->serializerMock->expects($this->once())
72+
->method('unserialize')
73+
->willReturn(json_decode($themeFileContents, true));
6474
$this->assertEquals('package', $this->themePackageInfo->getPackageName('themeA'));
6575
}
6676

@@ -74,9 +84,13 @@ public function testGetPackageNameNonExist()
7484

7585
public function testGetFullThemePath()
7686
{
87+
$themeFileContents = '{"name": "package"}';
7788
$this->componentRegistrar->expects($this->once())->method('getPaths')->willReturn(['themeA' => 'path/to/A']);
7889
$this->dirRead->expects($this->once())->method('isExist')->willReturn(true);
79-
$this->dirRead->expects($this->once())->method('readFile')->willReturn('{"name": "package"}');
90+
$this->dirRead->expects($this->once())->method('readFile')->willReturn($themeFileContents);
91+
$this->serializerMock->expects($this->once())
92+
->method('unserialize')
93+
->willReturn(json_decode($themeFileContents, true));
8094
$this->assertEquals('themeA', $this->themePackageInfo->getFullThemePath('package'));
8195
// call one more time to make sure only initialize once
8296
$this->assertEquals('themeA', $this->themePackageInfo->getFullThemePath('package'));
@@ -90,14 +104,14 @@ public function testGetFullThemePathNonExist()
90104
$this->assertEquals('', $this->themePackageInfo->getFullThemePath('package-other'));
91105
}
92106

93-
/**
94-
* @expectedException \Zend_Json_Exception
95-
*/
96107
public function testGetPackageNameInvalidJson()
97108
{
98109
$this->componentRegistrar->expects($this->once())->method('getPath')->willReturn('path/to/A');
99110
$this->dirRead->expects($this->once())->method('isExist')->willReturn(true);
100111
$this->dirRead->expects($this->once())->method('readFile')->willReturn('{"name": }');
101-
$this->themePackageInfo->getPackageName('themeA');
112+
$this->serializerMock->expects($this->once())
113+
->method('unserialize')
114+
->willReturn(null);
115+
$this->assertEquals('', $this->themePackageInfo->getPackageName('themeA'));
102116
}
103117
}

0 commit comments

Comments
 (0)