|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Checkout\Model; |
| 8 | + |
| 9 | +use Magento\TestFramework\Helper\Bootstrap; |
| 10 | +use Magento\Checkout\Api\Data\ShippingInformationInterface; |
| 11 | +use Magento\Checkout\Api\PaymentInformationManagementInterface; |
| 12 | +use Magento\Checkout\Api\ShippingInformationManagementInterface; |
| 13 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 14 | +use Magento\Quote\Api\CartItemRepositoryInterface; |
| 15 | +use Magento\Quote\Api\CartManagementInterface; |
| 16 | +use Magento\Quote\Api\Data\AddressInterfaceFactory; |
| 17 | +use Magento\Quote\Api\Data\CartItemInterface; |
| 18 | +use Magento\Quote\Api\Data\PaymentInterface; |
| 19 | +use Magento\Quote\Api\ShipmentEstimationInterface; |
| 20 | +use Magento\Sales\Api\InvoiceOrderInterface; |
| 21 | + |
| 22 | +/** |
| 23 | + * Shipping information managment test. |
| 24 | + */ |
| 25 | +class ShippingInformationManagementTest extends \PHPUnit\Framework\TestCase |
| 26 | +{ |
| 27 | + /** @var CartManagementInterface */ |
| 28 | + private $cartManagement; |
| 29 | + |
| 30 | + /** @var CartItemRepositoryInterface */ |
| 31 | + private $cartItemRepository; |
| 32 | + |
| 33 | + /** @var CartItemInterface */ |
| 34 | + private $cartItem; |
| 35 | + |
| 36 | + /** @var ShippingInformationManagementInterface */ |
| 37 | + private $shippingInformationManagement; |
| 38 | + |
| 39 | + /** @var ShippingInformationInterface */ |
| 40 | + private $shippingInformation; |
| 41 | + |
| 42 | + /** @var CustomerRepositoryInterface */ |
| 43 | + private $customerRepository; |
| 44 | + |
| 45 | + /** @var AddressInterfaceFactory */ |
| 46 | + private $apiAddressFactory; |
| 47 | + |
| 48 | + /** @var ShipmentEstimationInterface */ |
| 49 | + private $shipmentEstimation; |
| 50 | + |
| 51 | + /** @var PaymentInformationManagementInterface */ |
| 52 | + private $paymentInformationManagement; |
| 53 | + |
| 54 | + /** @var PaymentInterface */ |
| 55 | + private $payment; |
| 56 | + |
| 57 | + /** @var InvoiceOrderInterface */ |
| 58 | + private $invoiceOrder; |
| 59 | + |
| 60 | + public function setUp() |
| 61 | + { |
| 62 | + $objectManager = Bootstrap::getObjectManager(); |
| 63 | + |
| 64 | + $this->cartManagement = $objectManager->create(CartManagementInterface::class); |
| 65 | + $this->cartItemRepository = $objectManager->create(CartItemRepositoryInterface::class); |
| 66 | + $this->cartItem = $objectManager->create(CartItemInterface::class); |
| 67 | + $this->shippingInformationManagement = $objectManager->create(ShippingInformationManagementInterface::class); |
| 68 | + $this->shippingInformation = $objectManager->create(ShippingInformationInterface::class); |
| 69 | + $this->customerRepository = $objectManager->create(CustomerRepositoryInterface::class); |
| 70 | + $this->apiAddressFactory = $objectManager->create(AddressInterfaceFactory::class); |
| 71 | + $this->shipmentEstimation = $objectManager->create(ShipmentEstimationInterface::class); |
| 72 | + $this->paymentInformationManagement = $objectManager->create(PaymentInformationManagementInterface::class); |
| 73 | + $this->payment = $objectManager->create(PaymentInterface::class); |
| 74 | + $this->invoiceOrder = $objectManager->create(InvoiceOrderInterface::class); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @magentoDataFixture Magento/Customer/_files/customer.php |
| 79 | + * @magentoDataFixture Magento/Customer/_files/customer_address.php |
| 80 | + * @magentoDataFixture Magento/Catalog/_files/product_virtual_in_stock.php |
| 81 | + */ |
| 82 | + public function testQuoteApiWithOnlyVirtualProducts() |
| 83 | + { |
| 84 | + $customer = $this->customerRepository->getById(1); |
| 85 | + |
| 86 | + // Create empty quote |
| 87 | + $quoteId = $this->cartManagement->createEmptyCartForCustomer($customer->getId()); |
| 88 | + |
| 89 | + $cartItem = $this->cartItem |
| 90 | + ->setSku('virtual-product') |
| 91 | + ->setQty(1) |
| 92 | + ->setQuoteId($quoteId); |
| 93 | + |
| 94 | + // Add item to cart |
| 95 | + $this->cartItemRepository->save($cartItem); |
| 96 | + |
| 97 | + $billingAddress = $shippingAddress = null; |
| 98 | + foreach ($customer->getAddresses() as $address) { |
| 99 | + $billingAddress = $address; |
| 100 | + $shippingAddress = $address; |
| 101 | + break; |
| 102 | + } |
| 103 | + |
| 104 | + /** @var \Magento\Quote\Model\Quote\Address $apiBillingAddress */ |
| 105 | + $apiBillingAddress = $this->apiAddressFactory->create(); |
| 106 | + $apiBillingAddress->setRegion($billingAddress->getRegion()) |
| 107 | + ->setRegionId($billingAddress->getRegionId()) |
| 108 | + ->setCountryId($billingAddress->getCountryId()) |
| 109 | + ->setStreet($billingAddress->getStreet()) |
| 110 | + ->setPostcode($billingAddress->getPostcode()) |
| 111 | + ->setCity($billingAddress->getCity()) |
| 112 | + ->setFirstname($billingAddress->getFirstname()) |
| 113 | + ->setLastname($billingAddress->getLastname()) |
| 114 | + ->setEmail($customer->getEmail()) |
| 115 | + ->setTelephone($billingAddress->getTelephone()); |
| 116 | + |
| 117 | + /** @var \Magento\Quote\Model\Quote\Address $apiShippingAddress */ |
| 118 | + $apiShippingAddress = $this->apiAddressFactory->create(); |
| 119 | + $apiShippingAddress->setRegion($shippingAddress->getRegion()) |
| 120 | + ->setRegionId($shippingAddress->getRegionId()) |
| 121 | + ->setCountryId($shippingAddress->getCountryId()) |
| 122 | + ->setStreet($shippingAddress->getStreet()) |
| 123 | + ->setPostcode($shippingAddress->getPostcode()) |
| 124 | + ->setCity($shippingAddress->getCity()) |
| 125 | + ->setFirstname($shippingAddress->getFirstname()) |
| 126 | + ->setLastname($shippingAddress->getLastname()) |
| 127 | + ->setEmail($customer->getEmail()) |
| 128 | + ->setTelephone($shippingAddress->getTelephone()); |
| 129 | + |
| 130 | + // Estimate shipping |
| 131 | + $this->shipmentEstimation->estimateByExtendedAddress($quoteId, $apiShippingAddress); |
| 132 | + |
| 133 | + $addressInformation = $this->shippingInformation |
| 134 | + ->setBillingAddress($apiBillingAddress) |
| 135 | + ->setShippingAddress($apiShippingAddress) |
| 136 | + ->setShippingCarrierCode('flatrate') |
| 137 | + ->setShippingMethodCode('flatrate'); |
| 138 | + |
| 139 | + // Set address information on quote |
| 140 | + $this->shippingInformationManagement->saveAddressInformation($quoteId, $addressInformation); |
| 141 | + } |
| 142 | +} |
0 commit comments