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 from weee #9261

Merged
merged 2 commits into from
Apr 19, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 15 additions & 3 deletions app/code/Magento/Weee/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,38 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
protected $cacheProductWeeeAmount = '_cache_product_weee_amount';

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

/**
* Data constructor.
*
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Weee\Model\Tax $weeeTax
* @param \Magento\Weee\Model\Config $weeeConfig
* @param \Magento\Tax\Helper\Data $taxData
* @param \Magento\Framework\Registry $coreRegistry
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @throws \RuntimeException
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Weee\Model\Tax $weeeTax,
\Magento\Weee\Model\Config $weeeConfig,
\Magento\Tax\Helper\Data $taxData,
\Magento\Framework\Registry $coreRegistry
\Magento\Framework\Registry $coreRegistry,
\Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
$this->_storeManager = $storeManager;
$this->_weeeTax = $weeeTax;
$this->_coreRegistry = $coreRegistry;
$this->_taxData = $taxData;
$this->_weeeConfig = $weeeConfig;
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
parent::__construct($context);
}

Expand Down Expand Up @@ -373,7 +385,7 @@ public function getApplied($item)
if (empty($data)) {
return [];
}
return \Zend_Json::decode($item->getWeeeTaxApplied());
return $this->serializer->unserialize($item->getWeeeTaxApplied());
}

/**
Expand All @@ -385,7 +397,7 @@ public function getApplied($item)
*/
public function setApplied($item, $value)
{
$item->setWeeeTaxApplied(\Zend_Json::encode($value));
$item->setWeeeTaxApplied($this->serializer->serialize($value));
return $this;
}

Expand Down
78 changes: 49 additions & 29 deletions app/code/Magento/Weee/Test/Unit/Helper/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class DataTest extends \PHPUnit_Framework_TestCase
*/
protected $helperData;

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

protected function setUp()
{
$this->product = $this->getMock(\Magento\Catalog\Model\Product::class, [], [], '', false);
Expand All @@ -57,10 +60,14 @@ protected function setUp()
'',
false
);

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

$arguments = [
'weeeConfig' => $weeeConfig,
'weeeTax' => $this->weeeTax,
'taxData' => $this->taxData
'taxData' => $this->taxData,
'serializer' => $this->serializerMock
];
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->helperData = $helper->getObject(\Magento\Weee\Helper\Data::class, $arguments);
Expand All @@ -84,33 +91,38 @@ private function setupOrderItem()
->setMethods(['__wakeup'])
->getMock();

$weeeTaxApplied = [
[
WeeeHelper::KEY_WEEE_AMOUNT_INVOICED => self::ROW_AMOUNT_INVOICED,
WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED => self::BASE_ROW_AMOUNT_INVOICED,
WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED => self::TAX_AMOUNT_INVOICED,
WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED => self::BASE_TAX_AMOUNT_INVOICED,
WeeeHelper::KEY_WEEE_AMOUNT_REFUNDED => self::ROW_AMOUNT_REFUNDED,
WeeeHelper::KEY_BASE_WEEE_AMOUNT_REFUNDED => self::BASE_ROW_AMOUNT_REFUNDED,
WeeeHelper::KEY_WEEE_TAX_AMOUNT_REFUNDED => self::TAX_AMOUNT_REFUNDED,
WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED => self::BASE_TAX_AMOUNT_REFUNDED,
],
[
WeeeHelper::KEY_WEEE_AMOUNT_INVOICED => self::ROW_AMOUNT_INVOICED,
WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED => self::BASE_ROW_AMOUNT_INVOICED,
WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED => self::TAX_AMOUNT_INVOICED,
WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED => self::BASE_TAX_AMOUNT_INVOICED,
WeeeHelper::KEY_WEEE_AMOUNT_REFUNDED => self::ROW_AMOUNT_REFUNDED,
WeeeHelper::KEY_BASE_WEEE_AMOUNT_REFUNDED => self::BASE_ROW_AMOUNT_REFUNDED,
WeeeHelper::KEY_WEEE_TAX_AMOUNT_REFUNDED => self::TAX_AMOUNT_REFUNDED,
WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED => self::BASE_TAX_AMOUNT_REFUNDED,
],
];

$orderItem->setData(
'weee_tax_applied',
\Zend_Json::encode(
[
[
WeeeHelper::KEY_WEEE_AMOUNT_INVOICED => self::ROW_AMOUNT_INVOICED,
WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED => self::BASE_ROW_AMOUNT_INVOICED,
WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED => self::TAX_AMOUNT_INVOICED,
WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED => self::BASE_TAX_AMOUNT_INVOICED,
WeeeHelper::KEY_WEEE_AMOUNT_REFUNDED => self::ROW_AMOUNT_REFUNDED,
WeeeHelper::KEY_BASE_WEEE_AMOUNT_REFUNDED => self::BASE_ROW_AMOUNT_REFUNDED,
WeeeHelper::KEY_WEEE_TAX_AMOUNT_REFUNDED => self::TAX_AMOUNT_REFUNDED,
WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED => self::BASE_TAX_AMOUNT_REFUNDED,
],
[
WeeeHelper::KEY_WEEE_AMOUNT_INVOICED => self::ROW_AMOUNT_INVOICED,
WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED => self::BASE_ROW_AMOUNT_INVOICED,
WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED => self::TAX_AMOUNT_INVOICED,
WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED => self::BASE_TAX_AMOUNT_INVOICED,
WeeeHelper::KEY_WEEE_AMOUNT_REFUNDED => self::ROW_AMOUNT_REFUNDED,
WeeeHelper::KEY_BASE_WEEE_AMOUNT_REFUNDED => self::BASE_ROW_AMOUNT_REFUNDED,
WeeeHelper::KEY_WEEE_TAX_AMOUNT_REFUNDED => self::TAX_AMOUNT_REFUNDED,
WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED => self::BASE_TAX_AMOUNT_REFUNDED,
],
]
)
json_encode($weeeTaxApplied)
);

$this->serializerMock->expects($this->any())
->method('unserialize')
->will($this->returnValue($weeeTaxApplied));

return $orderItem;
}

Expand Down Expand Up @@ -290,14 +302,18 @@ public function dataProviderGetWeeeAttributesForBundle()
public function testGetAppliedSimple()
{
$testArray = ['key' => 'value'];
$itemProductSimple=$this->getMock(\Magento\Quote\Model\Quote\Item::class, ['getWeeeTaxApplied'], [], '', false);
$itemProductSimple = $this->getMock(\Magento\Quote\Model\Quote\Item::class, ['getWeeeTaxApplied'], [], '', false);
$itemProductSimple->expects($this->any())
->method('getHasChildren')
->will($this->returnValue(false));

$itemProductSimple->expects($this->any())
->method('getWeeeTaxApplied')
->will($this->returnValue(\Zend_Json::encode($testArray)));
->will($this->returnValue(json_encode($testArray)));

$this->serializerMock->expects($this->any())
->method('unserialize')
->will($this->returnValue($testArray));

$this->assertEquals($testArray, $this->helperData->getApplied($itemProductSimple));
}
Expand Down Expand Up @@ -326,11 +342,11 @@ public function testGetAppliedBundle()

$itemProductSimple1->expects($this->any())
->method('getWeeeTaxApplied')
->will($this->returnValue(\Zend_Json::encode($testArray1)));
->will($this->returnValue(json_encode($testArray1)));

$itemProductSimple2->expects($this->any())
->method('getWeeeTaxApplied')
->will($this->returnValue(\Zend_Json::encode($testArray2)));
->will($this->returnValue(json_encode($testArray2)));

$itemProductBundle=$this->getMock(
\Magento\Quote\Model\Quote\Item::class,
Expand All @@ -349,6 +365,10 @@ public function testGetAppliedBundle()
->method('getChildren')
->will($this->returnValue([$itemProductSimple1, $itemProductSimple2]));

$this->serializerMock->expects($this->any())
->method('unserialize')
->will($this->returnValue($testArray));

$this->assertEquals($testArray, $this->helperData->getApplied($itemProductBundle));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@

class TaxTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Weee\Model\Attribute\Backend\Weee\Tax
*/
protected $model;

/**
* @var ObjectManager
*/
Expand All @@ -28,12 +23,14 @@ class TaxTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$this->objectManager = new ObjectManager($this);
$this->model = $this->objectManager->getObject(\Magento\Weee\Model\Attribute\Backend\Weee\Tax::class);
}

public function testGetBackendModelName()
{
$this->assertEquals(\Magento\Weee\Model\Attribute\Backend\Weee\Tax::class, $this->model->getBackendModelName());
$this->assertEquals(
\Magento\Weee\Model\Attribute\Backend\Weee\Tax::class,
\Magento\Weee\Model\Attribute\Backend\Weee\Tax::getBackendModelName()
);
}

/**
Expand Down
12 changes: 10 additions & 2 deletions app/code/Magento/Weee/Test/Unit/Model/Total/Quote/WeeeTaxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,12 @@ public function verifyTotals($address, $addressData)

public function testFetch()
{
$serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock();
$weeeTotal = 17;
$totalMock = new \Magento\Quote\Model\Quote\Address\Total();
$totalMock = new \Magento\Quote\Model\Quote\Address\Total(
[],
$serializerMock
);
$taxHelper = $this->setupTaxHelper([]);
$weeeHelper = $this->setupWeeeHelper(['getTotalAmounts' => $weeeTotal]);
$this->weeeCollector = $this->objectManagerHelper->getObject(
Expand All @@ -262,7 +266,11 @@ public function testFetch()

public function testFetchWithZeroAmounts()
{
$totalMock = new \Magento\Quote\Model\Quote\Address\Total();
$serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock();
$totalMock = new \Magento\Quote\Model\Quote\Address\Total(
[],
$serializerMock
);
$taxHelper = $this->setupTaxHelper([]);
$weeeHelper = $this->setupWeeeHelper(['getTotalAmounts' => null]);
$this->weeeCollector = $this->objectManagerHelper->getObject(
Expand Down
19 changes: 17 additions & 2 deletions app/code/Magento/Weee/Test/Unit/Model/Total/Quote/WeeeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class WeeeTest extends \PHPUnit_Framework_TestCase
*/
protected $weeeCollector;

private $serializerMock;

/**
* Setup tax helper with an array of methodName, returnValue
*
Expand Down Expand Up @@ -77,7 +79,17 @@ protected function setupTaxCalculation($taxRates)
*/
protected function setupWeeeHelper($weeeConfig)
{
$weeeHelper = $this->getMock(\Magento\Weee\Helper\Data::class, [], [], '', false);
$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock();

$weeeHelper = $this->getMock(
\Magento\Weee\Helper\Data::class,
[],
[
'serializer' => $this->serializerMock
],
'',
false
);

foreach ($weeeConfig as $method => $value) {
$weeeHelper->expects($this->any())->method($method)->will($this->returnValue($value));
Expand Down Expand Up @@ -286,7 +298,10 @@ public function testCollect(
$storeMock = $this->getMock(\Magento\Store\Model\Store::class, [], [], '', false);
$quoteMock->expects($this->any())->method('getStore')->will($this->returnValue($storeMock));
$addressMock = $this->setupAddressMock($items);
$totalMock = new \Magento\Quote\Model\Quote\Address\Total();
$totalMock = new \Magento\Quote\Model\Quote\Address\Total(
[],
$this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock()
);
$shippingAssignmentMock = $this->setupShippingAssignmentMock($addressMock, $items);

$taxHelper = $this->setupTaxHelper($taxConfig);
Expand Down