Skip to content

Commit 2b8ab46

Browse files
author
Kostiantyn Poida
committed
MAGETWO-31363: Unit and Integration tests coverage
1 parent 2f89843 commit 2b8ab46

File tree

1 file changed

+141
-13
lines changed

1 file changed

+141
-13
lines changed

dev/tests/unit/testsuite/Magento/OfflineShipping/Model/SalesRule/CalculatorTest.php

Lines changed: 141 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,163 @@
55
*/
66
namespace Magento\OfflineShipping\Model\SalesRule;
77

8+
class CalculatorForTest extends Calculator
9+
{
10+
public function setValidatorUtility($validatorUtility)
11+
{
12+
$this->validatorUtility = $validatorUtility;
13+
}
14+
}
15+
816
class CalculatorTest extends \PHPUnit_Framework_TestCase
917
{
1018
/**
11-
* @var \Magento\SalesRule\Model\Validator|\PHPUnit_Framework_MockObject_MockObject
19+
* @var \Magento\OfflineShipping\Model\SalesRule\CalculatorForTest|\PHPUnit_Framework_MockObject_MockObject
1220
*/
1321
protected $_model;
1422

23+
/**
24+
* @var \Magento\Sales\Model\Quote\Item\AbstractItem|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
protected $itemMock;
27+
28+
/**
29+
* @var \Magento\SalesRule\Model\Utility|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
protected $utilityMock;
32+
33+
/**
34+
* @var \Magento\SalesRule\Model\Rule|\PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
protected $ruleMock;
37+
38+
/**
39+
* @var \Magento\Sales\Model\Quote\Address|\PHPUnit_Framework_MockObject_MockObject
40+
*/
41+
protected $addressMock;
42+
1543
protected function setUp()
1644
{
17-
$this->_model = $this->getMock(
18-
'Magento\OfflineShipping\Model\SalesRule\Calculator',
19-
['_getRules', '__wakeup'],
20-
[],
21-
'',
22-
false
23-
);
24-
$this->_model->expects($this->any())->method('_getRules')->will($this->returnValue([]));
45+
$this->utilityMock = $this->getMockBuilder('Magento\SalesRule\Model\Utility')
46+
->disableOriginalConstructor()
47+
->setMethods(['canProcessRule'])
48+
->getMock();
49+
50+
$this->_model = $this->getMockBuilder('Magento\OfflineShipping\Model\SalesRule\CalculatorForTest')
51+
->disableOriginalConstructor()
52+
->setMethods(['_getRules', '__wakeup'])
53+
->getMock();
54+
55+
$this->ruleMock = $this->getMockBuilder('Magento\SalesRule\Model\Rule')
56+
->disableOriginalConstructor()
57+
->setMethods(['getActions', 'getSimpleFreeShipping'])
58+
->getMock();
59+
60+
$this->addressMock = $this->getMockBuilder('Magento\Sales\Model\Quote\Address')
61+
->disableOriginalConstructor()
62+
->setMethods(['setFreeShipping'])
63+
->getMock();
64+
65+
$this->_model->setValidatorUtility($this->utilityMock);
66+
67+
$this->itemMock = $this->getMock('Magento\Sales\Model\Quote\Item', ['getAddress'], [], '', false);
68+
69+
$this->itemMock->expects($this->once())
70+
->method('getAddress')
71+
->willReturn($this->addressMock);
2572
}
2673

2774
public function testProcessFreeShipping()
2875
{
29-
$item = $this->getMock('Magento\Sales\Model\Quote\Item', ['getAddress', '__wakeup'], [], '', false);
30-
$item->expects($this->once())->method('getAddress')->will($this->returnValue(true));
76+
$this->_model->expects($this->any())->method('_getRules')->will($this->returnValue([]));
3177

3278
$this->assertInstanceOf(
3379
'Magento\OfflineShipping\Model\SalesRule\Calculator',
34-
$this->_model->processFreeShipping($item)
80+
$this->_model->processFreeShipping($this->itemMock)
3581
);
82+
}
83+
84+
public function testProcessFreeShippingContinueOnProcessRule()
85+
{
86+
$this->_model->expects($this->once())
87+
->method('_getRules')
88+
->willReturn(['rule1']);
89+
90+
$this->utilityMock->expects($this->once())
91+
->method('canProcessRule')
92+
->willReturn(false);
93+
94+
$this->_model->processFreeShipping($this->itemMock);
95+
}
96+
97+
public function testProcessFreeShippingContinueOnValidateItem()
98+
{
99+
$this->utilityMock->expects($this->once())
100+
->method('canProcessRule')
101+
->willReturn(true);
102+
103+
$actionsCollectionMock = $this->getMockBuilder('Magento\Rule\Model\Action\Collection')
104+
->disableOriginalConstructor()
105+
->setMethods(['validate'])
106+
->getMock();
107+
108+
$actionsCollectionMock->expects($this->once())
109+
->method('validate')
110+
->willReturn(false);
111+
112+
$this->ruleMock->expects($this->once())
113+
->method('getActions')
114+
->willReturn($actionsCollectionMock);
115+
116+
$this->_model->expects($this->once())
117+
->method('_getRules')
118+
->willReturn([$this->ruleMock]);
119+
120+
$this->_model->processFreeShipping($this->itemMock);
121+
}
36122

37-
return true;
123+
/**
124+
* @dataProvider rulesDataProvider
125+
*/
126+
public function testProcessFreeShippingFreeShippingItem($rule)
127+
{
128+
$this->utilityMock->expects($this->once())
129+
->method('canProcessRule')
130+
->willReturn(true);
131+
132+
$actionsCollectionMock = $this->getMockBuilder('Magento\Rule\Model\Action\Collection')
133+
->disableOriginalConstructor()
134+
->setMethods(['validate'])
135+
->getMock();
136+
137+
$actionsCollectionMock->expects($this->once())
138+
->method('validate')
139+
->willReturn(true);
140+
141+
$this->ruleMock->expects($this->once())
142+
->method('getActions')
143+
->willReturn($actionsCollectionMock);
144+
145+
$this->_model->expects($this->once())
146+
->method('_getRules')
147+
->willReturn([$this->ruleMock]);
148+
149+
$this->ruleMock->expects($this->once())
150+
->method('getSimpleFreeShipping')
151+
->willReturn($rule);
152+
153+
$this->addressMock->expects(
154+
$rule == \Magento\OfflineShipping\Model\SalesRule\Rule::FREE_SHIPPING_ADDRESS ? $this->once() : $this->never()
155+
)->method('setFreeShipping');
156+
157+
$this->_model->processFreeShipping($this->itemMock);
158+
}
159+
160+
public function rulesDataProvider()
161+
{
162+
return [
163+
[\Magento\OfflineShipping\Model\SalesRule\Rule::FREE_SHIPPING_ITEM],
164+
[\Magento\OfflineShipping\Model\SalesRule\Rule::FREE_SHIPPING_ADDRESS]
165+
];
38166
}
39167
}

0 commit comments

Comments
 (0)