diff --git a/app/code/Magento/Multishipping/Model/Checkout/Type/Multishipping.php b/app/code/Magento/Multishipping/Model/Checkout/Type/Multishipping.php index 4edae88143b110fc3875292126f2bf899f2557d6..630614a2ab0e563b5d8aa0c9c234c599adb69f76 100644 --- a/app/code/Magento/Multishipping/Model/Checkout/Type/Multishipping.php +++ b/app/code/Magento/Multishipping/Model/Checkout/Type/Multishipping.php @@ -395,6 +395,8 @@ class Multishipping extends \Magento\Framework\DataObject } } + $this->prepareShippingAssignment($quote); + /** * Delete all not virtual quote items which are not added to shipping address * MultishippingQty should be defined for each quote item when it processed with _addShippingItem diff --git a/app/code/Magento/Multishipping/Test/Unit/Model/Checkout/Type/MultishippingTest.php b/app/code/Magento/Multishipping/Test/Unit/Model/Checkout/Type/MultishippingTest.php index a1aee0d9f12d94246ceb67abbd02d74b0501c97c..9d1082d4bde62ecf79e02485373f376bac199ae9 100644 --- a/app/code/Magento/Multishipping/Test/Unit/Model/Checkout/Type/MultishippingTest.php +++ b/app/code/Magento/Multishipping/Test/Unit/Model/Checkout/Type/MultishippingTest.php @@ -5,130 +5,148 @@ */ namespace Magento\Multishipping\Test\Unit\Model\Checkout\Type; +use Magento\Checkout\Model\Session; +use Magento\Customer\Api\AddressRepositoryInterface; +use Magento\Customer\Api\Data\AddressInterface; +use Magento\Customer\Api\Data\AddressSearchResultsInterface; +use Magento\Customer\Api\Data\CustomerInterface; +use Magento\Customer\Model\Data\Address; +use Magento\Customer\Model\Session as CustomerSession; +use Magento\Framework\Api\FilterBuilder; +use Magento\Framework\Api\SearchCriteria; +use Magento\Framework\Api\SearchCriteriaBuilder; +use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Framework\Event\ManagerInterface; +use Magento\Framework\Pricing\PriceCurrencyInterface; +use Magento\Framework\Session\Generic; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Magento\Multishipping\Helper\Data; +use Magento\Multishipping\Model\Checkout\Type\Multishipping; +use Magento\Payment\Model\Method\SpecificationInterface; +use Magento\Quote\Api\CartRepositoryInterface; +use Magento\Quote\Api\Data\CartExtension; +use Magento\Quote\Api\Data\CartExtensionFactory; +use Magento\Quote\Model\Quote; +use Magento\Quote\Model\Quote\Address as QuoteAddress; +use Magento\Quote\Model\Quote\Address\Item as AddressItem; +use Magento\Quote\Model\Quote\Address\ToOrder; +use Magento\Quote\Model\Quote\Address\ToOrderAddress; +use Magento\Quote\Model\Quote\AddressFactory; +use Magento\Quote\Model\Quote\Item; +use Magento\Quote\Model\Quote\Item\ToOrderItem; +use Magento\Quote\Model\Quote\Payment\ToOrderPayment; +use Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProcessor; +use Magento\Quote\Model\Quote\TotalsCollector; +use Magento\Quote\Model\Shipping; +use Magento\Quote\Model\ShippingAssignment; +use Magento\Sales\Model\Order\Email\Sender\OrderSender; +use Magento\Sales\Model\OrderFactory; +use Magento\Store\Model\StoreManagerInterface; +use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit_Framework_TestCase; + /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ -class MultishippingTest extends \PHPUnit_Framework_TestCase +class MultishippingTest extends PHPUnit_Framework_TestCase { /** - * @var \Magento\Multishipping\Model\Checkout\Type\Multishipping + * @var Multishipping */ protected $model; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var PHPUnit_Framework_MockObject_MockObject */ protected $checkoutSessionMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var PHPUnit_Framework_MockObject_MockObject */ protected $customerSessionMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var PHPUnit_Framework_MockObject_MockObject */ protected $customerMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var PHPUnit_Framework_MockObject_MockObject */ protected $quoteMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var PHPUnit_Framework_MockObject_MockObject */ protected $helperMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var PHPUnit_Framework_MockObject_MockObject */ protected $filterBuilderMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var PHPUnit_Framework_MockObject_MockObject */ protected $addressRepositoryMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var PHPUnit_Framework_MockObject_MockObject */ protected $searchCriteriaBuilderMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var PHPUnit_Framework_MockObject_MockObject */ protected $totalsCollectorMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var PHPUnit_Framework_MockObject_MockObject */ private $cartExtensionFactoryMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var PHPUnit_Framework_MockObject_MockObject */ private $shippingAssignmentProcessorMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var PHPUnit_Framework_MockObject_MockObject */ private $quoteRepositoryMock; protected function setUp() { - $this->checkoutSessionMock = $this->getMock(\Magento\Checkout\Model\Session::class, [], [], '', false); - $this->customerSessionMock = $this->getMock(\Magento\Customer\Model\Session::class, [], [], '', false); - $orderFactoryMock = $this->getMock(\Magento\Sales\Model\OrderFactory::class, [], [], '', false); - $eventManagerMock = $this->getMock(\Magento\Framework\Event\ManagerInterface::class, [], [], '', false); - $scopeConfigMock = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class, [], [], '', false); - $sessionMock = $this->getMock(\Magento\Framework\Session\Generic::class, [], [], '', false); - $addressFactoryMock = $this->getMock(\Magento\Quote\Model\Quote\AddressFactory::class, [], [], '', false); - $toOrderMock = $this->getMock(\Magento\Quote\Model\Quote\Address\ToOrder::class, [], [], '', false); - $toOrderAddressMock = - $this->getMock(\Magento\Quote\Model\Quote\Address\ToOrderAddress::class, [], [], '', false); - $toOrderPaymentMock = - $this->getMock(\Magento\Quote\Model\Quote\Payment\ToOrderPayment::class, [], [], '', false); - $toOrderItemMock = $this->getMock(\Magento\Quote\Model\Quote\Item\ToOrderItem::class, [], [], '', false); - $storeManagerMock = $this->getMock(\Magento\Store\Model\StoreManagerInterface::class, [], [], '', false); - $paymentSpecMock = - $this->getMock(\Magento\Payment\Model\Method\SpecificationInterface::class, [], [], '', false); - $this->helperMock = $this->getMock(\Magento\Multishipping\Helper\Data::class, [], [], '', false); - $orderSenderMock = - $this->getMock(\Magento\Sales\Model\Order\Email\Sender\OrderSender::class, [], [], '', false); - $priceMock = $this->getMock(\Magento\Framework\Pricing\PriceCurrencyInterface::class, [], [], '', false); - $this->quoteRepositoryMock = $this->getMock(\Magento\Quote\Api\CartRepositoryInterface::class); - $this->filterBuilderMock = $this->getMock(\Magento\Framework\Api\FilterBuilder::class, [], [], '', false); - $this->searchCriteriaBuilderMock = $this->getMock( - \Magento\Framework\Api\SearchCriteriaBuilder::class, - [], - [], - '', - false - ); - $this->addressRepositoryMock = $this->getMock( - \Magento\Customer\Api\AddressRepositoryInterface::class, - [], - [], - '', - false - ); + $this->checkoutSessionMock = $this->createSimpleMock(Session::class); + $this->customerSessionMock = $this->createSimpleMock(CustomerSession::class); + $orderFactoryMock = $this->createSimpleMock(OrderFactory::class); + $eventManagerMock = $this->createSimpleMock(ManagerInterface::class); + $scopeConfigMock = $this->createSimpleMock(ScopeConfigInterface::class); + $sessionMock = $this->createSimpleMock(Generic::class); + $addressFactoryMock = $this->createSimpleMock(AddressFactory::class); + $toOrderMock = $this->createSimpleMock(ToOrder::class); + $toOrderAddressMock = $this->createSimpleMock(ToOrderAddress::class); + $toOrderPaymentMock = $this->createSimpleMock(ToOrderPayment::class); + $toOrderItemMock = $this->createSimpleMock(ToOrderItem::class); + $storeManagerMock = $this->createSimpleMock(StoreManagerInterface::class); + $paymentSpecMock = $this->createSimpleMock(SpecificationInterface::class); + $this->helperMock = $this->createSimpleMock(Data::class); + $orderSenderMock = $this->createSimpleMock(OrderSender::class); + $priceMock = $this->createSimpleMock(PriceCurrencyInterface::class); + $this->quoteRepositoryMock = $this->createSimpleMock(CartRepositoryInterface::class); + $this->filterBuilderMock = $this->createSimpleMock(FilterBuilder::class); + $this->searchCriteriaBuilderMock = $this->createSimpleMock(SearchCriteriaBuilder::class); + $this->addressRepositoryMock = $this->createSimpleMock(AddressRepositoryInterface::class); /** This is used to get past _init() which is called in construct. */ $data['checkout_session'] = $this->checkoutSessionMock; - $this->quoteMock = $this->getMock(\Magento\Quote\Model\Quote::class, [], [], '', false); - $this->customerMock = $this->getMock(\Magento\Customer\Api\Data\CustomerInterface::class, [], [], '', false); + $this->quoteMock = $this->createSimpleMock(Quote::class); + $this->customerMock = $this->createSimpleMock(CustomerInterface::class); $this->customerMock->expects($this->atLeastOnce())->method('getId')->willReturn(null); $this->checkoutSessionMock->expects($this->atLeastOnce())->method('getQuote')->willReturn($this->quoteMock); $this->customerSessionMock->expects($this->atLeastOnce())->method('getCustomerDataObject') ->willReturn($this->customerMock); - $this->totalsCollectorMock = $this->getMock( - \Magento\Quote\Model\Quote\TotalsCollector::class, - [], - [], - '', - false - ); - $this->model = new \Magento\Multishipping\Model\Checkout\Type\Multishipping( + $this->totalsCollectorMock = $this->createSimpleMock(TotalsCollector::class); + $this->model = new Multishipping( $this->checkoutSessionMock, $this->customerSessionMock, $orderFactoryMock, @@ -152,21 +170,14 @@ class MultishippingTest extends \PHPUnit_Framework_TestCase $this->totalsCollectorMock, $data ); - $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->cartExtensionFactoryMock = $this->getMock( - \Magento\Quote\Api\Data\CartExtensionFactory::class, - ['create'], - [], - '', - false - ); - $this->shippingAssignmentProcessorMock = $this->getMock( - \Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProcessor::class, - [], - [], - '', - false - ); + + $this->cartExtensionFactoryMock = $this->getMockBuilder(CartExtensionFactory::class) + ->setMethods(['create']) + ->disableOriginalConstructor() + ->getMock(); + $this->shippingAssignmentProcessorMock = $this->createSimpleMock(ShippingAssignmentProcessor::class); + + $objectManager = new ObjectManager($this); $objectManager->setBackwardCompatibleProperty( $this->model, 'cartExtensionFactory', @@ -204,23 +215,21 @@ class MultishippingTest extends \PHPUnit_Framework_TestCase $this->filterBuilderMock->expects($this->atLeastOnce())->method('setConditionType')->willReturnSelf(); $this->filterBuilderMock->expects($this->atLeastOnce())->method('create')->willReturnSelf(); - $searchCriteriaMock = $this->getMock(\Magento\Framework\Api\SearchCriteria::class, [], [], '', false); + $searchCriteriaMock = $this->createSimpleMock(SearchCriteria::class); $this->searchCriteriaBuilderMock->expects($this->atLeastOnce())->method('addFilters')->willReturnSelf(); - $this->searchCriteriaBuilderMock->expects($this->atLeastOnce())->method('create') + $this->searchCriteriaBuilderMock + ->expects($this->atLeastOnce()) + ->method('create') ->willReturn($searchCriteriaMock); - $resultMock = $this->getMock( - \Magento\Customer\Api\Data\AddressSearchResultsInterface::class, - [], - [], - '', - false - ); + $resultMock = $this->createSimpleMock(AddressSearchResultsInterface::class); $this->addressRepositoryMock->expects($this->atLeastOnce())->method('getList')->willReturn($resultMock); - $addressItemMock = $this->getMock(\Magento\Customer\Api\Data\AddressInterface::class, [], [], '', false); + $addressItemMock = $this->createSimpleMock(AddressInterface::class); $resultMock->expects($this->atLeastOnce())->method('getItems')->willReturn([$addressItemMock]); $addressItemMock->expects($this->atLeastOnce())->method('getId')->willReturn(null); + $this->mockShippingAssignment(); + $this->assertEquals($this->model, $this->model->setShippingItemsInformation($info)); } @@ -240,11 +249,11 @@ class MultishippingTest extends \PHPUnit_Framework_TestCase ]; $customerAddressId = 42; - $customerAddressMock = $this->getMock(\Magento\Customer\Model\Data\Address::class, [], [], '', false); - $customerAddressMock->expects($this->atLeastOnce())->method('getId')->willReturn($customerAddressId); - $customerAddresses = [$customerAddressMock]; + $customerAddresses = [ + $this->getCustomerAddressMock($customerAddressId) + ]; - $quoteItemMock = $this->getMock(\Magento\Quote\Model\Quote\Item::class, [], [], '', false); + $quoteItemMock = $this->createSimpleMock(Item::class); $this->quoteMock->expects($this->once())->method('getItemById')->willReturn($quoteItemMock); $this->quoteMock->expects($this->once())->method('getAllShippingAddresses')->willReturn([]); @@ -260,11 +269,11 @@ class MultishippingTest extends \PHPUnit_Framework_TestCase $addressId = 42; $customerAddressId = 42; - $customerAddressMock = $this->getMock(\Magento\Customer\Model\Data\Address::class, [], [], '', false); - $customerAddressMock->expects($this->atLeastOnce())->method('getId')->willReturn($customerAddressId); - $customerAddresses = [$customerAddressMock]; - $this->customerMock->expects($this->once())->method('getAddresses')->willReturn($customerAddresses); + $customerAddresses = [ + $this->getCustomerAddressMock($customerAddressId) + ]; + $this->customerMock->expects($this->once())->method('getAddresses')->willReturn($customerAddresses); $this->addressRepositoryMock->expects($this->once())->method('getById')->willReturn(null); $this->assertEquals($this->model, $this->model->updateQuoteCustomerShippingAddress($addressId)); @@ -279,9 +288,9 @@ class MultishippingTest extends \PHPUnit_Framework_TestCase $addressId = 43; $customerAddressId = 42; - $customerAddressMock = $this->getMock(\Magento\Customer\Model\Data\Address::class, [], [], '', false); - $customerAddressMock->expects($this->atLeastOnce())->method('getId')->willReturn($customerAddressId); - $customerAddresses = [$customerAddressMock]; + $customerAddresses = [ + $this->getCustomerAddressMock($customerAddressId) + ]; $this->customerMock->expects($this->once())->method('getAddresses')->willReturn($customerAddresses); $this->assertEquals($this->model, $this->model->updateQuoteCustomerShippingAddress($addressId)); @@ -292,9 +301,9 @@ class MultishippingTest extends \PHPUnit_Framework_TestCase $addressId = 42; $customerAddressId = 42; - $customerAddressMock = $this->getMock(\Magento\Customer\Model\Data\Address::class, [], [], '', false); - $customerAddressMock->expects($this->atLeastOnce())->method('getId')->willReturn($customerAddressId); - $customerAddresses = [$customerAddressMock]; + $customerAddresses = [ + $this->getCustomerAddressMock($customerAddressId) + ]; $this->customerMock->expects($this->once())->method('getAddresses')->willReturn($customerAddresses); $this->assertEquals($this->model, $this->model->setQuoteCustomerBillingAddress($addressId)); @@ -309,9 +318,9 @@ class MultishippingTest extends \PHPUnit_Framework_TestCase $addressId = 43; $customerAddressId = 42; - $customerAddressMock = $this->getMock(\Magento\Customer\Model\Data\Address::class, [], [], '', false); - $customerAddressMock->expects($this->atLeastOnce())->method('getId')->willReturn($customerAddressId); - $customerAddresses = [$customerAddressMock]; + $customerAddresses = [ + $this->getCustomerAddressMock($customerAddressId) + ]; $this->customerMock->expects($this->once())->method('getAddresses')->willReturn($customerAddresses); $this->assertEquals($this->model, $this->model->setQuoteCustomerBillingAddress($addressId)); @@ -319,7 +328,9 @@ class MultishippingTest extends \PHPUnit_Framework_TestCase public function testGetQuoteShippingAddressesItems() { - $quoteItem = $this->getMock(\Magento\Quote\Model\Quote\Address\Item::class, [], [], '', false); + $quoteItem = $this->getMockBuilder(AddressItem::class) + ->disableOriginalConstructor() + ->getMock(); $this->checkoutSessionMock->expects($this->once())->method('getQuote')->willReturn($this->quoteMock); $this->quoteMock->expects($this->once())->method('getShippingAddressesItems')->willReturn($quoteItem); $this->model->getQuoteShippingAddressesItems(); @@ -327,57 +338,113 @@ class MultishippingTest extends \PHPUnit_Framework_TestCase public function testSetShippingMethods() { - $methodsArray = [1 => 'flatrate_flatrate', 2 => 'tablerate_bestway']; $addressId = 1; - $addressMock = $this->getMock( - \Magento\Quote\Model\Quote\Address::class, - ['getId', 'setShippingMethod'], - [], - '', - false - ); - $extensionAttributesMock = $this->getMock( - \Magento\Quote\Api\Data\CartExtension::class, - ['setShippingAssignments'], - [], - '', - false - ); - $shippingAssignmentMock = $this->getMock( - \Magento\Quote\Model\ShippingAssignment::class, - ['getShipping', 'setShipping'], - [], - '', - false - ); - $shippingMock = $this->getMock(\Magento\Quote\Model\Shipping::class, ['setMethod'], [], '', false); + $addressMock = $this->getMockBuilder(QuoteAddress::class) + ->setMethods(['getId', 'setShippingMethod']) + ->disableOriginalConstructor() + ->getMock(); $addressMock->expects($this->once())->method('getId')->willReturn($addressId); $this->quoteMock->expects($this->once())->method('getAllShippingAddresses')->willReturn([$addressMock]); $addressMock->expects($this->once())->method('setShippingMethod')->with($methodsArray[$addressId]); - //mock for prepareShippingAssignment - $this->quoteMock + + $this->mockShippingAssignment(); + + //save mock + $this->quoteMock->expects($this->once())->method('collectTotals')->willReturnSelf(); + $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock); + $this->model->setShippingMethods($methodsArray); + } + + /** + * @param ShippingAssignment $shippingAssignmentMock + * @return CartExtension|PHPUnit_Framework_MockObject_MockObject + */ + private function getExtensionAttributesMock(ShippingAssignment $shippingAssignmentMock) + { + $extensionAttributesMock = $this->getMockBuilder(CartExtension::class) + ->setMethods(['setShippingAssignments']) + ->getMock(); + + $extensionAttributesMock ->expects($this->once()) - ->method('getExtensionAttributes') - ->willReturn($extensionAttributesMock); + ->method('setShippingAssignments') + ->with([$shippingAssignmentMock]) + ->willReturnSelf(); + + return $extensionAttributesMock; + } + + /** + * @return ShippingAssignment | PHPUnit_Framework_MockObject_MockObject + */ + private function getShippingAssignmentMock() + { + $shippingAssignmentMock = $this->getMockBuilder(ShippingAssignment::class) + ->disableOriginalConstructor() + ->setMethods(['getShipping', 'setShipping']) + ->getMock(); + $shippingMock = $this->getMockBuilder(Shipping::class) + ->disableOriginalConstructor() + ->setMethods(['setMethod']) + ->getMock(); + + $shippingAssignmentMock->expects($this->once())->method('getShipping')->willReturn($shippingMock); + $shippingMock->expects($this->once())->method('setMethod')->with(null)->willReturnSelf(); + $shippingAssignmentMock->expects($this->once())->method('setShipping')->with($shippingMock); + + return $shippingAssignmentMock; + } + + private function mockShippingAssignment() + { + $shippingAssignmentMock = $this->getShippingAssignmentMock(); + + $extensionAttributesMock = $this->getExtensionAttributesMock($shippingAssignmentMock); + $this->shippingAssignmentProcessorMock ->expects($this->once()) ->method('create') ->with($this->quoteMock) ->willReturn($shippingAssignmentMock); - $shippingAssignmentMock->expects($this->once())->method('getShipping')->willReturn($shippingMock); - $shippingMock->expects($this->once())->method('setMethod')->with(null)->willReturnSelf(); - $shippingAssignmentMock->expects($this->once())->method('setShipping')->with($shippingMock); - $extensionAttributesMock + + $this->quoteMock ->expects($this->once()) - ->method('setShippingAssignments') - ->with([$shippingAssignmentMock]) - ->willReturnSelf(); - $this->quoteMock->expects($this->once())->method('setExtensionAttributes')->with($extensionAttributesMock); - //save mock - $this->quoteMock->expects($this->once())->method('collectTotals')->willReturnSelf(); - $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock); - $this->model->setShippingMethods($methodsArray); + ->method('getExtensionAttributes') + ->willReturn($extensionAttributesMock); + + $this->quoteMock + ->expects($this->once()) + ->method('setExtensionAttributes') + ->with($extensionAttributesMock); + } + + /** + * @param $customerAddressId + * @return Address | PHPUnit_Framework_MockObject_MockObject + */ + private function getCustomerAddressMock($customerAddressId) + { + $customerAddressMock = $this->getMockBuilder(Address::class) + ->setMethods(['getId']) + ->disableOriginalConstructor() + ->getMock(); + $customerAddressMock + ->expects($this->atLeastOnce()) + ->method('getId') + ->willReturn($customerAddressId); + return $customerAddressMock; + } + + /** + * @param string $className + * @return PHPUnit_Framework_MockObject_MockObject + */ + private function createSimpleMock($className) + { + return $this->getMockBuilder($className) + ->disableOriginalConstructor() + ->getMock(); } }