Skip to content

Commit

Permalink
MAGETWO-64898: [GitHub][PR] Remove Zend_Json from Customer module #8617
Browse files Browse the repository at this point in the history
  • Loading branch information
Magento CICD authored Feb 21, 2017
2 parents 15de93d + 7dd8d4c commit c74b6b7
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
26 changes: 24 additions & 2 deletions app/code/Magento/Customer/Block/Account/AuthenticationPopup.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,34 @@ class AuthenticationPopup extends \Magento\Framework\View\Element\Template
*/
protected $jsLayout;

/**
* @var \Magento\Framework\Serialize\Serializer\Json
*/
private $serializer;

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param array $data
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @throws \RuntimeException
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
array $data = []
array $data = [],
\Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
parent::__construct($context, $data);
$this->jsLayout = isset($data['jsLayout']) && is_array($data['jsLayout']) ? $data['jsLayout'] : [];
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
}

/**
* @return string
*/
public function getJsLayout()
{
return \Zend_Json::encode($this->jsLayout);
return $this->serializer->serialize($this->jsLayout);
}

/**
Expand All @@ -50,6 +60,18 @@ public function getConfig()
];
}

/**
* Returns popup config in JSON format.
*
* Added in scope of https://github.com/magento/magento2/pull/8617
*
* @return bool|string
*/
public function getSerializedConfig()
{
return $this->serializer->serialize($this->getConfig());
}

/**
* Is autocomplete enabled for storefront
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class AuthenticationPopupTest extends \PHPUnit_Framework_TestCase
/** @var UrlInterface|\PHPUnit_Framework_MockObject_MockObject */
private $urlBuilderMock;

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

protected function setUp()
{
$this->contextMock = $this->getMockBuilder(Context::class)
Expand Down Expand Up @@ -72,8 +75,13 @@ function ($string) {
->method('getEscaper')
->willReturn($escaperMock);

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

$this->model = new AuthenticationPopup(
$this->contextMock
$this->contextMock,
[],
$this->serializerMock
);
}

Expand All @@ -83,6 +91,7 @@ function ($string) {
* @param string $registerUrl
* @param string $forgotUrl
* @param array $result
* @throws \PHPUnit_Framework_Exception
*
* @dataProvider dataProviderGetConfig
*/
Expand Down Expand Up @@ -172,4 +181,51 @@ public function dataProviderGetConfig()
],
];
}

/**
* @param mixed $isAutocomplete
* @param string $baseUrl
* @param string $registerUrl
* @param string $forgotUrl
* @param array $result
* @throws \PHPUnit_Framework_Exception
*
* @dataProvider dataProviderGetConfig
*/
public function testGetSerializedConfig($isAutocomplete, $baseUrl, $registerUrl, $forgotUrl, array $result)
{
$this->scopeConfigMock->expects($this->any())
->method('getValue')
->with(Form::XML_PATH_ENABLE_AUTOCOMPLETE, ScopeInterface::SCOPE_STORE, null)
->willReturn($isAutocomplete);

/** @var StoreInterface||\PHPUnit_Framework_MockObject_MockObject $storeMock */
$storeMock = $this->getMockBuilder(StoreInterface::class)
->setMethods(['getBaseUrl'])
->getMockForAbstractClass();

$this->storeManagerMock->expects($this->any())
->method('getStore')
->with(null)
->willReturn($storeMock);

$storeMock->expects($this->any())
->method('getBaseUrl')
->willReturn($baseUrl);

$this->urlBuilderMock->expects($this->any())
->method('getUrl')
->willReturnMap(
[
['customer/account/create', [], $registerUrl],
['customer/account/forgotpassword', [], $forgotUrl],
]
);
$this->serializerMock->expects($this->any())->method('serialize')
->willReturn(
json_encode($this->model->getConfig())
);

$this->assertEquals(json_encode($result), $this->model->getSerializedConfig());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
?>
<div id="authenticationPopup" data-bind="scope:'authenticationPopup'" style="display: none;">
<script>
window.authenticationPopup = <?php /* @noEscape */ echo \Zend_Json::encode($block->getConfig()); ?>;
window.authenticationPopup = <?php /* @noEscape */ echo $block->getSerializedConfig(); ?>;
</script>
<!-- ko template: getTemplate() --><!-- /ko -->
<script type="text/x-magento-init">
Expand Down

0 comments on commit c74b6b7

Please sign in to comment.