Skip to content

Code refactor, updated Unit Test with JsonHexTag Serializer #26136

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

Merged
merged 4 commits into from
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 38 additions & 17 deletions app/code/Magento/Checkout/Block/Cart/Shipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,73 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Checkout\Block\Cart;

use Magento\Checkout\Model\CompositeConfigProvider;
use Magento\Checkout\Block\Checkout\LayoutProcessorInterface;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\Serialize\Serializer\JsonHexTag;
use Magento\Framework\View\Element\Template\Context;
use Magento\Customer\Model\Session as customerSession;
use Magento\Checkout\Model\Session as checkoutSession;
use Magento\Framework\App\ObjectManager;

/**
* Cart Shipping Block
*
* @api
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Shipping extends \Magento\Checkout\Block\Cart\AbstractCart
{
/**
* @var \Magento\Checkout\Model\CompositeConfigProvider
* @var CompositeConfigProvider
*/
protected $configProvider;

/**
* @var array|\Magento\Checkout\Block\Checkout\LayoutProcessorInterface[]
* @var array|LayoutProcessorInterface[]
*/
protected $layoutProcessors;

/**
* @var \Magento\Framework\Serialize\Serializer\Json
* @var 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
* @var JsonHexTag
*/
private $jsonHexTagSerializer;

/**
* @param Context $context
* @param customerSession $customerSession
* @param checkoutSession $checkoutSession
* @param CompositeConfigProvider $configProvider
* @param array $layoutProcessors
* @param array $data
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @param Json|null $serializer
* @param JsonHexTag|null $jsonHexTagSerializer
* @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,
Context $context,
customerSession $customerSession,
checkoutSession $checkoutSession,
CompositeConfigProvider $configProvider,
array $layoutProcessors = [],
array $data = [],
\Magento\Framework\Serialize\Serializer\Json $serializer = null
Json $serializer = null,
JsonHexTag $jsonHexTagSerializer = 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);
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
$this->jsonHexTagSerializer = $jsonHexTagSerializer ?: ObjectManager::getInstance()->get(JsonHexTag::class);
}

/**
Expand All @@ -75,7 +94,7 @@ public function getJsLayout()
$this->jsLayout = $processor->process($this->jsLayout);
}

return json_encode($this->jsLayout, JSON_HEX_TAG);
return $this->jsonHexTagSerializer->serialize($this->jsLayout);
}

/**
Expand All @@ -90,11 +109,13 @@ public function getBaseUrl()
}

/**
* Get Serialized Checkout Config
*
* @return bool|string
* @since 100.2.0
*/
public function getSerializedCheckoutConfig()
{
return json_encode($this->getCheckoutConfig(), JSON_HEX_TAG);
return $this->jsonHexTagSerializer->serialize($this->getCheckoutConfig());
}
}
203 changes: 148 additions & 55 deletions app/code/Magento/Checkout/Test/Unit/Block/Cart/ShippingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,122 +3,215 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Checkout\Test\Unit\Block\Cart;

use Magento\Checkout\Block\Cart\Shipping;
use Magento\Checkout\Model\CompositeConfigProvider;
use Magento\Checkout\Block\Checkout\LayoutProcessorInterface;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\Serialize\Serializer\JsonHexTag;
use Magento\Framework\View\Element\Template\Context;
use Magento\Customer\Model\Session as customerSession;
use Magento\Checkout\Model\Session as checkoutSession;
use Magento\Store\Model\StoreManagerInterface;
use PHPUnit\Framework\MockObject\MockObject;

/**
* Unit Test for Magento\Checkout\Block\Cart\Shipping
*/
class ShippingTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Checkout\Block\Cart\Shipping
* @var Shipping
*/
protected $model;
protected $block;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var Context|MockObject
*/
protected $context;
protected $contextMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var customerSession|MockObject
*/
protected $customerSession;
protected $customerSessionMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var checkoutSession|MockObject
*/
protected $checkoutSession;
protected $checkoutSessionMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var CompositeConfigProvider|MockObject
*/
protected $configProvider;
protected $configProviderMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var LayoutProcessorInterface|MockObject
*/
protected $layoutProcessor;
protected $layoutProcessorMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var StoreManagerInterface|MockObject
*/
protected $storeManager;
protected $storeManagerMock;

/**
* @var array
*/
protected $layout;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var Json|MockObject
*/
private $serializer;
private $serializerMock;

protected function setUp()
/**
* @var JsonHexTag|MockObject
*/
private $jsonHexTagSerializerMock;

/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
$this->customerSession = $this->createMock(\Magento\Customer\Model\Session::class);
$this->checkoutSession = $this->createMock(\Magento\Checkout\Model\Session::class);
$this->configProvider = $this->createMock(\Magento\Checkout\Model\CompositeConfigProvider::class);
$this->layoutProcessor = $this->createMock(\Magento\Checkout\Block\Checkout\LayoutProcessorInterface::class);
$this->contextMock = $this->createMock(Context::class);
$this->customerSessionMock = $this->createMock(customerSession::class);
$this->checkoutSessionMock = $this->createMock(checkoutSession::class);
$this->configProviderMock = $this->createMock(CompositeConfigProvider::class);
$this->layoutProcessorMock = $this->createMock(LayoutProcessorInterface::class);
$this->serializerMock = $this->createMock(JsonHexTag::class);
$this->jsonHexTagSerializerMock = $this->createMock(JsonHexTag::class);
$this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
$this->layout = [
'components' => [
'firstComponent' => ['param' => 'value'],
'secondComponent' => ['param' => 'value'],
'firstComponent' => ['param' => 'value']
]
];

$this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
$this->context->expects($this->once())->method('getStoreManager')->willReturn($this->storeManager);
$this->serializer = $this->createMock(\Magento\Framework\Serialize\Serializer\Json::class);
$this->contextMock->expects($this->once())
->method('getStoreManager')
->willReturn($this->storeManagerMock);

$this->model = new \Magento\Checkout\Block\Cart\Shipping(
$this->context,
$this->customerSession,
$this->checkoutSession,
$this->configProvider,
[$this->layoutProcessor],
$this->block = new Shipping(
$this->contextMock,
$this->customerSessionMock,
$this->checkoutSessionMock,
$this->configProviderMock,
[$this->layoutProcessorMock],
['jsLayout' => $this->layout],
$this->serializer
$this->serializerMock,
$this->jsonHexTagSerializerMock
);
}

public function testGetCheckoutConfig()
/**
* Test for getCheckoutConfig
*
* @return void
*/
public function testGetCheckoutConfig(): void
{
$config = ['param' => 'value'];
$this->configProvider->expects($this->once())->method('getConfig')->willReturn($config);
$this->assertEquals($config, $this->model->getCheckoutConfig());
$this->configProviderMock->expects($this->once())
->method('getConfig')
->willReturn($config);

$this->assertEquals($config, $this->block->getCheckoutConfig());
}

public function testGetJsLayout()
/**
* Test for getJsLayout()
*
* @return void
* @dataProvider getJsLayoutDataProvider
*/
public function testGetJsLayout(array $layoutProcessed, string $jsonLayoutProcessed): void
{
$layoutProcessed = $this->layout;
$layoutProcessed['components']['thirdComponent'] = ['param' => 'value'];
$jsonLayoutProcessed = json_encode($layoutProcessed);

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

$this->assertEquals(
$jsonLayoutProcessed,
$this->model->getJsLayout()
);
$this->jsonHexTagSerializerMock->expects($this->once())
->method('serialize')
->willReturn($jsonLayoutProcessed);

$this->assertEquals($jsonLayoutProcessed, $this->block->getJsLayout());
}

public function testGetBaseUrl()
/**
* Data for getJsLayout()
*
* @return array
*/
public function getJsLayoutDataProvider(): array
{
$layoutProcessed = $this->layout;
$layoutProcessed['components']['secondComponent'] = ['param' => 'value'];
return [
[
$layoutProcessed,
'{"components":{"firstComponent":{"param":"value"},"secondComponent":{"param":"value"}}}'
]
];
}

/**
* Test for getBaseUrl()
*
* @return void
*/
public function testGetBaseUrl(): void
{
$baseUrl = 'baseUrl';
$storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getBaseUrl']);
$storeMock->expects($this->once())->method('getBaseUrl')->willReturn($baseUrl);
$this->storeManager->expects($this->once())->method('getStore')->willReturn($storeMock);
$this->assertEquals($baseUrl, $this->model->getBaseUrl());
$storeMock->expects($this->once())
->method('getBaseUrl')
->willReturn($baseUrl);

$this->storeManagerMock->expects($this->once())
->method('getStore')
->willReturn($storeMock);

$this->assertEquals($baseUrl, $this->block->getBaseUrl());
}

public function testGetSerializedCheckoutConfig()
/**
* Test for getSerializedCheckoutConfig()
*
* @return void
* @dataProvider jsonEncodeDataProvider
*/
public function testGetSerializedCheckoutConfig(array $checkoutConfig, string $expectedJson): void
{
$checkoutConfig = ['checkout', 'config'];
$this->configProvider->expects($this->once())->method('getConfig')->willReturn($checkoutConfig);
$this->configProviderMock->expects($this->once())
->method('getConfig')
->willReturn($checkoutConfig);

$this->jsonHexTagSerializerMock->expects($this->once())
->method('serialize')
->willReturn($expectedJson);

$this->assertEquals($expectedJson, $this->block->getSerializedCheckoutConfig());
}

$this->assertEquals(json_encode($checkoutConfig), $this->model->getSerializedCheckoutConfig());
/**
* Data for getSerializedCheckoutConfig()
*
* @return array
*/
public function jsonEncodeDataProvider(): array
{
return [
[
['checkout', 'config'],
'["checkout","config"]'
]
];
}
}