123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Backend\Test\Unit\Model\Session;
- /**
- * Class QuoteTest
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @SuppressWarnings(PHPMD.TooManyFields)
- */
- class QuoteTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
- */
- private $objectManager;
- /**
- * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $storeManagerMock;
- /**
- * @var \Magento\Sales\Model\OrderFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $orderFactoryMock;
- /**
- * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $cookieMetadataFactoryMock;
- /**
- * @var \Magento\Framework\Stdlib\CookieManagerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $cookieManagerMock;
- /**
- * @var \Magento\Framework\Session\StorageInterface
- */
- protected $storage;
- /**
- * @var \Magento\Framework\Session\ValidatorInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $validatorMock;
- /**
- * @var \Magento\Framework\Session\SaveHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $saveHandlerMock;
- /**
- * @var \Magento\Framework\Session\Config\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $sessionConfigMock;
- /**
- * @var \Magento\Framework\Session\SidResolverInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $sidResolverMock;
- /**
- * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $requestMock;
- /**
- * @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerRepositoryMock;
- /**
- * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $scopeConfigMock;
- /**
- * @var \Magento\Quote\Model\QuoteFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $quoteRepositoryMock;
- /**
- * @var \Magento\Backend\Model\Session\Quote|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $quote;
- /**
- * @var \Magento\Customer\Api\GroupManagementInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $groupManagementMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $quoteFactoryMock;
- /**
- * Set up
- *
- * @return void
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- protected function setUp()
- {
- $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->customerRepositoryMock = $this->getMockForAbstractClass(
- \Magento\Customer\Api\CustomerRepositoryInterface::class,
- [],
- '',
- false,
- true,
- true,
- ['getCustomer']
- );
- $this->groupManagementMock = $this->getMockForAbstractClass(
- \Magento\Customer\Api\GroupManagementInterface::class,
- [],
- '',
- false,
- true,
- true,
- ['getDefaultGroup']
- );
- $this->scopeConfigMock = $this->getMockForAbstractClass(
- \Magento\Framework\App\Config\ScopeConfigInterface::class,
- [],
- '',
- false,
- true,
- true,
- ['getValue']
- );
- $this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
- $this->requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
- $this->sidResolverMock = $this->getMockForAbstractClass(
- \Magento\Framework\Session\SidResolverInterface::class,
- [],
- '',
- false
- );
- $this->sessionConfigMock = $this->getMockForAbstractClass(
- \Magento\Framework\Session\Config\ConfigInterface::class,
- [],
- '',
- false
- );
- $this->saveHandlerMock = $this->getMockForAbstractClass(
- \Magento\Framework\Session\SaveHandlerInterface::class,
- [],
- '',
- false
- );
- $this->validatorMock = $this->getMockForAbstractClass(
- \Magento\Framework\Session\ValidatorInterface::class,
- [],
- '',
- false
- );
- $this->storage = new \Magento\Framework\Session\Storage();
- $this->cookieManagerMock = $this->createMock(\Magento\Framework\Stdlib\CookieManagerInterface::class);
- $this->cookieMetadataFactoryMock = $this->createMock(
- \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class
- );
- $this->orderFactoryMock = $this->createPartialMock(\Magento\Sales\Model\OrderFactory::class, ['create']);
- $appStateMock = $this->createMock(\Magento\Framework\App\State::class);
- $this->storeManagerMock = $this->getMockForAbstractClass(
- \Magento\Store\Model\StoreManagerInterface::class,
- [],
- '',
- false
- );
- $this->quoteFactoryMock = $this->createPartialMock(\Magento\Quote\Model\QuoteFactory::class, ['create']);
- $this->quote = $this->getMockBuilder(\Magento\Backend\Model\Session\Quote::class)
- ->setMethods(['getStoreId', 'getQuoteId', 'setQuoteId', 'hasCustomerId', 'getCustomerId'])
- ->setConstructorArgs(
- [
- 'request' => $this->requestMock,
- 'sidResolver' => $this->sidResolverMock,
- 'sessionConfig' => $this->sessionConfigMock,
- 'saveHandler' => $this->saveHandlerMock,
- 'validator' => $this->validatorMock,
- 'storage' => $this->storage,
- 'cookieManager' => $this->cookieManagerMock,
- 'cookieMetadataFactory' => $this->cookieMetadataFactoryMock,
- 'appState' => $appStateMock,
- 'customerRepository' => $this->customerRepositoryMock,
- 'quoteRepository' => $this->quoteRepositoryMock,
- 'orderFactory' => $this->orderFactoryMock,
- 'storeManager' => $this->storeManagerMock,
- 'groupManagement' => $this->groupManagementMock,
- 'quoteFactory' => $this->quoteFactoryMock
- ]
- )
- ->getMock();
- }
- /**
- * Run test getQuote method
- *
- * @return void
- */
- public function testGetQuoteWithoutQuoteId()
- {
- $quoteId = 22;
- $storeId = 10;
- $customerId = 66;
- $customerGroupId = 77;
- $this->quote->expects($this->any())->method('getQuoteId')->will($this->returnValue(null));
- $this->quote->expects($this->any())->method('setQuoteId')->with($quoteId);
- $cartInterfaceMock = $this->createPartialMock(
- \Magento\Quote\Api\Data\CartInterface::class,
- [
- 'getId',
- 'setId',
- 'getCreatedAt',
- 'setCreatedAt',
- 'getUpdatedAt',
- 'setUpdatedAt',
- 'getConvertedAt',
- 'setConvertedAt',
- 'getIsActive',
- 'setIsActive',
- 'getIsVirtual',
- 'getItems',
- 'setItems',
- 'getItemsCount',
- 'setItemsCount',
- 'getItemsQty',
- 'setItemsQty',
- 'getCustomer',
- 'setCustomer',
- 'getBillingAddress',
- 'setBillingAddress',
- 'getReservedOrderId',
- 'setReservedOrderId',
- 'getOrigOrderId',
- 'setOrigOrderId',
- 'getCurrency',
- 'setCurrency',
- 'getCustomerIsGuest',
- 'setCustomerIsGuest',
- 'getCustomerNote',
- 'setCustomerNote',
- 'getCustomerNoteNotify',
- 'setCustomerNoteNotify',
- 'getCustomerTaxClassId',
- 'setCustomerTaxClassId',
- 'getStoreId',
- 'setStoreId',
- 'getExtensionAttributes',
- 'setExtensionAttributes',
- 'setIgnoreOldQty',
- 'setIsSuperMode',
- 'setCustomerGroupId'
- ]
- );
- $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($cartInterfaceMock);
- $this->quote->expects($this->any())->method('getStoreId')->will($this->returnValue($storeId));
- $this->quote->expects($this->any())->method('getCustomerId')->will($this->returnValue($customerId));
- $cartInterfaceMock->expects($this->atLeastOnce())->method('getId')->willReturn($quoteId);
- $defaultGroup = $this->getMockBuilder(\Magento\Customer\Api\Data\GroupInterface::class)->getMock();
- $defaultGroup->expects($this->any())->method('getId')->will($this->returnValue($customerGroupId));
- $this->groupManagementMock->expects($this->any())->method('getDefaultGroup')->willReturn($defaultGroup);
- $dataCustomerMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->customerRepositoryMock->expects($this->once())
- ->method('getById')
- ->with($customerId)
- ->willReturn($dataCustomerMock);
- $quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, [
- 'setStoreId',
- 'setCustomerGroupId',
- 'setIsActive',
- 'assignCustomer',
- 'setIgnoreOldQty',
- 'setIsSuperMode',
- '__wakeup'
- ]);
- $this->quoteRepositoryMock->expects($this->once())->method('get')->willReturn($quoteMock);
- $cartInterfaceMock->expects($this->once())->method('setCustomerGroupId')->with($customerGroupId)
- ->willReturnSelf();
- $quoteMock->expects($this->once())->method('assignCustomer')->with($dataCustomerMock);
- $quoteMock->expects($this->once())->method('setIgnoreOldQty')->with(true);
- $quoteMock->expects($this->once())->method('setIsSuperMode')->with(true);
- $this->assertEquals($quoteMock, $this->quote->getQuote());
- }
- /**
- * Run test getQuote method
- *
- * @return void
- * @dataProvider getQuoteDataProvider
- */
- public function testGetQuoteWithQuoteId($customerId, $quoteCustomerId, $expectedNumberOfInvokes)
- {
- $quoteId = 22;
- $storeId = 10;
- $this->quote->expects($this->any())
- ->method('getQuoteId')
- ->will($this->returnValue($quoteId));
- $this->quote->expects($this->any())
- ->method('setQuoteId')
- ->with($quoteId);
- $this->quote->expects($this->any())
- ->method('getStoreId')
- ->will($this->returnValue($storeId));
- $this->quote->expects($this->any())
- ->method('getCustomerId')
- ->will($this->returnValue($customerId));
- $dataCustomerMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->customerRepositoryMock->expects($this->$expectedNumberOfInvokes())
- ->method('getById')
- ->with($customerId)
- ->willReturn($dataCustomerMock);
- $quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, [
- 'setStoreId',
- 'setCustomerGroupId',
- 'setIsActive',
- 'getId',
- 'assignCustomer',
- 'setIgnoreOldQty',
- 'setIsSuperMode',
- 'getCustomerId',
- '__wakeup'
- ]);
- $quoteMock->expects($this->once())
- ->method('setStoreId')
- ->with($storeId);
- $quoteMock->expects($this->$expectedNumberOfInvokes())
- ->method('assignCustomer')
- ->with($dataCustomerMock);
- $quoteMock->expects($this->once())
- ->method('setIgnoreOldQty')
- ->with(true);
- $quoteMock->expects($this->once())
- ->method('setIsSuperMode')
- ->with(true);
- $quoteMock->expects($this->once())
- ->method('getCustomerId')
- ->will($this->returnValue($quoteCustomerId));
- $this->quoteRepositoryMock->expects($this->once())
- ->method('get')
- ->with($quoteId)
- ->willReturn($quoteMock);
- $this->assertEquals($quoteMock, $this->quote->getQuote());
- }
- /**
- * @return array
- */
- public function getQuoteDataProvider()
- {
- return [
- 'customer ids different' => [66, null, 'once'],
- 'customer ids same' => [66, 66, 'never'],
- ];
- }
- }
|