ShippingInformationManagementTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\Quote\Api\CartRepositoryInterface;
  12. use Magento\Quote\Api\Data\ShippingAssignmentInterface;
  13. use Magento\TestFramework\Helper\Bootstrap;
  14. use PHPUnit\Framework\TestCase;
  15. /**
  16. * Test ShippingInformationManagement API.
  17. */
  18. class ShippingInformationManagementTest extends TestCase
  19. {
  20. /**
  21. * @var ShippingInformationManagementInterface
  22. */
  23. private $management;
  24. /**
  25. * @var CartRepositoryInterface
  26. */
  27. private $cartRepo;
  28. /**
  29. * @var CustomerRepositoryInterface
  30. */
  31. private $customerRepo;
  32. /**
  33. * @var ShippingInformationInterfaceFactory
  34. */
  35. private $shippingFactory;
  36. protected function setUp()
  37. {
  38. $objectManager = Bootstrap::getObjectManager();
  39. $this->management = $objectManager->get(ShippingInformationManagementInterface::class);
  40. $this->cartRepo = $objectManager->get(CartRepositoryInterface::class);
  41. $this->customerRepo = $objectManager->get(CustomerRepositoryInterface::class);
  42. $this->shippingFactory = $objectManager->get(ShippingInformationInterfaceFactory::class);
  43. }
  44. /**
  45. * Test using another address for quote.
  46. *
  47. * @param bool $swapShipping Whether to swap shipping or billing addresses.
  48. * @return void
  49. *
  50. * @magentoDataFixture Magento/Sales/_files/quote_with_customer.php
  51. * @magentoDataFixture Magento/Customer/_files/customer_with_addresses.php
  52. * @dataProvider getAddressesVariation
  53. * @expectedException \Magento\Framework\Exception\InputException
  54. * @expectedExceptionMessage The shipping information was unable to be saved. Verify the input data and try again.
  55. */
  56. public function testDifferentAddresses(bool $swapShipping)
  57. {
  58. $cart = $this->cartRepo->getForCustomer(1);
  59. $otherCustomer = $this->customerRepo->get('customer_with_addresses@test.com');
  60. $otherAddresses = $otherCustomer->getAddresses();
  61. $otherAddress = array_pop($otherAddresses);
  62. //Setting invalid IDs.
  63. /** @var ShippingAssignmentInterface $shippingAssignment */
  64. $shippingAssignment = $cart->getExtensionAttributes()->getShippingAssignments()[0];
  65. $shippingAddress = $shippingAssignment->getShipping()->getAddress();
  66. $billingAddress = $cart->getBillingAddress();
  67. if ($swapShipping) {
  68. $address = $shippingAddress;
  69. } else {
  70. $address = $billingAddress;
  71. }
  72. $address->setCustomerAddressId($otherAddress->getId());
  73. $address->setCustomerId($otherCustomer->getId());
  74. $address->setId(null);
  75. /** @var ShippingInformationInterface $shippingInformation */
  76. $shippingInformation = $this->shippingFactory->create();
  77. $shippingInformation->setBillingAddress($billingAddress);
  78. $shippingInformation->setShippingAddress($shippingAddress);
  79. $shippingInformation->setShippingMethodCode('flatrate');
  80. $this->management->saveAddressInformation($cart->getId(), $shippingInformation);
  81. }
  82. /**
  83. * Different variations for addresses test.
  84. *
  85. * @return array
  86. */
  87. public function getAddressesVariation(): array
  88. {
  89. return [
  90. 'Shipping address swap' => [true],
  91. 'Billing address swap' => [false]
  92. ];
  93. }
  94. }