123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Quote\Test\Unit\Model\Quote\Item;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
- /**
- * Tests for Magento\Quote\Model\Service\Quote\Updater
- *
- */
- class UpdaterTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Quote\Model\Quote\Item\Updater |\PHPUnit_Framework_MockObject_MockObject
- */
- protected $object;
- /**
- * @var \Magento\Quote\Model\Quote\Item |\PHPUnit_Framework_MockObject_MockObject
- */
- protected $itemMock;
- /**
- * @var \Magento\CatalogInventory\Model\Stock\Item |\PHPUnit_Framework_MockObject_MockObject
- */
- protected $stockItemMock;
- /**
- * @var \Magento\Framework\Locale\Format |\PHPUnit_Framework_MockObject_MockObject
- */
- protected $localeFormat;
- /**
- * @var \Magento\Catalog\Model\Product |\PHPUnit_Framework_MockObject_MockObject
- */
- protected $productMock;
- /**
- * @var \Magento\Framework\Serialize\Serializer\Json
- */
- private $serializer;
- protected function setUp()
- {
- $this->productMock = $this->createPartialMock(\Magento\Catalog\Model\Product::class, [
- 'getStockItem',
- '__wakeup',
- 'setIsSuperMode',
- 'unsSkipCheckRequiredOption'
- ]);
- $this->localeFormat = $this->createPartialMock(\Magento\Framework\Locale\Format::class, [
- 'getNumber', 'getPriceFormat'
- ]);
- $this->itemMock = $this->createPartialMock(\Magento\Quote\Model\Quote\Item::class, [
- 'updateItem',
- 'getProduct',
- 'setQty',
- 'setNoDiscount',
- 'checkData',
- '__wakeup',
- 'getBuyRequest',
- 'addOption',
- 'setCustomPrice',
- 'setOriginalCustomPrice',
- 'setData',
- 'hasData',
- 'setIsQtyDecimal'
- ]);
- $this->stockItemMock = $this->createPartialMock(\Magento\CatalogInventory\Model\Stock\Item::class, [
- 'getIsQtyDecimal',
- '__wakeup'
- ]);
- $this->serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
- ->setMethods(['serialize'])
- ->getMockForAbstractClass();
- $this->object = (new ObjectManager($this))
- ->getObject(
- \Magento\Quote\Model\Quote\Item\Updater::class,
- [
- 'localeFormat' => $this->localeFormat,
- 'serializer' => $this->serializer
- ]
- );
- }
- /**
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage The qty value is required to update quote item.
- */
- public function testUpdateNoQty()
- {
- $this->object->update($this->itemMock, []);
- }
- /**
- * @dataProvider qtyProvider
- */
- public function testUpdateNotQtyDecimal($qty, $expectedQty)
- {
- $this->itemMock->expects($this->any())
- ->method('setNoDiscount')
- ->will($this->returnValue(true));
- $this->itemMock->expects($this->any())
- ->method('setQty')
- ->with($this->equalTo($expectedQty));
- $this->productMock->expects($this->any())
- ->method('getStockItem')
- ->will($this->returnValue($this->stockItemMock));
- $this->productMock->expects($this->any())
- ->method('setIsSuperMode')
- ->with($this->equalTo(true));
- $this->productMock->expects($this->any())
- ->method('unsSkipCheckRequiredOption');
- $this->itemMock->expects($this->any())
- ->method('getProduct')
- ->will($this->returnValue($this->productMock));
- $result = $this->object->update($this->itemMock, ['qty' => $qty]);
- $this->assertEquals($result, $this->object);
- }
- /**
- * @return array
- */
- public function qtyProvider()
- {
- return [
- [1, 1],
- [5.66, 5],
- ['test', 1],
- [-3, 1],
- [0, 1],
- [-2.99, 1]
- ];
- }
- /**
- * @return array
- */
- public function qtyProviderDecimal()
- {
- return [
- [1, 1],
- [5.66, 5.66],
- ['test', 1],
- [-3, 1],
- [0, 1],
- [-2.99, 1]
- ];
- }
- /**
- * @dataProvider qtyProviderDecimal
- */
- public function testUpdateQtyDecimal($qty, $expectedQty)
- {
- $this->itemMock->expects($this->any())
- ->method('setNoDiscount')
- ->will($this->returnValue(true));
- $this->itemMock->expects($this->any())
- ->method('setQty')
- ->with($this->equalTo($expectedQty));
- $this->itemMock->expects($this->any())
- ->method('setIsQtyDecimal')
- ->will($this->returnValue(true));
- $this->stockItemMock->expects($this->any())
- ->method('getIsQtyDecimal')
- ->will($this->returnValue(true));
- $this->productMock->expects($this->any())
- ->method('getStockItem')
- ->will($this->returnValue($this->stockItemMock));
- $this->productMock->expects($this->any())
- ->method('setIsSuperMode')
- ->with($this->equalTo(true));
- $this->productMock->expects($this->any())
- ->method('unsSkipCheckRequiredOption');
- $this->itemMock->expects($this->any())
- ->method('getProduct')
- ->will($this->returnValue($this->productMock));
- $object = $this->object->update($this->itemMock, ['qty' => $qty]);
- $this->assertEquals($this->object, $object);
- }
- public function testUpdateQtyDecimalWithConfiguredOption()
- {
- $this->itemMock->expects($this->any())
- ->method('setIsQtyDecimal')
- ->with($this->equalTo(1));
- $this->stockItemMock->expects($this->any())
- ->method('getIsQtyDecimal')
- ->will($this->returnValue(true));
- $this->productMock->expects($this->any())
- ->method('getStockItem')
- ->will($this->returnValue($this->stockItemMock));
- $this->itemMock->expects($this->any())
- ->method('getProduct')
- ->will($this->returnValue($this->productMock));
- $object = $this->object->update($this->itemMock, ['qty' => 3, 'use_discount' => true]);
- $this->assertEquals($this->object, $object);
- }
- /**
- * @covers \Magento\Quote\Model\Quote\Item\Updater::setCustomPrice()
- */
- public function testUpdateCustomPrice()
- {
- $customPrice = 9.99;
- $qty = 3;
- $buyRequestMock = $this->createPartialMock(\Magento\Framework\DataObject::class, [
- 'setCustomPrice',
- 'setValue',
- 'setCode',
- 'setProduct',
- 'getData'
- ]);
- $buyRequestMock->expects($this->any())
- ->method('setCustomPrice')
- ->with($this->equalTo($customPrice));
- $buyRequestMock->expects($this->any())
- ->method('getData')
- ->will($this->returnValue(['custom_price' => $customPrice]));
- $this->serializer->expects($this->any())
- ->method('serialize')
- ->willReturn(json_encode($buyRequestMock->getData()));
- $buyRequestMock->expects($this->any())
- ->method('setValue')
- ->with($this->equalTo('{"custom_price":' . $customPrice . '}'));
- $buyRequestMock->expects($this->any())
- ->method('setCode')
- ->with($this->equalTo('info_buyRequest'));
- $buyRequestMock->expects($this->any())
- ->method('setProduct')
- ->with($this->equalTo($this->productMock));
- $this->itemMock->expects($this->any())
- ->method('setIsQtyDecimal')
- ->with($this->equalTo(1));
- $this->itemMock->expects($this->any())
- ->method('getBuyRequest')
- ->will($this->returnValue($buyRequestMock));
- $this->stockItemMock->expects($this->any())
- ->method('getIsQtyDecimal')
- ->will($this->returnValue(true));
- $this->productMock->expects($this->any())
- ->method('getStockItem')
- ->will($this->returnValue($this->stockItemMock));
- $this->itemMock->expects($this->any())
- ->method('getProduct')
- ->will($this->returnValue($this->productMock));
- $this->itemMock->expects($this->any())
- ->method('addOption')
- ->will($this->returnValue($buyRequestMock));
- $this->itemMock->expects($this->any())
- ->method('setQty')
- ->with($this->equalTo($qty));
- $this->localeFormat->expects($this->any())
- ->method('getNumber')
- ->will($this->returnArgument(0));
- $object = $this->object->update($this->itemMock, ['qty' => $qty, 'custom_price' => $customPrice]);
- $this->assertEquals($this->object, $object);
- }
- /**
- * @covers \Magento\Quote\Model\Quote\Item\Updater::unsetCustomPrice()
- */
- public function testUpdateUnsetCustomPrice()
- {
- $qty = 3;
- $buyRequestMock = $this->createPartialMock(\Magento\Framework\DataObject::class, [
- 'setCustomPrice',
- 'setValue',
- 'setCode',
- 'setProduct',
- 'getData',
- 'unsetData',
- 'hasData',
- ]);
- $buyRequestMock->expects($this->never())->method('setCustomPrice');
- $buyRequestMock->expects($this->once())->method('getData')->will($this->returnValue([]));
- $serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
- ->setMethods(['serialize'])
- ->getMockForAbstractClass();
- $serializer->expects($this->any())
- ->method('serialize')
- ->willReturn('{}');
- $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $objectManagerHelper->setBackwardCompatibleProperty($this->object, 'serializer', $serializer);
- $buyRequestMock->expects($this->once())->method('unsetData')->with($this->equalTo('custom_price'));
- $buyRequestMock->expects($this->once())
- ->method('hasData')
- ->with($this->equalTo('custom_price'))
- ->will($this->returnValue(true));
- $buyRequestMock->expects($this->any())
- ->method('setValue')
- ->with($this->equalTo('{}'));
- $buyRequestMock->expects($this->any())
- ->method('setCode')
- ->with($this->equalTo('info_buyRequest'));
- $buyRequestMock->expects($this->any())
- ->method('setProduct')
- ->with($this->equalTo($this->productMock));
- $this->itemMock->expects($this->any())
- ->method('setIsQtyDecimal')
- ->with($this->equalTo(1));
- $this->itemMock->expects($this->any())
- ->method('getBuyRequest')
- ->will($this->returnValue($buyRequestMock));
- $this->stockItemMock->expects($this->any())
- ->method('getIsQtyDecimal')
- ->will($this->returnValue(true));
- $this->productMock->expects($this->any())
- ->method('getStockItem')
- ->will($this->returnValue($this->stockItemMock));
- $this->itemMock->expects($this->any())
- ->method('getProduct')
- ->will($this->returnValue($this->productMock));
- $this->itemMock->expects($this->any())
- ->method('addOption')
- ->will($this->returnValue($buyRequestMock));
- $this->itemMock->expects($this->exactly(2))
- ->method('setData')
- ->withConsecutive(
- ['custom_price', null],
- ['original_custom_price', null]
- );
- $this->itemMock->expects($this->once())
- ->method('hasData')
- ->with($this->equalTo('custom_price'))
- ->will($this->returnValue(true));
- $this->itemMock->expects($this->never())->method('setCustomPrice');
- $this->itemMock->expects($this->never())->method('setOriginalCustomPrice');
- $this->localeFormat->expects($this->any())
- ->method('getNumber')
- ->will($this->returnArgument(0));
- $object = $this->object->update($this->itemMock, ['qty' => $qty]);
- $this->assertEquals($this->object, $object);
- }
- }
|