Skip to content

Commit c4f4e29

Browse files
author
Oleksii Korshenko
authored
MAGETWO-67510: Remove zend json from weee #9261
2 parents f768e75 + f36ef99 commit c4f4e29

File tree

6 files changed

+115
-45
lines changed

6 files changed

+115
-45
lines changed

app/code/Magento/Weee/Helper/Data.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,38 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
7777
protected $cacheProductWeeeAmount = '_cache_product_weee_amount';
7878

7979
/**
80+
* @var \Magento\Framework\Serialize\Serializer\Json
81+
*/
82+
private $serializer;
83+
84+
/**
85+
* Data constructor.
86+
*
8087
* @param \Magento\Framework\App\Helper\Context $context
8188
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
8289
* @param \Magento\Weee\Model\Tax $weeeTax
8390
* @param \Magento\Weee\Model\Config $weeeConfig
8491
* @param \Magento\Tax\Helper\Data $taxData
8592
* @param \Magento\Framework\Registry $coreRegistry
93+
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
94+
* @throws \RuntimeException
8695
*/
8796
public function __construct(
8897
\Magento\Framework\App\Helper\Context $context,
8998
\Magento\Store\Model\StoreManagerInterface $storeManager,
9099
\Magento\Weee\Model\Tax $weeeTax,
91100
\Magento\Weee\Model\Config $weeeConfig,
92101
\Magento\Tax\Helper\Data $taxData,
93-
\Magento\Framework\Registry $coreRegistry
102+
\Magento\Framework\Registry $coreRegistry,
103+
\Magento\Framework\Serialize\Serializer\Json $serializer = null
94104
) {
95105
$this->_storeManager = $storeManager;
96106
$this->_weeeTax = $weeeTax;
97107
$this->_coreRegistry = $coreRegistry;
98108
$this->_taxData = $taxData;
99109
$this->_weeeConfig = $weeeConfig;
110+
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
111+
->get(\Magento\Framework\Serialize\Serializer\Json::class);
100112
parent::__construct($context);
101113
}
102114

@@ -373,7 +385,7 @@ public function getApplied($item)
373385
if (empty($data)) {
374386
return [];
375387
}
376-
return \Zend_Json::decode($item->getWeeeTaxApplied());
388+
return $this->serializer->unserialize($item->getWeeeTaxApplied());
377389
}
378390

379391
/**
@@ -385,7 +397,7 @@ public function getApplied($item)
385397
*/
386398
public function setApplied($item, $value)
387399
{
388-
$item->setWeeeTaxApplied(\Zend_Json::encode($value));
400+
$item->setWeeeTaxApplied($this->serializer->serialize($value));
389401
return $this;
390402
}
391403

app/code/Magento/Weee/Model/Attribute/Backend/Weee/Tax.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Weee\Model\Attribute\Backend\Weee;
77

88
use Magento\Framework\Exception\LocalizedException;
9+
use Magento\Catalog\Model\Attribute\ScopeOverriddenValue;
910

1011
class Tax extends \Magento\Catalog\Model\Product\Attribute\Backend\Price
1112
{
@@ -25,13 +26,16 @@ class Tax extends \Magento\Catalog\Model\Product\Attribute\Backend\Price
2526
protected $_directoryHelper;
2627

2728
/**
29+
* Initialize dependencies.
30+
*
2831
* @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
2932
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
3033
* @param \Magento\Catalog\Helper\Data $catalogData
3134
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
3235
* @param \Magento\Framework\Locale\FormatInterface $localeFormat
3336
* @param \Magento\Directory\Helper\Data $directoryHelper
3437
* @param \Magento\Weee\Model\ResourceModel\Attribute\Backend\Weee\Tax $attributeTax
38+
* @param ScopeOverriddenValue|null $scopeOverriddenValue
3539
*/
3640
public function __construct(
3741
\Magento\Directory\Model\CurrencyFactory $currencyFactory,
@@ -40,12 +44,20 @@ public function __construct(
4044
\Magento\Framework\App\Config\ScopeConfigInterface $config,
4145
\Magento\Framework\Locale\FormatInterface $localeFormat,
4246
\Magento\Directory\Helper\Data $directoryHelper,
43-
\Magento\Weee\Model\ResourceModel\Attribute\Backend\Weee\Tax $attributeTax
47+
\Magento\Weee\Model\ResourceModel\Attribute\Backend\Weee\Tax $attributeTax,
48+
ScopeOverriddenValue $scopeOverriddenValue = null
4449
) {
4550
$this->_directoryHelper = $directoryHelper;
4651
$this->_storeManager = $storeManager;
4752
$this->_attributeTax = $attributeTax;
48-
parent::__construct($currencyFactory, $storeManager, $catalogData, $config, $localeFormat);
53+
parent::__construct(
54+
$currencyFactory,
55+
$storeManager,
56+
$catalogData,
57+
$config,
58+
$localeFormat,
59+
$scopeOverriddenValue
60+
);
4961
}
5062

5163
/**

app/code/Magento/Weee/Test/Unit/Helper/DataTest.php

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class DataTest extends \PHPUnit_Framework_TestCase
4242
*/
4343
protected $helperData;
4444

45+
/** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
46+
private $serializerMock;
47+
4548
protected function setUp()
4649
{
4750
$this->product = $this->getMock(\Magento\Catalog\Model\Product::class, [], [], '', false);
@@ -57,10 +60,14 @@ protected function setUp()
5760
'',
5861
false
5962
);
63+
64+
$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock();
65+
6066
$arguments = [
6167
'weeeConfig' => $weeeConfig,
6268
'weeeTax' => $this->weeeTax,
63-
'taxData' => $this->taxData
69+
'taxData' => $this->taxData,
70+
'serializer' => $this->serializerMock
6471
];
6572
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
6673
$this->helperData = $helper->getObject(\Magento\Weee\Helper\Data::class, $arguments);
@@ -84,33 +91,38 @@ private function setupOrderItem()
8491
->setMethods(['__wakeup'])
8592
->getMock();
8693

94+
$weeeTaxApplied = [
95+
[
96+
WeeeHelper::KEY_WEEE_AMOUNT_INVOICED => self::ROW_AMOUNT_INVOICED,
97+
WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED => self::BASE_ROW_AMOUNT_INVOICED,
98+
WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED => self::TAX_AMOUNT_INVOICED,
99+
WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED => self::BASE_TAX_AMOUNT_INVOICED,
100+
WeeeHelper::KEY_WEEE_AMOUNT_REFUNDED => self::ROW_AMOUNT_REFUNDED,
101+
WeeeHelper::KEY_BASE_WEEE_AMOUNT_REFUNDED => self::BASE_ROW_AMOUNT_REFUNDED,
102+
WeeeHelper::KEY_WEEE_TAX_AMOUNT_REFUNDED => self::TAX_AMOUNT_REFUNDED,
103+
WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED => self::BASE_TAX_AMOUNT_REFUNDED,
104+
],
105+
[
106+
WeeeHelper::KEY_WEEE_AMOUNT_INVOICED => self::ROW_AMOUNT_INVOICED,
107+
WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED => self::BASE_ROW_AMOUNT_INVOICED,
108+
WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED => self::TAX_AMOUNT_INVOICED,
109+
WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED => self::BASE_TAX_AMOUNT_INVOICED,
110+
WeeeHelper::KEY_WEEE_AMOUNT_REFUNDED => self::ROW_AMOUNT_REFUNDED,
111+
WeeeHelper::KEY_BASE_WEEE_AMOUNT_REFUNDED => self::BASE_ROW_AMOUNT_REFUNDED,
112+
WeeeHelper::KEY_WEEE_TAX_AMOUNT_REFUNDED => self::TAX_AMOUNT_REFUNDED,
113+
WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED => self::BASE_TAX_AMOUNT_REFUNDED,
114+
],
115+
];
116+
87117
$orderItem->setData(
88118
'weee_tax_applied',
89-
\Zend_Json::encode(
90-
[
91-
[
92-
WeeeHelper::KEY_WEEE_AMOUNT_INVOICED => self::ROW_AMOUNT_INVOICED,
93-
WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED => self::BASE_ROW_AMOUNT_INVOICED,
94-
WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED => self::TAX_AMOUNT_INVOICED,
95-
WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED => self::BASE_TAX_AMOUNT_INVOICED,
96-
WeeeHelper::KEY_WEEE_AMOUNT_REFUNDED => self::ROW_AMOUNT_REFUNDED,
97-
WeeeHelper::KEY_BASE_WEEE_AMOUNT_REFUNDED => self::BASE_ROW_AMOUNT_REFUNDED,
98-
WeeeHelper::KEY_WEEE_TAX_AMOUNT_REFUNDED => self::TAX_AMOUNT_REFUNDED,
99-
WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED => self::BASE_TAX_AMOUNT_REFUNDED,
100-
],
101-
[
102-
WeeeHelper::KEY_WEEE_AMOUNT_INVOICED => self::ROW_AMOUNT_INVOICED,
103-
WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED => self::BASE_ROW_AMOUNT_INVOICED,
104-
WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED => self::TAX_AMOUNT_INVOICED,
105-
WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED => self::BASE_TAX_AMOUNT_INVOICED,
106-
WeeeHelper::KEY_WEEE_AMOUNT_REFUNDED => self::ROW_AMOUNT_REFUNDED,
107-
WeeeHelper::KEY_BASE_WEEE_AMOUNT_REFUNDED => self::BASE_ROW_AMOUNT_REFUNDED,
108-
WeeeHelper::KEY_WEEE_TAX_AMOUNT_REFUNDED => self::TAX_AMOUNT_REFUNDED,
109-
WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED => self::BASE_TAX_AMOUNT_REFUNDED,
110-
],
111-
]
112-
)
119+
json_encode($weeeTaxApplied)
113120
);
121+
122+
$this->serializerMock->expects($this->any())
123+
->method('unserialize')
124+
->will($this->returnValue($weeeTaxApplied));
125+
114126
return $orderItem;
115127
}
116128

@@ -290,14 +302,24 @@ public function dataProviderGetWeeeAttributesForBundle()
290302
public function testGetAppliedSimple()
291303
{
292304
$testArray = ['key' => 'value'];
293-
$itemProductSimple=$this->getMock(\Magento\Quote\Model\Quote\Item::class, ['getWeeeTaxApplied'], [], '', false);
305+
$itemProductSimple = $this->getMock(
306+
\Magento\Quote\Model\Quote\Item::class,
307+
['getWeeeTaxApplied'],
308+
[],
309+
'',
310+
false
311+
);
294312
$itemProductSimple->expects($this->any())
295313
->method('getHasChildren')
296314
->will($this->returnValue(false));
297315

298316
$itemProductSimple->expects($this->any())
299317
->method('getWeeeTaxApplied')
300-
->will($this->returnValue(\Zend_Json::encode($testArray)));
318+
->will($this->returnValue(json_encode($testArray)));
319+
320+
$this->serializerMock->expects($this->any())
321+
->method('unserialize')
322+
->will($this->returnValue($testArray));
301323

302324
$this->assertEquals($testArray, $this->helperData->getApplied($itemProductSimple));
303325
}
@@ -326,11 +348,11 @@ public function testGetAppliedBundle()
326348

327349
$itemProductSimple1->expects($this->any())
328350
->method('getWeeeTaxApplied')
329-
->will($this->returnValue(\Zend_Json::encode($testArray1)));
351+
->will($this->returnValue(json_encode($testArray1)));
330352

331353
$itemProductSimple2->expects($this->any())
332354
->method('getWeeeTaxApplied')
333-
->will($this->returnValue(\Zend_Json::encode($testArray2)));
355+
->will($this->returnValue(json_encode($testArray2)));
334356

335357
$itemProductBundle=$this->getMock(
336358
\Magento\Quote\Model\Quote\Item::class,
@@ -349,6 +371,10 @@ public function testGetAppliedBundle()
349371
->method('getChildren')
350372
->will($this->returnValue([$itemProductSimple1, $itemProductSimple2]));
351373

374+
$this->serializerMock->expects($this->any())
375+
->method('unserialize')
376+
->will($this->returnValue($testArray));
377+
352378
$this->assertEquals($testArray, $this->helperData->getApplied($itemProductBundle));
353379
}
354380

app/code/Magento/Weee/Test/Unit/Model/Attribute/Backend/Weee/TaxTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515

1616
class TaxTest extends \PHPUnit_Framework_TestCase
1717
{
18-
/**
19-
* @var \Magento\Weee\Model\Attribute\Backend\Weee\Tax
20-
*/
21-
protected $model;
22-
2318
/**
2419
* @var ObjectManager
2520
*/
@@ -28,12 +23,14 @@ class TaxTest extends \PHPUnit_Framework_TestCase
2823
protected function setUp()
2924
{
3025
$this->objectManager = new ObjectManager($this);
31-
$this->model = $this->objectManager->getObject(\Magento\Weee\Model\Attribute\Backend\Weee\Tax::class);
3226
}
3327

3428
public function testGetBackendModelName()
3529
{
36-
$this->assertEquals(\Magento\Weee\Model\Attribute\Backend\Weee\Tax::class, $this->model->getBackendModelName());
30+
$this->assertEquals(
31+
\Magento\Weee\Model\Attribute\Backend\Weee\Tax::class,
32+
\Magento\Weee\Model\Attribute\Backend\Weee\Tax::getBackendModelName()
33+
);
3734
}
3835

3936
/**

app/code/Magento/Weee/Test/Unit/Model/Total/Quote/WeeeTaxTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,12 @@ public function verifyTotals($address, $addressData)
242242

243243
public function testFetch()
244244
{
245+
$serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock();
245246
$weeeTotal = 17;
246-
$totalMock = new \Magento\Quote\Model\Quote\Address\Total();
247+
$totalMock = new \Magento\Quote\Model\Quote\Address\Total(
248+
[],
249+
$serializerMock
250+
);
247251
$taxHelper = $this->setupTaxHelper([]);
248252
$weeeHelper = $this->setupWeeeHelper(['getTotalAmounts' => $weeeTotal]);
249253
$this->weeeCollector = $this->objectManagerHelper->getObject(
@@ -262,7 +266,11 @@ public function testFetch()
262266

263267
public function testFetchWithZeroAmounts()
264268
{
265-
$totalMock = new \Magento\Quote\Model\Quote\Address\Total();
269+
$serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock();
270+
$totalMock = new \Magento\Quote\Model\Quote\Address\Total(
271+
[],
272+
$serializerMock
273+
);
266274
$taxHelper = $this->setupTaxHelper([]);
267275
$weeeHelper = $this->setupWeeeHelper(['getTotalAmounts' => null]);
268276
$this->weeeCollector = $this->objectManagerHelper->getObject(

app/code/Magento/Weee/Test/Unit/Model/Total/Quote/WeeeTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class WeeeTest extends \PHPUnit_Framework_TestCase
2222
*/
2323
protected $weeeCollector;
2424

25+
private $serializerMock;
26+
2527
/**
2628
* Setup tax helper with an array of methodName, returnValue
2729
*
@@ -77,7 +79,17 @@ protected function setupTaxCalculation($taxRates)
7779
*/
7880
protected function setupWeeeHelper($weeeConfig)
7981
{
80-
$weeeHelper = $this->getMock(\Magento\Weee\Helper\Data::class, [], [], '', false);
82+
$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock();
83+
84+
$weeeHelper = $this->getMock(
85+
\Magento\Weee\Helper\Data::class,
86+
[],
87+
[
88+
'serializer' => $this->serializerMock
89+
],
90+
'',
91+
false
92+
);
8193

8294
foreach ($weeeConfig as $method => $value) {
8395
$weeeHelper->expects($this->any())->method($method)->will($this->returnValue($value));
@@ -286,7 +298,10 @@ public function testCollect(
286298
$storeMock = $this->getMock(\Magento\Store\Model\Store::class, [], [], '', false);
287299
$quoteMock->expects($this->any())->method('getStore')->will($this->returnValue($storeMock));
288300
$addressMock = $this->setupAddressMock($items);
289-
$totalMock = new \Magento\Quote\Model\Quote\Address\Total();
301+
$totalMock = new \Magento\Quote\Model\Quote\Address\Total(
302+
[],
303+
$this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock()
304+
);
290305
$shippingAssignmentMock = $this->setupShippingAssignmentMock($addressMock, $items);
291306

292307
$taxHelper = $this->setupTaxHelper($taxConfig);

0 commit comments

Comments
 (0)