Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove zend json checkout #8481

Merged
merged 9 commits into from
Feb 19, 2017
23 changes: 20 additions & 3 deletions app/code/Magento/Checkout/Block/Cart/Shipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,36 @@ class Shipping extends \Magento\Checkout\Block\Cart\AbstractCart
*/
protected $layoutProcessors;

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

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Checkout\Model\CompositeConfigProvider $configProvider
* @param array $layoutProcessors
* @param array $data
* @codeCoverageIgnore
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @throws \RuntimeException
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Customer\Model\Session $customerSession,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Checkout\Model\CompositeConfigProvider $configProvider,
array $layoutProcessors = [],
array $data = []
array $data = [],
\Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
$this->configProvider = $configProvider;
$this->layoutProcessors = $layoutProcessors;
parent::__construct($context, $customerSession, $checkoutSession, $data);
$this->_isScopePrivate = true;
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
}

/**
Expand All @@ -64,7 +73,7 @@ public function getJsLayout()
foreach ($this->layoutProcessors as $processor) {
$this->jsLayout = $processor->process($this->jsLayout);
}
return \Zend_Json::encode($this->jsLayout);
return $this->serializer->serialize($this->jsLayout);
}

/**
Expand All @@ -77,4 +86,12 @@ public function getBaseUrl()
{
return $this->_storeManager->getStore()->getBaseUrl();
}

/**
* @return bool|string
*/
public function getSerializedCheckoutConfig()
{
return $this->serializer->serialize($this->getCheckoutConfig());
}
}
21 changes: 19 additions & 2 deletions app/code/Magento/Checkout/Block/Cart/Sidebar.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,29 @@ class Sidebar extends AbstractCart
*/
protected $imageHelper;

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

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Catalog\Helper\Image $imageHelper
* @param \Magento\Customer\CustomerData\JsLayoutDataProviderPoolInterface $jsLayoutDataProvider
* @param array $data
* @codeCoverageIgnore
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @throws \RuntimeException
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Customer\Model\Session $customerSession,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Catalog\Helper\Image $imageHelper,
\Magento\Customer\CustomerData\JsLayoutDataProviderPoolInterface $jsLayoutDataProvider,
array $data = []
array $data = [],
\Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
if (isset($data['jsLayout'])) {
$this->jsLayout = array_merge_recursive($jsLayoutDataProvider->getData(), $data['jsLayout']);
Expand All @@ -53,6 +60,8 @@ public function __construct(
parent::__construct($context, $customerSession, $checkoutSession, $data);
$this->_isScopePrivate = false;
$this->imageHelper = $imageHelper;
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
}

/**
Expand All @@ -74,6 +83,14 @@ public function getConfig()
];
}

/**
* @return string
*/
public function getSerializedConfig()
{
return $this->serializer->serialize($this->getConfig());
}

/**
* @return string
*/
Expand Down
22 changes: 20 additions & 2 deletions app/code/Magento/Checkout/Block/Onepage.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,36 @@ class Onepage extends \Magento\Framework\View\Element\Template
*/
protected $layoutProcessors;

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

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

/**
Expand All @@ -66,7 +76,7 @@ public function getJsLayout()
foreach ($this->layoutProcessors as $processor) {
$this->jsLayout = $processor->process($this->jsLayout);
}
return \Zend_Json::encode($this->jsLayout);
return $this->serializer->serialize($this->jsLayout);
}

/**
Expand Down Expand Up @@ -101,4 +111,12 @@ public function getBaseUrl()
{
return $this->_storeManager->getStore()->getBaseUrl();
}

/**
* @return bool|string
*/
public function getSerializedCheckoutConfig()
{
return $this->serializer->serialize($this->getCheckoutConfig());
}
}
27 changes: 25 additions & 2 deletions app/code/Magento/Checkout/Test/Unit/Block/Cart/ShippingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class ShippingTest extends \PHPUnit_Framework_TestCase
*/
protected $layout;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $serializer;

protected function setUp()
{
$this->context = $this->getMock(\Magento\Framework\View\Element\Template\Context::class, [], [], '', false);
Expand All @@ -69,14 +74,16 @@ protected function setUp()

$this->storeManager = $this->getMock(\Magento\Store\Model\StoreManagerInterface::class);
$this->context->expects($this->once())->method('getStoreManager')->willReturn($this->storeManager);
$this->serializer = $this->getMock(\Magento\Framework\Serialize\Serializer\Json::class, [], [], '', false);

$this->model = new \Magento\Checkout\Block\Cart\Shipping(
$this->context,
$this->customerSession,
$this->checkoutSession,
$this->configProvider,
[$this->layoutProcessor],
['jsLayout' => $this->layout]
['jsLayout' => $this->layout],
$this->serializer
);
}

Expand All @@ -91,13 +98,18 @@ public function testGetJsLayout()
{
$layoutProcessed = $this->layout;
$layoutProcessed['components']['thirdComponent'] = ['param' => 'value'];
$jsonLayoutProcessed = json_encode($layoutProcessed);

$this->layoutProcessor->expects($this->once())
->method('process')
->with($this->layout)
->willReturn($layoutProcessed);

$this->serializer->expects($this->once())->method('serialize')->will(
$this->returnValue($jsonLayoutProcessed)
);
$this->assertEquals(
\Zend_Json::encode($layoutProcessed),
$jsonLayoutProcessed,
$this->model->getJsLayout()
);
}
Expand All @@ -110,4 +122,15 @@ public function testGetBaseUrl()
$this->storeManager->expects($this->once())->method('getStore')->willReturn($storeMock);
$this->assertEquals($baseUrl, $this->model->getBaseUrl());
}

public function testGetSerializedCheckoutConfig()
{
$checkoutConfig = ['checkout', 'config'];
$this->configProvider->expects($this->once())->method('getConfig')->willReturn($checkoutConfig);
$this->serializer->expects($this->once())->method('serialize')->will(
$this->returnValue(json_encode($checkoutConfig))
);

$this->assertEquals(json_encode($checkoutConfig), $this->model->getSerializedCheckoutConfig());
}
}
10 changes: 9 additions & 1 deletion app/code/Magento/Checkout/Test/Unit/Block/Cart/SidebarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class SidebarTest extends \PHPUnit_Framework_TestCase
*/
protected $requestMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $serializer;

protected function setUp()
{
$this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
Expand Down Expand Up @@ -94,12 +99,15 @@ protected function setUp()
->method('getRequest')
->will($this->returnValue($this->requestMock));

$this->serializer = $this->getMock(\Magento\Framework\Serialize\Serializer\Json::class, [], [], '', false);

$this->model = $this->_objectManager->getObject(
\Magento\Checkout\Block\Cart\Sidebar::class,
[
'context' => $contextMock,
'imageHelper' => $this->imageHelper,
'checkoutSession' => $this->checkoutSessionMock
'checkoutSession' => $this->checkoutSessionMock,
'serializer' => $this->serializer
]
);
}
Expand Down
25 changes: 24 additions & 1 deletion app/code/Magento/Checkout/Test/Unit/Block/OnepageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class OnepageTest extends \PHPUnit_Framework_TestCase
*/
protected $layoutProcessorMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $serializer;

protected function setUp()
{
$contextMock = $this->getMock(\Magento\Framework\View\Element\Template\Context::class, [], [], '', false);
Expand All @@ -54,11 +59,15 @@ protected function setUp()
false
);

$this->serializer = $this->getMock(\Magento\Framework\Serialize\Serializer\Json::class, [], [], '', false);

$this->model = new \Magento\Checkout\Block\Onepage(
$contextMock,
$this->formKeyMock,
$this->configProviderMock,
[$this->layoutProcessorMock]
[$this->layoutProcessorMock],
[],
$this->serializer
);
}

Expand Down Expand Up @@ -94,7 +103,21 @@ public function testGetJsLayout()
$processedLayout = ['layout' => ['processed' => true]];
$jsonLayout = '{"layout":{"processed":true}}';
$this->layoutProcessorMock->expects($this->once())->method('process')->with([])->willReturn($processedLayout);
$this->serializer->expects($this->once())->method('serialize')->will(
$this->returnValue(json_encode($processedLayout))
);

$this->assertEquals($jsonLayout, $this->model->getJsLayout());
}

public function testGetSerializedCheckoutConfig()
{
$checkoutConfig = ['checkout', 'config'];
$this->configProviderMock->expects($this->once())->method('getConfig')->willReturn($checkoutConfig);
$this->serializer->expects($this->once())->method('serialize')->will(
$this->returnValue(json_encode($checkoutConfig))
);

$this->assertEquals(json_encode($checkoutConfig), $this->model->getSerializedCheckoutConfig());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
</div>
<?php endif ?>
<script>
window.checkout = <?php /* @escapeNotVerified */ echo \Zend_Json::encode($block->getConfig()); ?>;
window.checkout = <?php /* @escapeNotVerified */ echo $block->getSerializedConfig(); ?>;
</script>
<script type="text/x-magento-init">
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
}
</script>
<script>
window.checkoutConfig = <?php /* @escapeNotVerified */ echo \Zend_Json::encode($block->getCheckoutConfig()); ?>;
window.checkoutConfig = <?php /* @escapeNotVerified */ echo $block->getSerializedCheckoutConfig(); ?>;
window.customerData = window.checkoutConfig.customerData;
window.isCustomerLoggedIn = window.checkoutConfig.isCustomerLoggedIn;
require([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}
</script>
<script>
window.checkoutConfig = <?php /* @escapeNotVerified */ echo \Zend_Json::encode($block->getCheckoutConfig()); ?>;
window.checkoutConfig = <?php /* @escapeNotVerified */ echo $block->getSerializedCheckoutConfig(); ?>;
// Create aliases for customer.js model from customer module
window.isCustomerLoggedIn = window.checkoutConfig.isCustomerLoggedIn;
window.customerData = window.checkoutConfig.customerData;
Expand Down