Skip to content

Commit

Permalink
MAGETWO-70775: Remove zend json from customer data #10259
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksii Korshenko authored Jul 19, 2017
2 parents 9f812f6 + 7a4e077 commit d83e0e9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
27 changes: 23 additions & 4 deletions app/code/Magento/Customer/Block/CustomerScopeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\Customer\Block;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Serialize\Serializer\Json;

/**
* Class CustomerScopeData provide scope (website, store or store_group) information on front
* Can be used, for example, on store front, in order to determine
Expand All @@ -20,23 +23,27 @@ class CustomerScopeData extends \Magento\Framework\View\Element\Template
private $storeManager;

/**
* @var \Magento\Framework\Json\EncoderInterface
* @var Json
*/
private $jsonEncoder;
private $serializer;

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
* @param array $data
* @param Json|null $serializer
* @throws \RuntimeException
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
array $data = []
array $data = [],
Json $serializer = null
) {
parent::__construct($context, $data);
$this->storeManager = $context->getStoreManager();
$this->jsonEncoder = $jsonEncoder;
$this->serializer = $serializer?: ObjectManager::getInstance()->get(Json::class);
}

/**
Expand All @@ -50,4 +57,16 @@ public function getWebsiteId()
{
return (int)$this->_storeManager->getStore()->getWebsiteId();
}

/**
* Encode invalidation rules.
*
* @param array $configuration
* @return bool|string
* @throws \InvalidArgumentException
*/
public function encodeConfiguration(array $configuration)
{
return $this->serializer->serialize($configuration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Customer\Block\CustomerScopeData;
use Magento\Framework\Json\EncoderInterface;

class CustomerScopeDataTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -29,6 +28,9 @@ class CustomerScopeDataTest extends \PHPUnit_Framework_TestCase
/** @var \Magento\Framework\Json\EncoderInterface|\PHPUnit_Framework_MockObject_MockObject */
private $encoderMock;

/** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
private $serializerMock;

protected function setUp()
{
$this->contextMock = $this->getMockBuilder(Context::class)
Expand All @@ -41,7 +43,10 @@ protected function setUp()
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
->getMock();

$this->encoderMock = $this->getMockBuilder(EncoderInterface::class)
$this->encoderMock = $this->getMockBuilder(\Magento\Framework\Json\EncoderInterface::class)
->getMock();

$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
->getMock();

$this->contextMock->expects($this->exactly(2))
Expand All @@ -55,7 +60,8 @@ protected function setUp()
$this->model = new CustomerScopeData(
$this->contextMock,
$this->encoderMock,
[]
[],
$this->serializerMock
);
}

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

$this->assertEquals($storeId, $this->model->getWebsiteId());
}

public function testEncodeConfiguration()
{
$rules = [
'*' => [
'Magento_Customer/js/invalidation-processor' => [
'invalidationRules' => [
'website-rule' => [
'Magento_Customer/js/invalidation-rules/website-rule' => [
'scopeConfig' => [
'websiteId' => 1,
]
]
]
]
]
],
];

$this->serializerMock->expects($this->any())
->method('serialize')
->with($rules)
->willReturn(json_encode($rules));

$this->assertEquals(
json_encode($rules),
$this->model->encodeConfiguration($rules)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
*/

// @codingStandardsIgnoreFile
?>
<?php
/* @var $block \Magento\Customer\Block\CustomerScopeData */
?>
<script type="text/x-magento-init">
<?php
/* @noEscape */
echo \Zend_Json::encode([
<?= /* @noEscape */ $block->encodeConfiguration([
'*' => ['Magento_Customer/js/invalidation-processor' => [
'invalidationRules' => [
'website-rule' => [
Expand All @@ -25,5 +21,5 @@
]
]],
]);
?>
?>
</script>

0 comments on commit d83e0e9

Please sign in to comment.