123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Checkout\Test\Unit\Model;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @SuppressWarnings(PHPMD.TooManyFields)
- */
- class ShippingInformationManagementTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
- */
- private $objectManager;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $paymentMethodManagementMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $paymentDetailsFactoryMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $cartTotalsRepositoryMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $quoteRepositoryMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $shippingAddressMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $quoteMock;
- /**
- * @var \Magento\Checkout\Model\ShippingInformationManagement
- */
- protected $model;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $shippingAssignmentFactoryMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $cartExtensionFactoryMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $shippingFactoryMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $cartExtensionMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $shippingAssignmentMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $shippingMock;
- protected function setUp()
- {
- $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->paymentMethodManagementMock = $this->createMock(
- \Magento\Quote\Api\PaymentMethodManagementInterface::class
- );
- $this->paymentDetailsFactoryMock = $this->createPartialMock(
- \Magento\Checkout\Model\PaymentDetailsFactory::class,
- ['create']
- );
- $this->cartTotalsRepositoryMock = $this->createMock(\Magento\Quote\Api\CartTotalRepositoryInterface::class);
- $this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
- $this->shippingAddressMock = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Address::class,
- [
- 'getSaveInAddressBook',
- 'getSameAsBilling',
- 'getCustomerAddressId',
- 'setShippingAddress',
- 'getShippingAddress',
- 'setSaveInAddressBook',
- 'setSameAsBilling',
- 'setCollectShippingRates',
- 'getCountryId',
- 'importCustomerAddressData',
- 'save',
- 'getShippingRateByCode',
- 'getShippingMethod',
- 'setLimitCarrier'
- ]
- );
- $this->quoteMock = $this->createPartialMock(
- \Magento\Quote\Model\Quote::class,
- [
- 'isVirtual',
- 'getItemsCount',
- 'getIsMultiShipping',
- 'setIsMultiShipping',
- 'validateMinimumAmount',
- 'getStoreId',
- 'setShippingAddress',
- 'getShippingAddress',
- 'collectTotals',
- 'getExtensionAttributes',
- 'setExtensionAttributes',
- 'setBillingAddress'
- ],
- [],
- '',
- false
- );
- $this->shippingAssignmentFactoryMock =
- $this->createPartialMock(\Magento\Quote\Model\ShippingAssignmentFactory::class, ['create']);
- $this->cartExtensionFactoryMock =
- $this->createPartialMock(\Magento\Quote\Api\Data\CartExtensionFactory::class, ['create']);
- $this->shippingFactoryMock =
- $this->createPartialMock(\Magento\Quote\Model\ShippingFactory::class, ['create']);
- $this->model = $this->objectManager->getObject(
- \Magento\Checkout\Model\ShippingInformationManagement::class,
- [
- 'paymentMethodManagement' => $this->paymentMethodManagementMock,
- 'paymentDetailsFactory' => $this->paymentDetailsFactoryMock,
- 'cartTotalsRepository' => $this->cartTotalsRepositoryMock,
- 'quoteRepository' => $this->quoteRepositoryMock,
- 'shippingAssignmentFactory' => $this->shippingAssignmentFactoryMock,
- 'cartExtensionFactory' => $this->cartExtensionFactoryMock,
- 'shippingFactory' => $this->shippingFactoryMock
- ]
- );
- }
- /**
- * @expectedException \Magento\Framework\Exception\InputException
- * @expectedExceptionMessage The shipping method can't be set for an empty cart. Add an item to cart and try again.
- */
- public function testSaveAddressInformationIfCartIsEmpty()
- {
- $cartId = 100;
- $carrierCode = 'carrier_code';
- $shippingMethod = 'shipping_method';
- $addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
- $billingAddress = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
- $addressInformationMock->expects($this->once())
- ->method('getShippingAddress')
- ->willReturn($this->shippingAddressMock);
- $addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
- $addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
- $addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
- $this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn('USA');
- $this->setShippingAssignmentsMocks($carrierCode . '_' . $shippingMethod);
- $this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(0);
- $this->quoteRepositoryMock->expects($this->once())
- ->method('getActive')
- ->with($cartId)
- ->willReturn($this->quoteMock);
- $this->model->saveAddressInformation($cartId, $addressInformationMock);
- }
- /**
- * @param string $shippingMethod
- */
- private function setShippingAssignmentsMocks($shippingMethod)
- {
- $this->quoteMock->expects($this->once())->method('getExtensionAttributes')->willReturn(null);
- $this->shippingAddressMock->expects($this->once())->method('setLimitCarrier');
- $this->cartExtensionMock = $this->createPartialMock(
- \Magento\Quote\Api\Data\CartExtension::class,
- ['getShippingAssignments', 'setShippingAssignments']
- );
- $this->cartExtensionFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($this->cartExtensionMock);
- $this->cartExtensionMock->expects($this->once())->method('getShippingAssignments')->willReturn(null);
- $this->shippingAssignmentMock = $this->createMock(
- \Magento\Quote\Model\ShippingAssignment::class
- );
- $this->shippingAssignmentFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($this->shippingAssignmentMock);
- $this->shippingAssignmentMock->expects($this->once())->method('getShipping')->willReturn(null);
- $this->shippingMock = $this->createMock(\Magento\Quote\Model\Shipping::class);
- $this->shippingFactoryMock->expects($this->once())->method('create')->willReturn($this->shippingMock);
- $this->shippingMock->expects($this->once())
- ->method('setAddress')
- ->with($this->shippingAddressMock)
- ->willReturnSelf();
- $this->shippingMock->expects($this->once())->method('setMethod')->with($shippingMethod)->willReturnSelf();
- $this->shippingAssignmentMock->expects($this->once())
- ->method('setShipping')
- ->with($this->shippingMock)
- ->willReturnSelf();
- $this->cartExtensionMock->expects($this->once())
- ->method('setShippingAssignments')
- ->with([$this->shippingAssignmentMock])
- ->willReturnSelf();
- $this->quoteMock->expects($this->once())
- ->method('setExtensionAttributes')
- ->with($this->cartExtensionMock)
- ->willReturnSelf();
- }
- /**
- * @expectedException \Magento\Framework\Exception\StateException
- * @expectedExceptionMessage The shipping address is missing. Set the address and try again.
- */
- public function testSaveAddressInformationIfShippingAddressNotSet()
- {
- $cartId = 100;
- $carrierCode = 'carrier_code';
- $shippingMethod = 'shipping_method';
- $addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
- $addressInformationMock->expects($this->once())
- ->method('getShippingAddress')
- ->willReturn($this->shippingAddressMock);
- $addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
- $addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
- $billingAddress = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
- $addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
- $this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn(null);
- $this->model->saveAddressInformation($cartId, $addressInformationMock);
- }
- /**
- * @expectedException \Magento\Framework\Exception\InputException
- * @expectedExceptionMessage The shipping information was unable to be saved. Verify the input data and try again.
- */
- public function testSaveAddressInformationIfCanNotSaveQuote()
- {
- $cartId = 100;
- $carrierCode = 'carrier_code';
- $shippingMethod = 'shipping_method';
- $addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
- $this->quoteRepositoryMock->expects($this->once())
- ->method('getActive')
- ->with($cartId)
- ->willReturn($this->quoteMock);
- $addressInformationMock->expects($this->once())
- ->method('getShippingAddress')
- ->willReturn($this->shippingAddressMock);
- $addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
- $addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
- $billingAddress = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
- $addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
- $this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn('USA');
- $this->setShippingAssignmentsMocks($carrierCode . '_' . $shippingMethod);
- $this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(100);
- $this->quoteMock->expects($this->once())->method('setIsMultiShipping')->with(false)->willReturnSelf();
- $this->quoteMock->expects($this->once())->method('setBillingAddress')->with($billingAddress)->willReturnSelf();
- $this->quoteRepositoryMock->expects($this->once())
- ->method('save')
- ->with($this->quoteMock)
- ->willThrowException(new \Exception());
- $this->model->saveAddressInformation($cartId, $addressInformationMock);
- }
- /**
- * @expectedException \Magento\Framework\Exception\NoSuchEntityException
- * @expectedExceptionMessage Carrier with such method not found: carrier_code, shipping_method
- */
- public function testSaveAddressInformationIfCarrierCodeIsInvalid()
- {
- $cartId = 100;
- $carrierCode = 'carrier_code';
- $shippingMethod = 'shipping_method';
- $addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
- $this->quoteRepositoryMock->expects($this->once())
- ->method('getActive')
- ->with($cartId)
- ->willReturn($this->quoteMock);
- $addressInformationMock->expects($this->once())
- ->method('getShippingAddress')
- ->willReturn($this->shippingAddressMock);
- $addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
- $addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
- $billingAddress = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
- $addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
- $this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn('USA');
- $this->setShippingAssignmentsMocks($carrierCode . '_' . $shippingMethod);
- $this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(100);
- $this->quoteMock->expects($this->once())->method('setIsMultiShipping')->with(false)->willReturnSelf();
- $this->quoteMock->expects($this->once())->method('setBillingAddress')->with($billingAddress)->willReturnSelf();
- $this->quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($this->shippingAddressMock);
- $this->quoteRepositoryMock->expects($this->once())
- ->method('save')
- ->with($this->quoteMock);
- $this->shippingAddressMock->expects($this->once())->method('getShippingMethod')->willReturn($shippingMethod);
- $this->shippingAddressMock->expects($this->once())
- ->method('getShippingRateByCode')
- ->with($shippingMethod)
- ->willReturn(false);
- $this->model->saveAddressInformation($cartId, $addressInformationMock);
- }
- public function testSaveAddressInformation()
- {
- $cartId = 100;
- $carrierCode = 'carrier_code';
- $shippingMethod = 'shipping_method';
- $addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
- $this->quoteRepositoryMock->expects($this->once())
- ->method('getActive')
- ->with($cartId)
- ->willReturn($this->quoteMock);
- $addressInformationMock->expects($this->once())
- ->method('getShippingAddress')
- ->willReturn($this->shippingAddressMock);
- $addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
- $addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
- $billingAddress = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
- $addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
- $this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn('USA');
- $this->setShippingAssignmentsMocks($carrierCode . '_' . $shippingMethod);
- $this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(100);
- $this->quoteMock->expects($this->once())->method('setIsMultiShipping')->with(false)->willReturnSelf();
- $this->quoteMock->expects($this->once())->method('setBillingAddress')->with($billingAddress)->willReturnSelf();
- $this->quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($this->shippingAddressMock);
- $this->quoteRepositoryMock->expects($this->once())
- ->method('save')
- ->with($this->quoteMock);
- $this->shippingAddressMock->expects($this->once())->method('getShippingMethod')->willReturn($shippingMethod);
- $this->shippingAddressMock->expects($this->once())
- ->method('getShippingRateByCode')
- ->with($shippingMethod)
- ->willReturn('rates');
- $paymentDetailsMock = $this->createMock(\Magento\Checkout\Api\Data\PaymentDetailsInterface::class);
- $this->paymentDetailsFactoryMock->expects($this->once())->method('create')->willReturn($paymentDetailsMock);
- $paymentMethodMock = $this->createMock(\Magento\Quote\Api\Data\PaymentMethodInterface::class);
- $this->paymentMethodManagementMock->expects($this->once())
- ->method('getList')
- ->with($cartId)
- ->willReturn([$paymentMethodMock]);
- $cartTotalsMock = $this->createMock(\Magento\Quote\Api\Data\TotalsInterface::class);
- $this->cartTotalsRepositoryMock->expects($this->once())
- ->method('get')
- ->with($cartId)
- ->willReturn($cartTotalsMock);
- $paymentDetailsMock->expects($this->once())
- ->method('setPaymentMethods')
- ->with([$paymentMethodMock])
- ->willReturnSelf();
- $paymentDetailsMock->expects($this->once())->method('setTotals')->with()->willReturnSelf($cartTotalsMock);
- $this->assertEquals(
- $paymentDetailsMock,
- $this->model->saveAddressInformation($cartId, $addressInformationMock)
- );
- }
- }
|