12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Quote\Test\Unit\Model;
- use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
- use Magento\Quote\Api\Data\CartInterface;
- use Magento\Quote\Model\Quote\Address;
- use Magento\Store\Model\ScopeInterface;
- /**
- * Test class for \Magento\Quote\Model
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @SuppressWarnings(PHPMD.TooManyFields)
- */
- class QuoteTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Quote\Model\Quote\AddressFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $quoteAddressFactoryMock;
- /**
- * @var \Magento\Quote\Model\Quote\Address|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $quoteAddressMock;
- /**
- * @var \Magento\Quote\Model\ResourceModel\Quote\Address\Collection|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $quoteAddressCollectionMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $storeManagerMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $resourceMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $contextMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerFactoryMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $eventManagerMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $groupRepositoryMock;
- /**
- * @var \Magento\Quote\Model\Quote
- */
- protected $quote;
- /**
- * @var \Magento\Catalog\Model\Product |\PHPUnit_Framework_MockObject_MockObject
- */
- protected $productMock;
- /**
- * @var \Magento\Framework\DataObject\Factory |\PHPUnit_Framework_MockObject_MockObject
- */
- protected $objectFactoryMock;
- /**
- * @var \Magento\Quote\Model\ResourceModel\Quote\Item\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $quoteItemCollectionFactoryMock;
- /**
- * @var \Magento\Quote\Model\Quote\PaymentFactory
- */
- protected $paymentFactoryMock;
- /**
- * @var \Magento\Quote\Model\ResourceModel\Quote\Payment\CollectionFactory
- */
- protected $quotePaymentCollectionFactoryMock;
- /**
- * @var \Magento\Framework\App\Config | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $scopeConfig;
- /**
- * @var \Magento\Customer\Api\AddressRepositoryInterface | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $addressRepositoryMock;
- /**
- * @var \Magento\Framework\Api\SearchCriteriaBuilder | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $criteriaBuilderMock;
- /**
- * @var \Magento\Framework\Api\FilterBuilder | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $filterBuilderMock;
- /**
- * @var \Magento\Framework\Api\ExtensibleDataObjectConverter | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $extensibleDataObjectConverterMock;
- /**
- * @var \Magento\Customer\Api\CustomerRepositoryInterface | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerRepositoryMock;
- /**
- * @var \Magento\Framework\DataObject\Copy | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $objectCopyServiceMock;
- /**
- * @var JoinProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $extensionAttributesJoinProcessorMock;
- /**
- * @var \Magento\Customer\Api\Data\CustomerInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- private $customerDataFactoryMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $itemProcessor;
- /**
- * @var \Magento\Sales\Model\OrderIncrementIdChecker|\PHPUnit_Framework_MockObject_MockObject
- */
- private $orderIncrementIdChecker;
- /**
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- protected function setUp()
- {
- $this->quoteAddressFactoryMock = $this->createPartialMock(
- \Magento\Quote\Model\Quote\AddressFactory::class,
- ['create']
- );
- $this->quoteAddressMock = $this->createPartialMock(\Magento\Quote\Model\Quote\Address::class, [
- 'isDeleted', 'getCollection', 'getId', 'getCustomerAddressId',
- '__wakeup', 'getAddressType', 'getDeleteImmediately', 'validateMinimumAmount', 'setData'
- ]);
- $this->quoteAddressCollectionMock = $this->createMock(
- \Magento\Quote\Model\ResourceModel\Quote\Address\Collection::class
- );
- $this->extensibleDataObjectConverterMock = $this->createPartialMock(
- \Magento\Framework\Api\ExtensibleDataObjectConverter::class,
- ['toFlatArray']
- );
- $this->customerRepositoryMock = $this->getMockForAbstractClass(
- \Magento\Customer\Api\CustomerRepositoryInterface::class,
- [],
- '',
- false,
- true,
- true,
- ['getById', 'save']
- );
- $this->objectCopyServiceMock = $this->createPartialMock(
- \Magento\Framework\DataObject\Copy::class,
- ['copyFieldsetToTarget']
- );
- $this->productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $this->objectFactoryMock = $this->createPartialMock(\Magento\Framework\DataObject\Factory::class, ['create']);
- $this->quoteAddressFactoryMock->expects(
- $this->any()
- )->method(
- 'create'
- )->will(
- $this->returnValue($this->quoteAddressMock)
- );
- $this->quoteAddressMock->expects(
- $this->any()
- )->method(
- 'getCollection'
- )->will(
- $this->returnValue($this->quoteAddressCollectionMock)
- );
- $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\Manager::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManager::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->resourceMock = $this->getMockBuilder(\Magento\Quote\Model\ResourceModel\Quote::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->contextMock = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->customerFactoryMock = $this->getMockBuilder(\Magento\Customer\Model\CustomerFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->groupRepositoryMock = $this->getMockBuilder(\Magento\Customer\Api\GroupRepositoryInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->contextMock->expects($this->any())
- ->method('getEventDispatcher')
- ->will($this->returnValue($this->eventManagerMock));
- $this->quoteItemCollectionFactoryMock = $this->createPartialMock(
- \Magento\Quote\Model\ResourceModel\Quote\Item\CollectionFactory::class,
- ['create']
- );
- $this->quotePaymentCollectionFactoryMock = $this->createPartialMock(
- \Magento\Quote\Model\ResourceModel\Quote\Payment\CollectionFactory::class,
- ['create']
- );
- $this->paymentFactoryMock = $this->createPartialMock(
- \Magento\Quote\Model\Quote\PaymentFactory::class,
- ['create']
- );
- $this->scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->addressRepositoryMock = $this->getMockForAbstractClass(
- \Magento\Customer\Api\AddressRepositoryInterface::class,
- [],
- '',
- false
- );
- $this->criteriaBuilderMock = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteriaBuilder::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->filterBuilderMock = $this->getMockBuilder(\Magento\Framework\Api\FilterBuilder::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->itemProcessor = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item\Processor::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->extensionAttributesJoinProcessorMock = $this->createMock(
- \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface::class
- );
- $this->customerDataFactoryMock = $this->createPartialMock(
- \Magento\Customer\Api\Data\CustomerInterfaceFactory::class,
- ['create']
- );
- $this->orderIncrementIdChecker = $this->createMock(\Magento\Sales\Model\OrderIncrementIdChecker::class);
- $this->quote = (new ObjectManager($this))
- ->getObject(
- \Magento\Quote\Model\Quote::class,
- [
- 'quoteAddressFactory' => $this->quoteAddressFactoryMock,
- 'storeManager' => $this->storeManagerMock,
- 'resource' => $this->resourceMock,
- 'context' => $this->contextMock,
- 'customerFactory' => $this->customerFactoryMock,
- 'groupRepository' => $this->groupRepositoryMock,
- 'objectFactory' => $this->objectFactoryMock,
- 'addressRepository' => $this->addressRepositoryMock,
- 'criteriaBuilder' => $this->criteriaBuilderMock,
- 'filterBuilder' => $this->filterBuilderMock,
- 'quoteItemCollectionFactory' => $this->quoteItemCollectionFactoryMock,
- 'quotePaymentCollectionFactory' => $this->quotePaymentCollectionFactoryMock,
- 'quotePaymentFactory' => $this->paymentFactoryMock,
- 'scopeConfig' => $this->scopeConfig,
- 'extensibleDataObjectConverter' => $this->extensibleDataObjectConverterMock,
- 'customerRepository' => $this->customerRepositoryMock,
- 'objectCopyService' => $this->objectCopyServiceMock,
- 'extensionAttributesJoinProcessor' => $this->extensionAttributesJoinProcessorMock,
- 'customerDataFactory' => $this->customerDataFactoryMock,
- 'itemProcessor' => $this->itemProcessor,
- 'orderIncrementIdChecker' => $this->orderIncrementIdChecker,
- 'data' => [
- 'reserved_order_id' => 1000001,
- ],
- ]
- );
- }
- /**
- * @param array $addresses
- * @param bool $expected
- * @dataProvider isMultipleShippingAddressesDataProvider
- */
- public function testIsMultipleShippingAddresses($addresses, $expected)
- {
- $this->quoteAddressCollectionMock->expects(
- $this->any()
- )->method(
- 'setQuoteFilter'
- )->will(
- $this->returnValue($this->quoteAddressCollectionMock)
- );
- $this->quoteAddressCollectionMock->expects(
- $this->once()
- )->method(
- 'getIterator'
- )->will(
- $this->returnValue(new \ArrayIterator($addresses))
- );
- $this->assertEquals($expected, $this->quote->isMultipleShippingAddresses());
- }
- /**
- * Customer group ID is not set to quote object and customer data is not available.
- */
- public function testGetCustomerGroupIdNotSet()
- {
- $this->assertEquals(
- \Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID,
- $this->quote->getCustomerGroupId(),
- "Customer group ID is invalid"
- );
- }
- /**
- * Customer group ID is set to quote object.
- */
- public function testGetCustomerGroupId()
- {
- /** Preconditions */
- $customerGroupId = 33;
- $this->quote->setCustomerGroupId($customerGroupId);
- /** SUT execution */
- $this->assertEquals($customerGroupId, $this->quote->getCustomerGroupId(), "Customer group ID is invalid");
- }
- /**
- * @return array
- */
- public function isMultipleShippingAddressesDataProvider()
- {
- return [
- [
- [$this->getAddressMock(Address::TYPE_SHIPPING), $this->getAddressMock(Address::TYPE_SHIPPING)],
- true,
- ],
- [
- [$this->getAddressMock(Address::TYPE_SHIPPING), $this->getAddressMock(Address::TYPE_BILLING)],
- false
- ]
- ];
- }
- /**
- * @param string $type One of \Magento\Customer\Model\Address\AbstractAddress::TYPE_ const
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- protected function getAddressMock($type)
- {
- $shippingAddressMock = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Address::class,
- ['getAddressType', '__wakeup', 'isDeleted']
- );
- $shippingAddressMock->expects($this->any())->method('getAddressType')->will($this->returnValue($type));
- $shippingAddressMock->expects($this->any())->method('isDeleted')->will($this->returnValue(false));
- return $shippingAddressMock;
- }
- public function testGetStoreIdNoId()
- {
- $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
- ->disableOriginalConstructor()
- ->getMock();
- $storeMock->expects($this->once())
- ->method('getId')
- ->will($this->returnValue(null));
- $this->storeManagerMock->expects($this->once())
- ->method('getStore')
- ->will($this->returnValue($storeMock));
- $result = $this->quote->getStoreId();
- $this->assertNull($result);
- }
- public function testGetStoreId()
- {
- $storeId = 1;
- $result = $this->quote->setStoreId($storeId)->getStoreId();
- $this->assertEquals($storeId, $result);
- }
- public function testGetStore()
- {
- $storeId = 1;
- $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->storeManagerMock->expects($this->once())
- ->method('getStore')
- ->with($storeId)
- ->will($this->returnValue($storeMock));
- $this->quote->setStoreId($storeId);
- $result = $this->quote->getStore();
- $this->assertInstanceOf(\Magento\Store\Model\Store::class, $result);
- }
- public function testSetStore()
- {
- $storeId = 1;
- $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
- ->disableOriginalConstructor()
- ->getMock();
- $storeMock->expects($this->once())
- ->method('getId')
- ->will($this->returnValue($storeId));
- $result = $this->quote->setStore($storeMock);
- $this->assertInstanceOf(\Magento\Quote\Model\Quote::class, $result);
- }
- public function testGetSharedWebsiteStoreIds()
- {
- $sharedIds = null;
- $storeIds = [1, 2, 3];
- $websiteMock = $this->getMockBuilder(\Magento\Store\Model\Website::class)
- ->disableOriginalConstructor()
- ->getMock();
- $websiteMock->expects($this->once())
- ->method('getStoreIds')
- ->will($this->returnValue($storeIds));
- $this->quote->setData('shared_store_ids', $sharedIds);
- $this->quote->setWebsite($websiteMock);
- $result = $this->quote->getSharedStoreIds();
- $this->assertEquals($storeIds, $result);
- }
- public function testGetSharedStoreIds()
- {
- $sharedIds = null;
- $storeIds = [1, 2, 3];
- $storeId = 1;
- $websiteMock = $this->getMockBuilder(\Magento\Store\Model\Website::class)
- ->disableOriginalConstructor()
- ->getMock();
- $websiteMock->expects($this->once())
- ->method('getStoreIds')
- ->will($this->returnValue($storeIds));
- $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
- ->disableOriginalConstructor()
- ->getMock();
- $storeMock->expects($this->once())
- ->method('getWebsite')
- ->will($this->returnValue($websiteMock));
- $this->storeManagerMock->expects($this->once())
- ->method('getStore')
- ->with($storeId)
- ->will($this->returnValue($storeMock));
- $this->quote->setData('shared_store_ids', $sharedIds);
- $this->quote->setStoreId($storeId);
- $result = $this->quote->getSharedStoreIds();
- $this->assertEquals($storeIds, $result);
- }
- public function testLoadActive()
- {
- $quoteId = 1;
- $this->resourceMock->expects($this->once())
- ->method('loadActive')
- ->with($this->quote, $quoteId);
- $this->eventManagerMock->expects($this->any())
- ->method('dispatch');
- $result = $this->quote->loadActive($quoteId);
- $this->assertInstanceOf(\Magento\Quote\Model\Quote::class, $result);
- }
- public function testloadByIdWithoutStore()
- {
- $quoteId = 1;
- $this->resourceMock->expects($this->once())
- ->method('loadByIdWithoutStore')
- ->with($this->quote, $quoteId);
- $this->eventManagerMock->expects($this->any())
- ->method('dispatch');
- $result = $this->quote->loadByIdWithoutStore($quoteId);
- $this->assertInstanceOf(\Magento\Quote\Model\Quote::class, $result);
- }
- /**
- * @SuppressWarnings(PHPMD.UnusedLocalVariable)
- */
- public function testSetCustomerAddressData()
- {
- $customerId = 1;
- $addressMock = $this->getMockForAbstractClass(
- \Magento\Customer\Api\Data\AddressInterface::class,
- [],
- '',
- false,
- true,
- true,
- ['getId']
- );
- $addressMock->expects($this->any())
- ->method('getId')
- ->will($this->returnValue(null));
- $addresses = [$addressMock];
- $customerMock = $this->getMockForAbstractClass(
- \Magento\Customer\Api\Data\CustomerInterface::class,
- [],
- '',
- false
- );
- $customerResultMock = $this->getMockForAbstractClass(
- \Magento\Customer\Api\Data\CustomerInterface::class,
- [],
- '',
- false
- );
- $requestMock = $this->createMock(
- \Magento\Framework\DataObject::class
- );
- $this->extensibleDataObjectConverterMock->expects($this->any())
- ->method('toFlatArray')
- ->will($this->returnValue(['customer_id' => $customerId]));
- $this->customerRepositoryMock->expects($this->any())
- ->method('getById')
- ->will($this->returnValue($customerMock));
- $this->customerDataFactoryMock->expects($this->any())
- ->method('create')
- ->will($this->returnValue($customerMock));
- $this->customerRepositoryMock->expects($this->never())
- ->method('save')
- ->will($this->returnValue($customerMock));
- $customerMock->expects($this->any())
- ->method('getAddresses')
- ->will($this->returnValue($addresses));
- $this->objectFactoryMock->expects($this->once())
- ->method('create')
- ->with($this->equalTo(['customer_id' => $customerId]))
- ->will($this->returnValue($requestMock));
- $result = $this->quote->setCustomerAddressData([$addressMock]);
- $this->assertInstanceOf(\Magento\Quote\Model\Quote::class, $result);
- $this->assertEquals($customerResultMock, $this->quote->getCustomer());
- }
- public function testGetCustomerTaxClassId()
- {
- $groupId = 1;
- $taxClassId = 1;
- $groupMock = $this->getMockForAbstractClass(\Magento\Customer\Api\Data\GroupInterface::class, [], '', false);
- $groupMock->expects($this->once())
- ->method('getTaxClassId')
- ->willReturn($taxClassId);
- $this->groupRepositoryMock->expects($this->once())
- ->method('getById')
- ->with($groupId)
- ->will($this->returnValue($groupMock));
- $this->quote->setData('customer_group_id', $groupId);
- $result = $this->quote->getCustomerTaxClassId();
- $this->assertEquals($taxClassId, $result);
- }
- public function testGetAllAddresses()
- {
- $id = 1;
- $this->quoteAddressCollectionMock->expects($this->once())
- ->method('setQuoteFilter')
- ->with($id)
- ->will($this->returnSelf());
- $this->quoteAddressMock->expects($this->once())
- ->method('isDeleted')
- ->will($this->returnValue(false));
- $iterator = new \ArrayIterator([$this->quoteAddressMock]);
- $this->quoteAddressCollectionMock->expects($this->any())
- ->method('getIterator')
- ->will($this->returnValue($iterator));
- $this->quote->setId($id);
- $result = $this->quote->getAllAddresses();
- $this->assertEquals([$this->quoteAddressMock], $result);
- }
- /**
- * @dataProvider dataProviderGetAddress
- */
- public function testGetAddressById($addressId, $expected)
- {
- $id = 1;
- $this->quoteAddressCollectionMock->expects($this->once())
- ->method('setQuoteFilter')
- ->with($id)
- ->will($this->returnSelf());
- $this->quoteAddressMock->expects($this->once())
- ->method('getId')
- ->will($this->returnValue($id));
- $iterator = new \ArrayIterator([$this->quoteAddressMock]);
- $this->quoteAddressCollectionMock->expects($this->any())
- ->method('getIterator')
- ->will($this->returnValue($iterator));
- $this->quote->setId($id);
- $result = $this->quote->getAddressById($addressId);
- $this->assertEquals((bool)$expected, (bool)$result);
- }
- /**
- * @return array
- */
- public static function dataProviderGetAddress()
- {
- return [
- [1, true],
- [2, false]
- ];
- }
- /**
- * @param $isDeleted
- * @param $customerAddressId
- * @param $expected
- *
- * @dataProvider dataProviderGetAddressByCustomer
- */
- public function testGetAddressByCustomerAddressId($isDeleted, $customerAddressId, $expected)
- {
- $id = 1;
- $this->quoteAddressCollectionMock->expects($this->once())
- ->method('setQuoteFilter')
- ->with($id)
- ->will($this->returnSelf());
- $this->quoteAddressMock->expects($this->once())
- ->method('isDeleted')
- ->will($this->returnValue($isDeleted));
- $this->quoteAddressMock->expects($this->once())
- ->method('getCustomerAddressId')
- ->will($this->returnValue($customerAddressId));
- $iterator = new \ArrayIterator([$this->quoteAddressMock]);
- $this->quoteAddressCollectionMock->expects($this->any())
- ->method('getIterator')
- ->will($this->returnValue($iterator));
- $this->quote->setId($id);
- $result = $this->quote->getAddressByCustomerAddressId($id);
- $this->assertEquals((bool)$expected, (bool)$result);
- }
- /**
- * @return array
- */
- public static function dataProviderGetAddressByCustomer()
- {
- return [
- [false, 1, true],
- [false, 2, false]
- ];
- }
- /**
- * @param $isDeleted
- * @param $addressType
- * @param $customerAddressId
- * @param $expected
- *
- * @dataProvider dataProviderShippingAddress
- */
- public function testGetShippingAddressByCustomerAddressId($isDeleted, $addressType, $customerAddressId, $expected)
- {
- $id = 1;
- $this->quoteAddressCollectionMock->expects($this->once())
- ->method('setQuoteFilter')
- ->with($id)
- ->will($this->returnSelf());
- $this->quoteAddressMock->expects($this->once())
- ->method('isDeleted')
- ->will($this->returnValue($isDeleted));
- $this->quoteAddressMock->expects($this->once())
- ->method('getCustomerAddressId')
- ->will($this->returnValue($customerAddressId));
- $this->quoteAddressMock->expects($this->once())
- ->method('getAddressType')
- ->will($this->returnValue($addressType));
- $iterator = new \ArrayIterator([$this->quoteAddressMock]);
- $this->quoteAddressCollectionMock->expects($this->any())
- ->method('getIterator')
- ->will($this->returnValue($iterator));
- $this->quote->setId($id);
- $result = $this->quote->getShippingAddressByCustomerAddressId($id);
- $this->assertEquals($expected, (bool)$result);
- }
- /**
- * @return array
- */
- public static function dataProviderShippingAddress()
- {
- return [
- [false, \Magento\Customer\Model\Address\AbstractAddress::TYPE_SHIPPING, 1, true],
- [false, \Magento\Customer\Model\Address\AbstractAddress::TYPE_SHIPPING, 2, false],
- ];
- }
- public function testRemoveAddress()
- {
- $id = 1;
- $this->quoteAddressCollectionMock->expects($this->once())
- ->method('setQuoteFilter')
- ->with($id)
- ->will($this->returnSelf());
- $this->quoteAddressMock->expects($this->once())
- ->method('isDeleted')
- ->with(true);
- $this->quoteAddressMock->expects($this->once())
- ->method('getId')
- ->will($this->returnValue($id));
- $iterator = new \ArrayIterator([$this->quoteAddressMock]);
- $this->quoteAddressCollectionMock->expects($this->any())
- ->method('getIterator')
- ->will($this->returnValue($iterator));
- $this->quote->setId($id);
- $result = $this->quote->removeAddress($id);
- $this->assertInstanceOf(\Magento\Quote\Model\Quote::class, $result);
- }
- public function testRemoveAllAddresses()
- {
- $id = 1;
- $this->quoteAddressCollectionMock->expects($this->once())
- ->method('setQuoteFilter')
- ->with($id)
- ->will($this->returnSelf());
- $this->quoteAddressMock->expects($this->any())
- ->method('getAddressType')
- ->will($this->returnValue(\Magento\Customer\Model\Address\AbstractAddress::TYPE_SHIPPING));
- $this->quoteAddressMock->expects($this->any())
- ->method('getAddressType')
- ->will($this->returnValue(\Magento\Customer\Model\Address\AbstractAddress::TYPE_SHIPPING));
- $this->quoteAddressMock->expects($this->any())
- ->method('isDeleted')
- ->will($this->returnValue(false));
- $this->quoteAddressMock->expects($this->any())
- ->method('setData')
- ->will($this->returnSelf());
- $this->quoteAddressMock->expects($this->once())
- ->method('getId')
- ->will($this->returnValue($id));
- $this->quoteAddressMock->expects($this->once())
- ->method('getDeleteImmediately')
- ->will($this->returnValue(true));
- $iterator = new \ArrayIterator([$id => $this->quoteAddressMock]);
- $this->quoteAddressCollectionMock->expects($this->any())
- ->method('getIterator')
- ->will($this->returnValue($iterator));
- $this->quoteAddressCollectionMock->expects($this->once())
- ->method('removeItemByKey')
- ->with($id)
- ->will($this->returnValue($iterator));
- $this->quote->setId($id);
- $result = $this->quote->removeAllAddresses();
- $this->assertInstanceOf(\Magento\Quote\Model\Quote::class, $result);
- }
- /**
- * @expectedException \Magento\Framework\Exception\LocalizedException
- */
- public function testAddProductException()
- {
- $this->quote->addProduct($this->productMock, 'test');
- }
- public function testAddProductNoCandidates()
- {
- $expectedResult = 'test_string';
- $requestMock = $this->createMock(
- \Magento\Framework\DataObject::class
- );
- $this->objectFactoryMock->expects($this->once())
- ->method('create')
- ->with($this->equalTo(['qty' => 1]))
- ->will($this->returnValue($requestMock));
- $this->productMock->expects($this->once())
- ->method('isSalable')
- ->willReturn(true);
- $typeInstanceMock = $this->createPartialMock(\Magento\Catalog\Model\Product\Type\Simple::class, [
- 'prepareForCartAdvanced'
- ]);
- $typeInstanceMock->expects($this->once())
- ->method('prepareForCartAdvanced')
- ->will($this->returnValue($expectedResult));
- $this->productMock->expects($this->once())
- ->method('getTypeInstance')
- ->will($this->returnValue($typeInstanceMock));
- $result = $this->quote->addProduct($this->productMock, null);
- $this->assertEquals($expectedResult, $result);
- }
- public function testAddProductItemPreparation()
- {
- $itemMock = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
- $expectedResult = $itemMock;
- $requestMock = $this->createMock(
- \Magento\Framework\DataObject::class
- );
- $this->objectFactoryMock->expects($this->once())
- ->method('create')
- ->with($this->equalTo(['qty' => 1]))
- ->will($this->returnValue($requestMock));
- $typeInstanceMock = $this->createPartialMock(\Magento\Catalog\Model\Product\Type\Simple::class, [
- 'prepareForCartAdvanced'
- ]);
- $productMock = $this->createPartialMock(\Magento\Catalog\Model\Product::class, [
- 'getParentProductId',
- 'setStickWithinParent',
- '__wakeup'
- ]);
- $collectionMock = $this->createMock(\Magento\Quote\Model\ResourceModel\Quote\Item\Collection::class);
- $itemMock->expects($this->any())
- ->method('representProduct')
- ->will($this->returnValue(true));
- $iterator = new \ArrayIterator([$itemMock]);
- $collectionMock->expects($this->any())
- ->method('getIterator')
- ->will($this->returnValue($iterator));
- $this->quoteItemCollectionFactoryMock->expects($this->once())
- ->method('create')
- ->will($this->returnValue($collectionMock));
- $this->productMock->expects($this->once())
- ->method('isSalable')
- ->willReturn(true);
- $typeInstanceMock->expects($this->once())
- ->method('prepareForCartAdvanced')
- ->will($this->returnValue([$productMock]));
- $this->productMock->expects($this->once())
- ->method('getTypeInstance')
- ->will($this->returnValue($typeInstanceMock));
- $result = $this->quote->addProduct($this->productMock, null);
- $this->assertEquals($expectedResult, $result);
- }
- public function testAddProductItemNew()
- {
- $itemMock = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
- $expectedResult = $itemMock;
- $requestMock = $this->createMock(
- \Magento\Framework\DataObject::class
- );
- $this->objectFactoryMock->expects($this->once())
- ->method('create')
- ->with($this->equalTo(['qty' => 1]))
- ->will($this->returnValue($requestMock));
- $typeInstanceMock = $this->createPartialMock(\Magento\Catalog\Model\Product\Type\Simple::class, [
- 'prepareForCartAdvanced'
- ]);
- $productMock = $this->createPartialMock(\Magento\Catalog\Model\Product::class, [
- 'getParentProductId',
- 'setStickWithinParent',
- '__wakeup'
- ]);
- $collectionMock = $this->createMock(\Magento\Quote\Model\ResourceModel\Quote\Item\Collection::class);
- $itemMock->expects($this->any())
- ->method('representProduct')
- ->will($this->returnValue(false));
- $iterator = new \ArrayIterator([$itemMock]);
- $collectionMock->expects($this->any())
- ->method('getIterator')
- ->will($this->returnValue($iterator));
- $this->quoteItemCollectionFactoryMock->expects($this->once())
- ->method('create')
- ->will($this->returnValue($collectionMock));
- $this->productMock->expects($this->once())
- ->method('isSalable')
- ->willReturn(true);
- $this->itemProcessor
- ->expects($this->once())
- ->method('init')
- ->willReturn($itemMock);
- $itemMock->expects($this->once())
- ->method('setProduct');
- $itemMock->expects($this->once())
- ->method('setOptions');
- $itemMock->expects($this->any())
- ->method('setQuote')
- ->with($this->quote);
- $typeInstanceMock->expects($this->once())
- ->method('prepareForCartAdvanced')
- ->will($this->returnValue([$productMock]));
- $this->productMock->expects($this->once())
- ->method('getTypeInstance')
- ->will($this->returnValue($typeInstanceMock));
- $result = $this->quote->addProduct($this->productMock, null);
- $this->assertEquals($expectedResult, $result);
- }
- public function testValidateMinimumAmount()
- {
- $storeId = 1;
- $this->quote->setStoreId($storeId);
- $valueMap = [
- ['sales/minimum_order/active', ScopeInterface::SCOPE_STORE, $storeId, true],
- ['sales/minimum_order/multi_address', ScopeInterface::SCOPE_STORE, $storeId, true],
- ['sales/minimum_order/amount', ScopeInterface::SCOPE_STORE, $storeId, 20],
- ['sales/minimum_order/include_discount_amount', ScopeInterface::SCOPE_STORE, $storeId, true],
- ['sales/minimum_order/tax_including', ScopeInterface::SCOPE_STORE, $storeId, true],
- ];
- $this->scopeConfig->expects($this->any())
- ->method('isSetFlag')
- ->will($this->returnValueMap($valueMap));
- $this->quoteAddressMock->expects($this->once())
- ->method('validateMinimumAmount')
- ->willReturn(true);
- $this->quoteAddressCollectionMock->expects($this->once())
- ->method('setQuoteFilter')
- ->willReturn([$this->quoteAddressMock]);
- $this->assertTrue($this->quote->validateMinimumAmount());
- }
- public function testValidateMinimumAmountNegative()
- {
- $storeId = 1;
- $this->quote->setStoreId($storeId);
- $valueMap = [
- ['sales/minimum_order/active', ScopeInterface::SCOPE_STORE, $storeId, true],
- ['sales/minimum_order/multi_address', ScopeInterface::SCOPE_STORE, $storeId, true],
- ['sales/minimum_order/amount', ScopeInterface::SCOPE_STORE, $storeId, 20],
- ['sales/minimum_order/include_discount_amount', ScopeInterface::SCOPE_STORE, $storeId, true],
- ['sales/minimum_order/tax_including', ScopeInterface::SCOPE_STORE, $storeId, true],
- ];
- $this->scopeConfig->expects($this->any())
- ->method('isSetFlag')
- ->will($this->returnValueMap($valueMap));
- $this->quoteAddressMock->expects($this->once())
- ->method('validateMinimumAmount')
- ->willReturn(false);
- $this->quoteAddressCollectionMock->expects($this->once())
- ->method('setQuoteFilter')
- ->willReturn([$this->quoteAddressMock]);
- $this->assertFalse($this->quote->validateMinimumAmount());
- }
- public function testGetPaymentIsNotDeleted()
- {
- $this->quote->setId(1);
- $payment = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Payment::class,
- ['setQuote', 'isDeleted', '__wakeup']
- );
- $payment->expects($this->once())
- ->method('setQuote');
- $payment->expects($this->once())
- ->method('isDeleted')
- ->willReturn(false);
- $quotePaymentCollectionMock = $this->createPartialMock(
- \Magento\Quote\Model\ResourceModel\Quote\Payment\Collection::class,
- ['setQuoteFilter', 'getFirstItem']
- );
- $quotePaymentCollectionMock->expects($this->once())
- ->method('setQuoteFilter')
- ->with(1)
- ->will($this->returnSelf());
- $quotePaymentCollectionMock->expects($this->once())
- ->method('getFirstItem')
- ->willReturn($payment);
- $this->quotePaymentCollectionFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($quotePaymentCollectionMock);
- $this->assertInstanceOf(\Magento\Quote\Model\Quote\Payment::class, $this->quote->getPayment());
- }
- public function testGetPaymentIsDeleted()
- {
- $this->quote->setId(1);
- $payment = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Payment::class,
- ['setQuote', 'isDeleted', 'getId', '__wakeup']
- );
- $payment->expects($this->exactly(2))
- ->method('setQuote');
- $payment->expects($this->once())
- ->method('isDeleted')
- ->willReturn(true);
- $payment->expects($this->once())
- ->method('getId')
- ->willReturn(1);
- $quotePaymentCollectionMock = $this->createPartialMock(
- \Magento\Quote\Model\ResourceModel\Quote\Payment\Collection::class,
- ['setQuoteFilter', 'getFirstItem']
- );
- $quotePaymentCollectionMock->expects($this->once())
- ->method('setQuoteFilter')
- ->with(1)
- ->will($this->returnSelf());
- $quotePaymentCollectionMock->expects($this->once())
- ->method('getFirstItem')
- ->willReturn($payment);
- $this->quotePaymentCollectionFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($quotePaymentCollectionMock);
- $this->paymentFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($payment);
- $this->assertInstanceOf(\Magento\Quote\Model\Quote\Payment::class, $this->quote->getPayment());
- }
- public function testAddItem()
- {
- $item = $this->createPartialMock(\Magento\Quote\Model\Quote\Item::class, ['setQuote', 'getId']);
- $item->expects($this->once())
- ->method('setQuote');
- $item->expects($this->once())
- ->method('getId')
- ->willReturn(false);
- $itemsMock = $this->createPartialMock(
- \Magento\Eav\Model\Entity\Collection\AbstractCollection::class,
- ['setQuote', 'addItem']
- );
- $itemsMock->expects($this->once())
- ->method('setQuote');
- $itemsMock->expects($this->once())
- ->method('addItem')
- ->with($item);
- $this->quoteItemCollectionFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($itemsMock);
- $this->eventManagerMock->expects($this->once())
- ->method('dispatch');
- $this->quote->addItem($item);
- }
- /**
- * @param array $productTypes
- * @param int $expected
- * @dataProvider dataProviderForTestBeforeSaveIsVirtualQuote
- */
- public function testBeforeSaveIsVirtualQuote(array $productTypes, $expected)
- {
- $storeId = 1;
- $currencyMock = $this->getMockBuilder(\Magento\Directory\Model\Currency::class)
- ->disableOriginalConstructor()
- ->getMock();
- $currencyMock->expects($this->any())
- ->method('getCode')
- ->will($this->returnValue('test_code'));
- $currencyMock->expects($this->any())
- ->method('getRate')
- ->will($this->returnValue('test_rate'));
- $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
- ->disableOriginalConstructor()
- ->getMock();
- $storeMock->expects($this->once())
- ->method('getBaseCurrency')
- ->will($this->returnValue($currencyMock));
- $storeMock->expects($this->once())
- ->method('getCurrentCurrency')
- ->will($this->returnValue($currencyMock));
- $this->storeManagerMock->expects($this->any())
- ->method('getStore')
- ->with($storeId)
- ->will($this->returnValue($storeMock));
- $this->quote->setStoreId($storeId);
- $collectionMock = $this->createMock(\Magento\Quote\Model\ResourceModel\Quote\Item\Collection::class);
- $items = [];
- foreach ($productTypes as $type) {
- $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $productMock->expects($this->any())->method('getIsVirtual')->willReturn($type);
- $itemMock = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Item::class,
- ['isDeleted', 'getParentItemId', 'getProduct']
- );
- $itemMock->expects($this->any())
- ->method('isDeleted')
- ->willReturn(false);
- $itemMock->expects($this->any())
- ->method('getParentItemId')
- ->willReturn(false);
- $itemMock->expects($this->any())
- ->method('getProduct')
- ->willReturn($productMock);
- $items[] = $itemMock;
- }
- $iterator = new \ArrayIterator($items);
- $collectionMock->expects($this->any())
- ->method('getIterator')
- ->will($this->returnValue($iterator));
- $this->quoteItemCollectionFactoryMock->expects($this->once())
- ->method('create')
- ->will($this->returnValue($collectionMock));
- $this->quote->beforeSave();
- $this->assertEquals($expected, $this->quote->getDataByKey(CartInterface::KEY_IS_VIRTUAL));
- $this->assertNull($this->quote->getUpdatedAt());
- }
- /**
- * @return array
- */
- public function dataProviderForTestBeforeSaveIsVirtualQuote()
- {
- return [
- [[true], 1],
- [[true, true], 1],
- [[false], 0],
- [[true, false], 0],
- [[false, false], 0]
- ];
- }
- public function testGetItemsCollection()
- {
- $itemCollectionMock = $this->getMockBuilder(\Magento\Quote\Model\ResourceModel\Quote\Collection::class)
- ->disableOriginalConstructor()
- ->setMethods(['setQuote'])
- ->getMock();
- $this->quoteItemCollectionFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($itemCollectionMock);
- $this->extensionAttributesJoinProcessorMock->expects($this->once())
- ->method('process')
- ->with(
- $this->isInstanceOf(\Magento\Quote\Model\ResourceModel\Quote\Collection::class)
- );
- $itemCollectionMock->expects($this->once())->method('setQuote')->with($this->quote);
- $this->quote->getItemsCollection();
- }
- public function testGetAllItems()
- {
- $itemOneMock = $this->getMockBuilder(\Magento\Quote\Model\ResourceModel\Quote\Item::class)
- ->setMethods(['isDeleted'])
- ->disableOriginalConstructor()
- ->getMock();
- $itemOneMock->expects($this->once())
- ->method('isDeleted')
- ->willReturn(false);
- $itemTwoMock = $this->getMockBuilder(\Magento\Quote\Model\ResourceModel\Quote\Item::class)
- ->setMethods(['isDeleted'])
- ->disableOriginalConstructor()
- ->getMock();
- $itemTwoMock->expects($this->once())
- ->method('isDeleted')
- ->willReturn(true);
- $items = [$itemOneMock, $itemTwoMock];
- $itemResult = [$itemOneMock];
- $this->quote->setData('items_collection', $items);
- $this->assertEquals($itemResult, $this->quote->getAllItems());
- }
- /**
- * Test to verify if existing reserved_order_id in use.
- *
- * @param bool $isReservedOrderIdExist
- * @param int $reservedOrderId
- * @return void
- * @dataProvider reservedOrderIdDataProvider
- */
- public function testReserveOrderId(bool $isReservedOrderIdExist, int $reservedOrderId): void
- {
- $this->orderIncrementIdChecker
- ->expects($this->once())
- ->method('isIncrementIdUsed')
- ->with(1000001)->willReturn($isReservedOrderIdExist);
- $this->resourceMock->expects($this->any())->method('getReservedOrderId')->willReturn($reservedOrderId);
- $this->quote->reserveOrderId();
- $this->assertEquals($reservedOrderId, $this->quote->getReservedOrderId());
- }
- /**
- * @return array
- */
- public function reservedOrderIdDataProvider(): array
- {
- return [
- 'id_already_in_use' => [true, 100002],
- 'id_not_in_use' => [false, 1000001],
- ];
- }
- }
|