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,20 +47,33 @@ 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 \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
54
+ */
55
+ protected $ helperManager ;
39
56
40
57
protected function setUp ()
41
58
{
42
- $ helperManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
59
+ $ this -> helperManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
43
60
$ this ->orderMock = $ this ->getMockBuilder (
44
61
'Magento\Sales\Model\Order '
45
62
)->disableOriginalConstructor ()->setMethods (
46
- ['getPayment ' , '__wakeup ' , 'load ' , 'setHistoryEntityName ' ]
63
+ [
64
+ 'getPayment ' , '__wakeup ' , 'load ' , 'setHistoryEntityName ' , 'getStore ' , 'getBillingAddress ' ,
65
+ 'getShippingAddress '
66
+ ]
47
67
)->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 (
49
74
'Magento\Sales\Model\Order\Payment '
50
75
)->disableOriginalConstructor ()->setMethods (
51
- ['canVoid ' , '__wakeup ' ]
76
+ ['canVoid ' , '__wakeup ' , ' canCapture ' ]
52
77
)->getMock ();
53
78
54
79
$ this ->orderFactory = $ this ->getMock ('Magento\Sales\Model\OrderFactory ' , ['create ' ], [], '' , false );
@@ -91,8 +116,9 @@ protected function setUp()
91
116
false
92
117
),
93
118
];
94
- $ this ->model = $ helperManager ->getObject ('Magento\Sales\Model\Order\Invoice ' , $ arguments );
119
+ $ this ->model = $ this -> helperManager ->getObject ('Magento\Sales\Model\Order\Invoice ' , $ arguments );
95
120
$ this ->model ->setOrder ($ this ->orderMock );
121
+ $ this ->modelWithoutOrder = $ this ->helperManager ->getObject ('Magento\Sales\Model\Order\Invoice ' , $ arguments );
96
122
}
97
123
98
124
/**
@@ -101,20 +127,10 @@ protected function setUp()
101
127
*/
102
128
public function testCanVoid ($ canVoid )
103
129
{
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 );
118
134
119
135
$ this ->model ->setState (\Magento \Sales \Model \Order \Invoice::STATE_PAID );
120
136
$ this ->assertEquals ($ canVoid , $ this ->model ->canVoid ());
@@ -139,25 +155,133 @@ public function canVoidDataProvider()
139
155
140
156
public function testGetOrder ()
141
157
{
142
- $ orderId = 100000041 ;
143
- $ this ->model ->setOrderId ($ orderId );
144
- $ entityName = 'invoice ' ;
145
- $ this ->orderMock ->expects ($ this ->atLeastOnce ())
158
+ $ this ->orderMock ->expects ($ this ->once ())
146
159
->method ('setHistoryEntityName ' )
147
- ->with ($ entityName )
160
+ ->with ($ this -> entityType )
148
161
->will ($ this ->returnSelf ());
149
162
150
163
$ this ->assertEquals ($ this ->orderMock , $ this ->model ->getOrder ());
151
164
}
152
165
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
+
153
185
public function testGetEntityType ()
154
186
{
155
- $ this ->assertEquals (' invoice ' , $ this ->model ->getEntityType ());
187
+ $ this ->assertEquals ($ this -> entityType , $ this ->model ->getEntityType ());
156
188
}
157
189
158
190
public function testGetIncrementId ()
159
191
{
160
192
$ this ->model ->setIncrementId ('test_increment_id ' );
161
193
$ this ->assertEquals ('test_increment_id ' , $ this ->model ->getIncrementId ());
162
194
}
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
+ }
163
287
}
0 commit comments