123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Quote\Test\Unit\Model;
- use Magento\Framework\Api\SortOrder;
- use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
- use Magento\Quote\Api\Data\CartInterface;
- use Magento\Quote\Model\QuoteRepository\LoadHandler;
- use Magento\Quote\Model\QuoteRepository\SaveHandler;
- use Magento\Quote\Model\ResourceModel\Quote\CollectionFactory;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class QuoteRepositoryTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Quote\Api\CartRepositoryInterface
- */
- private $model;
- /**
- * @var \Magento\Quote\Model\QuoteFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- private $quoteFactoryMock;
- /**
- * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $storeManagerMock;
- /**
- * @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
- */
- private $storeMock;
- /**
- * @var \Magento\Quote\Model\Quote|\PHPUnit_Framework_MockObject_MockObject
- */
- private $quoteMock;
- /**
- * @var \Magento\Quote\Api\Data\CartSearchResultsInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- private $searchResultsDataFactory;
- /**
- * @var \Magento\Quote\Model\ResourceModel\Quote\Collection|\PHPUnit_Framework_MockObject_MockObject
- */
- private $quoteCollectionMock;
- /**
- * @var JoinProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $extensionAttributesJoinProcessorMock;
- /**
- * @var LoadHandler|\PHPUnit_Framework_MockObject_MockObject
- */
- private $loadHandlerMock;
- /**
- * @var LoadHandler|\PHPUnit_Framework_MockObject_MockObject
- */
- private $saveHandlerMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $collectionProcessor;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $objectManagerMock;
- /**
- * @var \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- private $quoteCollectionFactoryMock;
- protected function setUp()
- {
- $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
- \Magento\Framework\App\ObjectManager::setInstance($this->objectManagerMock);
- $this->quoteFactoryMock = $this->createPartialMock(\Magento\Quote\Model\QuoteFactory::class, ['create']);
- $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
- $this->quoteMock = $this->createPartialMock(
- \Magento\Quote\Model\Quote::class,
- [
- 'load',
- 'loadByIdWithoutStore',
- 'loadByCustomer',
- 'getIsActive',
- 'getId',
- '__wakeup',
- 'setSharedStoreIds',
- 'save',
- 'delete',
- 'getCustomerId',
- 'getStoreId',
- 'getData'
- ]
- );
- $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
- $this->searchResultsDataFactory = $this->createPartialMock(
- \Magento\Quote\Api\Data\CartSearchResultsInterfaceFactory::class,
- ['create']
- );
- $this->quoteCollectionMock =
- $this->createMock(\Magento\Quote\Model\ResourceModel\Quote\Collection::class);
- $this->extensionAttributesJoinProcessorMock = $this->createMock(
- \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface::class
- );
- $this->collectionProcessor = $this->createMock(
- \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
- );
- $this->quoteCollectionFactoryMock = $this->createPartialMock(
- \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory::class,
- ['create']
- );
- $this->model = $objectManager->getObject(
- \Magento\Quote\Model\QuoteRepository::class,
- [
- 'quoteFactory' => $this->quoteFactoryMock,
- 'storeManager' => $this->storeManagerMock,
- 'searchResultsDataFactory' => $this->searchResultsDataFactory,
- 'quoteCollection' => $this->quoteCollectionMock,
- 'extensionAttributesJoinProcessor' => $this->extensionAttributesJoinProcessorMock,
- 'collectionProcessor' => $this->collectionProcessor,
- 'quoteCollectionFactory' => $this->quoteCollectionFactoryMock
- ]
- );
- $this->loadHandlerMock = $this->createMock(LoadHandler::class);
- $this->saveHandlerMock = $this->createMock(SaveHandler::class);
- $reflection = new \ReflectionClass(get_class($this->model));
- $reflectionProperty = $reflection->getProperty('loadHandler');
- $reflectionProperty->setAccessible(true);
- $reflectionProperty->setValue($this->model, $this->loadHandlerMock);
- $reflectionProperty = $reflection->getProperty('saveHandler');
- $reflectionProperty->setAccessible(true);
- $reflectionProperty->setValue($this->model, $this->saveHandlerMock);
- }
- /**
- * @expectedException \Magento\Framework\Exception\NoSuchEntityException
- * @expectedExceptionMessage No such entity with cartId = 14
- */
- public function testGetWithExceptionById()
- {
- $cartId = 14;
- $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteMock);
- $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
- $this->storeMock->expects($this->once())->method('getId')->willReturn(1);
- $this->quoteMock->expects($this->never())->method('setSharedStoreIds');
- $this->quoteMock->expects($this->once())
- ->method('loadByIdWithoutStore')
- ->with($cartId)
- ->willReturn($this->storeMock);
- $this->quoteMock->expects($this->once())->method('getId')->willReturn(false);
- $this->model->get($cartId);
- }
- public function testGet()
- {
- $cartId = 15;
- $this->quoteFactoryMock->expects(static::once())
- ->method('create')
- ->willReturn($this->quoteMock);
- $this->storeManagerMock->expects(static::once())
- ->method('getStore')
- ->willReturn($this->storeMock);
- $this->storeMock->expects(static::once())
- ->method('getId')
- ->willReturn(1);
- $this->quoteMock->expects(static::never())
- ->method('setSharedStoreIds');
- $this->quoteMock->expects(static::once())
- ->method('loadByIdWithoutStore')
- ->with($cartId)
- ->willReturn($this->storeMock);
- $this->quoteMock->expects(static::once())
- ->method('getId')
- ->willReturn($cartId);
- $this->quoteMock->expects(static::never())
- ->method('getCustomerId');
- $this->loadHandlerMock->expects(static::once())
- ->method('load')
- ->with($this->quoteMock);
- static::assertEquals($this->quoteMock, $this->model->get($cartId));
- static::assertEquals($this->quoteMock, $this->model->get($cartId));
- }
- public function testGetForCustomerAfterGet()
- {
- $cartId = 15;
- $customerId = 23;
- $this->quoteFactoryMock->expects(static::exactly(2))
- ->method('create')
- ->willReturn($this->quoteMock);
- $this->storeManagerMock->expects(static::exactly(2))
- ->method('getStore')
- ->willReturn($this->storeMock);
- $this->storeMock->expects(static::exactly(2))
- ->method('getId')
- ->willReturn(1);
- $this->quoteMock->expects(static::never())
- ->method('setSharedStoreIds');
- $this->quoteMock->expects(static::once())
- ->method('loadByIdWithoutStore')
- ->with($cartId)
- ->willReturn($this->storeMock);
- $this->quoteMock->expects(static::once())
- ->method('loadByCustomer')
- ->with($customerId)
- ->willReturn($this->storeMock);
- $this->quoteMock->expects(static::exactly(3))
- ->method('getId')
- ->willReturn($cartId);
- $this->quoteMock->expects(static::any())
- ->method('getCustomerId')
- ->willReturn($customerId);
- $this->loadHandlerMock->expects(static::exactly(2))
- ->method('load')
- ->with($this->quoteMock);
- static::assertEquals($this->quoteMock, $this->model->get($cartId));
- static::assertEquals($this->quoteMock, $this->model->getForCustomer($customerId));
- }
- public function testGetWithSharedStoreIds()
- {
- $cartId = 16;
- $sharedStoreIds = [1, 2];
- $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteMock);
- $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
- $this->storeMock->expects($this->once())->method('getId')->willReturn(1);
- $this->quoteMock->expects($this->once())
- ->method('setSharedStoreIds')
- ->with($sharedStoreIds)
- ->willReturnSelf();
- $this->quoteMock->expects($this->once())
- ->method('loadByIdWithoutStore')
- ->with($cartId)
- ->willReturn($this->storeMock);
- $this->quoteMock->expects($this->once())->method('getId')->willReturn($cartId);
- $this->loadHandlerMock->expects($this->once())
- ->method('load')
- ->with($this->quoteMock)
- ->willReturn($this->quoteMock);
- $this->assertEquals($this->quoteMock, $this->model->get($cartId, $sharedStoreIds));
- }
- public function testGetForCustomer()
- {
- $cartId = 17;
- $customerId = 23;
- $this->quoteFactoryMock->expects(static::once())
- ->method('create')
- ->willReturn($this->quoteMock);
- $this->storeManagerMock->expects(static::once())
- ->method('getStore')
- ->willReturn($this->storeMock);
- $this->storeMock->expects(static::once())
- ->method('getId')
- ->willReturn(1);
- $this->quoteMock->expects(static::never())
- ->method('setSharedStoreIds');
- $this->quoteMock->expects(static::once())
- ->method('loadByCustomer')
- ->with($customerId)
- ->willReturn($this->storeMock);
- $this->quoteMock->expects(static::exactly(2))
- ->method('getId')
- ->willReturn($cartId);
- $this->loadHandlerMock->expects(static::once())
- ->method('load')
- ->with($this->quoteMock);
- static::assertEquals($this->quoteMock, $this->model->getForCustomer($customerId));
- static::assertEquals($this->quoteMock, $this->model->getForCustomer($customerId));
- }
- /**
- * @expectedException \Magento\Framework\Exception\NoSuchEntityException
- * @expectedExceptionMessage No such entity with cartId = 14
- */
- public function testGetActiveWithExceptionById()
- {
- $cartId = 14;
- $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteMock);
- $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
- $this->storeMock->expects($this->once())->method('getId')->willReturn(1);
- $this->quoteMock->expects($this->never())->method('setSharedStoreIds');
- $this->quoteMock->expects($this->once())
- ->method('loadByIdWithoutStore')
- ->with($cartId)
- ->willReturn($this->storeMock);
- $this->quoteMock->expects($this->once())->method('getId')->willReturn(false);
- $this->quoteMock->expects($this->never())->method('getIsActive');
- $this->model->getActive($cartId);
- }
- /**
- * @expectedException \Magento\Framework\Exception\NoSuchEntityException
- * @expectedExceptionMessage No such entity with cartId = 15
- */
- public function testGetActiveWithExceptionByIsActive()
- {
- $cartId = 15;
- $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteMock);
- $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
- $this->storeMock->expects($this->once())->method('getId')->willReturn(1);
- $this->quoteMock->expects($this->never())->method('setSharedStoreIds');
- $this->quoteMock->expects($this->once())
- ->method('loadByIdWithoutStore')
- ->with($cartId)
- ->willReturn($this->storeMock);
- $this->quoteMock->expects($this->once())->method('getId')->willReturn($cartId);
- $this->quoteMock->expects($this->once())->method('getIsActive')->willReturn(0);
- $this->loadHandlerMock->expects($this->once())
- ->method('load')
- ->with($this->quoteMock)
- ->willReturn($this->quoteMock);
- $this->model->getActive($cartId);
- }
- public function testGetActive()
- {
- $cartId = 15;
- $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteMock);
- $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
- $this->storeMock->expects($this->once())->method('getId')->willReturn(1);
- $this->quoteMock->expects($this->never())->method('setSharedStoreIds');
- $this->quoteMock->expects($this->once())
- ->method('loadByIdWithoutStore')
- ->with($cartId)
- ->willReturn($this->storeMock);
- $this->quoteMock->expects($this->once())->method('getId')->willReturn($cartId);
- $this->quoteMock->expects($this->once())->method('getIsActive')->willReturn(1);
- $this->loadHandlerMock->expects($this->once())
- ->method('load')
- ->with($this->quoteMock)
- ->willReturn($this->quoteMock);
- $this->assertEquals($this->quoteMock, $this->model->getActive($cartId));
- }
- public function testGetActiveWithSharedStoreIds()
- {
- $cartId = 16;
- $sharedStoreIds = [1, 2];
- $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteMock);
- $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
- $this->storeMock->expects($this->once())->method('getId')->willReturn(1);
- $this->quoteMock->expects($this->once())
- ->method('setSharedStoreIds')
- ->with($sharedStoreIds)
- ->willReturnSelf();
- $this->quoteMock->expects($this->once())
- ->method('loadByIdWithoutStore')
- ->with($cartId)
- ->willReturn($this->storeMock);
- $this->quoteMock->expects($this->once())->method('getId')->willReturn($cartId);
- $this->quoteMock->expects($this->once())->method('getIsActive')->willReturn(1);
- $this->loadHandlerMock->expects($this->once())
- ->method('load')
- ->with($this->quoteMock)
- ->willReturn($this->quoteMock);
- $this->assertEquals($this->quoteMock, $this->model->getActive($cartId, $sharedStoreIds));
- }
- public function testGetActiveForCustomer()
- {
- $cartId = 17;
- $customerId = 23;
- $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteMock);
- $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
- $this->storeMock->expects($this->once())->method('getId')->willReturn(1);
- $this->quoteMock->expects($this->never())->method('setSharedStoreIds');
- $this->quoteMock->expects($this->once())
- ->method('loadByCustomer')
- ->with($customerId)
- ->willReturn($this->storeMock);
- $this->quoteMock->expects($this->exactly(2))->method('getId')->willReturn($cartId);
- $this->quoteMock->expects($this->exactly(2))->method('getIsActive')->willReturn(1);
- $this->loadHandlerMock->expects($this->once())
- ->method('load')
- ->with($this->quoteMock)
- ->willReturn($this->quoteMock);
- $this->assertEquals($this->quoteMock, $this->model->getActiveForCustomer($customerId));
- $this->assertEquals($this->quoteMock, $this->model->getActiveForCustomer($customerId));
- }
- public function testSave()
- {
- $cartId = 100;
- $quoteMock = $this->createPartialMock(
- \Magento\Quote\Model\Quote::class,
- ['getId', 'getCustomerId', 'getStoreId', 'hasData', 'setData']
- );
- $quoteMock->expects($this->exactly(3))->method('getId')->willReturn($cartId);
- $quoteMock->expects($this->once())->method('getCustomerId')->willReturn(2);
- $quoteMock->expects($this->once())->method('getStoreId')->willReturn(5);
- $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteMock);
- $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
- $this->storeMock->expects($this->once())->method('getId')->willReturn(1);
- $this->quoteMock->expects($this->once())->method('getId')->willReturn($cartId);
- $this->quoteMock->expects($this->once())->method('setSharedStoreIds');
- $this->quoteMock->expects($this->once())
- ->method('loadByIdWithoutStore')
- ->with($cartId)
- ->willReturn($this->storeMock);
- $this->loadHandlerMock->expects($this->once())
- ->method('load')
- ->with($this->quoteMock)
- ->willReturn($this->quoteMock);
- $this->quoteMock->expects($this->once())->method('getData')->willReturn(['key' => 'value']);
- $quoteMock->expects($this->once())->method('hasData')->with('key')->willReturn(false);
- $quoteMock->expects($this->once())->method('setData')->with('key', 'value')->willReturnSelf();
- $this->saveHandlerMock->expects($this->once())->method('save')->with($quoteMock)->willReturn($quoteMock);
- $this->model->save($quoteMock);
- }
- public function testDelete()
- {
- $this->quoteMock->expects($this->once())
- ->method('delete');
- $this->quoteMock->expects($this->exactly(1))->method('getId')->willReturn(1);
- $this->quoteMock->expects($this->exactly(1))->method('getCustomerId')->willReturn(2);
- $this->model->delete($this->quoteMock);
- }
- public function testGetList()
- {
- $pageSize = 10;
- $this->quoteCollectionFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($this->quoteCollectionMock);
- $cartMock = $this->createMock(CartInterface::class);
- $this->loadHandlerMock->expects($this->once())
- ->method('load')
- ->with($cartMock);
- $searchResult = $this->createMock(\Magento\Quote\Api\Data\CartSearchResultsInterface::class);
- $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
- $this->searchResultsDataFactory
- ->expects($this->once())
- ->method('create')
- ->willReturn($searchResult);
- $this->collectionProcessor->expects($this->once())
- ->method('process')
- ->with($searchCriteriaMock, $this->quoteCollectionMock);
- $this->extensionAttributesJoinProcessorMock->expects($this->once())
- ->method('process')
- ->with(
- $this->isInstanceOf(\Magento\Quote\Model\ResourceModel\Quote\Collection::class)
- );
- $this->quoteCollectionMock->expects($this->atLeastOnce())->method('getItems')->willReturn([$cartMock]);
- $searchResult->expects($this->once())->method('setTotalCount')->with($pageSize);
- $this->quoteCollectionMock->expects($this->once())
- ->method('getSize')
- ->willReturn($pageSize);
- $searchResult->expects($this->once())
- ->method('setItems')
- ->with([$cartMock]);
- $this->assertEquals($searchResult, $this->model->getList($searchCriteriaMock));
- }
- /**
- * @deprecated
- * @return array
- */
- public function getListSuccessDataProvider()
- {
- return [
- 'asc' => [SortOrder::SORT_ASC, 'ASC'],
- 'desc' => [SortOrder::SORT_DESC, 'DESC']
- ];
- }
- }
|