From d92845a648efc9fee1e79f2179d93664c8c6bc68 Mon Sep 17 00:00:00 2001 From: dmanners Date: Fri, 3 Feb 2017 14:11:02 +0000 Subject: [PATCH 1/9] Remove the use of Zend_Json from the Onepage checkout block --- app/code/Magento/Checkout/Block/Onepage.php | 13 +++++++++++-- .../Checkout/Test/Unit/Block/OnepageTest.php | 11 ++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Checkout/Block/Onepage.php b/app/code/Magento/Checkout/Block/Onepage.php index 15e913d9338d0..d5fbb04155b5e 100644 --- a/app/code/Magento/Checkout/Block/Onepage.php +++ b/app/code/Magento/Checkout/Block/Onepage.php @@ -36,19 +36,26 @@ class Onepage extends \Magento\Framework\View\Element\Template */ protected $layoutProcessors; + /** + * @var \Magento\Framework\Serialize\SerializerInterface + */ + 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\SerializerInterface|null $serializer */ 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\SerializerInterface $serializer = null ) { parent::__construct($context, $data); $this->formKey = $formKey; @@ -56,6 +63,8 @@ public function __construct( $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\SerializerInterface::class); } /** @@ -66,7 +75,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); } /** diff --git a/app/code/Magento/Checkout/Test/Unit/Block/OnepageTest.php b/app/code/Magento/Checkout/Test/Unit/Block/OnepageTest.php index 47fddfeb4a827..9936dc5157af7 100644 --- a/app/code/Magento/Checkout/Test/Unit/Block/OnepageTest.php +++ b/app/code/Magento/Checkout/Test/Unit/Block/OnepageTest.php @@ -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); @@ -54,11 +59,15 @@ protected function setUp() false ); + $this->serializer = new \Magento\Framework\Serialize\Serializer\Json(); + $this->model = new \Magento\Checkout\Block\Onepage( $contextMock, $this->formKeyMock, $this->configProviderMock, - [$this->layoutProcessorMock] + [$this->layoutProcessorMock], + [], + $this->serializer ); } From 248cd85617e857e5dcae35531a33e9806d63fc07 Mon Sep 17 00:00:00 2001 From: dmanners Date: Fri, 3 Feb 2017 14:15:50 +0000 Subject: [PATCH 2/9] Mock the serialize method return value --- app/code/Magento/Checkout/Test/Unit/Block/OnepageTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Checkout/Test/Unit/Block/OnepageTest.php b/app/code/Magento/Checkout/Test/Unit/Block/OnepageTest.php index 9936dc5157af7..25b00379e5c44 100644 --- a/app/code/Magento/Checkout/Test/Unit/Block/OnepageTest.php +++ b/app/code/Magento/Checkout/Test/Unit/Block/OnepageTest.php @@ -59,7 +59,7 @@ protected function setUp() false ); - $this->serializer = new \Magento\Framework\Serialize\Serializer\Json(); + $this->serializer = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class,[],[],'',false); $this->model = new \Magento\Checkout\Block\Onepage( $contextMock, @@ -103,6 +103,9 @@ 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()); } From 072dde798832c708359c33c578f7990040e97f64 Mon Sep 17 00:00:00 2001 From: dmanners Date: Fri, 3 Feb 2017 14:27:31 +0000 Subject: [PATCH 3/9] Remove Zend_Json usage from the cart shipping block --- app/code/Magento/Checkout/Block/Cart/Shipping.php | 13 +++++++++++-- app/code/Magento/Checkout/Block/Onepage.php | 7 ++++--- .../Checkout/Test/Unit/Block/Cart/ShippingTest.php | 14 +++++++++++++- .../Checkout/Test/Unit/Block/OnepageTest.php | 4 ++-- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/app/code/Magento/Checkout/Block/Cart/Shipping.php b/app/code/Magento/Checkout/Block/Cart/Shipping.php index a7f808b9ccb5e..5b0d231bbdfbe 100644 --- a/app/code/Magento/Checkout/Block/Cart/Shipping.php +++ b/app/code/Magento/Checkout/Block/Cart/Shipping.php @@ -20,14 +20,20 @@ class Shipping extends \Magento\Checkout\Block\Cart\AbstractCart */ protected $layoutProcessors; + /** + * @var \Magento\Framework\Serialize\SerializerInterface + */ + 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 \Magento\Framework\Serialize\SerializerInterface|null $serializer * @param array $data - * @codeCoverageIgnore + * @throws \RuntimeException */ public function __construct( \Magento\Framework\View\Element\Template\Context $context, @@ -35,12 +41,15 @@ public function __construct( \Magento\Checkout\Model\Session $checkoutSession, \Magento\Checkout\Model\CompositeConfigProvider $configProvider, array $layoutProcessors = [], + \Magento\Framework\Serialize\SerializerInterface $serializer = null, array $data = [] ) { $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\SerializerInterface::class); } /** @@ -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); } /** diff --git a/app/code/Magento/Checkout/Block/Onepage.php b/app/code/Magento/Checkout/Block/Onepage.php index d5fbb04155b5e..61001abcc70d7 100644 --- a/app/code/Magento/Checkout/Block/Onepage.php +++ b/app/code/Magento/Checkout/Block/Onepage.php @@ -46,16 +46,17 @@ class Onepage extends \Magento\Framework\View\Element\Template * @param \Magento\Framework\Data\Form\FormKey $formKey * @param \Magento\Checkout\Model\CompositeConfigProvider $configProvider * @param array $layoutProcessors - * @param array $data * @param \Magento\Framework\Serialize\SerializerInterface|null $serializer + * @param array $data + * @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 = [], - \Magento\Framework\Serialize\SerializerInterface $serializer = null + \Magento\Framework\Serialize\SerializerInterface $serializer = null, + array $data = [] ) { parent::__construct($context, $data); $this->formKey = $formKey; diff --git a/app/code/Magento/Checkout/Test/Unit/Block/Cart/ShippingTest.php b/app/code/Magento/Checkout/Test/Unit/Block/Cart/ShippingTest.php index 8313695ff8e76..f30ea4a9c578a 100644 --- a/app/code/Magento/Checkout/Test/Unit/Block/Cart/ShippingTest.php +++ b/app/code/Magento/Checkout/Test/Unit/Block/Cart/ShippingTest.php @@ -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); @@ -69,6 +74,7 @@ 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\SerializerInterface::class,[],[],'',false); $this->model = new \Magento\Checkout\Block\Cart\Shipping( $this->context, @@ -76,6 +82,7 @@ protected function setUp() $this->checkoutSession, $this->configProvider, [$this->layoutProcessor], + $this->serializer, ['jsLayout' => $this->layout] ); } @@ -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() ); } diff --git a/app/code/Magento/Checkout/Test/Unit/Block/OnepageTest.php b/app/code/Magento/Checkout/Test/Unit/Block/OnepageTest.php index 25b00379e5c44..0ef5e89669dad 100644 --- a/app/code/Magento/Checkout/Test/Unit/Block/OnepageTest.php +++ b/app/code/Magento/Checkout/Test/Unit/Block/OnepageTest.php @@ -66,8 +66,8 @@ protected function setUp() $this->formKeyMock, $this->configProviderMock, [$this->layoutProcessorMock], - [], - $this->serializer + $this->serializer, + [] ); } From 287c3150bd793cc1bc3e94512f0423e8e64b470f Mon Sep 17 00:00:00 2001 From: dmanners Date: Fri, 3 Feb 2017 14:32:16 +0000 Subject: [PATCH 4/9] Remove Zend_Json usage from the onepage.phtml template --- app/code/Magento/Checkout/Block/Onepage.php | 8 ++++++++ .../Magento/Checkout/Test/Unit/Block/OnepageTest.php | 11 +++++++++++ .../Checkout/view/frontend/templates/onepage.phtml | 2 +- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Checkout/Block/Onepage.php b/app/code/Magento/Checkout/Block/Onepage.php index 61001abcc70d7..d9b1e9d08ad6c 100644 --- a/app/code/Magento/Checkout/Block/Onepage.php +++ b/app/code/Magento/Checkout/Block/Onepage.php @@ -111,4 +111,12 @@ public function getBaseUrl() { return $this->_storeManager->getStore()->getBaseUrl(); } + + /** + * @return bool|string + */ + public function getSerializedCheckoutConfig() + { + return $this->serializer->serialize($this->getCheckoutConfig()); + } } diff --git a/app/code/Magento/Checkout/Test/Unit/Block/OnepageTest.php b/app/code/Magento/Checkout/Test/Unit/Block/OnepageTest.php index 0ef5e89669dad..ac50ce76c45ad 100644 --- a/app/code/Magento/Checkout/Test/Unit/Block/OnepageTest.php +++ b/app/code/Magento/Checkout/Test/Unit/Block/OnepageTest.php @@ -109,4 +109,15 @@ public function testGetJsLayout() $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()); + } } diff --git a/app/code/Magento/Checkout/view/frontend/templates/onepage.phtml b/app/code/Magento/Checkout/view/frontend/templates/onepage.phtml index bf9bcb15c3ec8..6f1931583fb57 100644 --- a/app/code/Magento/Checkout/view/frontend/templates/onepage.phtml +++ b/app/code/Magento/Checkout/view/frontend/templates/onepage.phtml @@ -23,7 +23,7 @@ }