1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Quote\Test\Unit\Model\Quote;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class ItemTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Quote\Model\Quote\Item
- */
- private $model;
- /**
- * @var \Magento\Framework\Locale\FormatInterface
- */
- private $localeFormat;
- /**
- * @var \Magento\Framework\Model\Context
- */
- private $modelContext;
- /**
- * @var \Magento\Framework\Event\ManagerInterface
- */
- private $eventDispatcher;
- /**
- * @var \Magento\Sales\Model\Status\ListStatus
- */
- private $errorInfos;
- /**
- * @var \Magento\Quote\Model\Quote\Item\OptionFactory
- */
- private $itemOptionFactory;
- /**
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
- */
- private $objectManagerHelper;
- /**
- * @var \Magento\Quote\Model\Quote\Item\Compare|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $compareHelper;
- /** @var \PHPUnit_Framework_MockObject_MockObject */
- protected $stockItemMock;
- /**
- * @var \Magento\Framework\Serialize\Serializer\Json
- */
- private $serializer;
- const PRODUCT_ID = 1;
- const PRODUCT_TYPE = 'simple';
- const PRODUCT_SKU = '12345';
- const PRODUCT_NAME = 'test_product';
- const PRODUCT_WEIGHT = '1lb';
- const PRODUCT_TAX_CLASS_ID = 3;
- const PRODUCT_COST = '9.00';
- protected function setUp()
- {
- $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->localeFormat = $this->getMockBuilder(\Magento\Framework\Locale\FormatInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->modelContext = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
- ->disableOriginalConstructor()
- ->setMethods(['getEventDispatcher'])
- ->getMock();
- $this->eventDispatcher = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
- ->disableOriginalConstructor()
- ->setMethods(['dispatch'])
- ->getMock();
- $this->modelContext->expects($this->any())
- ->method('getEventDispatcher')
- ->will($this->returnValue($this->eventDispatcher));
- $this->errorInfos = $this->getMockBuilder(\Magento\Sales\Model\Status\ListStatus::class)
- ->disableOriginalConstructor()
- ->setMethods(['clear', 'addItem', 'getItems', 'removeItemsByParams'])
- ->getMock();
- $statusListFactory = $this->getMockBuilder(\Magento\Sales\Model\Status\ListFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $statusListFactory->expects($this->any())
- ->method('create')
- ->will($this->returnValue($this->errorInfos));
- $this->itemOptionFactory = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item\OptionFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->compareHelper = $this->createMock(\Magento\Quote\Model\Quote\Item\Compare::class);
- $this->stockItemMock = $this->createPartialMock(
- \Magento\CatalogInventory\Model\Stock\Item::class,
- ['getIsQtyDecimal', '__wakeup']
- );
- $this->serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
- ->setMethods(['unserialize'])
- ->getMockForAbstractClass();
- $this->model = $this->objectManagerHelper->getObject(
- \Magento\Quote\Model\Quote\Item::class,
- [
- 'localeFormat' => $this->localeFormat,
- 'context' => $this->modelContext,
- 'statusListFactory' => $statusListFactory,
- 'itemOptionFactory' => $this->itemOptionFactory,
- 'quoteItemCompare' => $this->compareHelper,
- 'serializer' => $this->serializer
- ]
- );
- }
- public function testGetAddress()
- {
- $quote = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
- ->setMethods(['getShippingAddress', 'getBillingAddress', 'getStoreId', '__wakeup'])
- ->disableOriginalConstructor()
- ->getMock();
- $quote->expects($this->once())
- ->method('getShippingAddress')
- ->will($this->returnValue('shipping'));
- $quote->expects($this->once())
- ->method('getBillingAddress')
- ->will($this->returnValue('billing'));
- $quote->expects($this->any())
- ->method('getStoreId')
- ->will($this->returnValue(1));
- $this->model->setQuote($quote);
- $quote->setItemsQty(2);
- $quote->setVirtualItemsQty(1);
- $this->assertEquals('shipping', $this->model->getAddress(), 'Wrong shipping address');
- $quote->setItemsQty(2);
- $quote->setVirtualItemsQty(2);
- $this->assertEquals('billing', $this->model->getAddress(), 'Wrong billing address');
- }
- public function testSetAndQuote()
- {
- $idValue = "id_value";
- $quote = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
- ->setMethods(['getId', 'getStoreId', '__wakeup'])
- ->disableOriginalConstructor()
- ->getMock();
- $quote->expects($this->once())
- ->method('getId')
- ->will($this->returnValue($idValue));
- $quote->expects($this->any())
- ->method('getStoreId')
- ->will($this->returnValue(1));
- $this->model->setQuote($quote);
- $this->assertSame($quote, $this->model->getQuote());
- $this->assertEquals($idValue, $this->model->getQuoteId());
- }
- /**
- * Tests that adding a quantity to an item without a parent item or an id will add additional quantity.
- */
- public function testAddQtyNormal()
- {
- $existingQuantity = 2;
- $quantityToAdd = 3;
- $preparedQuantityToAdd = 4;
- $this->model->setData('qty', $existingQuantity);
- $this->localeFormat->expects($this->at(0))
- ->method('getNumber')
- ->with($quantityToAdd)
- ->will($this->returnValue($preparedQuantityToAdd));
- $this->localeFormat->expects($this->at(1))
- ->method('getNumber')
- ->with($preparedQuantityToAdd + $existingQuantity)
- ->will($this->returnValue($preparedQuantityToAdd + $existingQuantity));
- $this->model->addQty($quantityToAdd);
- $this->assertEquals($preparedQuantityToAdd, $this->model->getQtyToAdd());
- $this->assertEquals($preparedQuantityToAdd + $existingQuantity, $this->model->getQty());
- }
- /**
- * Tests that adding a quantity to an item with a parent item and an id will not change the quantity.
- */
- public function testAddQtyExistingParentItemAndId()
- {
- $existingQuantity = 2;
- $quantityToAdd = 3;
- $parentItemMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
- ->setMethods(['addChild', '__wakeup'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->model->setParentItem($parentItemMock);
- $this->model->setId(1);
- $this->model->setData('qty', $existingQuantity);
- $this->model->addQty($quantityToAdd);
- $this->assertEquals($existingQuantity, $this->model->getQty());
- $this->assertNull($this->model->getQtyToAdd());
- }
- public function testSetQty()
- {
- $existingQuantity = 2;
- $quantityToAdd = 3;
- $preparedQuantityToAdd = 4;
- $this->localeFormat->expects($this->once())
- ->method('getNumber')
- ->with($quantityToAdd)
- ->will($this->returnValue($preparedQuantityToAdd));
- $this->model->setData('qty', $existingQuantity);
- $this->eventDispatcher->expects($this->once())
- ->method('dispatch')
- ->with('sales_quote_item_qty_set_after', ['item' => $this->model]);
- $this->model->setQty($quantityToAdd);
- $this->assertEquals($preparedQuantityToAdd, $this->model->getQty());
- }
- public function testSetQtyQuoteIgnoreOldQuantity()
- {
- $existingQuantity = 2;
- $quantityToAdd = 3;
- $preparedQuantityToAdd = 4;
- $this->localeFormat->expects($this->once())
- ->method('getNumber')
- ->with($quantityToAdd)
- ->will($this->returnValue($preparedQuantityToAdd));
- $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
- ->disableOriginalConstructor()
- ->setMethods(['getIgnoreOldQty', 'getStoreId', '__wakeup'])
- ->getMock();
- $quoteMock->expects($this->once())
- ->method('getIgnoreOldQty')
- ->will($this->returnValue(true));
- $quoteMock->expects($this->any())
- ->method('getStoreId')
- ->will($this->returnValue(1));
- $this->model->setQuote($quoteMock);
- $this->model->setData('qty', $existingQuantity);
- $this->eventDispatcher->expects($this->once())
- ->method('dispatch')
- ->with('sales_quote_item_qty_set_after', ['item' => $this->model]);
- $this->model->setQty($quantityToAdd);
- $this->assertEquals($preparedQuantityToAdd, $this->model->getQty());
- }
- public function testSetQtyUseOldQuantity()
- {
- $existingQuantity = 2;
- $quantityToAdd = 3;
- $preparedQuantityToAdd = 4;
- $this->localeFormat->expects($this->once())
- ->method('getNumber')
- ->with($quantityToAdd)
- ->will($this->returnValue($preparedQuantityToAdd));
- $this->model->setData('qty', $existingQuantity);
- $this->model->setUseOldQty(true);
- $this->eventDispatcher->expects($this->once())
- ->method('dispatch')
- ->with('sales_quote_item_qty_set_after', ['item' => $this->model]);
- $this->model->setQty($quantityToAdd);
- $this->assertEquals($existingQuantity, $this->model->getQty());
- }
- public function testSetQtyOptions()
- {
- $value = ['a' => 'b'];
- $this->model->setQtyOptions($value);
- $this->assertEquals($value, $this->model->getQtyOptions());
- }
- public function testSetProduct()
- {
- $productMock = $this->generateProductMock(
- self::PRODUCT_ID,
- self::PRODUCT_TYPE,
- self::PRODUCT_SKU,
- self::PRODUCT_NAME,
- self::PRODUCT_WEIGHT,
- self::PRODUCT_TAX_CLASS_ID,
- self::PRODUCT_COST
- );
- $this->eventDispatcher->expects($this->once())
- ->method('dispatch')
- ->with('sales_quote_item_set_product', ['product' => $productMock, 'quote_item' => $this->model]);
- $this->model->setProduct($productMock);
- $this->assertEquals($productMock, $this->model->getProduct());
- $this->assertEquals(self::PRODUCT_ID, $this->model->getProductId());
- $this->assertEquals(self::PRODUCT_TYPE, $this->model->getData('product_type'));
- $this->assertEquals(self::PRODUCT_SKU, $this->model->getSku());
- $this->assertEquals(self::PRODUCT_NAME, $this->model->getName());
- $this->assertEquals(self::PRODUCT_WEIGHT, $this->model->getWeight());
- $this->assertEquals(self::PRODUCT_TAX_CLASS_ID, $this->model->getTaxClassId());
- $this->assertEquals(self::PRODUCT_COST, $this->model->getBaseCost());
- $this->assertNull($this->model->getIsQtyDecimal());
- }
- public function testSetProductWithQuoteAndStockItem()
- {
- $productMock = $this->generateProductMock(
- self::PRODUCT_ID,
- self::PRODUCT_TYPE,
- self::PRODUCT_SKU,
- self::PRODUCT_NAME,
- self::PRODUCT_WEIGHT,
- self::PRODUCT_TAX_CLASS_ID,
- self::PRODUCT_COST
- );
- $this->eventDispatcher->expects($this->once())
- ->method('dispatch')
- ->with('sales_quote_item_set_product', ['product' => $productMock, 'quote_item' => $this->model]);
- $isQtyDecimal = true;
- $this->stockItemMock->expects($this->once())
- ->method('getIsQtyDecimal')
- ->will($this->returnValue($isQtyDecimal));
- $storeId = 15;
- $customerGroupId = 11;
- $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
- ->disableOriginalConstructor()
- ->setMethods(['getStoreId', 'getCustomerGroupId', '__wakeup'])
- ->getMock();
- $quoteMock->expects($this->any())
- ->method('getStoreId')
- ->will($this->returnValue($storeId));
- $quoteMock->expects($this->once())
- ->method('getCustomerGroupId')
- ->will($this->returnValue($customerGroupId));
- $this->model->setQuote($quoteMock);
- $productMock->expects($this->once())
- ->method('setStoreId')
- ->with($storeId);
- $productMock->expects($this->once())
- ->method('setCustomerGroupId')
- ->with($customerGroupId);
- $this->model->setProduct($productMock);
- $this->assertEquals($productMock, $this->model->getProduct());
- $this->assertEquals(self::PRODUCT_ID, $this->model->getProductId());
- $this->assertEquals(self::PRODUCT_TYPE, $this->model->getRealProductType());
- $this->assertEquals(self::PRODUCT_SKU, $this->model->getSku());
- $this->assertEquals(self::PRODUCT_NAME, $this->model->getName());
- $this->assertEquals(self::PRODUCT_WEIGHT, $this->model->getWeight());
- $this->assertEquals(self::PRODUCT_TAX_CLASS_ID, $this->model->getTaxClassId());
- $this->assertEquals(self::PRODUCT_COST, $this->model->getBaseCost());
- $this->assertEquals($isQtyDecimal, $this->model->getIsQtyDecimal());
- }
- /**
- * Generate product mock.
- *
- * @param int $productId
- * @param string $productType
- * @param string $productSku
- * @param string $productName
- * @param string $productWeight
- * @param int $productTaxClassId
- * @param float $productCost
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- private function generateProductMock(
- $productId,
- $productType,
- $productSku,
- $productName,
- $productWeight,
- $productTaxClassId,
- $productCost
- ) {
- $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
- ->disableOriginalConstructor()
- ->setMethods(
- [
- 'getId',
- 'getTypeId',
- 'getSku',
- 'getName',
- 'getWeight',
- 'getTaxClassId',
- 'getCost',
- 'setStoreId',
- 'setCustomerGroupId',
- 'getTypeInstance',
- 'getStickWithinParent',
- 'getCustomOptions',
- 'getExtensionAttributes',
- 'toArray',
- '__wakeup',
- 'getStore',
- ]
- )
- ->getMock();
- $productMock->expects($this->any())
- ->method('getId')
- ->will($this->returnValue($productId));
- $productMock->expects($this->any())
- ->method('getTypeId')
- ->will($this->returnValue($productType));
- $productMock->expects($this->any())
- ->method('getSku')
- ->will($this->returnValue($productSku));
- $productMock->expects($this->any())
- ->method('getName')
- ->will($this->returnValue($productName));
- $productMock->expects($this->any())
- ->method('getWeight')
- ->will($this->returnValue($productWeight));
- $productMock->expects($this->any())
- ->method('getTaxClassId')
- ->will($this->returnValue($productTaxClassId));
- $productMock->expects($this->any())
- ->method('getCost')
- ->will($this->returnValue($productCost));
- $store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getWebsiteId']);
- $store->expects($this->any())
- ->method('getWebsiteId')
- ->will($this->returnValue(10));
- $productMock->expects($this->any())
- ->method('getStore')
- ->will($this->returnValue($store));
- $extensionAttribute = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductExtensionInterface::class)
- ->setMethods(['getStockItem'])
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $extensionAttribute->expects($this->atLeastOnce())
- ->method('getStockItem')
- ->will($this->returnValue($this->stockItemMock));
- $productMock->expects($this->atLeastOnce())->method('getExtensionAttributes')->willReturn($extensionAttribute);
- return $productMock;
- }
- public function testRepresentProductNoProduct()
- {
- $productMock = $this->generateProductMock(
- self::PRODUCT_ID,
- self::PRODUCT_TYPE,
- self::PRODUCT_SKU,
- self::PRODUCT_NAME,
- self::PRODUCT_WEIGHT,
- self::PRODUCT_TAX_CLASS_ID,
- self::PRODUCT_COST
- );
- $this->model->setProduct($productMock);
- $this->assertFalse($this->model->representProduct(null));
- }
- public function testRepresentProductStickWithinParentNotSameAsParentItem()
- {
- $productMock = $this->generateProductMock(
- self::PRODUCT_ID,
- self::PRODUCT_TYPE,
- self::PRODUCT_SKU,
- self::PRODUCT_NAME,
- self::PRODUCT_WEIGHT,
- self::PRODUCT_TAX_CLASS_ID,
- self::PRODUCT_COST
- );
- $parentItemMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
- ->setMethods(['addChild', '__wakeup'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->model->setProduct($productMock);
- $this->model->setParentItem($parentItemMock);
- $productMock->expects($this->once())
- ->method('getStickWithinParent')
- ->will($this->returnValue(true));
- $this->assertFalse($this->model->representProduct($productMock));
- }
- public function testRepresentProductItemOptionsNotInProductOptions()
- {
- $productMock = $this->generateProductMock(
- self::PRODUCT_ID,
- self::PRODUCT_TYPE,
- self::PRODUCT_SKU,
- self::PRODUCT_NAME,
- self::PRODUCT_WEIGHT,
- self::PRODUCT_TAX_CLASS_ID,
- self::PRODUCT_COST
- );
- $parentItemMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
- ->setMethods(['addChild', '__wakeup'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->model->setProduct($productMock);
- $this->model->setParentItem($parentItemMock);
- $optionCode1 = 1234;
- $optionMock1 = $this->createOptionMock($optionCode1);
- $optionMock1->expects($this->any())
- ->method('getValue')
- ->will($this->returnValue(1234));
- $optionCode2 = 7890;
- $optionMock2 = $this->createOptionMock($optionCode2);
- $optionMock2->expects($this->any())
- ->method('getValue')
- ->will($this->returnValue(7890));
- $this->model->setOptions([$optionMock1, $optionMock2]);
- $productMock->expects($this->once())
- ->method('getStickWithinParent')
- ->will($this->returnValue($parentItemMock));
- $productMock->expects($this->once())
- ->method('getCustomOptions')
- ->will($this->returnValue([$optionCode1 => $optionMock1]));
- $this->assertFalse($this->model->representProduct($productMock));
- }
- public function testRepresentProductProductOptionsNotInItemOptions()
- {
- $productMock = $this->generateProductMock(
- self::PRODUCT_ID,
- self::PRODUCT_TYPE,
- self::PRODUCT_SKU,
- self::PRODUCT_NAME,
- self::PRODUCT_WEIGHT,
- self::PRODUCT_TAX_CLASS_ID,
- self::PRODUCT_COST
- );
- $parentItemMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
- ->setMethods(['addChild', '__wakeup'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->model->setProduct($productMock);
- $this->model->setParentItem($parentItemMock);
- $optionCode1 = 1234;
- $optionMock1 = $this->createOptionMock($optionCode1);
- $optionMock1->expects($this->any())
- ->method('getValue')
- ->will($this->returnValue(1234));
- $optionCode2 = 7890;
- $optionMock2 = $this->createOptionMock($optionCode2);
- $optionMock2->expects($this->any())
- ->method('getValue')
- ->will($this->returnValue(7890));
- $this->model->setOptions([$optionMock1]);
- $productMock->expects($this->once())
- ->method('getStickWithinParent')
- ->will($this->returnValue($parentItemMock));
- $productMock->expects($this->once())
- ->method('getCustomOptions')
- ->will($this->returnValue([$optionCode1 => $optionMock1, $optionCode2 => $optionMock2]));
- $this->assertFalse($this->model->representProduct($productMock));
- }
- public function testRepresentProductTrue()
- {
- $productMock = $this->generateProductMock(
- self::PRODUCT_ID,
- self::PRODUCT_TYPE,
- self::PRODUCT_SKU,
- self::PRODUCT_NAME,
- self::PRODUCT_WEIGHT,
- self::PRODUCT_TAX_CLASS_ID,
- self::PRODUCT_COST
- );
- $parentItemMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
- ->setMethods(['addChild', '__wakeup'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->model->setProduct($productMock);
- $this->model->setParentItem($parentItemMock);
- $optionCode1 = 1234;
- $optionMock1 = $this->createOptionMock($optionCode1);
- $optionMock1->expects($this->any())
- ->method('getValue')
- ->will($this->returnValue(1234));
- $optionCode2 = 7890;
- $optionMock2 = $this->createOptionMock($optionCode2);
- $optionMock2->expects($this->any())
- ->method('getValue')
- ->will($this->returnValue(7890));
- $this->model->setOptions([$optionMock1, $optionMock2]);
- $productMock->expects($this->once())
- ->method('getStickWithinParent')
- ->will($this->returnValue($parentItemMock));
- $productMock->expects($this->once())
- ->method('getCustomOptions')
- ->will($this->returnValue([$optionCode1 => $optionMock1, $optionCode2 => $optionMock2]));
- $this->assertTrue($this->model->representProduct($productMock));
- }
- /**
- * test compare
- */
- public function testCompare()
- {
- $itemMock = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
- $this->compareHelper->expects($this->once())
- ->method('compare')
- ->with($this->equalTo($this->model), $this->equalTo($itemMock))
- ->will($this->returnValue(true));
- $this->assertTrue($this->model->compare($itemMock));
- }
- public function testCompareOptionsEqual()
- {
- $optionCode1 = 1234;
- $optionMock1 = $this->createOptionMock($optionCode1);
- $optionMock1->expects($this->any())
- ->method('getValue')
- ->will($this->returnValue(1234));
- $this->assertTrue(
- $this->model->compareOptions([$optionCode1 => $optionMock1], [$optionCode1 => $optionMock1])
- );
- }
- public function testCompareOptionsDifferentValues()
- {
- $optionCode1 = 1234;
- $optionMock1 = $this->createOptionMock($optionCode1);
- $optionMock1->expects($this->any())
- ->method('getValue')
- ->will($this->returnValue(1234));
- $optionCode2 = 1234;
- $optionMock2 = $this->createOptionMock($optionCode1);
- $optionMock2->expects($this->any())
- ->method('getValue')
- ->will($this->returnValue(7890));
- $this->assertFalse(
- $this->model->compareOptions([$optionCode1 => $optionMock1], [$optionCode2 => $optionMock2])
- );
- }
- public function testCompareOptionsNullValues()
- {
- $optionCode1 = 1234;
- $optionMock1 = $this->createOptionMock($optionCode1);
- $optionMock1->expects($this->any())
- ->method('getValue')
- ->will($this->returnValue(1234));
- $optionCode2 = 1234;
- $optionMock2 = $this->createOptionMock($optionCode1);
- $optionMock2->expects($this->any())
- ->method('getValue')
- ->will($this->returnValue(null));
- $this->assertFalse(
- $this->model->compareOptions([$optionCode1 => $optionMock1], [$optionCode2 => $optionMock2])
- );
- }
- public function testCompareOptionsMultipleEquals()
- {
- $optionCode1 = 1234;
- $optionMock1 = $this->createOptionMock($optionCode1);
- $optionMock1->expects($this->any())
- ->method('getValue')
- ->will($this->returnValue(1234));
- $optionCode2 = 7890;
- $optionMock2 = $this->createOptionMock($optionCode1);
- $optionMock2->expects($this->any())
- ->method('getValue')
- ->will($this->returnValue(7890));
- $this->assertFalse(
- $this->model->compareOptions(
- [$optionCode1 => $optionMock1, $optionCode2 => $optionMock2],
- [$optionCode1 => $optionMock1, $optionCode2 => $optionMock2]
- )
- );
- }
- public function testGetQtyOptions()
- {
- $optionCode1 = 1234;
- $optionMock1 = $this->createOptionMock($optionCode1);
- $productMock = $this->generateProductMock(
- self::PRODUCT_ID,
- self::PRODUCT_TYPE,
- self::PRODUCT_SKU,
- self::PRODUCT_NAME,
- self::PRODUCT_WEIGHT,
- self::PRODUCT_TAX_CLASS_ID,
- self::PRODUCT_COST
- );
- $optionMock1->expects($this->any())
- ->method('getProduct')
- ->will($this->returnValue($productMock));
- $optionCode2 = 'product_qty_' . self::PRODUCT_ID;
- $optionMock2 = $this->createOptionMock($optionCode2);
- $productMock2 = $this->generateProductMock(
- self::PRODUCT_ID + 1,
- self::PRODUCT_TYPE,
- self::PRODUCT_SKU,
- self::PRODUCT_NAME,
- self::PRODUCT_WEIGHT,
- self::PRODUCT_TAX_CLASS_ID,
- self::PRODUCT_COST
- );
- $this->model->setProduct($productMock);
- $this->model->setProduct($productMock2);
- $this->model->setOptions([$optionCode1 => $optionMock1, $optionCode2 => $optionMock2]);
- $this->assertEquals([self::PRODUCT_ID => $optionMock2], $this->model->getQtyOptions());
- }
- public function testToArray()
- {
- $productMock = $this->generateProductMock(
- self::PRODUCT_ID,
- self::PRODUCT_TYPE,
- self::PRODUCT_SKU,
- self::PRODUCT_NAME,
- self::PRODUCT_WEIGHT,
- self::PRODUCT_TAX_CLASS_ID,
- self::PRODUCT_COST
- );
- $this->model->setProduct($productMock);
- $toArrayValue = ['a' => 'b'];
- $productMock->expects($this->once())
- ->method('toArray')
- ->will($this->returnValue($toArrayValue));
- $data = $this->model->toArray();
- $this->assertEquals($toArrayValue, $data['product']);
- }
- public function testGetProductTypeOption()
- {
- $optionProductType = 'product_type';
- $optionProductTypeValue = 'abcd';
- $optionMock = $this->createOptionMock($optionProductType);
- $optionMock->expects($this->once())
- ->method('getValue')
- ->will($this->returnValue($optionProductTypeValue));
- $this->model->addOption($optionMock);
- $this->assertEquals($optionProductTypeValue, $this->model->getProductType());
- }
- public function testGetProductTypeWithProduct()
- {
- $productMock = $this->generateProductMock(
- self::PRODUCT_ID,
- self::PRODUCT_TYPE,
- self::PRODUCT_SKU,
- self::PRODUCT_NAME,
- self::PRODUCT_WEIGHT,
- self::PRODUCT_TAX_CLASS_ID,
- self::PRODUCT_COST
- );
- $this->model->setProduct($productMock);
- $this->assertEquals(self::PRODUCT_TYPE, $this->model->getProductType());
- }
- public function testSetOptions()
- {
- $optionCode1 = 1234;
- $optionMock1 = $this->createOptionMock($optionCode1);
- $optionCode2 = 7890;
- $optionMock2 = $this->createOptionMock($optionCode2);
- $this->assertSame($this->model, $this->model->setOptions([$optionMock1, $optionMock2]));
- $this->assertEquals([$optionMock1, $optionMock2], $this->model->getOptions());
- $this->assertEquals($optionMock1, $this->model->getOptionByCode($optionCode1));
- $this->assertEquals($optionMock2, $this->model->getOptionByCode($optionCode2));
- }
- public function testSetOptionsWithNull()
- {
- $this->assertEquals($this->model, $this->model->setOptions(null));
- }
- /**
- * @param $optionCode
- * @param array $optionData
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- private function createOptionMock($optionCode, $optionData = [])
- {
- $optionMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item\Option::class)
- ->setMethods([
- 'setData',
- 'setItem',
- 'getItem',
- 'getCode',
- '__wakeup',
- 'isDeleted',
- 'delete',
- 'getValue',
- 'getProduct',
- 'save'
- ])
- ->disableOriginalConstructor()
- ->getMock();
- $optionMock->expects($this->any())
- ->method('setData')
- ->with($optionData)
- ->will($this->returnValue($optionMock));
- $optionMock->expects($this->any())
- ->method('setItem')
- ->with($this->model)
- ->will($this->returnValue($optionMock));
- $optionMock->expects($this->any())
- ->method('getCode')
- ->will($this->returnValue($optionCode));
- return $optionMock;
- }
- public function testAddOptionArray()
- {
- $optionCode = 1234;
- $optionData = ['product' => 'test', 'code' => $optionCode];
- $optionMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item\Option::class)
- ->setMethods(['setData', 'setItem', 'getCode', '__wakeup', 'isDeleted'])
- ->disableOriginalConstructor()
- ->getMock();
- $optionMock->expects($this->once())
- ->method('setData')
- ->with($optionData)
- ->will($this->returnValue($optionMock));
- $optionMock->expects($this->once())
- ->method('setItem')
- ->with($this->model)
- ->will($this->returnValue($optionMock));
- $optionMock->expects($this->exactly(3))
- ->method('getCode')
- ->will($this->returnValue($optionCode));
- $this->itemOptionFactory->expects($this->at(0))
- ->method('create')
- ->will($this->returnValue($optionMock));
- $this->model->addOption($optionData);
- $this->assertEquals([$optionMock], $this->model->getOptions());
- $this->assertEquals([$optionCode => $optionMock], $this->model->getOptionsByCode());
- $this->assertEquals($optionMock, $this->model->getOptionByCode($optionCode));
- }
- public function testUpdateQtyOption()
- {
- $productMock = $this->generateProductMock(
- self::PRODUCT_ID,
- self::PRODUCT_TYPE,
- self::PRODUCT_SKU,
- self::PRODUCT_NAME,
- self::PRODUCT_WEIGHT,
- self::PRODUCT_TAX_CLASS_ID,
- self::PRODUCT_COST
- );
- $typeInstanceMock = $this->getMockForAbstractClass(
- \Magento\Catalog\Model\Product\Type\AbstractType::class,
- [],
- '',
- false,
- false,
- true,
- ['updateQtyOption']
- );
- $productMock->expects($this->once())
- ->method('getTypeInstance')
- ->will($this->returnValue($typeInstanceMock));
- $optionMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
- ->disableOriginalConstructor()
- ->setMethods(['getProduct'])
- ->getMock();
- $optionMock->expects($this->once())
- ->method('getProduct')
- ->will($this->returnValue($productMock));
- $quantityValue = 12;
- $this->model->setProduct($productMock);
- $typeInstanceMock->expects($this->once())
- ->method('updateQtyOption')
- ->with($this->model->getOptions(), $optionMock, $quantityValue, $productMock);
- $this->assertEquals($this->model, $this->model->updateQtyOption($optionMock, $quantityValue));
- }
- public function testRemoveOption()
- {
- $optionCode = 1234;
- $optionMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item\Option::class)
- ->setMethods(['setItem', 'getCode', '__wakeup', 'isDeleted'])
- ->disableOriginalConstructor()
- ->getMock();
- $optionMock->expects($this->once())
- ->method('setItem')
- ->with($this->model)
- ->will($this->returnValue($optionMock));
- $optionMock->expects($this->exactly(3))
- ->method('getCode')
- ->will($this->returnValue($optionCode));
- $optionMock->expects($this->at(0))
- ->method('isDeleted')
- ->will($this->returnValue(false));
- $optionMock->expects($this->at(1))
- ->method('isDeleted')
- ->will($this->returnValue(true));
- $this->model->addOption($optionMock);
- $this->assertEquals($this->model, $this->model->removeOption($optionCode));
- }
- public function testRemoveOptionNoOptionCodeExists()
- {
- $this->assertEquals($this->model, $this->model->removeOption('random'));
- }
- public function testGetOptionByCodeNonExistent()
- {
- $this->assertNull($this->model->getOptionByCode('random'));
- }
- public function testGetOptionByCodeDeletedCode()
- {
- $optionCode = 1234;
- $optionMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item\Option::class)
- ->setMethods(['setItem', 'getCode', '__wakeup', 'isDeleted'])
- ->disableOriginalConstructor()
- ->getMock();
- $optionMock->expects($this->once())
- ->method('setItem')
- ->with($this->model)
- ->will($this->returnValue($optionMock));
- $optionMock->expects($this->exactly(3))
- ->method('getCode')
- ->will($this->returnValue($optionCode));
- $optionMock->expects($this->once())
- ->method('isDeleted')
- ->will($this->returnValue(true));
- $this->model->addOption($optionMock);
- $this->assertNull($this->model->getOptionByCode($optionCode));
- }
- public function testGetOptionByCodeNotDeletedCode()
- {
- $optionCode = 1234;
- $optionMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item\Option::class)
- ->setMethods(['setItem', 'getCode', '__wakeup', 'isDeleted'])
- ->disableOriginalConstructor()
- ->getMock();
- $optionMock->expects($this->once())
- ->method('setItem')
- ->with($this->model)
- ->will($this->returnValue($optionMock));
- $optionMock->expects($this->exactly(3))
- ->method('getCode')
- ->will($this->returnValue($optionCode));
- $optionMock->expects($this->once())
- ->method('isDeleted')
- ->will($this->returnValue(false));
- $this->model->addOption($optionMock);
- $this->assertSame($optionMock, $this->model->getOptionByCode($optionCode));
- }
- public function testGetBuyRequestNoOptionByCode()
- {
- $quantity = 12;
- $this->localeFormat->expects($this->at(0))
- ->method('getNumber')
- ->with($quantity)
- ->will($this->returnValue($quantity));
- $this->model->setQty($quantity);
- $this->assertEquals($quantity, $this->model->getQty());
- $buyRequest = $this->model->getBuyRequest();
- $this->assertEquals(0, $buyRequest->getOriginalQty());
- $this->assertEquals($quantity, $buyRequest->getQty());
- }
- public function testGetBuyRequestOptionByCode()
- {
- $optionCode = "info_buyRequest";
- $buyRequestQuantity = 23;
- $optionMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item\Option::class)
- ->setMethods(['setItem', 'getCode', '__wakeup', 'getValue'])
- ->disableOriginalConstructor()
- ->getMock();
- $optionMock->expects($this->once())
- ->method('setItem')
- ->with($this->model)
- ->will($this->returnValue($optionMock));
- $optionMock->expects($this->exactly(3))
- ->method('getCode')
- ->will($this->returnValue($optionCode));
- $optionMock->expects($this->any())
- ->method('getValue')
- ->will($this->returnValue('{"qty":23}'));
- $this->model->addOption($optionMock);
- $quantity = 12;
- $this->localeFormat->expects($this->at(0))
- ->method('getNumber')
- ->with($quantity)
- ->will($this->returnValue($quantity));
- $this->model->setQty($quantity);
- $this->assertEquals($quantity, $this->model->getQty());
- $this->serializer->expects($this->any())
- ->method('unserialize')
- ->willReturn(json_decode($optionMock->getValue(), true));
- $buyRequest = $this->model->getBuyRequest();
- $this->assertEquals($buyRequestQuantity, $buyRequest->getOriginalQty());
- $this->assertEquals($quantity, $buyRequest->getQty());
- }
- public function testSetHasErrorFalse()
- {
- $this->errorInfos->expects($this->once())
- ->method('clear');
- $this->assertEquals($this->model, $this->model->setHasError(false));
- $this->assertFalse($this->model->getHasError());
- }
- public function testSetHasErrorTrue()
- {
- $this->errorInfos->expects($this->once())
- ->method('addItem')
- ->with(null, null, null, null);
- $this->assertEquals($this->model, $this->model->setHasError(true));
- $this->assertTrue($this->model->getHasError());
- $this->assertEquals('', $this->model->getMessage());
- }
- public function testAddErrorInfo()
- {
- $origin = 'origin';
- $code = 1;
- $message = "message";
- $additionalData = new \Magento\Framework\DataObject();
- $additionalData->setTemp(true);
- $this->errorInfos->expects($this->once())
- ->method('addItem')
- ->with($origin, $code, $message, $additionalData);
- $this->assertEquals($this->model, $this->model->addErrorInfo($origin, $code, $message, $additionalData));
- $this->assertTrue($this->model->getHasError());
- $this->assertEquals($message, $this->model->getMessage());
- }
- public function testGetErrorInfos()
- {
- $retValue = 'return value';
- $this->errorInfos->expects($this->once())
- ->method('getItems')
- ->will($this->returnValue($retValue));
- $this->assertEquals($retValue, $this->model->getErrorInfos());
- }
- public function testRemoveErrorInfosByParams()
- {
- $message = "message";
- $message2 = "message2";
- $this->errorInfos->expects($this->at(0))
- ->method('addItem')
- ->with(null, null, $message);
- $this->errorInfos->expects($this->at(1))
- ->method('addItem')
- ->with(null, null, $message2);
- $this->assertEquals($this->model, $this->model->addErrorInfo(null, null, $message));
- $this->assertEquals($this->model, $this->model->addErrorInfo(null, null, $message2));
- $this->assertEquals($message . "\n" . $message2, $this->model->getMessage());
- $params = [];
- $removedItems = [['message' => $message]];
- $this->errorInfos->expects($this->once())
- ->method('removeItemsByParams')
- ->with($params)
- ->will($this->returnValue($removedItems));
- $this->errorInfos->expects($this->once())
- ->method('getItems')
- ->will($this->returnValue(true));
- $this->assertEquals($this->model, $this->model->removeErrorInfosByParams($params));
- $this->assertEquals($message2, $this->model->getMessage());
- }
- public function testRemoveErrorInfosByParamsAllErrorsRemoved()
- {
- $message = "message";
- $message2 = "message2";
- $this->errorInfos->expects($this->at(0))
- ->method('addItem')
- ->with(null, null, $message);
- $this->errorInfos->expects($this->at(1))
- ->method('addItem')
- ->with(null, null, $message2);
- $this->assertEquals($this->model, $this->model->addErrorInfo(null, null, $message));
- $this->assertEquals($this->model, $this->model->addErrorInfo(null, null, $message2));
- $this->assertEquals($message . "\n" . $message2, $this->model->getMessage());
- $params = [];
- $removedItems = [['message' => $message], ['message' => $message2]];
- $this->errorInfos->expects($this->once())
- ->method('removeItemsByParams')
- ->with($params)
- ->will($this->returnValue($removedItems));
- $this->errorInfos->expects($this->once())
- ->method('getItems')
- ->will($this->returnValue([]));
- $this->assertEquals($this->model, $this->model->removeErrorInfosByParams($params));
- $this->assertFalse($this->model->getHasError());
- $this->assertEquals('', $this->model->getMessage());
- }
- /**
- * Test method \Magento\Quote\Model\Quote\Item::saveItemOptions
- */
- public function testSaveItemOptions()
- {
- $optionMockDeleted = $this->createOptionMock(100);
- $optionMockDeleted->expects(self::once())->method('isDeleted')->willReturn(true);
- $optionMockDeleted->expects(self::once())->method('delete');
- $optionMock1 = $this->createOptionMock(200);
- $optionMock1->expects(self::once())->method('isDeleted')->willReturn(false);
- $quoteItemMock1 = $this->createPartialMock(\Magento\Quote\Model\Quote\Item::class, ['getId']);
- $quoteItemMock1->expects(self::once())->method('getId')->willReturn(null);
- $optionMock1->expects(self::exactly(2))->method('getItem')->willReturn($quoteItemMock1);
- $optionMock1->expects(self::exactly(2))->method('setItem')->with($this->model);
- $optionMock1->expects(self::once())->method('save');
- $optionMock2 = $this->createOptionMock(300);
- $optionMock2->expects(self::once())->method('isDeleted')->willReturn(false);
- $quoteItemMock2 = $this->createPartialMock(\Magento\Quote\Model\Quote\Item::class, ['getId']);
- $quoteItemMock2->expects(self::once())->method('getId')->willReturn(11);
- $optionMock2->expects(self::exactly(2))->method('getItem')->willReturn($quoteItemMock2);
- $optionMock2->expects(self::once())->method('setItem')->with($this->model);
- $optionMock2->expects(self::once())->method('save');
- $this->model->setOptions([$optionMockDeleted, $optionMock1, $optionMock2]);
- $this->model->saveItemOptions();
- }
- }
|