Skip to content

Commit 29c08c1

Browse files
author
nsyvokonenko
committed
MAGETWO-37089: [Firedrakes] Unit test coverage for MLS10
1 parent 4fd68c3 commit 29c08c1

File tree

1 file changed

+150
-26
lines changed

1 file changed

+150
-26
lines changed

app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceTest.php

Lines changed: 150 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Magento\Sales\Test\Unit\Model\Order;
1010

11+
use Magento\Sales\Model\Order\Invoice;
1112
use Magento\Sales\Model\Resource\OrderFactory;
1213

1314
/**
@@ -22,6 +23,17 @@ class InvoiceTest extends \PHPUnit_Framework_TestCase
2223
*/
2324
protected $model;
2425

26+
/**
27+
* Same as $model but Order was not set
28+
* @var \Magento\Sales\Model\Order\Invoice
29+
*/
30+
protected $modelWithoutOrder;
31+
32+
/**
33+
* @var string
34+
*/
35+
protected $entityType = 'invoice';
36+
2537
/**
2638
* @var OrderFactory |\PHPUnit_Framework_MockObject_MockObject
2739
*/
@@ -35,20 +47,33 @@ class InvoiceTest extends \PHPUnit_Framework_TestCase
3547
/**
3648
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Sales\Model\Order\Payment
3749
*/
38-
protected $_paymentMock;
50+
protected $paymentMock;
51+
52+
/**
53+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
54+
*/
55+
protected $helperManager;
3956

4057
protected function setUp()
4158
{
42-
$helperManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
59+
$this->helperManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
4360
$this->orderMock = $this->getMockBuilder(
4461
'Magento\Sales\Model\Order'
4562
)->disableOriginalConstructor()->setMethods(
46-
['getPayment', '__wakeup', 'load', 'setHistoryEntityName']
63+
[
64+
'getPayment', '__wakeup', 'load', 'setHistoryEntityName', 'getStore', 'getBillingAddress',
65+
'getShippingAddress'
66+
]
4767
)->getMock();
48-
$this->_paymentMock = $this->getMockBuilder(
68+
$this->orderMock->expects($this->any())
69+
->method('setHistoryEntityName')
70+
->with($this->entityType)
71+
->will($this->returnSelf());
72+
73+
$this->paymentMock = $this->getMockBuilder(
4974
'Magento\Sales\Model\Order\Payment'
5075
)->disableOriginalConstructor()->setMethods(
51-
['canVoid', '__wakeup']
76+
['canVoid', '__wakeup', 'canCapture']
5277
)->getMock();
5378

5479
$this->orderFactory = $this->getMock('Magento\Sales\Model\OrderFactory', ['create'], [], '', false);
@@ -91,8 +116,9 @@ protected function setUp()
91116
false
92117
),
93118
];
94-
$this->model = $helperManager->getObject('Magento\Sales\Model\Order\Invoice', $arguments);
119+
$this->model = $this->helperManager->getObject('Magento\Sales\Model\Order\Invoice', $arguments);
95120
$this->model->setOrder($this->orderMock);
121+
$this->modelWithoutOrder = $this->helperManager->getObject('Magento\Sales\Model\Order\Invoice', $arguments);
96122
}
97123

98124
/**
@@ -101,20 +127,10 @@ protected function setUp()
101127
*/
102128
public function testCanVoid($canVoid)
103129
{
104-
$entityName = 'invoice';
105-
$this->orderMock->expects($this->once())->method('getPayment')->will($this->returnValue($this->_paymentMock));
106-
$this->orderMock->expects($this->once())
107-
->method('setHistoryEntityName')
108-
->with($entityName)
109-
->will($this->returnSelf());
110-
$this->_paymentMock->expects(
111-
$this->once()
112-
)->method(
113-
'canVoid',
114-
'__wakeup'
115-
)->will(
116-
$this->returnValue($canVoid)
117-
);
130+
$this->orderMock->expects($this->once())->method('getPayment')->willReturn($this->paymentMock);
131+
$this->paymentMock->expects($this->once())
132+
->method('canVoid', '__wakeup')
133+
->willReturn($canVoid);
118134

119135
$this->model->setState(\Magento\Sales\Model\Order\Invoice::STATE_PAID);
120136
$this->assertEquals($canVoid, $this->model->canVoid());
@@ -139,25 +155,133 @@ public function canVoidDataProvider()
139155

140156
public function testGetOrder()
141157
{
142-
$orderId = 100000041;
143-
$this->model->setOrderId($orderId);
144-
$entityName = 'invoice';
145-
$this->orderMock->expects($this->atLeastOnce())
158+
$this->orderMock->expects($this->once())
146159
->method('setHistoryEntityName')
147-
->with($entityName)
160+
->with($this->entityType)
148161
->will($this->returnSelf());
149162

150163
$this->assertEquals($this->orderMock, $this->model->getOrder());
151164
}
152165

166+
public function testGetOrderLoadedById()
167+
{
168+
$orderId = 100000041;
169+
$this->modelWithoutOrder->setOrderId($orderId);
170+
$this->orderMock->expects($this->once())
171+
->method('load')
172+
->with($orderId)
173+
->willReturnSelf();
174+
$this->orderMock->expects($this->once())
175+
->method('setHistoryEntityName')
176+
->with($this->entityType)
177+
->willReturnSelf();
178+
$this->orderFactory->expects($this->once())
179+
->method('create')
180+
->willReturn($this->orderMock);
181+
182+
$this->assertEquals($this->orderMock, $this->modelWithoutOrder->getOrder());
183+
}
184+
153185
public function testGetEntityType()
154186
{
155-
$this->assertEquals('invoice', $this->model->getEntityType());
187+
$this->assertEquals($this->entityType, $this->model->getEntityType());
156188
}
157189

158190
public function testGetIncrementId()
159191
{
160192
$this->model->setIncrementId('test_increment_id');
161193
$this->assertEquals('test_increment_id', $this->model->getIncrementId());
162194
}
195+
196+
public function testSetOrder()
197+
{
198+
$orderId = 1111;
199+
$storeId = 2221;
200+
$this->orderMock->setId($orderId);
201+
$this->orderMock->setStoreId($storeId);
202+
$this->assertNull($this->model->getOrderId());
203+
$this->assertNull($this->model->getStoreId());
204+
205+
$this->assertEquals($this->model, $this->model->setOrder($this->orderMock));
206+
$this->assertEquals($this->orderMock, $this->model->getOrder());
207+
$this->assertEquals($orderId, $this->model->getOrderId());
208+
$this->assertEquals($storeId, $this->model->getStoreId());
209+
}
210+
211+
public function testGetStore()
212+
{
213+
$store = $this->helperManager->getObject('\Magento\Store\Model\Store', []);
214+
$this->orderMock->expects($this->once())->method('getStore')->willReturn($store);
215+
$this->assertEquals($store, $this->model->getStore());
216+
217+
}
218+
219+
public function testGetShippingAddress()
220+
{
221+
$address = $this->helperManager->getObject('\Magento\Sales\Model\Order\Address', []);
222+
$this->orderMock->expects($this->once())->method('getShippingAddress')->willReturn($address);
223+
$this->assertEquals($address, $this->model->getShippingAddress());
224+
225+
}
226+
227+
/**
228+
* @dataProvider canCaptureDataProvider
229+
* @param string $state
230+
* @param bool|null $canPaymentCapture
231+
* @param bool $expectedResult
232+
*/
233+
public function testCanCapture($state, $canPaymentCapture, $expectedResult)
234+
{
235+
$this->model->setState($state);
236+
if (null !== $canPaymentCapture) {
237+
$this->orderMock->expects($this->once())->method('getPayment')->willReturn($this->paymentMock);
238+
$this->paymentMock->expects($this->once())->method('canCapture')->willReturn($canPaymentCapture);
239+
} else {
240+
$this->orderMock->expects($this->never())->method('getPayment');
241+
$this->paymentMock->expects($this->never())->method('canCapture');
242+
}
243+
$this->assertEquals($expectedResult, $this->model->canCapture());
244+
}
245+
246+
/**
247+
* Data provider for testCanCapture
248+
*
249+
* @return array
250+
*/
251+
public function canCaptureDataProvider()
252+
{
253+
return [
254+
[Invoice::STATE_OPEN, true, true],
255+
[Invoice::STATE_OPEN, false, false],
256+
[Invoice::STATE_CANCELED, null, false],
257+
[Invoice::STATE_CANCELED, null, false],
258+
[Invoice::STATE_PAID, null, false],
259+
[Invoice::STATE_PAID, null, false]
260+
];
261+
}
262+
263+
/**
264+
* @dataProvider canCancelDataProvider
265+
* @param string $state
266+
* @param bool $expectedResult
267+
*/
268+
public function testCanCancel($state, $expectedResult)
269+
{
270+
$this->model->setState($state);
271+
$this->assertEquals($expectedResult, $this->model->canCancel());
272+
}
273+
274+
/**
275+
* Data provider for testCanCancel
276+
*
277+
* @return array
278+
*/
279+
public function canCancelDataProvider()
280+
{
281+
return [
282+
[Invoice::STATE_OPEN, true],
283+
[Invoice::STATE_CANCELED, false],
284+
[Invoice::STATE_PAID, false]
285+
];
286+
}
163287
}

0 commit comments

Comments
 (0)