Skip to content

Commit 73ebe79

Browse files
committed
Merge branch 'MAGETWO-97004' of https://github.com/magento-mpi/magento2ce into PR_2019_06_18
2 parents 7282eb4 + 2b5dc00 commit 73ebe79

File tree

2 files changed

+99
-48
lines changed

2 files changed

+99
-48
lines changed

app/code/Magento/Shipping/Model/Shipping.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,7 @@ public function collectRates(\Magento\Quote\Model\Quote\Address\RateRequest $req
251251
*/
252252
public function collectCarrierRates($carrierCode, $request)
253253
{
254-
/* @var $carrier \Magento\Shipping\Model\Carrier\AbstractCarrier */
255-
$carrier = $this->_carrierFactory->createIfActive($carrierCode, $request->getQuoteStoreId());
254+
$carrier = $this->_carrierFactory->create($carrierCode, $request->getQuoteStoreId());
256255
if (!$carrier) {
257256
return $this;
258257
}

app/code/Magento/Shipping/Test/Unit/Model/ShippingTest.php

Lines changed: 98 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@
55
*/
66
namespace Magento\Shipping\Test\Unit\Model;
77

8-
use \Magento\Shipping\Model\Shipping;
9-
8+
use Magento\Catalog\Model\Product;
9+
use Magento\Catalog\Model\Product\Type as ProductType;
10+
use Magento\CatalogInventory\Model\Stock\Item as StockItem;
11+
use Magento\CatalogInventory\Model\StockRegistry;
12+
use Magento\Quote\Model\Quote\Item as QuoteItem;
13+
use Magento\Shipping\Model\Carrier\AbstractCarrier;
14+
use Magento\Shipping\Model\Carrier\AbstractCarrierInterface;
15+
use Magento\Shipping\Model\Carrier\CarrierInterface;
16+
use Magento\Shipping\Model\CarrierFactory;
17+
use Magento\Shipping\Model\Shipping;
1018
use Magento\Quote\Model\Quote\Address\RateRequest;
1119
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
20+
use Magento\Store\Model\Store;
21+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1222

1323
class ShippingTest extends \PHPUnit\Framework\TestCase
1424
{
@@ -25,71 +35,69 @@ class ShippingTest extends \PHPUnit\Framework\TestCase
2535
protected $shipping;
2636

2737
/**
28-
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Shipping\Model\Carrier\AbstractCarrier
38+
* @var MockObject|StockRegistry
2939
*/
30-
protected $carrier;
40+
protected $stockRegistry;
3141

3242
/**
33-
* @var \PHPUnit_Framework_MockObject_MockObject
43+
* @var MockObject|StockItem
3444
*/
35-
protected $stockRegistry;
45+
protected $stockItemData;
3646

3747
/**
38-
* @var \PHPUnit_Framework_MockObject_MockObject
48+
* @var MockObject|AbstractCarrierInterface
3949
*/
40-
protected $stockItemData;
50+
private $carrier;
4151

4252
protected function setUp()
4353
{
44-
$this->carrier = $this->createMock(\Magento\Shipping\Model\Carrier\AbstractCarrier::class);
45-
$this->carrier->expects($this->any())->method('getConfigData')->will($this->returnCallback(function ($key) {
46-
$configData = [
47-
'max_package_weight' => 10,
48-
];
49-
return isset($configData[$key]) ? $configData[$key] : 0;
50-
}));
51-
$this->stockRegistry = $this->createMock(\Magento\CatalogInventory\Model\StockRegistry::class);
52-
$this->stockItemData = $this->createMock(\Magento\CatalogInventory\Model\Stock\Item::class);
53-
54-
$objectManagerHelper = new ObjectManagerHelper($this);
55-
$this->shipping = $objectManagerHelper->getObject(
56-
\Magento\Shipping\Model\Shipping::class,
57-
['stockRegistry' => $this->stockRegistry]
54+
$this->stockRegistry = $this->createMock(StockRegistry::class);
55+
$this->stockItemData = $this->createMock(StockItem::class);
56+
57+
$this->shipping = (new ObjectManagerHelper($this))->getObject(
58+
Shipping::class,
59+
[
60+
'stockRegistry' => $this->stockRegistry,
61+
'carrierFactory' => $this->getCarrierFactory(),
62+
]
5863
);
5964
}
6065

6166
/**
62-
* @covers \Magento\Shipping\Model\Shipping::composePackagesForCarrier
67+
*
6368
*/
6469
public function testComposePackages()
6570
{
6671
$request = new RateRequest();
67-
/** \Magento\Catalog\Model\Product\Configuration\Item\ItemInterface */
68-
$item = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
72+
$item = $this->getMockBuilder(QuoteItem::class)
6973
->disableOriginalConstructor()
70-
->setMethods([
71-
'getQty', 'getIsQtyDecimal', 'getProductType', 'getProduct', 'getWeight', '__wakeup', 'getStore',
72-
])
73-
->getMock();
74-
$product = $this->createMock(\Magento\Catalog\Model\Product::class);
75-
76-
$item->expects($this->any())->method('getQty')->will($this->returnValue(1));
77-
$item->expects($this->any())->method('getWeight')->will($this->returnValue(10));
78-
$item->expects($this->any())->method('getIsQtyDecimal')->will($this->returnValue(true));
79-
$item->expects($this->any())->method('getProductType')
80-
->will($this->returnValue(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE));
81-
$item->expects($this->any())->method('getProduct')->will($this->returnValue($product));
82-
83-
$store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getWebsiteId']);
84-
$store->expects($this->any())
85-
->method('getWebsiteId')
86-
->will($this->returnValue(10));
87-
$item->expects($this->any())->method('getStore')->will($this->returnValue($store));
88-
89-
$product->expects($this->any())->method('getId')->will($this->returnValue($this->productId));
74+
->setMethods(
75+
[
76+
'getQty',
77+
'getIsQtyDecimal',
78+
'getProductType',
79+
'getProduct',
80+
'getWeight',
81+
'__wakeup',
82+
'getStore',
83+
]
84+
)->getMock();
85+
$product = $this->createMock(Product::class);
86+
87+
$item->method('getQty')->will($this->returnValue(1));
88+
$item->method('getWeight')->will($this->returnValue(10));
89+
$item->method('getIsQtyDecimal')->will($this->returnValue(true));
90+
$item->method('getProductType')->will($this->returnValue(ProductType::TYPE_SIMPLE));
91+
$item->method('getProduct')->will($this->returnValue($product));
92+
93+
$store = $this->createPartialMock(Store::class, ['getWebsiteId']);
94+
$store->method('getWebsiteId')->will($this->returnValue(10));
95+
$item->method('getStore')->will($this->returnValue($store));
96+
97+
$product->method('getId')->will($this->returnValue($this->productId));
9098
$request->setData('all_items', [$item]);
9199

92-
$this->stockItemData->expects($this->any())->method('getIsDecimalDivided')->will($this->returnValue(true));
100+
$this->stockItemData->method('getIsDecimalDivided')->will($this->returnValue(true));
93101

94102
/** Testable service calls to CatalogInventory module */
95103
$this->stockRegistry->expects($this->atLeastOnce())->method('getStockItem')
@@ -101,7 +109,51 @@ public function testComposePackages()
101109
->will($this->returnValue(true));
102110
$this->stockItemData->expects($this->atLeastOnce())->method('getQtyIncrements')
103111
->will($this->returnValue(0.5));
112+
$this->carrier->method('getConfigData')
113+
->willReturnCallback(function ($key) {
114+
$configData = [
115+
'max_package_weight' => 10,
116+
];
117+
return isset($configData[$key]) ? $configData[$key] : 0;
118+
});
104119

105120
$this->shipping->composePackagesForCarrier($this->carrier, $request);
106121
}
122+
123+
/**
124+
* Active flag should be set before collecting carrier rates.
125+
*/
126+
public function testCollectCarrierRatesSetActiveFlag()
127+
{
128+
$this->carrier->expects($this->atLeastOnce())
129+
->method('setActiveFlag')
130+
->with('active');
131+
132+
$this->shipping->collectCarrierRates('carrier', new RateRequest());
133+
}
134+
135+
/**
136+
* @return CarrierFactory|MockObject
137+
*/
138+
private function getCarrierFactory()
139+
{
140+
$carrierFactory = $this->getMockBuilder(CarrierFactory::class)
141+
->disableOriginalConstructor()
142+
->getMock();
143+
144+
$this->carrier = $this->getMockBuilder(AbstractCarrierInterface::class)
145+
->setMethods(
146+
[
147+
'setActiveFlag',
148+
'checkAvailableShipCountries',
149+
'processAdditionalValidation',
150+
'getConfigData',
151+
'collectRates',
152+
]
153+
)
154+
->getMockForAbstractClass();
155+
$carrierFactory->method('create')->willReturn($this->carrier);
156+
157+
return $carrierFactory;
158+
}
107159
}

0 commit comments

Comments
 (0)