ShippingMethodManagementTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model;
  7. /**
  8. * Class ShippingMethodManagementTest
  9. *
  10. * @magentoDbIsolation enabled
  11. */
  12. class ShippingMethodManagementTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @magentoDataFixture Magento/SalesRule/_files/cart_rule_100_percent_off.php
  16. * @magentoDataFixture Magento/Sales/_files/quote_with_customer.php
  17. * @return void
  18. */
  19. public function testRateAppliedToShipping(): void
  20. {
  21. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  22. /** @var \Magento\Quote\Api\CartRepositoryInterface $quoteRepository */
  23. $quoteRepository = $objectManager->create(\Magento\Quote\Api\CartRepositoryInterface::class);
  24. $customerQuote = $quoteRepository->getForCustomer(1);
  25. $this->assertEquals(0, $customerQuote->getBaseGrandTotal());
  26. }
  27. /**
  28. * @magentoConfigFixture current_store carriers/tablerate/active 1
  29. * @magentoConfigFixture current_store carriers/flatrate/active 0
  30. * @magentoConfigFixture current_store carriers/freeshipping/active 0
  31. * @magentoConfigFixture current_store carriers/tablerate/condition_name package_qty
  32. * @magentoDataFixture Magento/SalesRule/_files/cart_rule_free_shipping_by_cart.php
  33. * @magentoDataFixture Magento/Sales/_files/quote.php
  34. * @magentoDataFixture Magento/OfflineShipping/_files/tablerates.php
  35. * @return void
  36. */
  37. public function testTableRateFreeShipping()
  38. {
  39. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  40. /** @var \Magento\Quote\Model\Quote $quote */
  41. $quote = $objectManager->get(\Magento\Quote\Model\Quote::class);
  42. $quote->load('test01', 'reserved_order_id');
  43. $cartId = $quote->getId();
  44. if (!$cartId) {
  45. $this->fail('quote fixture failed');
  46. }
  47. /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
  48. $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  49. ->create(\Magento\Quote\Model\QuoteIdMaskFactory::class)
  50. ->create();
  51. $quoteIdMask->load($cartId, 'quote_id');
  52. //Use masked cart Id
  53. $cartId = $quoteIdMask->getMaskedId();
  54. $data = [
  55. 'data' => [
  56. 'country_id' => "US",
  57. 'postcode' => null,
  58. 'region' => null,
  59. 'region_id' => null
  60. ]
  61. ];
  62. /** @var \Magento\Quote\Api\Data\EstimateAddressInterface $address */
  63. $address = $objectManager->create(\Magento\Quote\Api\Data\EstimateAddressInterface::class, $data);
  64. /** @var \Magento\Quote\Api\GuestShippingMethodManagementInterface $shippingEstimation */
  65. $shippingEstimation = $objectManager->get(\Magento\Quote\Api\GuestShippingMethodManagementInterface::class);
  66. $result = $shippingEstimation->estimateByAddress($cartId, $address);
  67. $this->assertNotEmpty($result);
  68. $expectedResult = [
  69. 'method_code' => 'bestway',
  70. 'amount' => 0
  71. ];
  72. foreach ($result as $rate) {
  73. $this->assertEquals($expectedResult['amount'], $rate->getAmount());
  74. $this->assertEquals($expectedResult['method_code'], $rate->getMethodCode());
  75. }
  76. }
  77. /**
  78. * @magentoConfigFixture current_store carriers/tablerate/active 1
  79. * @magentoConfigFixture current_store carriers/tablerate/condition_name package_qty
  80. * @magentoDataFixture Magento/SalesRule/_files/cart_rule_free_shipping.php
  81. * @magentoDataFixture Magento/Sales/_files/quote.php
  82. * @magentoDataFixture Magento/OfflineShipping/_files/tablerates.php
  83. * @return void
  84. */
  85. public function testEstimateByAddressWithCartPriceRuleByItem()
  86. {
  87. $this->executeTestFlow(0, 0);
  88. }
  89. /**
  90. * @magentoConfigFixture current_store carriers/tablerate/active 1
  91. * @magentoConfigFixture current_store carriers/tablerate/condition_name package_qty
  92. * @magentoDataFixture Magento/SalesRule/_files/cart_rule_free_shipping_by_cart.php
  93. * @magentoDataFixture Magento/Sales/_files/quote.php
  94. * @magentoDataFixture Magento/OfflineShipping/_files/tablerates.php
  95. * @return void
  96. */
  97. public function testEstimateByAddressWithCartPriceRuleByShipment()
  98. {
  99. $this->markTestSkipped('According to MAGETWO-69940 it is an incorrect behavior');
  100. // Rule applied to entire shipment should not overwrite flat or table rate shipping prices
  101. // Only rules applied to specific items should modify those prices (MAGETWO-63844)
  102. $this->executeTestFlow(5, 10);
  103. }
  104. /**
  105. * @magentoConfigFixture current_store carriers/tablerate/active 1
  106. * @magentoConfigFixture current_store carriers/tablerate/condition_name package_qty
  107. * @magentoDataFixture Magento/Sales/_files/quote.php
  108. * @magentoDataFixture Magento/OfflineShipping/_files/tablerates.php
  109. * @return void
  110. */
  111. public function testEstimateByAddress()
  112. {
  113. $this->executeTestFlow(5, 10);
  114. }
  115. /**
  116. * Provide testing of shipping method estimation based on address
  117. *
  118. * @param int $flatRateAmount
  119. * @param int $tableRateAmount
  120. * @return void
  121. */
  122. private function executeTestFlow($flatRateAmount, $tableRateAmount)
  123. {
  124. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  125. /** @var \Magento\Quote\Model\Quote $quote */
  126. $quote = $objectManager->get(\Magento\Quote\Model\Quote::class);
  127. $quote->load('test01', 'reserved_order_id');
  128. $cartId = $quote->getId();
  129. if (!$cartId) {
  130. $this->fail('quote fixture failed');
  131. }
  132. /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
  133. $quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  134. ->create(\Magento\Quote\Model\QuoteIdMaskFactory::class)
  135. ->create();
  136. $quoteIdMask->load($cartId, 'quote_id');
  137. //Use masked cart Id
  138. $cartId = $quoteIdMask->getMaskedId();
  139. $data = [
  140. 'data' => [
  141. 'country_id' => "US",
  142. 'postcode' => null,
  143. 'region' => null,
  144. 'region_id' => null
  145. ]
  146. ];
  147. /** @var \Magento\Quote\Api\Data\EstimateAddressInterface $address */
  148. $address = $objectManager->create(\Magento\Quote\Api\Data\EstimateAddressInterface::class, $data);
  149. /** @var \Magento\Quote\Api\GuestShippingMethodManagementInterface $shippingEstimation */
  150. $shippingEstimation = $objectManager->get(\Magento\Quote\Api\GuestShippingMethodManagementInterface::class);
  151. $result = $shippingEstimation->estimateByAddress($cartId, $address);
  152. $this->assertNotEmpty($result);
  153. $expectedResult = [
  154. 'tablerate' => [
  155. 'method_code' => 'bestway',
  156. 'amount' => $tableRateAmount
  157. ],
  158. 'flatrate' => [
  159. 'method_code' => 'flatrate',
  160. 'amount' => $flatRateAmount
  161. ]
  162. ];
  163. foreach ($result as $rate) {
  164. $this->assertEquals($expectedResult[$rate->getCarrierCode()]['amount'], $rate->getAmount());
  165. $this->assertEquals($expectedResult[$rate->getCarrierCode()]['method_code'], $rate->getMethodCode());
  166. }
  167. }
  168. }