diff --git a/app/code/Magento/Shipping/Model/Shipping.php b/app/code/Magento/Shipping/Model/Shipping.php index eb58d7c9aa803f4bdbd18bf7f2e358b1d105bde6..79e47727d519df0b86203bbaf0e737a94aac4703 100644 --- a/app/code/Magento/Shipping/Model/Shipping.php +++ b/app/code/Magento/Shipping/Model/Shipping.php @@ -5,8 +5,10 @@ */ namespace Magento\Shipping\Model; -use Magento\Sales\Model\Order\Shipment; +use Magento\Framework\App\ObjectManager; use Magento\Quote\Model\Quote\Address\RateCollectorInterface; +use Magento\Quote\Model\Quote\Address\RateRequestFactory; +use Magento\Sales\Model\Order\Shipment; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) @@ -81,6 +83,11 @@ class Shipping implements RateCollectorInterface */ protected $stockRegistry; + /** + * @var RateRequestFactory + */ + private $rateRequestFactory; + /** * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig * @param \Magento\Shipping\Model\Config $shippingConfig @@ -91,6 +98,7 @@ class Shipping implements RateCollectorInterface * @param \Magento\Directory\Model\RegionFactory $regionFactory * @param \Magento\Framework\Math\Division $mathDivision * @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry + * @param RateRequestFactory $rateRequestFactory */ public function __construct( \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, @@ -101,7 +109,8 @@ class Shipping implements RateCollectorInterface \Magento\Shipping\Model\Shipment\RequestFactory $shipmentRequestFactory, \Magento\Directory\Model\RegionFactory $regionFactory, \Magento\Framework\Math\Division $mathDivision, - \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry + \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, + RateRequestFactory $rateRequestFactory = null ) { $this->_scopeConfig = $scopeConfig; $this->_shippingConfig = $shippingConfig; @@ -112,6 +121,7 @@ class Shipping implements RateCollectorInterface $this->_regionFactory = $regionFactory; $this->mathDivision = $mathDivision; $this->stockRegistry = $stockRegistry; + $this->rateRequestFactory = $rateRequestFactory ?: ObjectManager::getInstance()->get(RateRequestFactory::class); } /** @@ -463,7 +473,7 @@ class Shipping implements RateCollectorInterface public function collectRatesByAddress(\Magento\Framework\DataObject $address, $limitCarrier = null) { /** @var $request \Magento\Quote\Model\Quote\Address\RateRequest */ - $request = $this->_shipmentRequestFactory->create(); + $request = $this->rateRequestFactory->create(); $request->setAllItems($address->getAllItems()); $request->setDestCountryId($address->getCountryId()); $request->setDestRegionId($address->getRegionId()); @@ -473,10 +483,13 @@ class Shipping implements RateCollectorInterface $request->setPackageWeight($address->getWeight()); $request->setFreeMethodWeight($address->getFreeMethodWeight()); $request->setPackageQty($address->getItemQty()); - $request->setStoreId($this->_storeManager->getStore()->getId()); - $request->setWebsiteId($this->_storeManager->getStore()->getWebsiteId()); - $request->setBaseCurrency($this->_storeManager->getStore()->getBaseCurrency()); - $request->setPackageCurrency($this->_storeManager->getStore()->getCurrentCurrency()); + + /** @var \Magento\Store\Api\Data\StoreInterface $store */ + $store = $this->_storeManager->getStore(); + $request->setStoreId($store->getId()); + $request->setWebsiteId($store->getWebsiteId()); + $request->setBaseCurrency($store->getBaseCurrency()); + $request->setPackageCurrency($store->getCurrentCurrency()); $request->setLimitCarrier($limitCarrier); $request->setBaseSubtotalInclTax($address->getBaseSubtotalInclTax()); diff --git a/dev/tests/integration/testsuite/Magento/Shipping/Model/ShippingTest.php b/dev/tests/integration/testsuite/Magento/Shipping/Model/ShippingTest.php new file mode 100644 index 0000000000000000000000000000000000000000..749fd266befb9a59bf67d121fbd261b9562bd038 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Shipping/Model/ShippingTest.php @@ -0,0 +1,86 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Shipping\Model; + +use Magento\Framework\DataObject; +use Magento\Framework\ObjectManagerInterface; +use Magento\Quote\Model\Quote\Address\RateResult\Method; +use Magento\Shipping\Model\Rate\Result; +use Magento\TestFramework\Helper\Bootstrap; + +/** + * Contains list of tests for Shipping model + */ +class ShippingTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var Shipping + */ + private $model; + + /** + * @var ObjectManagerInterface + */ + private $objectManager; + + /** + * @inheritdoc + */ + protected function setUp() + { + $this->objectManager = Bootstrap::getObjectManager(); + $this->model = $this->objectManager->get(Shipping::class); + } + + /** + * Checks shipping rates processing by address. + * @covers \Magento\Shipping\Model\Shipping::collectRatesByAddress + * @return Result + */ + public function testCollectRatesByAddress() + { + $address = $this->objectManager->create(DataObject::class, [ + 'data' => [ + 'region_id' => 'CA', + 'postcode' => '11111', + 'lastname' => 'John', + 'firstname' => 'Doe', + 'street' => 'Some street', + 'city' => 'Los Angeles', + 'email' => 'john.doe@example.com', + 'telephone' => '11111111', + 'country_id' => 'US', + 'item_qty' => 1 + ] + ]); + /** @var Shipping $result */ + $result = $this->model->collectRatesByAddress($address, 'flatrate'); + static::assertInstanceOf(Shipping::class, $result); + + return $result->getResult(); + } + + /** + * Checks shipping rate details for processed address. + * @covers \Magento\Shipping\Model\Shipping::collectRatesByAddress + * @param Result $result + * @depends testCollectRatesByAddress + * @magentoConfigFixture carriers/flatrate/active 1 + * @magentoConfigFixture carriers/flatrate/price 5.00 + */ + public function testCollectRates(Result $result) + { + $rates = $result->getAllRates(); + static::assertNotEmpty($rates); + + /** @var Method $rate */ + $rate = array_pop($rates); + + static::assertInstanceOf(Method::class, $rate); + static::assertEquals('flatrate', $rate->getData('carrier')); + static::assertEquals(5, $rate->getData('price')); + } +}