DataTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Test\Unit\Helper;
  7. use Magento\Weee\Helper\Data as WeeeHelper;
  8. /**
  9. * @SuppressWarnings(PHPMD.TooManyMethods)
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class DataTest extends \PHPUnit\Framework\TestCase
  13. {
  14. const ROW_AMOUNT_INVOICED = '200';
  15. const BASE_ROW_AMOUNT_INVOICED = '400';
  16. const TAX_AMOUNT_INVOICED = '20';
  17. const BASE_TAX_AMOUNT_INVOICED = '40';
  18. const ROW_AMOUNT_REFUNDED = '100';
  19. const BASE_ROW_AMOUNT_REFUNDED = '201';
  20. const TAX_AMOUNT_REFUNDED = '10';
  21. const BASE_TAX_AMOUNT_REFUNDED = '21';
  22. /**
  23. * @var \Magento\Catalog\Model\Product
  24. */
  25. protected $product;
  26. /**
  27. * @var \Magento\Weee\Model\Tax
  28. */
  29. protected $weeeTax;
  30. /**
  31. * @var \Magento\Tax\Helper\Data
  32. */
  33. protected $taxData;
  34. /**
  35. * @var \Magento\Weee\Helper\Data
  36. */
  37. protected $helperData;
  38. /** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
  39. private $serializerMock;
  40. protected function setUp()
  41. {
  42. $this->product = $this->createMock(\Magento\Catalog\Model\Product::class);
  43. $weeeConfig = $this->createMock(\Magento\Weee\Model\Config::class);
  44. $weeeConfig->expects($this->any())->method('isEnabled')->will($this->returnValue(true));
  45. $weeeConfig->expects($this->any())->method('getListPriceDisplayType')->will($this->returnValue(1));
  46. $this->weeeTax = $this->createMock(\Magento\Weee\Model\Tax::class);
  47. $this->weeeTax->expects($this->any())->method('getWeeeAmount')->will($this->returnValue('11.26'));
  48. $this->taxData = $this->createPartialMock(
  49. \Magento\Tax\Helper\Data::class,
  50. ['getPriceDisplayType', 'priceIncludesTax']
  51. );
  52. $this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock();
  53. $arguments = [
  54. 'weeeConfig' => $weeeConfig,
  55. 'weeeTax' => $this->weeeTax,
  56. 'taxData' => $this->taxData,
  57. 'serializer' => $this->serializerMock
  58. ];
  59. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  60. $this->helperData = $helper->getObject(\Magento\Weee\Helper\Data::class, $arguments);
  61. }
  62. public function testGetAmount()
  63. {
  64. $this->product->expects($this->any())->method('hasData')->will($this->returnValue(false));
  65. $this->product->expects($this->any())->method('getData')->will($this->returnValue(11.26));
  66. $this->assertEquals('11.26', $this->helperData->getAmountExclTax($this->product));
  67. }
  68. /**
  69. * @return \Magento\Sales\Model\Order\Item|\PHPUnit_Framework_MockObject_MockObject
  70. */
  71. private function setupOrderItem()
  72. {
  73. $orderItem = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  74. ->disableOriginalConstructor()
  75. ->setMethods(['__wakeup'])
  76. ->getMock();
  77. $weeeTaxApplied = [
  78. [
  79. WeeeHelper::KEY_WEEE_AMOUNT_INVOICED => self::ROW_AMOUNT_INVOICED,
  80. WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED => self::BASE_ROW_AMOUNT_INVOICED,
  81. WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED => self::TAX_AMOUNT_INVOICED,
  82. WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED => self::BASE_TAX_AMOUNT_INVOICED,
  83. WeeeHelper::KEY_WEEE_AMOUNT_REFUNDED => self::ROW_AMOUNT_REFUNDED,
  84. WeeeHelper::KEY_BASE_WEEE_AMOUNT_REFUNDED => self::BASE_ROW_AMOUNT_REFUNDED,
  85. WeeeHelper::KEY_WEEE_TAX_AMOUNT_REFUNDED => self::TAX_AMOUNT_REFUNDED,
  86. WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED => self::BASE_TAX_AMOUNT_REFUNDED,
  87. ],
  88. [
  89. WeeeHelper::KEY_WEEE_AMOUNT_INVOICED => self::ROW_AMOUNT_INVOICED,
  90. WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED => self::BASE_ROW_AMOUNT_INVOICED,
  91. WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED => self::TAX_AMOUNT_INVOICED,
  92. WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED => self::BASE_TAX_AMOUNT_INVOICED,
  93. WeeeHelper::KEY_WEEE_AMOUNT_REFUNDED => self::ROW_AMOUNT_REFUNDED,
  94. WeeeHelper::KEY_BASE_WEEE_AMOUNT_REFUNDED => self::BASE_ROW_AMOUNT_REFUNDED,
  95. WeeeHelper::KEY_WEEE_TAX_AMOUNT_REFUNDED => self::TAX_AMOUNT_REFUNDED,
  96. WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED => self::BASE_TAX_AMOUNT_REFUNDED,
  97. ],
  98. ];
  99. $orderItem->setData(
  100. 'weee_tax_applied',
  101. json_encode($weeeTaxApplied)
  102. );
  103. $this->serializerMock->expects($this->any())
  104. ->method('unserialize')
  105. ->will($this->returnValue($weeeTaxApplied));
  106. return $orderItem;
  107. }
  108. public function testGetWeeeAmountInvoiced()
  109. {
  110. $orderItem = $this->setupOrderItem();
  111. $value = $this->helperData->getWeeeAmountInvoiced($orderItem);
  112. $this->assertEquals(self::ROW_AMOUNT_INVOICED, $value);
  113. }
  114. public function testGetBaseWeeeAmountInvoiced()
  115. {
  116. $orderItem = $this->setupOrderItem();
  117. $value = $this->helperData->getBaseWeeeAmountInvoiced($orderItem);
  118. $this->assertEquals(self::BASE_ROW_AMOUNT_INVOICED, $value);
  119. }
  120. public function testGetWeeeTaxAmountInvoiced()
  121. {
  122. $orderItem = $this->setupOrderItem();
  123. $value = $this->helperData->getWeeeTaxAmountInvoiced($orderItem);
  124. $this->assertEquals(self::TAX_AMOUNT_INVOICED, $value);
  125. }
  126. public function testGetWeeeBaseTaxAmountInvoiced()
  127. {
  128. $orderItem = $this->setupOrderItem();
  129. $value = $this->helperData->getBaseWeeeTaxAmountInvoiced($orderItem);
  130. $this->assertEquals(self::BASE_TAX_AMOUNT_INVOICED, $value);
  131. }
  132. public function testGetWeeeAmountRefunded()
  133. {
  134. $orderItem = $this->setupOrderItem();
  135. $value = $this->helperData->getWeeeAmountRefunded($orderItem);
  136. $this->assertEquals(self::ROW_AMOUNT_REFUNDED, $value);
  137. }
  138. public function testGetBaseWeeeAmountRefunded()
  139. {
  140. $orderItem = $this->setupOrderItem();
  141. $value = $this->helperData->getBaseWeeeAmountRefunded($orderItem);
  142. $this->assertEquals(self::BASE_ROW_AMOUNT_REFUNDED, $value);
  143. }
  144. public function testGetWeeeTaxAmountRefunded()
  145. {
  146. $orderItem = $this->setupOrderItem();
  147. $value = $this->helperData->getWeeeTaxAmountRefunded($orderItem);
  148. $this->assertEquals(self::TAX_AMOUNT_REFUNDED, $value);
  149. }
  150. public function testGetBaseWeeeTaxAmountRefunded()
  151. {
  152. $orderItem = $this->setupOrderItem();
  153. $value = $this->helperData->getBaseWeeeTaxAmountRefunded($orderItem);
  154. $this->assertEquals(self::BASE_TAX_AMOUNT_REFUNDED, $value);
  155. }
  156. /**
  157. * @dataProvider dataProviderGetWeeeAttributesForBundle
  158. * @param int $priceIncludesTax
  159. * @param bool $priceDisplay
  160. * @param array $expectedAmount
  161. */
  162. public function testGetWeeeAttributesForBundle($priceDisplay, $priceIncludesTax, $expectedAmount)
  163. {
  164. $prodId1 = 1;
  165. $prodId2 = 2;
  166. $fptCode1 = 'fpt' . $prodId1;
  167. $fptCode2 = 'fpt' . $prodId2;
  168. $weeeObject1 = new \Magento\Framework\DataObject(
  169. [
  170. 'code' => $fptCode1,
  171. 'amount' => '15',
  172. 'amount_excl_tax' => '15.0000',
  173. 'tax_amount' => '1'
  174. ]
  175. );
  176. $weeeObject2 = new \Magento\Framework\DataObject(
  177. [
  178. 'code' => $fptCode2,
  179. 'amount' => '10',
  180. 'amount_excl_tax' => '10.0000',
  181. 'tax_amount' => '5'
  182. ]
  183. );
  184. $expectedObject1 = new \Magento\Framework\DataObject(
  185. [
  186. 'code' => $fptCode1,
  187. 'amount' => $expectedAmount[0],
  188. 'amount_excl_tax' => '15.0000',
  189. 'tax_amount' => '1'
  190. ]
  191. );
  192. $expectedObject2 = new \Magento\Framework\DataObject(
  193. [
  194. 'code' => $fptCode2,
  195. 'amount' => $expectedAmount[1],
  196. 'amount_excl_tax' => '10.0000',
  197. 'tax_amount' => '5'
  198. ]
  199. );
  200. $expectedArray = [$prodId1 => [$fptCode1 => $expectedObject1], $prodId2 => [$fptCode2 => $expectedObject2]];
  201. $this->weeeTax->expects($this->any())
  202. ->method('getProductWeeeAttributes')
  203. ->will($this->returnValue([$weeeObject1, $weeeObject2]));
  204. $this->taxData->expects($this->any())
  205. ->method('getPriceDisplayType')
  206. ->willReturn($priceDisplay);
  207. $this->taxData->expects($this->any())
  208. ->method('priceIncludesTax')
  209. ->willReturn($priceIncludesTax);
  210. $productSimple = $this->createPartialMock(\Magento\Catalog\Model\Product\Type\Simple::class, ['getId']);
  211. $productSimple->expects($this->at(0))
  212. ->method('getId')
  213. ->will($this->returnValue($prodId1));
  214. $productSimple->expects($this->at(1))
  215. ->method('getId')
  216. ->will($this->returnValue($prodId2));
  217. $productInstance = $this->createMock(\Magento\Bundle\Model\Product\Type::class);
  218. $productInstance->expects($this->any())
  219. ->method('getSelectionsCollection')
  220. ->will($this->returnValue([$productSimple]));
  221. $store=$this->createMock(\Magento\Store\Model\Store::class);
  222. /** @var \Magento\Catalog\Model\Product $product */
  223. $product = $this->createPartialMock(
  224. \Magento\Catalog\Model\Product::class,
  225. ['getTypeInstance', 'getStoreId', 'getStore', 'getTypeId']
  226. );
  227. $product->expects($this->any())
  228. ->method('getTypeInstance')
  229. ->will($this->returnValue($productInstance));
  230. $product->expects($this->any())
  231. ->method('getStoreId')
  232. ->will($this->returnValue(1));
  233. $product->expects($this->any())
  234. ->method('getStore')
  235. ->will($this->returnValue($store));
  236. $product->expects($this->any())
  237. ->method('getTypeId')
  238. ->will($this->returnValue('bundle'));
  239. $registry=$this->createMock(\Magento\Framework\Registry::class);
  240. $registry->expects($this->any())
  241. ->method('registry')
  242. ->with('current_product')
  243. ->will($this->returnValue($product));
  244. $result = $this->helperData->getWeeeAttributesForBundle($product);
  245. $this->assertEquals($expectedArray, $result);
  246. }
  247. /**
  248. * @return array
  249. */
  250. public function dataProviderGetWeeeAttributesForBundle()
  251. {
  252. return [
  253. [2, false, ["16.00", "15.00"]],
  254. [2, true, ["15.00", "10.00"]],
  255. [1, false, ["15.00", "10.00"]],
  256. [1, true, ["15.00", "10.00"]],
  257. [3, false, ["16.00", "15.00"]],
  258. [3, true, ["15.00", "10.00"]],
  259. ];
  260. }
  261. public function testGetAppliedSimple()
  262. {
  263. $testArray = ['key' => 'value'];
  264. $itemProductSimple = $this->createPartialMock(
  265. \Magento\Quote\Model\Quote\Item::class,
  266. ['getWeeeTaxApplied', 'getHasChildren']
  267. );
  268. $itemProductSimple->expects($this->any())
  269. ->method('getHasChildren')
  270. ->will($this->returnValue(false));
  271. $itemProductSimple->expects($this->any())
  272. ->method('getWeeeTaxApplied')
  273. ->will($this->returnValue(json_encode($testArray)));
  274. $this->serializerMock->expects($this->any())
  275. ->method('unserialize')
  276. ->will($this->returnValue($testArray));
  277. $this->assertEquals($testArray, $this->helperData->getApplied($itemProductSimple));
  278. }
  279. public function testGetAppliedBundle()
  280. {
  281. $testArray1 = ['key1' => 'value1'];
  282. $testArray2 = ['key2' => 'value2'];
  283. $testArray = array_merge($testArray1, $testArray2);
  284. $itemProductSimple1=$this->createPartialMock(\Magento\Quote\Model\Quote\Item::class, ['getWeeeTaxApplied']);
  285. $itemProductSimple2=$this->createPartialMock(\Magento\Quote\Model\Quote\Item::class, ['getWeeeTaxApplied']);
  286. $itemProductSimple1->expects($this->any())
  287. ->method('getWeeeTaxApplied')
  288. ->will($this->returnValue(json_encode($testArray1)));
  289. $itemProductSimple2->expects($this->any())
  290. ->method('getWeeeTaxApplied')
  291. ->will($this->returnValue(json_encode($testArray2)));
  292. $itemProductBundle = $this->createPartialMock(
  293. \Magento\Quote\Model\Quote\Item::class,
  294. ['getHasChildren', 'isChildrenCalculated', 'getChildren']
  295. );
  296. $itemProductBundle->expects($this->any())
  297. ->method('getHasChildren')
  298. ->will($this->returnValue(true));
  299. $itemProductBundle->expects($this->any())
  300. ->method('isChildrenCalculated')
  301. ->will($this->returnValue(true));
  302. $itemProductBundle->expects($this->any())
  303. ->method('getChildren')
  304. ->will($this->returnValue([$itemProductSimple1, $itemProductSimple2]));
  305. $this->serializerMock->expects($this->any())
  306. ->method('unserialize')
  307. ->will($this->returnValue($testArray));
  308. $this->assertEquals($testArray, $this->helperData->getApplied($itemProductBundle));
  309. }
  310. public function testGetRecursiveAmountSimple()
  311. {
  312. $testAmountUnit = 2;
  313. $testAmountRow = 34;
  314. $itemProductSimple = $this->createPartialMock(
  315. \Magento\Quote\Model\Quote\Item::class,
  316. ['getHasChildren', 'getWeeeTaxAppliedAmount', 'getWeeeTaxAppliedRowAmount']
  317. );
  318. $itemProductSimple->expects($this->any())
  319. ->method('getHasChildren')
  320. ->will($this->returnValue(false));
  321. $itemProductSimple->expects($this->any())
  322. ->method('getWeeeTaxAppliedAmount')
  323. ->will($this->returnValue($testAmountUnit));
  324. $itemProductSimple->expects($this->any())
  325. ->method('getWeeeTaxAppliedRowAmount')
  326. ->will($this->returnValue($testAmountRow));
  327. $this->assertEquals($testAmountUnit, $this->helperData->getWeeeTaxAppliedAmount($itemProductSimple));
  328. $this->assertEquals($testAmountRow, $this->helperData->getWeeeTaxAppliedRowAmount($itemProductSimple));
  329. }
  330. public function testGetRecursiveAmountBundle()
  331. {
  332. $testAmountUnit1 = 1;
  333. $testAmountUnit2 = 2;
  334. $testTotalUnit = $testAmountUnit1 + $testAmountUnit2;
  335. $testAmountRow1 = 33;
  336. $testAmountRow2 = 444;
  337. $testTotalRow = $testAmountRow1 + $testAmountRow2;
  338. $itemProductSimple1 = $this->createPartialMock(
  339. \Magento\Quote\Model\Quote\Item::class,
  340. ['getWeeeTaxAppliedAmount', 'getWeeeTaxAppliedRowAmount']
  341. );
  342. $itemProductSimple2 = $this->createPartialMock(
  343. \Magento\Quote\Model\Quote\Item::class,
  344. ['getWeeeTaxAppliedAmount', 'getWeeeTaxAppliedRowAmount']
  345. );
  346. $itemProductSimple1->expects($this->any())
  347. ->method('getWeeeTaxAppliedAmount')
  348. ->will($this->returnValue($testAmountUnit1));
  349. $itemProductSimple1->expects($this->any())
  350. ->method('getWeeeTaxAppliedRowAmount')
  351. ->will($this->returnValue($testAmountRow1));
  352. $itemProductSimple2->expects($this->any())
  353. ->method('getWeeeTaxAppliedAmount')
  354. ->will($this->returnValue($testAmountUnit2));
  355. $itemProductSimple2->expects($this->any())
  356. ->method('getWeeeTaxAppliedRowAmount')
  357. ->will($this->returnValue($testAmountRow2));
  358. $itemProductBundle = $this->createPartialMock(
  359. \Magento\Quote\Model\Quote\Item::class,
  360. ['getHasChildren', 'isChildrenCalculated', 'getChildren']
  361. );
  362. $itemProductBundle->expects($this->any())
  363. ->method('getHasChildren')
  364. ->will($this->returnValue(true));
  365. $itemProductBundle->expects($this->any())
  366. ->method('isChildrenCalculated')
  367. ->will($this->returnValue(true));
  368. $itemProductBundle->expects($this->any())
  369. ->method('getChildren')
  370. ->will($this->returnValue([$itemProductSimple1, $itemProductSimple2]));
  371. $this->assertEquals($testTotalUnit, $this->helperData->getWeeeTaxAppliedAmount($itemProductBundle));
  372. $this->assertEquals($testTotalRow, $this->helperData->getWeeeTaxAppliedRowAmount($itemProductBundle));
  373. }
  374. public function testGetProductWeeeAttributesForDisplay()
  375. {
  376. $store = $this->createMock(\Magento\Store\Model\Store::class);
  377. $this->product->expects($this->any())
  378. ->method('getStore')
  379. ->will($this->returnValue($store));
  380. $result = $this->helperData->getProductWeeeAttributesForDisplay($this->product);
  381. $this->assertNull($result);
  382. }
  383. public function testGetTaxDisplayConfig()
  384. {
  385. $expected = 1;
  386. $taxData = $this->createPartialMock(\Magento\Tax\Helper\Data::class, ['getPriceDisplayType']);
  387. $taxData->expects($this->any())->method('getPriceDisplayType')->will($this->returnValue($expected));
  388. $arguments = [
  389. 'taxData' => $taxData,
  390. ];
  391. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  392. $helperData = $helper->getObject(\Magento\Weee\Helper\Data::class, $arguments);
  393. $this->assertEquals($expected, $helperData->getTaxDisplayConfig());
  394. }
  395. public function testGetTotalAmounts()
  396. {
  397. $item1Weee = 5;
  398. $item2Weee = 7;
  399. $expected = $item1Weee + $item2Weee;
  400. $itemProductSimple1 = $this->createPartialMock(
  401. \Magento\Quote\Model\Quote\Item::class,
  402. ['getWeeeTaxAppliedRowAmount']
  403. );
  404. $itemProductSimple2 = $this->createPartialMock(
  405. \Magento\Quote\Model\Quote\Item::class,
  406. ['getWeeeTaxAppliedRowAmount']
  407. );
  408. $items = [$itemProductSimple1, $itemProductSimple2];
  409. $itemProductSimple1->expects($this->any())
  410. ->method('getWeeeTaxAppliedRowAmount')
  411. ->willReturn($item1Weee);
  412. $itemProductSimple2->expects($this->any())
  413. ->method('getWeeeTaxAppliedRowAmount')
  414. ->willReturn($item2Weee);
  415. $this->assertEquals($expected, $this->helperData->getTotalAmounts($items));
  416. }
  417. public function testGetBaseTotalAmounts()
  418. {
  419. $item1BaseWeee = 4;
  420. $item2BaseWeee = 3;
  421. $expected = $item1BaseWeee + $item2BaseWeee;
  422. $itemProductSimple1 = $this->createPartialMock(
  423. \Magento\Quote\Model\Quote\Item::class,
  424. ['getBaseWeeeTaxAppliedRowAmnt']
  425. );
  426. $itemProductSimple2 = $this->createPartialMock(
  427. \Magento\Quote\Model\Quote\Item::class,
  428. ['getBaseWeeeTaxAppliedRowAmnt']
  429. );
  430. $items = [$itemProductSimple1, $itemProductSimple2];
  431. $itemProductSimple1->expects($this->any())
  432. ->method('getBaseWeeeTaxAppliedRowAmnt')
  433. ->willReturn($item1BaseWeee);
  434. $itemProductSimple2->expects($this->any())
  435. ->method('getBaseWeeeTaxAppliedRowAmnt')
  436. ->willReturn($item2BaseWeee);
  437. $this->assertEquals($expected, $this->helperData->getBaseTotalAmounts($items));
  438. }
  439. }