16
16
use Magento \Sales \Model \Order ;
17
17
use Magento \Sales \Model \Order \Config as OrderConfig ;
18
18
use Magento \Sales \Model \Order \Creditmemo \CreditmemoValidatorInterface ;
19
+ use Magento \Sales \Model \Order \Creditmemo \Item \Validation \CreationQuantityValidator ;
19
20
use Magento \Sales \Model \Order \CreditmemoDocumentFactory ;
20
21
use Magento \Sales \Model \Order \OrderStateResolverInterface ;
21
22
use Magento \Sales \Model \Order \OrderValidatorInterface ;
22
23
use Magento \Sales \Model \Order \PaymentAdapterInterface ;
23
24
use Magento \Sales \Model \Order \Creditmemo \NotifierInterface ;
24
25
use Magento \Sales \Model \RefundOrder ;
25
26
use Psr \Log \LoggerInterface ;
27
+ use Magento \Sales \Api \Data \CreditmemoItemCreationInterface ;
26
28
27
29
/**
28
30
* Class RefundOrderTest
@@ -116,78 +118,80 @@ class RefundOrderTest extends \PHPUnit_Framework_TestCase
116
118
*/
117
119
private $ loggerMock ;
118
120
121
+ /**
122
+ * @var Order\Creditmemo\ItemCreationValidatorInterface|\PHPUnit_Framework_MockObject_MockObject
123
+ */
124
+ private $ itemCreationValidatorMock ;
125
+
126
+ /**
127
+ * @var CreditmemoItemCreationInterface|\PHPUnit_Framework_MockObject_MockObject
128
+ */
129
+ private $ creditmemoItemCreationMock ;
130
+
119
131
protected function setUp ()
120
132
{
121
133
$ this ->resourceConnectionMock = $ this ->getMockBuilder (ResourceConnection::class)
122
134
->disableOriginalConstructor ()
123
135
->getMock ();
124
-
125
136
$ this ->orderRepositoryMock = $ this ->getMockBuilder (OrderRepositoryInterface::class)
126
137
->disableOriginalConstructor ()
127
138
->getMock ();
128
-
129
139
$ this ->creditmemoDocumentFactoryMock = $ this ->getMockBuilder (CreditmemoDocumentFactory::class)
130
140
->disableOriginalConstructor ()
131
141
->getMock ();
132
-
133
142
$ this ->creditmemoValidatorMock = $ this ->getMockBuilder (CreditmemoValidatorInterface::class)
134
143
->disableOriginalConstructor ()
135
144
->getMock ();
136
-
137
145
$ this ->orderValidatorMock = $ this ->getMockBuilder (OrderValidatorInterface::class)
138
146
->disableOriginalConstructor ()
139
147
->getMock ();
140
-
141
148
$ this ->paymentAdapterMock = $ this ->getMockBuilder (PaymentAdapterInterface::class)
142
149
->disableOriginalConstructor ()
143
150
->getMock ();
144
-
145
151
$ this ->orderStateResolverMock = $ this ->getMockBuilder (OrderStateResolverInterface::class)
146
152
->disableOriginalConstructor ()
147
153
->getMockForAbstractClass ();
148
-
149
154
$ this ->configMock = $ this ->getMockBuilder (OrderConfig::class)
150
155
->disableOriginalConstructor ()
151
156
->getMock ();
152
-
153
157
$ this ->creditmemoRepositoryMock = $ this ->getMockBuilder (CreditmemoRepositoryInterface::class)
154
158
->disableOriginalConstructor ()
155
159
->getMockForAbstractClass ();
156
-
157
160
$ this ->notifierMock = $ this ->getMockBuilder (NotifierInterface::class)
158
161
->disableOriginalConstructor ()
159
162
->getMockForAbstractClass ();
160
-
161
163
$ this ->loggerMock = $ this ->getMockBuilder (LoggerInterface::class)
162
164
->disableOriginalConstructor ()
163
165
->getMockForAbstractClass ();
164
-
165
166
$ this ->creditmemoCommentCreationMock = $ this ->getMockBuilder (CreditmemoCommentCreationInterface::class)
166
167
->disableOriginalConstructor ()
167
168
->getMockForAbstractClass ();
168
-
169
169
$ this ->creditmemoCreationArgumentsMock = $ this ->getMockBuilder (CreditmemoCreationArgumentsInterface::class)
170
170
->disableOriginalConstructor ()
171
171
->getMockForAbstractClass ();
172
-
173
172
$ this ->orderMock = $ this ->getMockBuilder (OrderInterface::class)
174
173
->disableOriginalConstructor ()
175
174
->getMockForAbstractClass ();
176
-
177
175
$ this ->creditmemoMock = $ this ->getMockBuilder (CreditmemoInterface::class)
178
176
->disableOriginalConstructor ()
179
177
->getMockForAbstractClass ();
180
-
181
178
$ this ->adapterInterface = $ this ->getMockBuilder (AdapterInterface::class)
182
179
->disableOriginalConstructor ()
183
180
->getMockForAbstractClass ();
181
+ $ this ->itemCreationValidatorMock = $ this ->getMockBuilder (Order \Creditmemo \ItemCreationValidatorInterface::class)
182
+ ->disableOriginalConstructor ()
183
+ ->getMockForAbstractClass ();
184
+ $ this ->creditmemoItemCreationMock = $ this ->getMockBuilder (CreditmemoItemCreationInterface::class)
185
+ ->disableOriginalConstructor ()
186
+ ->getMockForAbstractClass ();
184
187
185
188
$ this ->refundOrder = new RefundOrder (
186
189
$ this ->resourceConnectionMock ,
187
190
$ this ->orderStateResolverMock ,
188
191
$ this ->orderRepositoryMock ,
189
192
$ this ->orderValidatorMock ,
190
193
$ this ->creditmemoValidatorMock ,
194
+ $ this ->itemCreationValidatorMock ,
191
195
$ this ->creditmemoRepositoryMock ,
192
196
$ this ->paymentAdapterMock ,
193
197
$ this ->creditmemoDocumentFactoryMock ,
@@ -200,17 +204,16 @@ protected function setUp()
200
204
/**
201
205
* @dataProvider dataProvider
202
206
*/
203
- public function testOrderCreditmemo ($ orderId , $ items , $ notify , $ appendComment )
207
+ public function testOrderCreditmemo ($ orderId , $ notify , $ appendComment )
204
208
{
209
+ $ items = [$ this ->creditmemoItemCreationMock ];
205
210
$ this ->resourceConnectionMock ->expects ($ this ->once ())
206
211
->method ('getConnection ' )
207
212
->with ('sales ' )
208
213
->willReturn ($ this ->adapterInterface );
209
-
210
214
$ this ->orderRepositoryMock ->expects ($ this ->once ())
211
215
->method ('get ' )
212
216
->willReturn ($ this ->orderMock );
213
-
214
217
$ this ->creditmemoDocumentFactoryMock ->expects ($ this ->once ())
215
218
->method ('createFromOrder ' )
216
219
->with (
@@ -220,7 +223,6 @@ public function testOrderCreditmemo($orderId, $items, $notify, $appendComment)
220
223
($ appendComment && $ notify ),
221
224
$ this ->creditmemoCreationArgumentsMock
222
225
)->willReturn ($ this ->creditmemoMock );
223
-
224
226
$ this ->creditmemoValidatorMock ->expects ($ this ->once ())
225
227
->method ('validate ' )
226
228
->with ($ this ->creditmemoMock )
@@ -229,12 +231,17 @@ public function testOrderCreditmemo($orderId, $items, $notify, $appendComment)
229
231
->method ('validate ' )
230
232
->with ($ this ->orderMock )
231
233
->willReturn ([]);
232
-
234
+ $ this ->itemCreationValidatorMock ->expects ($ this ->once ())
235
+ ->method ('validate ' )
236
+ ->with (
237
+ reset ($ items ),
238
+ [CreationQuantityValidator::class],
239
+ $ this ->orderMock
240
+ )->willReturn ([]);
233
241
$ this ->paymentAdapterMock ->expects ($ this ->once ())
234
242
->method ('refund ' )
235
243
->with ($ this ->creditmemoMock , $ this ->orderMock )
236
244
->willReturn ($ this ->orderMock );
237
-
238
245
$ this ->orderStateResolverMock ->expects ($ this ->once ())
239
246
->method ('getStateForOrder ' )
240
247
->with ($ this ->orderMock , [])
@@ -303,7 +310,7 @@ public function testOrderCreditmemo($orderId, $items, $notify, $appendComment)
303
310
public function testDocumentValidationException ()
304
311
{
305
312
$ orderId = 1 ;
306
- $ items = [1 => 2 ];
313
+ $ items = [$ this -> creditmemoItemCreationMock ];
307
314
$ notify = true ;
308
315
$ appendComment = true ;
309
316
$ errorMessages = ['error1 ' , 'error2 ' ];
@@ -330,6 +337,10 @@ public function testDocumentValidationException()
330
337
->method ('validate ' )
331
338
->with ($ this ->orderMock )
332
339
->willReturn ([]);
340
+ $ this ->itemCreationValidatorMock ->expects ($ this ->once ())
341
+ ->method ('validate ' )
342
+ ->with (reset ($ items ), [CreationQuantityValidator::class], $ this ->orderMock )
343
+ ->willReturn ([]);
333
344
334
345
$ this ->assertEquals (
335
346
$ errorMessages ,
@@ -350,18 +361,16 @@ public function testDocumentValidationException()
350
361
public function testCouldNotCreditmemoException ()
351
362
{
352
363
$ orderId = 1 ;
353
- $ items = [1 => 2 ];
364
+ $ items = [$ this -> creditmemoItemCreationMock ];
354
365
$ notify = true ;
355
366
$ appendComment = true ;
356
367
$ this ->resourceConnectionMock ->expects ($ this ->once ())
357
368
->method ('getConnection ' )
358
369
->with ('sales ' )
359
370
->willReturn ($ this ->adapterInterface );
360
-
361
371
$ this ->orderRepositoryMock ->expects ($ this ->once ())
362
372
->method ('get ' )
363
373
->willReturn ($ this ->orderMock );
364
-
365
374
$ this ->creditmemoDocumentFactoryMock ->expects ($ this ->once ())
366
375
->method ('createFromOrder ' )
367
376
->with (
@@ -371,7 +380,10 @@ public function testCouldNotCreditmemoException()
371
380
($ appendComment && $ notify ),
372
381
$ this ->creditmemoCreationArgumentsMock
373
382
)->willReturn ($ this ->creditmemoMock );
374
-
383
+ $ this ->itemCreationValidatorMock ->expects ($ this ->once ())
384
+ ->method ('validate ' )
385
+ ->with (reset ($ items ), [CreationQuantityValidator::class], $ this ->orderMock )
386
+ ->willReturn ([]);
375
387
$ this ->creditmemoValidatorMock ->expects ($ this ->once ())
376
388
->method ('validate ' )
377
389
->with ($ this ->creditmemoMock )
@@ -381,16 +393,13 @@ public function testCouldNotCreditmemoException()
381
393
->with ($ this ->orderMock )
382
394
->willReturn ([]);
383
395
$ e = new \Exception ();
384
-
385
396
$ this ->paymentAdapterMock ->expects ($ this ->once ())
386
397
->method ('refund ' )
387
398
->with ($ this ->creditmemoMock , $ this ->orderMock )
388
399
->willThrowException ($ e );
389
-
390
400
$ this ->loggerMock ->expects ($ this ->once ())
391
401
->method ('critical ' )
392
402
->with ($ e );
393
-
394
403
$ this ->adapterInterface ->expects ($ this ->once ())
395
404
->method ('rollBack ' );
396
405
@@ -407,8 +416,8 @@ public function testCouldNotCreditmemoException()
407
416
public function dataProvider ()
408
417
{
409
418
return [
410
- 'TestWithNotifyTrue ' => [1 , [ 1 => 2 ], true , true ],
411
- 'TestWithNotifyFalse ' => [1 , [ 1 => 2 ], false , true ],
419
+ 'TestWithNotifyTrue ' => [1 , true , true ],
420
+ 'TestWithNotifyFalse ' => [1 , false , true ],
412
421
];
413
422
}
414
423
}
0 commit comments