Skip to content

Commit eaad78c

Browse files
committed
Update the CustomerScopeData changes to use serializer to fit with BiC rules
- keep the $jsonEncoder injection but remove the type, - mark the constructor as PHPMD.UnusedFormalParameter, - split out the config and the serilization to happen in two methods,
1 parent 0a7ee59 commit eaad78c

File tree

2 files changed

+114
-23
lines changed

2 files changed

+114
-23
lines changed

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

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,21 @@ class CustomerScopeData extends \Magento\Framework\View\Element\Template
2626

2727
/**
2828
* @param \Magento\Framework\View\Element\Template\Context $context
29-
* @param \Magento\Framework\Serialize\Serializer\Json $serializer
29+
* @param $jsonEncoder
3030
* @param array $data
31+
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
32+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
3133
*/
3234
public function __construct(
3335
\Magento\Framework\View\Element\Template\Context $context,
34-
\Magento\Framework\Serialize\Serializer\Json $serializer,
35-
array $data = []
36+
$jsonEncoder,
37+
array $data = [],
38+
\Magento\Framework\Serialize\Serializer\Json $serializer = null
3639
) {
3740
parent::__construct($context, $data);
3841
$this->storeManager = $context->getStoreManager();
39-
$this->serializer = $serializer;
42+
$this->serializer = $serializer?: \Magento\Framework\App\ObjectManager::getInstance()
43+
->get(\Magento\Framework\Serialize\Serializer\Json::class);
4044
}
4145

4246
/**
@@ -51,6 +55,28 @@ public function getWebsiteId()
5155
return (int)$this->_storeManager->getStore()->getWebsiteId();
5256
}
5357

58+
/**
59+
* @return array
60+
*/
61+
public function getInvalidationRules()
62+
{
63+
return [
64+
'*' => [
65+
'Magento_Customer/js/invalidation-processor' => [
66+
'invalidationRules' => [
67+
'website-rule' => [
68+
'Magento_Customer/js/invalidation-rules/website-rule' => [
69+
'scopeConfig' => [
70+
'websiteId' => $this->getWebsiteId(),
71+
]
72+
]
73+
]
74+
]
75+
]
76+
],
77+
];
78+
}
79+
5480
/**
5581
* Get the invalidation rules json encoded
5682
*
@@ -59,22 +85,6 @@ public function getWebsiteId()
5985
*/
6086
public function getSerializedInvalidationRules()
6187
{
62-
return $this->serializer->serialize(
63-
[
64-
'*' => [
65-
'Magento_Customer/js/invalidation-processor' => [
66-
'invalidationRules' => [
67-
'website-rule' => [
68-
'Magento_Customer/js/invalidation-rules/website-rule' => [
69-
'scopeConfig' => [
70-
'websiteId' => $this->getWebsiteId(),
71-
]
72-
]
73-
]
74-
]
75-
]
76-
],
77-
]
78-
);
88+
return $this->serializer->serialize($this->getInvalidationRules());
7989
}
8090
}

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

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ protected function setUp()
5454

5555
$this->model = new CustomerScopeData(
5656
$this->contextMock,
57-
$this->serializerMock,
58-
[]
57+
null,
58+
[],
59+
$this->serializerMock
5960
);
6061
}
6162

@@ -78,4 +79,84 @@ public function testGetWebsiteId()
7879

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

0 commit comments

Comments
 (0)