|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\SalesRule\Model\Quote\Address\Total; |
| 8 | + |
| 9 | +class ShippingTest extends \PHPUnit_Framework_TestCase |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @var \Magento\Quote\Api\GuestCartManagementInterface |
| 13 | + */ |
| 14 | + private $cartManagement; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var \Magento\Quote\Api\GuestCartItemRepositoryInterface |
| 18 | + */ |
| 19 | + private $itemRepository; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var \Magento\Framework\ObjectManagerInterface |
| 23 | + */ |
| 24 | + private $objectManager; |
| 25 | + |
| 26 | + protected function setUp() |
| 27 | + { |
| 28 | + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 29 | + $this->cartManagement = $this->objectManager->get(\Magento\Quote\Api\GuestCartManagementInterface::class); |
| 30 | + $this->itemRepository = $this->objectManager->get(\Magento\Quote\Api\GuestCartItemRepositoryInterface::class); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @magentoDataFixture Magento/SalesRule/_files/rule_free_shipping_by_product_weight.php |
| 35 | + * @magentoDataFixture Magento/Catalog/_files/product_simple.php |
| 36 | + */ |
| 37 | + public function testRuleByProductWeightWithFreeShipping() |
| 38 | + { |
| 39 | + $cartId = $this->prepareQuote(1); |
| 40 | + $methods = $this->estimateShipping($cartId); |
| 41 | + |
| 42 | + $this->assertTrue(count($methods) > 0); |
| 43 | + $this->assertEquals('flatrate', $methods[0]->getMethodCode()); |
| 44 | + $this->assertEquals(0, $methods[0]->getAmount()); |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @magentoDataFixture Magento/SalesRule/_files/rule_free_shipping_by_product_weight.php |
| 50 | + * @magentoDataFixture Magento/Catalog/_files/product_simple.php |
| 51 | + */ |
| 52 | + public function testRuleByProductWeightWithoutFreeShipping() |
| 53 | + { |
| 54 | + $cartId = $this->prepareQuote(5); |
| 55 | + $methods = $this->estimateShipping($cartId); |
| 56 | + |
| 57 | + $this->assertTrue(count($methods) > 0); |
| 58 | + $this->assertEquals('flatrate', $methods[0]->getMethodCode()); |
| 59 | + $this->assertEquals(25, $methods[0]->getAmount()); |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Estimate shipment for guest cart |
| 65 | + * |
| 66 | + * @param int $cartId |
| 67 | + * @return \Magento\Quote\Api\Data\ShippingMethodInterface[] |
| 68 | + */ |
| 69 | + private function estimateShipping($cartId) |
| 70 | + { |
| 71 | + $addressFactory = $this->objectManager->get(\Magento\Quote\Api\Data\AddressInterfaceFactory::class); |
| 72 | + /** @var \Magento\Quote\Api\Data\AddressInterface $address */ |
| 73 | + $address = $addressFactory->create(); |
| 74 | + $address->setCountryId('US'); |
| 75 | + $address->setRegionId(2); |
| 76 | + |
| 77 | + /** @var \Magento\Quote\Api\GuestShipmentEstimationInterface $estimation */ |
| 78 | + $estimation = $this->objectManager->get(\Magento\Quote\Api\GuestShipmentEstimationInterface::class); |
| 79 | + return $estimation->estimateByExtendedAddress($cartId, $address); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Create guest quote with products |
| 84 | + * |
| 85 | + * @param int $itemQty |
| 86 | + * @return int |
| 87 | + */ |
| 88 | + private function prepareQuote($itemQty) |
| 89 | + { |
| 90 | + $cartId = $this->cartManagement->createEmptyCart(); |
| 91 | + |
| 92 | + /** @var \Magento\Quote\Api\Data\CartItemInterfaceFactory $cartItemFactory */ |
| 93 | + $cartItemFactory = $this->objectManager->get(\Magento\Quote\Api\Data\CartItemInterfaceFactory::class); |
| 94 | + |
| 95 | + /** @var \Magento\Quote\Api\Data\CartItemInterface $cartItem */ |
| 96 | + $cartItem = $cartItemFactory->create(); |
| 97 | + $cartItem->setQuoteId($cartId); |
| 98 | + $cartItem->setQty($itemQty); |
| 99 | + $cartItem->setSku('simple'); |
| 100 | + $cartItem->setProductType(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE); |
| 101 | + |
| 102 | + $this->itemRepository->save($cartItem); |
| 103 | + |
| 104 | + return $cartId; |
| 105 | + } |
| 106 | +} |
0 commit comments