Skip to content

Commit 7a4e077

Browse files
author
Oleksii Korshenko
committed
MAGETWO-70775: Remove zend json from customer data #10259
- moved component configuration to the template file
1 parent 8a41c54 commit 7a4e077

File tree

3 files changed

+26
-88
lines changed

3 files changed

+26
-88
lines changed

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

Lines changed: 11 additions & 30 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,28 +23,27 @@ class CustomerScopeData extends \Magento\Framework\View\Element\Template
2023
private $storeManager;
2124

2225
/**
23-
* @var \Magento\Framework\Serialize\Serializer\Json
26+
* @var Json
2427
*/
2528
private $serializer;
2629

2730
/**
2831
* @param \Magento\Framework\View\Element\Template\Context $context
2932
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
3033
* @param array $data
31-
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
34+
* @param Json|null $serializer
3235
* @throws \RuntimeException
3336
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
3437
*/
3538
public function __construct(
3639
\Magento\Framework\View\Element\Template\Context $context,
3740
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
3841
array $data = [],
39-
\Magento\Framework\Serialize\Serializer\Json $serializer = null
42+
Json $serializer = null
4043
) {
4144
parent::__construct($context, $data);
4245
$this->storeManager = $context->getStoreManager();
43-
$this->serializer = $serializer?: \Magento\Framework\App\ObjectManager::getInstance()
44-
->get(\Magento\Framework\Serialize\Serializer\Json::class);
46+
$this->serializer = $serializer?: ObjectManager::getInstance()->get(Json::class);
4547
}
4648

4749
/**
@@ -57,35 +59,14 @@ public function getWebsiteId()
5759
}
5860

5961
/**
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
62+
* Encode invalidation rules.
8363
*
64+
* @param array $configuration
8465
* @return bool|string
8566
* @throws \InvalidArgumentException
8667
*/
87-
public function getSerializedInvalidationRules()
68+
public function encodeConfiguration(array $configuration)
8869
{
89-
return $this->serializer->serialize($this->getInvalidationRules());
70+
return $this->serializer->serialize($configuration);
9071
}
9172
}

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

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -85,46 +85,8 @@ public function testGetWebsiteId()
8585
$this->assertEquals($storeId, $this->model->getWebsiteId());
8686
}
8787

88-
public function testGetInvalidationRules()
88+
public function testEncodeConfiguration()
8989
{
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;
12890
$rules = [
12991
'*' => [
13092
'Magento_Customer/js/invalidation-processor' => [
@@ -141,27 +103,14 @@ public function testGetSerializedInvalidationRules()
141103
],
142104
];
143105

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-
157106
$this->serializerMock->expects($this->any())
158107
->method('serialize')
159108
->with($rules)
160109
->willReturn(json_encode($rules));
161110

162111
$this->assertEquals(
163112
json_encode($rules),
164-
$this->model->getSerializedInvalidationRules()
113+
$this->model->encodeConfiguration($rules)
165114
);
166115
}
167116
}

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@
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 $block->getSerializedInvalidationRules();
11+
<?= /* @noEscape */ $block->encodeConfiguration([
12+
'*' => ['Magento_Customer/js/invalidation-processor' => [
13+
'invalidationRules' => [
14+
'website-rule' => [
15+
'Magento_Customer/js/invalidation-rules/website-rule' => [
16+
'scopeConfig' => [
17+
'websiteId' => $block->getWebsiteId(),
18+
]
19+
]
20+
]
21+
]
22+
]],
23+
]);
1624
?>
1725
</script>

0 commit comments

Comments
 (0)