Skip to content

Commit f542e70

Browse files
author
Igor Melnikov
committed
Merge branch 'Fearless-Kiwis-MAGETWO-63844' into MAGETWO-67538-error-handling
2 parents 8fea374 + 0892213 commit f542e70

File tree

18 files changed

+1129
-19
lines changed

18 files changed

+1129
-19
lines changed

app/code/Magento/OfflineShipping/Model/Carrier/Flatrate.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,7 @@ private function getShippingPrice(RateRequest $request, $freeBoxes)
146146

147147
$shippingPrice = $this->getFinalPriceWithHandlingFee($shippingPrice);
148148

149-
if ($shippingPrice !== false && (
150-
$request->getFreeShipping() === true || $request->getPackageQty() == $freeBoxes
151-
)
152-
) {
149+
if ($shippingPrice !== false && $request->getPackageQty() == $freeBoxes) {
153150
$shippingPrice = '0.00';
154151
}
155152
return $shippingPrice;

app/code/Magento/OfflineShipping/Model/Carrier/Tablerate.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,14 @@ public function collectRates(RateRequest $request)
156156
$request->setPackageQty($oldQty);
157157

158158
if (!empty($rate) && $rate['price'] >= 0) {
159-
if ($request->getFreeShipping() === true || $request->getPackageQty() == $freeQty) {
159+
if ($request->getPackageQty() == $freeQty) {
160160
$shippingPrice = 0;
161161
} else {
162162
$shippingPrice = $this->getFinalPriceWithHandlingFee($rate['price']);
163163
}
164164
$method = $this->createShippingMethod($shippingPrice, $rate['cost']);
165165
$result->append($method);
166-
} elseif (empty($rate) && $request->getFreeShipping() === true || $request->getPackageQty() == $freeQty) {
166+
} elseif ($request->getPackageQty() == $freeQty) {
167167

168168
/**
169169
* Promotion rule was applied for the whole cart.
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\OfflineShipping\Test\Unit\Model\Carrier;
7+
8+
use Magento\Quote\Model\Quote\Address\RateResult\Method;
9+
use Magento\Shipping\Model\Carrier\AbstractCarrier;
10+
use Magento\Shipping\Model\Rate\Result;
11+
12+
/**
13+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
14+
*/
15+
class FlatrateTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* @var \Magento\OfflineShipping\Model\Carrier\Flatrate
19+
*/
20+
private $model;
21+
22+
/**
23+
* @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
24+
*/
25+
private $scopeConfigMock;
26+
27+
/**
28+
* @var \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory|\PHPUnit_Framework_MockObject_MockObject
29+
*/
30+
private $errorFactoryMock;
31+
32+
/**
33+
* @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
34+
*/
35+
private $loggerMock;
36+
37+
/**
38+
* @var \Magento\Shipping\Model\Rate\ResultFactory|\PHPUnit_Framework_MockObject_MockObject
39+
*/
40+
private $resultFactoryMock;
41+
42+
/**
43+
* @var \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory|\PHPUnit_Framework_MockObject_MockObject
44+
*/
45+
private $methodFactoryMock;
46+
47+
/**
48+
* @var \Magento\OfflineShipping\Model\Carrier\Flatrate\ItemPriceCalculator|\PHPUnit_Framework_MockObject_MockObject
49+
*/
50+
private $priceCalculatorMock;
51+
52+
/**
53+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
54+
*/
55+
private $helper;
56+
57+
protected function setUp()
58+
{
59+
60+
$this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
61+
->disableOriginalConstructor()
62+
->setMethods(['create', 'isSetFlag', 'getValue'])
63+
->getMock();
64+
65+
$this->errorFactoryMock = $this
66+
->getMockBuilder(\Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory::class)
67+
->disableOriginalConstructor()
68+
->getMock();
69+
70+
$this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
71+
->disableOriginalConstructor()
72+
->getMock();
73+
74+
$this->resultFactoryMock = $this->getMockBuilder(\Magento\Shipping\Model\Rate\ResultFactory::class)
75+
->disableOriginalConstructor()
76+
->setMethods(['create'])
77+
->getMock();
78+
79+
$this->methodFactoryMock = $this
80+
->getMockBuilder(\Magento\Quote\Model\Quote\Address\RateResult\MethodFactory::class)
81+
->disableOriginalConstructor()
82+
->setMethods(['create'])
83+
->getMock();
84+
85+
$this->priceCalculatorMock = $this
86+
->getMockBuilder(\Magento\OfflineShipping\Model\Carrier\Flatrate\ItemPriceCalculator::class)
87+
->disableOriginalConstructor()
88+
->setMethods(['getShippingPricePerOrder'])
89+
->getMock();
90+
91+
$this->helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
92+
$this->model = $this->helper->getObject(
93+
\Magento\OfflineShipping\Model\Carrier\Flatrate::class,
94+
[
95+
'scopeConfig' => $this->scopeConfigMock,
96+
'rateErrorFactory' => $this->errorFactoryMock,
97+
'logger' => $this->loggerMock,
98+
'rateResultFactory' => $this->resultFactoryMock,
99+
'rateMethodFactory' => $this->methodFactoryMock,
100+
'itemPriceCalculator' => $this->priceCalculatorMock
101+
]
102+
);
103+
}
104+
105+
/**
106+
* @param bool $freeshipping
107+
* @dataProvider collectRatesWithGlobalFreeShippingDataProvider
108+
* @return void
109+
*/
110+
public function testCollectRatesWithGlobalFreeShipping($freeshipping)
111+
{
112+
$expectedPrice = 5;
113+
114+
$request = $this->getMockBuilder(\Magento\Quote\Model\Quote\Address\RateRequest::class)
115+
->disableOriginalConstructor()
116+
->setMethods(['getAllItems'])
117+
->getMock();
118+
119+
$item = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
120+
->disableOriginalConstructor()
121+
->setMethods(
122+
[
123+
'getProduct',
124+
'getParentItem',
125+
'getHasChildren',
126+
'isShipSeparately',
127+
'getChildren',
128+
'getQty',
129+
'getFreeShipping'
130+
]
131+
)
132+
->getMock();
133+
134+
$product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
135+
->disableOriginalConstructor()
136+
->setMethods(['isVirtual'])
137+
->getMock();
138+
139+
$this->scopeConfigMock->expects($this->any())->method('isSetFlag')->willReturn(true);
140+
$this->scopeConfigMock->expects($this->any())->method('getValue')->willReturnMap([
141+
['carriers/flatrate/active', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null, true],
142+
['carriers/flatrate/price', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null, 5],
143+
['carriers/flatrate/type', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null, 'O'],
144+
['carriers/flatrate/handling_fee', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null, 0],
145+
[
146+
'carriers/flatrate/handling_type',
147+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
148+
null,
149+
AbstractCarrier::HANDLING_TYPE_FIXED
150+
],
151+
[
152+
'carriers/flatrate/handling_action',
153+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
154+
null,
155+
AbstractCarrier::HANDLING_ACTION_PERORDER
156+
],
157+
]);
158+
159+
$this->priceCalculatorMock
160+
->expects($this->once())
161+
->method('getShippingPricePerOrder')
162+
->willReturn($expectedPrice);
163+
164+
$method = $this->getMockBuilder(Method::class)
165+
->disableOriginalConstructor()
166+
->setMethods(['setCarrier', 'setCarrierTitle', 'setMethod', 'setMethodTitle', 'setPrice', 'setCost'])
167+
->getMock();
168+
$this->methodFactoryMock->expects($this->once())->method('create')->willReturn($method);
169+
170+
$result = $this->getMockBuilder(Result::class)
171+
->disableOriginalConstructor()
172+
->setMethods(['append'])
173+
->getMock();
174+
$this->resultFactoryMock->expects($this->once())->method('create')->willReturn($result);
175+
176+
$product->expects($this->any())->method('isVirtual')->willReturn(false);
177+
178+
$item->expects($this->any())->method('getProduct')->willReturn($product);
179+
$item->expects($this->any())->method('getFreeShipping')->willReturn(1);
180+
$item->expects($this->any())->method('getQty')->willReturn(1);
181+
182+
$request->expects($this->any())->method('getAllItems')->willReturn([$item]);
183+
$request->expects($this->any())->method('getPackageQty')->willReturn(1);
184+
185+
$request->expects($this->never())->method('getFreeShipping')->willReturn($freeshipping);
186+
187+
$returnPrice = null;
188+
$method->expects($this->once())->method('setPrice')->with($this->captureArg($returnPrice));
189+
190+
$returnCost = null;
191+
$method->expects($this->once())->method('setCost')->with($this->captureArg($returnCost));
192+
193+
$returnMethod = null;
194+
$result->expects($this->once())->method('append')->with($this->captureArg($returnMethod));
195+
196+
$returnResult = $this->model->collectRates($request);
197+
198+
$this->assertEquals($expectedPrice, $returnPrice);
199+
$this->assertEquals($expectedPrice, $returnCost);
200+
$this->assertEquals($method, $returnMethod);
201+
$this->assertEquals($result, $returnResult);
202+
}
203+
204+
/**
205+
* Captures the argument and saves it in the given variable
206+
*
207+
* @param $captureVar
208+
* @return \PHPUnit_Framework_Constraint_Callback
209+
*/
210+
private function captureArg(&$captureVar)
211+
{
212+
return $this->callback(function ($argToMock) use (&$captureVar) {
213+
$captureVar = $argToMock;
214+
215+
return true;
216+
});
217+
}
218+
219+
/**
220+
* @return array
221+
*/
222+
public function collectRatesWithGlobalFreeShippingDataProvider()
223+
{
224+
return [
225+
['freeshipping' => true],
226+
['freeshipping' => false]
227+
];
228+
}
229+
}

0 commit comments

Comments
 (0)