Skip to content

Commit 8a41c54

Browse files
author
Oleksii Korshenko
committed
MAGETWO-70775: Remove zend json from customer data #10259
- Merge Pull Request #10259 from dmanners/magento2:remove-zend-json-from-customer-data - Merged commits: 1. 2e72fb0 2. 0a7ee59 3. eaad78c 4. 5568b8d 5. 31764a5
2 parents 929a852 + 31764a5 commit 8a41c54

File tree

3 files changed

+133
-21
lines changed

3 files changed

+133
-21
lines changed

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

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,28 @@ class CustomerScopeData extends \Magento\Framework\View\Element\Template
2020
private $storeManager;
2121

2222
/**
23-
* @var \Magento\Framework\Json\EncoderInterface
23+
* @var \Magento\Framework\Serialize\Serializer\Json
2424
*/
25-
private $jsonEncoder;
25+
private $serializer;
2626

2727
/**
2828
* @param \Magento\Framework\View\Element\Template\Context $context
2929
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
3030
* @param array $data
31+
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
32+
* @throws \RuntimeException
33+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
3134
*/
3235
public function __construct(
3336
\Magento\Framework\View\Element\Template\Context $context,
3437
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
35-
array $data = []
38+
array $data = [],
39+
\Magento\Framework\Serialize\Serializer\Json $serializer = null
3640
) {
3741
parent::__construct($context, $data);
3842
$this->storeManager = $context->getStoreManager();
39-
$this->jsonEncoder = $jsonEncoder;
43+
$this->serializer = $serializer?: \Magento\Framework\App\ObjectManager::getInstance()
44+
->get(\Magento\Framework\Serialize\Serializer\Json::class);
4045
}
4146

4247
/**
@@ -50,4 +55,37 @@ public function getWebsiteId()
5055
{
5156
return (int)$this->_storeManager->getStore()->getWebsiteId();
5257
}
58+
59+
/**
60+
* @return array
61+
*/
62+
public function getInvalidationRules()
63+
{
64+
return [
65+
'*' => [
66+
'Magento_Customer/js/invalidation-processor' => [
67+
'invalidationRules' => [
68+
'website-rule' => [
69+
'Magento_Customer/js/invalidation-rules/website-rule' => [
70+
'scopeConfig' => [
71+
'websiteId' => $this->getWebsiteId(),
72+
]
73+
]
74+
]
75+
]
76+
]
77+
],
78+
];
79+
}
80+
81+
/**
82+
* Get the invalidation rules json encoded
83+
*
84+
* @return bool|string
85+
* @throws \InvalidArgumentException
86+
*/
87+
public function getSerializedInvalidationRules()
88+
{
89+
return $this->serializer->serialize($this->getInvalidationRules());
90+
}
5391
}

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

Lines changed: 89 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,84 @@ public function testGetWebsiteId()
7884

7985
$this->assertEquals($storeId, $this->model->getWebsiteId());
8086
}
87+
88+
public function testGetInvalidationRules()
89+
{
90+
$storeId = 1;
91+
92+
$storeMock = $this->getMockBuilder(StoreInterface::class)
93+
->setMethods(['getWebsiteId'])
94+
->getMockForAbstractClass();
95+
96+
$storeMock->expects($this->any())
97+
->method('getWebsiteId')
98+
->willReturn($storeId);
99+
100+
$this->storeManagerMock->expects($this->any())
101+
->method('getStore')
102+
->with(null)
103+
->willReturn($storeMock);
104+
105+
$this->assertEquals(
106+
[
107+
'*' => [
108+
'Magento_Customer/js/invalidation-processor' => [
109+
'invalidationRules' => [
110+
'website-rule' => [
111+
'Magento_Customer/js/invalidation-rules/website-rule' => [
112+
'scopeConfig' => [
113+
'websiteId' => 1,
114+
]
115+
]
116+
]
117+
]
118+
]
119+
],
120+
],
121+
$this->model->getInvalidationRules()
122+
);
123+
}
124+
125+
public function testGetSerializedInvalidationRules()
126+
{
127+
$storeId = 1;
128+
$rules = [
129+
'*' => [
130+
'Magento_Customer/js/invalidation-processor' => [
131+
'invalidationRules' => [
132+
'website-rule' => [
133+
'Magento_Customer/js/invalidation-rules/website-rule' => [
134+
'scopeConfig' => [
135+
'websiteId' => 1,
136+
]
137+
]
138+
]
139+
]
140+
]
141+
],
142+
];
143+
144+
$storeMock = $this->getMockBuilder(StoreInterface::class)
145+
->setMethods(['getWebsiteId'])
146+
->getMockForAbstractClass();
147+
148+
$storeMock->expects($this->any())
149+
->method('getWebsiteId')
150+
->willReturn($storeId);
151+
152+
$this->storeManagerMock->expects($this->any())
153+
->method('getStore')
154+
->with(null)
155+
->willReturn($storeMock);
156+
157+
$this->serializerMock->expects($this->any())
158+
->method('serialize')
159+
->with($rules)
160+
->willReturn(json_encode($rules));
161+
162+
$this->assertEquals(
163+
json_encode($rules),
164+
$this->model->getSerializedInvalidationRules()
165+
);
166+
}
81167
}

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,6 @@
1212
<script type="text/x-magento-init">
1313
<?php
1414
/* @noEscape */
15-
echo \Zend_Json::encode([
16-
'*' => ['Magento_Customer/js/invalidation-processor' => [
17-
'invalidationRules' => [
18-
'website-rule' => [
19-
'Magento_Customer/js/invalidation-rules/website-rule' => [
20-
'scopeConfig' => [
21-
'websiteId' => $block->getWebsiteId(),
22-
]
23-
]
24-
]
25-
]
26-
]],
27-
]);
28-
?>
15+
echo $block->getSerializedInvalidationRules();
16+
?>
2917
</script>

0 commit comments

Comments
 (0)