GuestShippingInformationManagementTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Checkout\Api;
  8. use Magento\Checkout\Api\Data\ShippingInformationInterface;
  9. use Magento\Checkout\Api\Data\ShippingInformationInterfaceFactory;
  10. use Magento\Customer\Api\CustomerRepositoryInterface;
  11. use Magento\Framework\Api\SearchCriteriaBuilder;
  12. use Magento\Quote\Api\CartRepositoryInterface;
  13. use Magento\Quote\Api\Data\ShippingAssignmentInterface;
  14. use Magento\TestFramework\Helper\Bootstrap;
  15. use PHPUnit\Framework\TestCase;
  16. use Magento\Quote\Model\QuoteIdMask;
  17. use Magento\Quote\Model\QuoteIdMaskFactory;
  18. /**
  19. * Test GuestShippingInformationManagement API.
  20. */
  21. class GuestShippingInformationManagementTest extends TestCase
  22. {
  23. /**
  24. * @var GuestShippingInformationManagementInterface
  25. */
  26. private $management;
  27. /**
  28. * @var CartRepositoryInterface
  29. */
  30. private $cartRepo;
  31. /**
  32. * @var CustomerRepositoryInterface
  33. */
  34. private $customerRepo;
  35. /**
  36. * @var ShippingInformationInterfaceFactory
  37. */
  38. private $shippingFactory;
  39. /**
  40. * @var SearchCriteriaBuilder
  41. */
  42. private $searchCriteria;
  43. /**
  44. * @var QuoteIdMaskFactory
  45. */
  46. private $maskFactory;
  47. protected function setUp()
  48. {
  49. $objectManager = Bootstrap::getObjectManager();
  50. $this->management = $objectManager->get(GuestShippingInformationManagementInterface::class);
  51. $this->cartRepo = $objectManager->get(CartRepositoryInterface::class);
  52. $this->customerRepo = $objectManager->get(CustomerRepositoryInterface::class);
  53. $this->shippingFactory = $objectManager->get(ShippingInformationInterfaceFactory::class);
  54. $this->searchCriteria = $objectManager->get(SearchCriteriaBuilder::class);
  55. $this->maskFactory = $objectManager->get(QuoteIdMaskFactory::class);
  56. }
  57. /**
  58. * Test using another address for quote.
  59. *
  60. * @param bool $swapShipping Whether to swap shipping or billing addresses.
  61. * @return void
  62. *
  63. * @magentoDataFixture Magento/Sales/_files/quote.php
  64. * @magentoDataFixture Magento/Customer/_files/customer_with_addresses.php
  65. * @dataProvider getAddressesVariation
  66. * @expectedException \Magento\Framework\Exception\InputException
  67. * @expectedExceptionMessage The shipping information was unable to be saved. Verify the input data and try again.
  68. */
  69. public function testDifferentAddresses(bool $swapShipping)
  70. {
  71. $carts = $this->cartRepo->getList(
  72. $this->searchCriteria->addFilter('reserved_order_id', 'test01')->create()
  73. )->getItems();
  74. $cart = array_pop($carts);
  75. $otherCustomer = $this->customerRepo->get('customer_with_addresses@test.com');
  76. $otherAddresses = $otherCustomer->getAddresses();
  77. $otherAddress = array_pop($otherAddresses);
  78. //Setting invalid IDs.
  79. /** @var ShippingAssignmentInterface $shippingAssignment */
  80. $shippingAssignment = $cart->getExtensionAttributes()->getShippingAssignments()[0];
  81. $shippingAddress = $shippingAssignment->getShipping()->getAddress();
  82. $billingAddress = $cart->getBillingAddress();
  83. if ($swapShipping) {
  84. $address = $shippingAddress;
  85. } else {
  86. $address = $billingAddress;
  87. }
  88. $address->setCustomerAddressId($otherAddress->getId());
  89. $address->setCustomerId($otherCustomer->getId());
  90. $address->setId(null);
  91. /** @var ShippingInformationInterface $shippingInformation */
  92. $shippingInformation = $this->shippingFactory->create();
  93. $shippingInformation->setBillingAddress($billingAddress);
  94. $shippingInformation->setShippingAddress($shippingAddress);
  95. $shippingInformation->setShippingMethodCode('flatrate');
  96. /** @var QuoteIdMask $idMask */
  97. $idMask = $this->maskFactory->create();
  98. $idMask->load($cart->getId(), 'quote_id');
  99. $this->management->saveAddressInformation($idMask->getMaskedId(), $shippingInformation);
  100. }
  101. /**
  102. * Different variations for addresses test.
  103. *
  104. * @return array
  105. */
  106. public function getAddressesVariation(): array
  107. {
  108. return [
  109. 'Shipping address swap' => [true],
  110. 'Billing address swap' => [false]
  111. ];
  112. }
  113. }