8
8
9
9
namespace Magento \Sales \Test \Unit \Model \Order ;
10
10
11
+ use Magento \Sales \Model \Order \Invoice ;
11
12
use Magento \Sales \Model \Resource \OrderFactory ;
12
13
13
14
/**
@@ -22,6 +23,17 @@ class InvoiceTest extends \PHPUnit_Framework_TestCase
22
23
*/
23
24
protected $ model ;
24
25
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
+
25
37
/**
26
38
* @var OrderFactory |\PHPUnit_Framework_MockObject_MockObject
27
39
*/
@@ -35,25 +47,50 @@ class InvoiceTest extends \PHPUnit_Framework_TestCase
35
47
/**
36
48
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Sales\Model\Order\Payment
37
49
*/
38
- protected $ _paymentMock ;
50
+ protected $ paymentMock ;
51
+
52
+ /**
53
+ * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Event\ManagerInterface
54
+ */
55
+ protected $ eventManagerMock ;
56
+
57
+ /**
58
+ * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
59
+ */
60
+ protected $ helperManager ;
39
61
40
62
protected function setUp ()
41
63
{
42
- $ helperManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
64
+ $ this -> helperManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
43
65
$ this ->orderMock = $ this ->getMockBuilder (
44
66
'Magento\Sales\Model\Order '
45
67
)->disableOriginalConstructor ()->setMethods (
46
- ['getPayment ' , '__wakeup ' , 'load ' , 'setHistoryEntityName ' ]
68
+ [
69
+ 'getPayment ' , '__wakeup ' , 'load ' , 'setHistoryEntityName ' , 'getStore ' , 'getBillingAddress ' ,
70
+ 'getShippingAddress '
71
+ ]
47
72
)->getMock ();
48
- $ this ->_paymentMock = $ this ->getMockBuilder (
73
+ $ this ->orderMock ->expects ($ this ->any ())
74
+ ->method ('setHistoryEntityName ' )
75
+ ->with ($ this ->entityType )
76
+ ->will ($ this ->returnSelf ());
77
+
78
+ $ this ->paymentMock = $ this ->getMockBuilder (
49
79
'Magento\Sales\Model\Order\Payment '
50
80
)->disableOriginalConstructor ()->setMethods (
51
- ['canVoid ' , '__wakeup ' ]
81
+ ['canVoid ' , '__wakeup ' , ' canCapture ' , ' capture ' , ' pay ' , ' hasForcedState ' , ' getForcedState ' ]
52
82
)->getMock ();
53
83
54
84
$ this ->orderFactory = $ this ->getMock ('Magento\Sales\Model\OrderFactory ' , ['create ' ], [], '' , false );
55
85
86
+ $ this ->eventManagerMock = $ this ->getMock ('\Magento\Framework\Event\ManagerInterface ' , [], [], '' , false );
87
+ $ contextMock = $ this ->getMock ('\Magento\Framework\Model\Context ' , [], [], '' , false );
88
+ $ contextMock ->expects ($ this ->any ())
89
+ ->method ('getEventDispatcher ' )
90
+ ->willReturn ($ this ->eventManagerMock );
91
+
56
92
$ arguments = [
93
+ 'context ' => $ contextMock ,
57
94
'orderFactory ' => $ this ->orderFactory ,
58
95
'orderResourceFactory ' => $ this ->getMock (
59
96
'Magento\Sales\Model\Resource\OrderFactory ' ,
@@ -91,8 +128,9 @@ protected function setUp()
91
128
false
92
129
),
93
130
];
94
- $ this ->model = $ helperManager ->getObject ('Magento\Sales\Model\Order\Invoice ' , $ arguments );
131
+ $ this ->model = $ this -> helperManager ->getObject ('Magento\Sales\Model\Order\Invoice ' , $ arguments );
95
132
$ this ->model ->setOrder ($ this ->orderMock );
133
+ $ this ->modelWithoutOrder = $ this ->helperManager ->getObject ('Magento\Sales\Model\Order\Invoice ' , $ arguments );
96
134
}
97
135
98
136
/**
@@ -101,20 +139,10 @@ protected function setUp()
101
139
*/
102
140
public function testCanVoid ($ canVoid )
103
141
{
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
- );
142
+ $ this ->orderMock ->expects ($ this ->once ())->method ('getPayment ' )->willReturn ($ this ->paymentMock );
143
+ $ this ->paymentMock ->expects ($ this ->once ())
144
+ ->method ('canVoid ' , '__wakeup ' )
145
+ ->willReturn ($ canVoid );
118
146
119
147
$ this ->model ->setState (\Magento \Sales \Model \Order \Invoice::STATE_PAID );
120
148
$ this ->assertEquals ($ canVoid , $ this ->model ->canVoid ());
@@ -139,25 +167,238 @@ public function canVoidDataProvider()
139
167
140
168
public function testGetOrder ()
141
169
{
142
- $ orderId = 100000041 ;
143
- $ this ->model ->setOrderId ($ orderId );
144
- $ entityName = 'invoice ' ;
145
- $ this ->orderMock ->expects ($ this ->atLeastOnce ())
170
+ $ this ->orderMock ->expects ($ this ->once ())
146
171
->method ('setHistoryEntityName ' )
147
- ->with ($ entityName )
172
+ ->with ($ this -> entityType )
148
173
->will ($ this ->returnSelf ());
149
174
150
175
$ this ->assertEquals ($ this ->orderMock , $ this ->model ->getOrder ());
151
176
}
152
177
178
+ public function testGetOrderLoadedById ()
179
+ {
180
+ $ orderId = 100000041 ;
181
+ $ this ->modelWithoutOrder ->setOrderId ($ orderId );
182
+ $ this ->orderMock ->expects ($ this ->once ())
183
+ ->method ('load ' )
184
+ ->with ($ orderId )
185
+ ->willReturnSelf ();
186
+ $ this ->orderMock ->expects ($ this ->once ())
187
+ ->method ('setHistoryEntityName ' )
188
+ ->with ($ this ->entityType )
189
+ ->willReturnSelf ();
190
+ $ this ->orderFactory ->expects ($ this ->once ())
191
+ ->method ('create ' )
192
+ ->willReturn ($ this ->orderMock );
193
+
194
+ $ this ->assertEquals ($ this ->orderMock , $ this ->modelWithoutOrder ->getOrder ());
195
+ }
196
+
153
197
public function testGetEntityType ()
154
198
{
155
- $ this ->assertEquals (' invoice ' , $ this ->model ->getEntityType ());
199
+ $ this ->assertEquals ($ this -> entityType , $ this ->model ->getEntityType ());
156
200
}
157
201
158
202
public function testGetIncrementId ()
159
203
{
160
204
$ this ->model ->setIncrementId ('test_increment_id ' );
161
205
$ this ->assertEquals ('test_increment_id ' , $ this ->model ->getIncrementId ());
162
206
}
207
+
208
+ public function testSetOrder ()
209
+ {
210
+ $ orderId = 1111 ;
211
+ $ storeId = 2221 ;
212
+ $ this ->orderMock ->setId ($ orderId );
213
+ $ this ->orderMock ->setStoreId ($ storeId );
214
+ $ this ->assertNull ($ this ->model ->getOrderId ());
215
+ $ this ->assertNull ($ this ->model ->getStoreId ());
216
+
217
+ $ this ->assertEquals ($ this ->model , $ this ->model ->setOrder ($ this ->orderMock ));
218
+ $ this ->assertEquals ($ this ->orderMock , $ this ->model ->getOrder ());
219
+ $ this ->assertEquals ($ orderId , $ this ->model ->getOrderId ());
220
+ $ this ->assertEquals ($ storeId , $ this ->model ->getStoreId ());
221
+ }
222
+
223
+ public function testGetStore ()
224
+ {
225
+ $ store = $ this ->helperManager ->getObject ('\Magento\Store\Model\Store ' , []);
226
+ $ this ->orderMock ->expects ($ this ->once ())->method ('getStore ' )->willReturn ($ store );
227
+ $ this ->assertEquals ($ store , $ this ->model ->getStore ());
228
+
229
+ }
230
+
231
+ public function testGetShippingAddress ()
232
+ {
233
+ $ address = $ this ->helperManager ->getObject ('\Magento\Sales\Model\Order\Address ' , []);
234
+ $ this ->orderMock ->expects ($ this ->once ())->method ('getShippingAddress ' )->willReturn ($ address );
235
+ $ this ->assertEquals ($ address , $ this ->model ->getShippingAddress ());
236
+
237
+ }
238
+
239
+ /**
240
+ * @dataProvider canCaptureDataProvider
241
+ * @param string $state
242
+ * @param bool|null $canPaymentCapture
243
+ * @param bool $expectedResult
244
+ */
245
+ public function testCanCapture ($ state , $ canPaymentCapture , $ expectedResult )
246
+ {
247
+ $ this ->model ->setState ($ state );
248
+ if (null !== $ canPaymentCapture ) {
249
+ $ this ->orderMock ->expects ($ this ->once ())->method ('getPayment ' )->willReturn ($ this ->paymentMock );
250
+ $ this ->paymentMock ->expects ($ this ->once ())->method ('canCapture ' )->willReturn ($ canPaymentCapture );
251
+ } else {
252
+ $ this ->orderMock ->expects ($ this ->never ())->method ('getPayment ' );
253
+ $ this ->paymentMock ->expects ($ this ->never ())->method ('canCapture ' );
254
+ }
255
+ $ this ->assertEquals ($ expectedResult , $ this ->model ->canCapture ());
256
+ }
257
+
258
+ /**
259
+ * Data provider for testCanCapture
260
+ *
261
+ * @return array
262
+ */
263
+ public function canCaptureDataProvider ()
264
+ {
265
+ return [
266
+ [Invoice::STATE_OPEN , true , true ],
267
+ [Invoice::STATE_OPEN , false , false ],
268
+ [Invoice::STATE_CANCELED , null , false ],
269
+ [Invoice::STATE_CANCELED , null , false ],
270
+ [Invoice::STATE_PAID , null , false ],
271
+ [Invoice::STATE_PAID , null , false ]
272
+ ];
273
+ }
274
+
275
+ /**
276
+ * @dataProvider canCancelDataProvider
277
+ * @param string $state
278
+ * @param bool $expectedResult
279
+ */
280
+ public function testCanCancel ($ state , $ expectedResult )
281
+ {
282
+ $ this ->model ->setState ($ state );
283
+ $ this ->assertEquals ($ expectedResult , $ this ->model ->canCancel ());
284
+ }
285
+
286
+ /**
287
+ * Data provider for testCanCancel
288
+ *
289
+ * @return array
290
+ */
291
+ public function canCancelDataProvider ()
292
+ {
293
+ return [
294
+ [Invoice::STATE_OPEN , true ],
295
+ [Invoice::STATE_CANCELED , false ],
296
+ [Invoice::STATE_PAID , false ]
297
+ ];
298
+ }
299
+
300
+ /**
301
+ * @dataProvider canRefundDataProvider
302
+ * @param string $state
303
+ * @param float $baseGrandTotal
304
+ * @param float $baseTotalRefunded
305
+ * @param bool $expectedResult
306
+ */
307
+ public function testCanRefund ($ state , $ baseGrandTotal , $ baseTotalRefunded , $ expectedResult )
308
+ {
309
+ $ this ->model ->setState ($ state );
310
+ $ this ->model ->setBaseGrandTotal ($ baseGrandTotal );
311
+ $ this ->model ->setBaseTotalRefunded ($ baseTotalRefunded );
312
+ $ this ->assertEquals ($ expectedResult , $ this ->model ->canRefund ());
313
+ }
314
+
315
+ /**
316
+ * Data provider for testCanRefund
317
+ *
318
+ * @return array
319
+ */
320
+ public function canRefundDataProvider ()
321
+ {
322
+ return [
323
+ [Invoice::STATE_OPEN , 0.00 , 0.00 , false ],
324
+ [Invoice::STATE_CANCELED , 1.00 , 0.01 , false ],
325
+ [Invoice::STATE_PAID , 1.00 , 0.00 , true ],
326
+ //[Invoice::STATE_PAID, 1.00, 1.00, false]
327
+ [Invoice::STATE_PAID , 1.000101 , 1.0000 , true ],
328
+ [Invoice::STATE_PAID , 1.0001 , 1.00 , false ],
329
+ [Invoice::STATE_PAID , 1.00 , 1.0001 , false ],
330
+ ];
331
+ }
332
+
333
+ public function testCaptureNotPaid ()
334
+ {
335
+ $ this ->model ->setIsPaid (false );
336
+ $ this ->orderMock ->expects ($ this ->once ())->method ('getPayment ' )->willReturn ($ this ->paymentMock );
337
+ $ this ->paymentMock ->expects ($ this ->once ())->method ('capture ' )->with ($ this ->model )->willReturnSelf ();
338
+ $ this ->paymentMock ->expects ($ this ->never ())->method ('pay ' );
339
+ $ this ->assertEquals ($ this ->model , $ this ->model ->capture ());
340
+ }
341
+
342
+ public function testCapturePaid ()
343
+ {
344
+ $ this ->model ->setIsPaid (true );
345
+ $ this ->orderMock ->expects ($ this ->any ())->method ('getPayment ' )->willReturn ($ this ->paymentMock );
346
+ $ this ->paymentMock ->expects ($ this ->any ())->method ('capture ' )->with ($ this ->model )->willReturnSelf ();
347
+ $ this ->mockPay (false , null );
348
+ $ this ->assertEquals ($ this ->model , $ this ->model ->capture ());
349
+ }
350
+
351
+ public function mockPay ($ hasForcedState , $ forcedState )
352
+ {
353
+ $ this ->orderMock ->expects ($ this ->any ())->method ('getPayment ' )->willReturn ($ this ->paymentMock );
354
+ $ this ->paymentMock ->expects ($ this ->once ())->method ('hasForcedState ' )->willReturn ($ hasForcedState );
355
+ if ($ hasForcedState ) {
356
+ $ this ->paymentMock ->expects ($ this ->once ())->method ('getForcedState ' )->willReturn ($ forcedState );
357
+ } else {
358
+ $ this ->paymentMock ->expects ($ this ->never ())->method ('getForcedState ' );
359
+ }
360
+ $ this ->paymentMock ->expects ($ this ->once ())->method ('pay ' )->with ($ this ->model )->willReturnSelf ();
361
+ $ this ->eventManagerMock
362
+ ->expects ($ this ->once ())
363
+ ->method ('dispatch ' )
364
+ ->with ('sales_order_invoice_pay ' );
365
+ }
366
+
367
+ /**
368
+ * @dataProvider payDataProvider
369
+ * @param bool $hasForcedState
370
+ * @param string|null $forcedState
371
+ * @param float $orderTotalPaid
372
+ * @param float $orderBaseTotalPaid
373
+ * @param float $grandTotal
374
+ * @param float $baseGrandTotal
375
+ * @param float $expectedState
376
+ * @param float $expectedTotalPaid
377
+ * @param float $expectedBaseTotalPaid
378
+ */
379
+ public function testPay ($ hasForcedState , $ forcedState , $ orderTotalPaid , $ orderBaseTotalPaid , $ grandTotal ,
380
+ $ baseGrandTotal , $ expectedState , $ expectedTotalPaid , $ expectedBaseTotalPaid )
381
+ {
382
+ $ this ->mockPay ($ hasForcedState , $ forcedState );
383
+ $ this ->model ->setGrandTotal ($ grandTotal );
384
+ $ this ->model ->setBaseGrandTotal ($ baseGrandTotal );
385
+ $ this ->orderMock ->setTotalPaid ($ orderTotalPaid );
386
+ $ this ->orderMock ->setBaseTotalPaid ($ orderBaseTotalPaid );
387
+ $ this ->assertFalse ($ this ->model ->wasPayCalled ());
388
+ $ this ->assertEquals ($ this ->model , $ this ->model ->pay ());
389
+ $ this ->assertTrue ($ this ->model ->wasPayCalled ());
390
+ $ this ->assertEquals ($ expectedState , $ this ->model ->getState ());
391
+ //$this->assertEquals($expectedTotalPaid, $this->model->getTotalPaid());
392
+ //$this->assertEquals($expectedBaseTotalPaid, $this->model->getBaseTotalPaid());
393
+ #second call of pay() method must do nothing
394
+ $ this ->model ->pay ();
395
+ }
396
+
397
+ public function payDataProvider ()
398
+ {
399
+ //ToDo: fill data provider and uncomment assertings totals in testPay
400
+ return [
401
+ [true , 'payment_state ' , 10.99 , 1.00 , 10.99 , 1.00 , 'payment_state ' , 11.99 , 11.99 ]
402
+ ];
403
+ }
163
404
}
0 commit comments