Skip to content

Commit

Permalink
Replace Zend_Json from the Magento Review module with the lib framewo…
Browse files Browse the repository at this point in the history
…rk json serializer
  • Loading branch information
dmanners committed Mar 9, 2017
1 parent f1dc91b commit 67a28f5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
21 changes: 17 additions & 4 deletions app/code/Magento/Review/Block/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,24 @@ class Form extends \Magento\Framework\View\Element\Template
protected $jsLayout;

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

/**
* Form constructor.
*
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Framework\Url\EncoderInterface $urlEncoder
* @param \Magento\Review\Helper\Data $reviewData
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
* @param \Magento\Review\Model\RatingFactory $ratingFactory
* @param \Magento\Framework\Message\ManagerInterface $messageManager
* @param \Magento\Framework\App\Http\Context $httpContext
* @param \Magento\Customer\Model\Url $customerUrl
* @param Url $customerUrl
* @param array $data
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @throws \RuntimeException
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -87,7 +96,8 @@ public function __construct(
\Magento\Framework\Message\ManagerInterface $messageManager,
\Magento\Framework\App\Http\Context $httpContext,
\Magento\Customer\Model\Url $customerUrl,
array $data = []
array $data = [],
\Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
$this->urlEncoder = $urlEncoder;
$this->_reviewData = $reviewData;
Expand All @@ -98,6 +108,8 @@ public function __construct(
$this->customerUrl = $customerUrl;
parent::__construct($context, $data);
$this->jsLayout = isset($data['jsLayout']) ? $data['jsLayout'] : [];
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
}

/**
Expand Down Expand Up @@ -133,13 +145,13 @@ protected function _construct()
*/
public function getJsLayout()
{
return \Zend_Json::encode($this->jsLayout);
return $this->serializer->serialize($this->jsLayout);
}

/**
* Get product info
*
* @return Product
* @return \Magento\Catalog\Api\Data\ProductInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getProductInfo()
Expand Down Expand Up @@ -171,6 +183,7 @@ public function getAction()
* Get collection of ratings
*
* @return RatingCollection
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getRatings()
{
Expand Down
22 changes: 22 additions & 0 deletions app/code/Magento/Review/Test/Unit/Block/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class FormTest extends \PHPUnit_Framework_TestCase
/** @var \Magento\Framework\UrlInterface|PHPUnit_Framework_MockObject_MockObject */
protected $urlBuilder;

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

protected function setUp()
{
$this->storeManager = $this->getMock(\Magento\Store\Model\StoreManagerInterface::class);
Expand Down Expand Up @@ -64,13 +67,21 @@ protected function setUp()
$this->context->expects($this->any())->method('getUrlBuilder')->willReturn($this->urlBuilder);
$this->productRepository = $this->getMock(\Magento\Catalog\Api\ProductRepositoryInterface::class);

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

$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->object = $this->objectManagerHelper->getObject(
\Magento\Review\Block\Form::class,
[
'context' => $this->context,
'reviewData' => $this->reviewDataMock,
'productRepository' => $this->productRepository,
'data' => [
'jsLayout' => [
'some-layout' => 'layout information'
]
],
'serializer' => $this->serializerMock
]
);
}
Expand Down Expand Up @@ -132,4 +143,15 @@ public function getActionDataProvider()
[true, 'https://localhost/review/product/post' ,3],
];
}

public function testGetJsLayout()
{
$jsLayout = [
'some-layout' => 'layout information'
];

$this->serializerMock->expects($this->once())->method('serialize')
->will($this->returnValue(json_encode($jsLayout)));
$this->assertEquals('{"some-layout":"layout information"}', $this->object->getJsLayout());
}
}

0 comments on commit 67a28f5

Please sign in to comment.