Skip to content

Commit d83e0e9

Browse files
author
Oleksii Korshenko
authored
MAGETWO-70775: Remove zend json from customer data #10259
2 parents 9f812f6 + 7a4e077 commit d83e0e9

File tree

3 files changed

+63
-13
lines changed

3 files changed

+63
-13
lines changed

app/code/Magento/Customer/Block/CustomerScopeData.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Customer\Block;
77

8+
use Magento\Framework\App\ObjectManager;
9+
use Magento\Framework\Serialize\Serializer\Json;
10+
811
/**
912
* Class CustomerScopeData provide scope (website, store or store_group) information on front
1013
* Can be used, for example, on store front, in order to determine
@@ -20,23 +23,27 @@ class CustomerScopeData extends \Magento\Framework\View\Element\Template
2023
private $storeManager;
2124

2225
/**
23-
* @var \Magento\Framework\Json\EncoderInterface
26+
* @var Json
2427
*/
25-
private $jsonEncoder;
28+
private $serializer;
2629

2730
/**
2831
* @param \Magento\Framework\View\Element\Template\Context $context
2932
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
3033
* @param array $data
34+
* @param Json|null $serializer
35+
* @throws \RuntimeException
36+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
3137
*/
3238
public function __construct(
3339
\Magento\Framework\View\Element\Template\Context $context,
3440
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
35-
array $data = []
41+
array $data = [],
42+
Json $serializer = null
3643
) {
3744
parent::__construct($context, $data);
3845
$this->storeManager = $context->getStoreManager();
39-
$this->jsonEncoder = $jsonEncoder;
46+
$this->serializer = $serializer?: ObjectManager::getInstance()->get(Json::class);
4047
}
4148

4249
/**
@@ -50,4 +57,16 @@ public function getWebsiteId()
5057
{
5158
return (int)$this->_storeManager->getStore()->getWebsiteId();
5259
}
60+
61+
/**
62+
* Encode invalidation rules.
63+
*
64+
* @param array $configuration
65+
* @return bool|string
66+
* @throws \InvalidArgumentException
67+
*/
68+
public function encodeConfiguration(array $configuration)
69+
{
70+
return $this->serializer->serialize($configuration);
71+
}
5372
}

app/code/Magento/Customer/Test/Unit/Block/CustomerScopeDataTest.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Magento\Store\Api\Data\StoreInterface;
1111
use Magento\Store\Model\StoreManagerInterface;
1212
use Magento\Customer\Block\CustomerScopeData;
13-
use Magento\Framework\Json\EncoderInterface;
1413

1514
class CustomerScopeDataTest extends \PHPUnit_Framework_TestCase
1615
{
@@ -29,6 +28,9 @@ class CustomerScopeDataTest extends \PHPUnit_Framework_TestCase
2928
/** @var \Magento\Framework\Json\EncoderInterface|\PHPUnit_Framework_MockObject_MockObject */
3029
private $encoderMock;
3130

31+
/** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
32+
private $serializerMock;
33+
3234
protected function setUp()
3335
{
3436
$this->contextMock = $this->getMockBuilder(Context::class)
@@ -41,7 +43,10 @@ protected function setUp()
4143
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
4244
->getMock();
4345

44-
$this->encoderMock = $this->getMockBuilder(EncoderInterface::class)
46+
$this->encoderMock = $this->getMockBuilder(\Magento\Framework\Json\EncoderInterface::class)
47+
->getMock();
48+
49+
$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
4550
->getMock();
4651

4752
$this->contextMock->expects($this->exactly(2))
@@ -55,7 +60,8 @@ protected function setUp()
5560
$this->model = new CustomerScopeData(
5661
$this->contextMock,
5762
$this->encoderMock,
58-
[]
63+
[],
64+
$this->serializerMock
5965
);
6066
}
6167

@@ -78,4 +84,33 @@ public function testGetWebsiteId()
7884

7985
$this->assertEquals($storeId, $this->model->getWebsiteId());
8086
}
87+
88+
public function testEncodeConfiguration()
89+
{
90+
$rules = [
91+
'*' => [
92+
'Magento_Customer/js/invalidation-processor' => [
93+
'invalidationRules' => [
94+
'website-rule' => [
95+
'Magento_Customer/js/invalidation-rules/website-rule' => [
96+
'scopeConfig' => [
97+
'websiteId' => 1,
98+
]
99+
]
100+
]
101+
]
102+
]
103+
],
104+
];
105+
106+
$this->serializerMock->expects($this->any())
107+
->method('serialize')
108+
->with($rules)
109+
->willReturn(json_encode($rules));
110+
111+
$this->assertEquals(
112+
json_encode($rules),
113+
$this->model->encodeConfiguration($rules)
114+
);
115+
}
81116
}

app/code/Magento/Customer/view/frontend/templates/js/customer-data/invalidation-rules.phtml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@
55
*/
66

77
// @codingStandardsIgnoreFile
8-
?>
9-
<?php
108
/* @var $block \Magento\Customer\Block\CustomerScopeData */
119
?>
1210
<script type="text/x-magento-init">
13-
<?php
14-
/* @noEscape */
15-
echo \Zend_Json::encode([
11+
<?= /* @noEscape */ $block->encodeConfiguration([
1612
'*' => ['Magento_Customer/js/invalidation-processor' => [
1713
'invalidationRules' => [
1814
'website-rule' => [
@@ -25,5 +21,5 @@
2521
]
2622
]],
2723
]);
28-
?>
24+
?>
2925
</script>

0 commit comments

Comments
 (0)