@@ -90,6 +90,10 @@ class DiscountTest extends TestCase
90
90
*/
91
91
private $ rulesApplierMock ;
92
92
93
+ /**
94
+ * @return void
95
+ * @throws \PHPUnit\Framework\MockObject\Exception
96
+ */
93
97
protected function setUp (): void
94
98
{
95
99
$ this ->storeManagerMock = $ this ->getMockForAbstractClass (StoreManagerInterface::class);
@@ -176,6 +180,9 @@ function ($argument) {
176
180
->willReturn ($ discountData );
177
181
}
178
182
183
+ /**
184
+ * @return void
185
+ */
179
186
public function testCollectItemNoDiscount ()
180
187
{
181
188
$ itemNoDiscount = $ this ->getMockBuilder (Item::class)
@@ -234,6 +241,9 @@ public function testCollectItemNoDiscount()
234
241
);
235
242
}
236
243
244
+ /**
245
+ * @return void
246
+ */
237
247
public function testCollectItemHasParent ()
238
248
{
239
249
$ itemWithParentId = $ this ->getMockBuilder (Item::class)
@@ -290,6 +300,9 @@ public function testCollectItemHasParent()
290
300
);
291
301
}
292
302
303
+ /**
304
+ * @return void
305
+ */
293
306
public function testCollectItemHasNoChildren ()
294
307
{
295
308
$ itemWithChildren = $ this ->getMockBuilder (Item::class)
@@ -370,6 +383,10 @@ public function testCollectItemHasNoChildren()
370
383
);
371
384
}
372
385
386
+ /**
387
+ * @return void
388
+ * @throws \PHPUnit\Framework\MockObject\Exception
389
+ */
373
390
public function testFetch ()
374
391
{
375
392
$ discountAmount = 100 ;
@@ -391,4 +408,71 @@ public function testFetch()
391
408
$ totalMock ->expects ($ this ->once ())->method ('getDiscountDescription ' )->willReturn ($ discountDescription );
392
409
$ this ->assertEquals ($ expectedResult , $ this ->discount ->fetch ($ quoteMock , $ totalMock ));
393
410
}
411
+
412
+ /**
413
+ * @return void
414
+ * @throws \PHPUnit\Framework\MockObject\Exception
415
+ */
416
+ public function testCollectAddressBaseDiscountAmount (): void
417
+ {
418
+ $ storeId = 1 ;
419
+ $ quote = $ this ->createMock (Quote::class);
420
+ $ quote ->expects ($ this ->once ())->method ('getStoreId ' )->willReturn ($ storeId );
421
+ $ total = $ this ->getMockBuilder (Total::class)
422
+ ->addMethods (
423
+ [
424
+ 'getBaseDiscountAmount '
425
+ ]
426
+ )
427
+ ->disableOriginalConstructor ()
428
+ ->getMock ();
429
+ $ total ->expects ($ this ->any ())->method ('getBaseDiscountAmount ' )->willReturn (20.00 );
430
+
431
+ $ store = $ this ->createMock (Store::class);
432
+ $ this ->storeManagerMock ->expects ($ this ->once ())->method ('getStore ' )->with ($ storeId )->willReturn ($ store );
433
+
434
+ $ rule1 = $ this ->createMock (Rule::class);
435
+ $ rule1 ->expects ($ this ->any ())->method ('getSimpleAction ' )
436
+ ->willReturn (null );
437
+ $ rule2 = $ this ->createMock (Rule::class);
438
+ $ rule2 ->expects ($ this ->any ())->method ('getSimpleAction ' )
439
+ ->willReturn (null );
440
+ $ this ->validatorMock ->expects ($ this ->once ())->method ('getRules ' )
441
+ ->with ($ this ->addressMock )
442
+ ->willReturn ([$ rule1 , $ rule2 ]);
443
+ $ item = $ this ->getMockBuilder (Item::class)
444
+ ->addMethods (['getNoDiscount ' , 'getBaseDiscountAmount ' ])
445
+ ->onlyMethods (['getParentItem ' , 'getId ' , 'getExtensionAttributes ' , 'getAddress ' ])
446
+ ->disableOriginalConstructor ()
447
+ ->getMock ();
448
+ $ item ->expects ($ this ->any ())->method ('getNoDiscount ' )->willReturn (false );
449
+ $ item ->expects ($ this ->any ())->method ('getId ' )->willReturn (1 );
450
+ $ item ->expects ($ this ->any ())->method ('getParentItem ' )->willReturn (false );
451
+ $ item ->expects ($ this ->any ())->method ('getExtensionAttributes ' )->willReturn (false );
452
+ $ item ->expects ($ this ->once ())->method ('getAddress ' )->willReturn ($ this ->addressMock );
453
+ $ index = 1 ;
454
+ $ item ->expects ($ this ->any ())->method ('getBaseDiscountAmount ' )->willReturnCallback (function () use (&$ index ) {
455
+ $ value = $ index * 10 ;
456
+ $ index ++;
457
+ return $ value ;
458
+ });
459
+ $ this ->addressMock ->expects ($ this ->any ())->method ('getAllItems ' )->willReturn ([$ item ]);
460
+ $ this ->shippingAssignmentMock ->expects ($ this ->any ())->method ('getItems ' )->willReturn ([$ item ]);
461
+ $ quote ->expects ($ this ->any ())->method ('getAllAddresses ' )->willReturn ([$ this ->addressMock ]);
462
+ $ this ->validatorMock ->expects ($ this ->any ())->method ('sortItemsByPriority ' )
463
+ ->with ([$ item ], $ this ->addressMock )
464
+ ->willReturnArgument (0 );
465
+
466
+ $ this ->addressMock ->expects ($ this ->exactly (5 ))
467
+ ->method ('setBaseDiscountAmount ' )
468
+ ->with ($ this ->logicalOr (
469
+ $ this ->equalTo (0 ),
470
+ $ this ->equalTo (10 ),
471
+ $ this ->equalTo (20 ),
472
+ $ this ->equalTo (20 ),
473
+ $ this ->equalTo (20.00 )
474
+ ));
475
+
476
+ $ this ->discount ->collect ($ quote , $ this ->shippingAssignmentMock , $ total );
477
+ }
394
478
}
0 commit comments