GuestCouponManagementTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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\Usps\Api;
  8. use Magento\Catalog\Model\Product\Type;
  9. use Magento\Framework\DataObject;
  10. use Magento\Framework\HTTP\ZendClient;
  11. use Magento\Framework\HTTP\ZendClientFactory;
  12. use Magento\Quote\Api\Data\AddressInterface;
  13. use Magento\Quote\Api\Data\AddressInterfaceFactory;
  14. use Magento\Quote\Api\Data\CartItemInterface;
  15. use Magento\Quote\Api\Data\CartItemInterfaceFactory;
  16. use Magento\Quote\Api\Data\ShippingMethodInterface;
  17. use Magento\Quote\Api\GuestCartItemRepositoryInterface;
  18. use Magento\Quote\Api\GuestCartManagementInterface;
  19. use Magento\Quote\Api\GuestCouponManagementInterface;
  20. use Magento\Quote\Api\GuestShipmentEstimationInterface;
  21. use Magento\TestFramework\Helper\Bootstrap;
  22. use Magento\TestFramework\ObjectManager;
  23. use PHPUnit\Framework\TestCase;
  24. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  25. /**
  26. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  27. */
  28. class GuestCouponManagementTest extends TestCase
  29. {
  30. /**
  31. * @var GuestCouponManagementInterface
  32. */
  33. private $management;
  34. /**
  35. * @var ObjectManager
  36. */
  37. private $objectManager;
  38. /**
  39. * @var ZendClient|MockObject
  40. */
  41. private $httpClient;
  42. /**
  43. * @inheritdoc
  44. */
  45. protected function setUp()
  46. {
  47. $this->objectManager = Bootstrap::getObjectManager();
  48. $this->management = $this->objectManager->get(GuestCouponManagementInterface::class);
  49. $clientFactory = $this->getMockBuilder(ZendClientFactory::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->httpClient = $this->getMockBuilder(ZendClient::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $clientFactory->method('create')
  56. ->willReturn($this->httpClient);
  57. $this->objectManager->addSharedInstance($clientFactory, ZendClientFactory::class);
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. protected function tearDown()
  63. {
  64. $this->objectManager->removeSharedInstance(ZendClientFactory::class);
  65. }
  66. /**
  67. * Checks a case when coupon is applied for a guest cart and USPS Priority Mail 1-Day configured as free method.
  68. *
  69. * @magentoConfigFixture default_store carriers/usps/active 1
  70. * @magentoConfigFixture default_store carriers/usps/free_method 1
  71. * @magentoDataFixture Magento/Usps/Fixtures/cart_rule_coupon_free_shipping.php
  72. * @magentoDataFixture Magento/Quote/_files/is_salable_product.php
  73. * @return void
  74. */
  75. public function testFreeShippingWithCoupon(): void
  76. {
  77. $couponCode = 'IMPHBR852R61';
  78. $cartId = $this->createGuestCart();
  79. $request = new DataObject(['body' => file_get_contents(__DIR__ . '/../Fixtures/rates_response.xml')]);
  80. $this->httpClient->method('request')
  81. ->willReturn($request);
  82. self::assertTrue($this->management->set($cartId, $couponCode));
  83. $methods = $this->estimateShipping($cartId);
  84. $methods = $this->filterFreeShippingMethods($methods);
  85. self::assertEquals(['Fixed', 'Priority Mail 1-Day'], $methods);
  86. }
  87. /**
  88. * Creates guest shopping cart.
  89. *
  90. * @return string
  91. */
  92. private function createGuestCart(): string
  93. {
  94. /** @var GuestCartManagementInterface $cartManagement */
  95. $cartManagement = $this->objectManager->get(GuestCartManagementInterface::class);
  96. $cartId = $cartManagement->createEmptyCart();
  97. /** @var CartItemInterfaceFactory $cartItemFactory */
  98. $cartItemFactory = $this->objectManager->get(CartItemInterfaceFactory::class);
  99. /** @var CartItemInterface $cartItem */
  100. $cartItem = $cartItemFactory->create();
  101. $cartItem->setQuoteId($cartId);
  102. $cartItem->setQty(1);
  103. $cartItem->setSku('simple-99');
  104. $cartItem->setProductType(Type::TYPE_SIMPLE);
  105. /** @var GuestCartItemRepositoryInterface $itemRepository */
  106. $itemRepository = $this->objectManager->get(GuestCartItemRepositoryInterface::class);
  107. $itemRepository->save($cartItem);
  108. return $cartId;
  109. }
  110. /**
  111. * Estimates shipment for guest cart.
  112. *
  113. * @param string $cartId
  114. * @return array ShippingMethodInterface[]
  115. */
  116. private function estimateShipping(string $cartId): array
  117. {
  118. $addressFactory = $this->objectManager->get(AddressInterfaceFactory::class);
  119. /** @var AddressInterface $address */
  120. $address = $addressFactory->create();
  121. $address->setCountryId('US');
  122. $address->setRegionId(12);
  123. $address->setPostcode(90230);
  124. /** @var GuestShipmentEstimationInterface $estimation */
  125. $estimation = $this->objectManager->get(GuestShipmentEstimationInterface::class);
  126. return $estimation->estimateByExtendedAddress($cartId, $address);
  127. }
  128. /**
  129. * Filters free shipping methods.
  130. *
  131. * @param array $methods
  132. * @return array
  133. */
  134. private function filterFreeShippingMethods(array $methods): array
  135. {
  136. $result = [];
  137. /** @var ShippingMethodInterface $method */
  138. foreach ($methods as $method) {
  139. if ($method->getAmount() == 0) {
  140. $result[] = $method->getMethodTitle();
  141. }
  142. }
  143. return $result;
  144. }
  145. }