quoteResourceModelMock = $this->getMockBuilder(QuoteResourceModel::class) ->disableOriginalConstructor() ->getMock(); $this->cartItemPersisterMock = $this->getMockBuilder(CartItemPersister::class) ->disableOriginalConstructor() ->getMock(); $this->billingAddressPersisterMock = $this->getMockBuilder(BillingAddressPersister::class) ->disableOriginalConstructor() ->getMock(); $this->shippingAssignmentPersisterMock = $this->getMockBuilder(ShippingAssignmentPersister::class) ->disableOriginalConstructor() ->getMock(); $this->addressRepositoryMock = $this->getMockBuilder(AddressRepositoryInterface::class) ->getMockForAbstractClass(); $this->quoteMock = $this->getMockBuilder(Quote::class) ->disableOriginalConstructor() ->setMethods( [ 'getItems', 'setLastAddedItem', 'getBillingAddress', 'getExtensionAttributes', 'isVirtual', 'collectTotals' ] ) ->getMock(); $this->billingAddressMock = $this->getMockBuilder(QuoteAddress::class) ->setMethods(['getCustomerAddress', 'getCustomerAddressId', 'setCustomerAddressId']) ->disableOriginalConstructor() ->getMock(); $this->extensionAttributesMock = $this->getMockBuilder(CartExtensionInterface::class) ->setMethods(['getShippingAssignments']) ->getMockForAbstractClass(); $this->quoteMock->expects(static::any()) ->method('getBillingAddress') ->willReturn($this->billingAddressMock); $this->quoteMock->expects(static::any()) ->method('getExtensionAttributes') ->willReturn($this->extensionAttributesMock); $this->objectManagerHelper = new ObjectManagerHelper($this); $this->saveHandler = $this->objectManagerHelper->getObject( SaveHandler::class, [ 'quoteResource' => $this->quoteResourceModelMock, 'cartItemPersister' => $this->cartItemPersisterMock, 'billingAddressPersister' => $this->billingAddressPersisterMock, 'shippingAssignmentPersister' => $this->shippingAssignmentPersisterMock, 'addressRepository' => $this->addressRepositoryMock ] ); } public function testSaveForVirtualQuote() { $quoteItemMock = $this->createQuoteItemMock(false); $this->quoteMock->expects(static::atLeastOnce()) ->method('getItems') ->willReturn([$quoteItemMock]); $this->cartItemPersisterMock->expects(static::once()) ->method('save') ->with($this->quoteMock, $quoteItemMock) ->willReturn($quoteItemMock); $this->quoteMock->expects(static::once()) ->method('setLastAddedItem') ->with($quoteItemMock) ->willReturnSelf(); $this->billingAddressMock->expects(static::atLeastOnce()) ->method('getCustomerAddressId') ->willReturn(null); $this->billingAddressMock->expects(static::never()) ->method('getCustomerAddress'); $this->billingAddressPersisterMock->expects(static::once()) ->method('save') ->with($this->quoteMock, $this->billingAddressMock); $this->quoteMock->expects(static::atLeastOnce()) ->method('isVirtual') ->willReturn(true); $this->extensionAttributesMock->expects(static::never()) ->method('getShippingAssignments'); $this->quoteMock->expects(static::atLeastOnce()) ->method('collectTotals') ->willReturnSelf(); $this->quoteResourceModelMock->expects(static::once()) ->method('save') ->with($this->quoteMock) ->willReturnSelf(); $this->assertSame($this->quoteMock, $this->saveHandler->save($this->quoteMock)); } public function testSaveWithNotExistingCustomerAddress() { $customerAddressId = 5; $this->quoteMock->expects(static::atLeastOnce()) ->method('getItems') ->willReturn([]); $this->quoteMock->expects(static::never()) ->method('setLastAddedItem'); $this->billingAddressMock->expects(static::atLeastOnce()) ->method('getCustomerAddressId') ->willReturn($customerAddressId); $this->addressRepositoryMock->expects(static::once()) ->method('getById') ->with($customerAddressId) ->willThrowException(new NoSuchEntityException()); $this->billingAddressMock->expects(static::once()) ->method('setCustomerAddressId') ->willReturn(null); $this->billingAddressPersisterMock->expects(static::once()) ->method('save') ->with($this->quoteMock, $this->billingAddressMock); $this->quoteMock->expects(static::atLeastOnce()) ->method('isVirtual') ->willReturn(true); $this->extensionAttributesMock->expects(static::never()) ->method('getShippingAssignments'); $this->quoteMock->expects(static::atLeastOnce()) ->method('collectTotals') ->willReturnSelf(); $this->quoteResourceModelMock->expects(static::once()) ->method('save') ->with($this->quoteMock) ->willReturnSelf(); $this->assertSame($this->quoteMock, $this->saveHandler->save($this->quoteMock)); } /** * Create quote item mock * * @param bool $isDeleted * @return QuoteItem|\PHPUnit_Framework_MockObject_MockObject */ private function createQuoteItemMock($isDeleted) { $quoteItemMock = $this->getMockBuilder(QuoteItem::class) ->disableOriginalConstructor() ->getMock(); $quoteItemMock->expects(static::any()) ->method('isDeleted') ->willReturn($isDeleted); return $quoteItemMock; } }