123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Weee\Test\Unit\Helper;
- use Magento\Weee\Helper\Data as WeeeHelper;
- /**
- * @SuppressWarnings(PHPMD.TooManyMethods)
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class DataTest extends \PHPUnit\Framework\TestCase
- {
- const ROW_AMOUNT_INVOICED = '200';
- const BASE_ROW_AMOUNT_INVOICED = '400';
- const TAX_AMOUNT_INVOICED = '20';
- const BASE_TAX_AMOUNT_INVOICED = '40';
- const ROW_AMOUNT_REFUNDED = '100';
- const BASE_ROW_AMOUNT_REFUNDED = '201';
- const TAX_AMOUNT_REFUNDED = '10';
- const BASE_TAX_AMOUNT_REFUNDED = '21';
- /**
- * @var \Magento\Catalog\Model\Product
- */
- protected $product;
- /**
- * @var \Magento\Weee\Model\Tax
- */
- protected $weeeTax;
- /**
- * @var \Magento\Tax\Helper\Data
- */
- protected $taxData;
- /**
- * @var \Magento\Weee\Helper\Data
- */
- protected $helperData;
- /** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
- private $serializerMock;
- protected function setUp()
- {
- $this->product = $this->createMock(\Magento\Catalog\Model\Product::class);
- $weeeConfig = $this->createMock(\Magento\Weee\Model\Config::class);
- $weeeConfig->expects($this->any())->method('isEnabled')->will($this->returnValue(true));
- $weeeConfig->expects($this->any())->method('getListPriceDisplayType')->will($this->returnValue(1));
- $this->weeeTax = $this->createMock(\Magento\Weee\Model\Tax::class);
- $this->weeeTax->expects($this->any())->method('getWeeeAmount')->will($this->returnValue('11.26'));
- $this->taxData = $this->createPartialMock(
- \Magento\Tax\Helper\Data::class,
- ['getPriceDisplayType', 'priceIncludesTax']
- );
- $this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock();
- $arguments = [
- 'weeeConfig' => $weeeConfig,
- 'weeeTax' => $this->weeeTax,
- '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);
- }
- public function testGetAmount()
- {
- $this->product->expects($this->any())->method('hasData')->will($this->returnValue(false));
- $this->product->expects($this->any())->method('getData')->will($this->returnValue(11.26));
- $this->assertEquals('11.26', $this->helperData->getAmountExclTax($this->product));
- }
- /**
- * @return \Magento\Sales\Model\Order\Item|\PHPUnit_Framework_MockObject_MockObject
- */
- private function setupOrderItem()
- {
- $orderItem = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
- ->disableOriginalConstructor()
- ->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',
- json_encode($weeeTaxApplied)
- );
- $this->serializerMock->expects($this->any())
- ->method('unserialize')
- ->will($this->returnValue($weeeTaxApplied));
- return $orderItem;
- }
- public function testGetWeeeAmountInvoiced()
- {
- $orderItem = $this->setupOrderItem();
- $value = $this->helperData->getWeeeAmountInvoiced($orderItem);
- $this->assertEquals(self::ROW_AMOUNT_INVOICED, $value);
- }
- public function testGetBaseWeeeAmountInvoiced()
- {
- $orderItem = $this->setupOrderItem();
- $value = $this->helperData->getBaseWeeeAmountInvoiced($orderItem);
- $this->assertEquals(self::BASE_ROW_AMOUNT_INVOICED, $value);
- }
- public function testGetWeeeTaxAmountInvoiced()
- {
- $orderItem = $this->setupOrderItem();
- $value = $this->helperData->getWeeeTaxAmountInvoiced($orderItem);
- $this->assertEquals(self::TAX_AMOUNT_INVOICED, $value);
- }
- public function testGetWeeeBaseTaxAmountInvoiced()
- {
- $orderItem = $this->setupOrderItem();
- $value = $this->helperData->getBaseWeeeTaxAmountInvoiced($orderItem);
- $this->assertEquals(self::BASE_TAX_AMOUNT_INVOICED, $value);
- }
- public function testGetWeeeAmountRefunded()
- {
- $orderItem = $this->setupOrderItem();
- $value = $this->helperData->getWeeeAmountRefunded($orderItem);
- $this->assertEquals(self::ROW_AMOUNT_REFUNDED, $value);
- }
- public function testGetBaseWeeeAmountRefunded()
- {
- $orderItem = $this->setupOrderItem();
- $value = $this->helperData->getBaseWeeeAmountRefunded($orderItem);
- $this->assertEquals(self::BASE_ROW_AMOUNT_REFUNDED, $value);
- }
- public function testGetWeeeTaxAmountRefunded()
- {
- $orderItem = $this->setupOrderItem();
- $value = $this->helperData->getWeeeTaxAmountRefunded($orderItem);
- $this->assertEquals(self::TAX_AMOUNT_REFUNDED, $value);
- }
- public function testGetBaseWeeeTaxAmountRefunded()
- {
- $orderItem = $this->setupOrderItem();
- $value = $this->helperData->getBaseWeeeTaxAmountRefunded($orderItem);
- $this->assertEquals(self::BASE_TAX_AMOUNT_REFUNDED, $value);
- }
- /**
- * @dataProvider dataProviderGetWeeeAttributesForBundle
- * @param int $priceIncludesTax
- * @param bool $priceDisplay
- * @param array $expectedAmount
- */
- public function testGetWeeeAttributesForBundle($priceDisplay, $priceIncludesTax, $expectedAmount)
- {
- $prodId1 = 1;
- $prodId2 = 2;
- $fptCode1 = 'fpt' . $prodId1;
- $fptCode2 = 'fpt' . $prodId2;
- $weeeObject1 = new \Magento\Framework\DataObject(
- [
- 'code' => $fptCode1,
- 'amount' => '15',
- 'amount_excl_tax' => '15.0000',
- 'tax_amount' => '1'
- ]
- );
- $weeeObject2 = new \Magento\Framework\DataObject(
- [
- 'code' => $fptCode2,
- 'amount' => '10',
- 'amount_excl_tax' => '10.0000',
- 'tax_amount' => '5'
- ]
- );
- $expectedObject1 = new \Magento\Framework\DataObject(
- [
- 'code' => $fptCode1,
- 'amount' => $expectedAmount[0],
- 'amount_excl_tax' => '15.0000',
- 'tax_amount' => '1'
- ]
- );
- $expectedObject2 = new \Magento\Framework\DataObject(
- [
- 'code' => $fptCode2,
- 'amount' => $expectedAmount[1],
- 'amount_excl_tax' => '10.0000',
- 'tax_amount' => '5'
- ]
- );
- $expectedArray = [$prodId1 => [$fptCode1 => $expectedObject1], $prodId2 => [$fptCode2 => $expectedObject2]];
- $this->weeeTax->expects($this->any())
- ->method('getProductWeeeAttributes')
- ->will($this->returnValue([$weeeObject1, $weeeObject2]));
- $this->taxData->expects($this->any())
- ->method('getPriceDisplayType')
- ->willReturn($priceDisplay);
- $this->taxData->expects($this->any())
- ->method('priceIncludesTax')
- ->willReturn($priceIncludesTax);
- $productSimple = $this->createPartialMock(\Magento\Catalog\Model\Product\Type\Simple::class, ['getId']);
- $productSimple->expects($this->at(0))
- ->method('getId')
- ->will($this->returnValue($prodId1));
- $productSimple->expects($this->at(1))
- ->method('getId')
- ->will($this->returnValue($prodId2));
- $productInstance = $this->createMock(\Magento\Bundle\Model\Product\Type::class);
- $productInstance->expects($this->any())
- ->method('getSelectionsCollection')
- ->will($this->returnValue([$productSimple]));
- $store=$this->createMock(\Magento\Store\Model\Store::class);
- /** @var \Magento\Catalog\Model\Product $product */
- $product = $this->createPartialMock(
- \Magento\Catalog\Model\Product::class,
- ['getTypeInstance', 'getStoreId', 'getStore', 'getTypeId']
- );
- $product->expects($this->any())
- ->method('getTypeInstance')
- ->will($this->returnValue($productInstance));
- $product->expects($this->any())
- ->method('getStoreId')
- ->will($this->returnValue(1));
- $product->expects($this->any())
- ->method('getStore')
- ->will($this->returnValue($store));
- $product->expects($this->any())
- ->method('getTypeId')
- ->will($this->returnValue('bundle'));
- $registry=$this->createMock(\Magento\Framework\Registry::class);
- $registry->expects($this->any())
- ->method('registry')
- ->with('current_product')
- ->will($this->returnValue($product));
- $result = $this->helperData->getWeeeAttributesForBundle($product);
- $this->assertEquals($expectedArray, $result);
- }
- /**
- * @return array
- */
- public function dataProviderGetWeeeAttributesForBundle()
- {
- return [
- [2, false, ["16.00", "15.00"]],
- [2, true, ["15.00", "10.00"]],
- [1, false, ["15.00", "10.00"]],
- [1, true, ["15.00", "10.00"]],
- [3, false, ["16.00", "15.00"]],
- [3, true, ["15.00", "10.00"]],
- ];
- }
- public function testGetAppliedSimple()
- {
- $testArray = ['key' => 'value'];
- $itemProductSimple = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Item::class,
- ['getWeeeTaxApplied', 'getHasChildren']
- );
- $itemProductSimple->expects($this->any())
- ->method('getHasChildren')
- ->will($this->returnValue(false));
- $itemProductSimple->expects($this->any())
- ->method('getWeeeTaxApplied')
- ->will($this->returnValue(json_encode($testArray)));
- $this->serializerMock->expects($this->any())
- ->method('unserialize')
- ->will($this->returnValue($testArray));
- $this->assertEquals($testArray, $this->helperData->getApplied($itemProductSimple));
- }
- public function testGetAppliedBundle()
- {
- $testArray1 = ['key1' => 'value1'];
- $testArray2 = ['key2' => 'value2'];
- $testArray = array_merge($testArray1, $testArray2);
- $itemProductSimple1=$this->createPartialMock(\Magento\Quote\Model\Quote\Item::class, ['getWeeeTaxApplied']);
- $itemProductSimple2=$this->createPartialMock(\Magento\Quote\Model\Quote\Item::class, ['getWeeeTaxApplied']);
- $itemProductSimple1->expects($this->any())
- ->method('getWeeeTaxApplied')
- ->will($this->returnValue(json_encode($testArray1)));
- $itemProductSimple2->expects($this->any())
- ->method('getWeeeTaxApplied')
- ->will($this->returnValue(json_encode($testArray2)));
- $itemProductBundle = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Item::class,
- ['getHasChildren', 'isChildrenCalculated', 'getChildren']
- );
- $itemProductBundle->expects($this->any())
- ->method('getHasChildren')
- ->will($this->returnValue(true));
- $itemProductBundle->expects($this->any())
- ->method('isChildrenCalculated')
- ->will($this->returnValue(true));
- $itemProductBundle->expects($this->any())
- ->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));
- }
- public function testGetRecursiveAmountSimple()
- {
- $testAmountUnit = 2;
- $testAmountRow = 34;
- $itemProductSimple = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Item::class,
- ['getHasChildren', 'getWeeeTaxAppliedAmount', 'getWeeeTaxAppliedRowAmount']
- );
- $itemProductSimple->expects($this->any())
- ->method('getHasChildren')
- ->will($this->returnValue(false));
- $itemProductSimple->expects($this->any())
- ->method('getWeeeTaxAppliedAmount')
- ->will($this->returnValue($testAmountUnit));
- $itemProductSimple->expects($this->any())
- ->method('getWeeeTaxAppliedRowAmount')
- ->will($this->returnValue($testAmountRow));
- $this->assertEquals($testAmountUnit, $this->helperData->getWeeeTaxAppliedAmount($itemProductSimple));
- $this->assertEquals($testAmountRow, $this->helperData->getWeeeTaxAppliedRowAmount($itemProductSimple));
- }
- public function testGetRecursiveAmountBundle()
- {
- $testAmountUnit1 = 1;
- $testAmountUnit2 = 2;
- $testTotalUnit = $testAmountUnit1 + $testAmountUnit2;
- $testAmountRow1 = 33;
- $testAmountRow2 = 444;
- $testTotalRow = $testAmountRow1 + $testAmountRow2;
- $itemProductSimple1 = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Item::class,
- ['getWeeeTaxAppliedAmount', 'getWeeeTaxAppliedRowAmount']
- );
- $itemProductSimple2 = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Item::class,
- ['getWeeeTaxAppliedAmount', 'getWeeeTaxAppliedRowAmount']
- );
- $itemProductSimple1->expects($this->any())
- ->method('getWeeeTaxAppliedAmount')
- ->will($this->returnValue($testAmountUnit1));
- $itemProductSimple1->expects($this->any())
- ->method('getWeeeTaxAppliedRowAmount')
- ->will($this->returnValue($testAmountRow1));
- $itemProductSimple2->expects($this->any())
- ->method('getWeeeTaxAppliedAmount')
- ->will($this->returnValue($testAmountUnit2));
- $itemProductSimple2->expects($this->any())
- ->method('getWeeeTaxAppliedRowAmount')
- ->will($this->returnValue($testAmountRow2));
- $itemProductBundle = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Item::class,
- ['getHasChildren', 'isChildrenCalculated', 'getChildren']
- );
- $itemProductBundle->expects($this->any())
- ->method('getHasChildren')
- ->will($this->returnValue(true));
- $itemProductBundle->expects($this->any())
- ->method('isChildrenCalculated')
- ->will($this->returnValue(true));
- $itemProductBundle->expects($this->any())
- ->method('getChildren')
- ->will($this->returnValue([$itemProductSimple1, $itemProductSimple2]));
- $this->assertEquals($testTotalUnit, $this->helperData->getWeeeTaxAppliedAmount($itemProductBundle));
- $this->assertEquals($testTotalRow, $this->helperData->getWeeeTaxAppliedRowAmount($itemProductBundle));
- }
- public function testGetProductWeeeAttributesForDisplay()
- {
- $store = $this->createMock(\Magento\Store\Model\Store::class);
- $this->product->expects($this->any())
- ->method('getStore')
- ->will($this->returnValue($store));
- $result = $this->helperData->getProductWeeeAttributesForDisplay($this->product);
- $this->assertNull($result);
- }
- public function testGetTaxDisplayConfig()
- {
- $expected = 1;
- $taxData = $this->createPartialMock(\Magento\Tax\Helper\Data::class, ['getPriceDisplayType']);
- $taxData->expects($this->any())->method('getPriceDisplayType')->will($this->returnValue($expected));
- $arguments = [
- 'taxData' => $taxData,
- ];
- $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $helperData = $helper->getObject(\Magento\Weee\Helper\Data::class, $arguments);
- $this->assertEquals($expected, $helperData->getTaxDisplayConfig());
- }
- public function testGetTotalAmounts()
- {
- $item1Weee = 5;
- $item2Weee = 7;
- $expected = $item1Weee + $item2Weee;
- $itemProductSimple1 = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Item::class,
- ['getWeeeTaxAppliedRowAmount']
- );
- $itemProductSimple2 = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Item::class,
- ['getWeeeTaxAppliedRowAmount']
- );
- $items = [$itemProductSimple1, $itemProductSimple2];
- $itemProductSimple1->expects($this->any())
- ->method('getWeeeTaxAppliedRowAmount')
- ->willReturn($item1Weee);
- $itemProductSimple2->expects($this->any())
- ->method('getWeeeTaxAppliedRowAmount')
- ->willReturn($item2Weee);
- $this->assertEquals($expected, $this->helperData->getTotalAmounts($items));
- }
- public function testGetBaseTotalAmounts()
- {
- $item1BaseWeee = 4;
- $item2BaseWeee = 3;
- $expected = $item1BaseWeee + $item2BaseWeee;
- $itemProductSimple1 = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Item::class,
- ['getBaseWeeeTaxAppliedRowAmnt']
- );
- $itemProductSimple2 = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Item::class,
- ['getBaseWeeeTaxAppliedRowAmnt']
- );
- $items = [$itemProductSimple1, $itemProductSimple2];
- $itemProductSimple1->expects($this->any())
- ->method('getBaseWeeeTaxAppliedRowAmnt')
- ->willReturn($item1BaseWeee);
- $itemProductSimple2->expects($this->any())
- ->method('getBaseWeeeTaxAppliedRowAmnt')
- ->willReturn($item2BaseWeee);
- $this->assertEquals($expected, $this->helperData->getBaseTotalAmounts($items));
- }
- }
|