@@ -90,6 +90,10 @@ class DiscountTest extends TestCase
9090 */
9191 private $ rulesApplierMock ;
9292
93+ /**
94+ * @return void
95+ * @throws \PHPUnit\Framework\MockObject\Exception
96+ */
9397 protected function setUp (): void
9498 {
9599 $ this ->storeManagerMock = $ this ->getMockForAbstractClass (StoreManagerInterface::class);
@@ -176,6 +180,9 @@ function ($argument) {
176180 ->willReturn ($ discountData );
177181 }
178182
183+ /**
184+ * @return void
185+ */
179186 public function testCollectItemNoDiscount ()
180187 {
181188 $ itemNoDiscount = $ this ->getMockBuilder (Item::class)
@@ -234,6 +241,9 @@ public function testCollectItemNoDiscount()
234241 );
235242 }
236243
244+ /**
245+ * @return void
246+ */
237247 public function testCollectItemHasParent ()
238248 {
239249 $ itemWithParentId = $ this ->getMockBuilder (Item::class)
@@ -290,6 +300,9 @@ public function testCollectItemHasParent()
290300 );
291301 }
292302
303+ /**
304+ * @return void
305+ */
293306 public function testCollectItemHasNoChildren ()
294307 {
295308 $ itemWithChildren = $ this ->getMockBuilder (Item::class)
@@ -370,6 +383,10 @@ public function testCollectItemHasNoChildren()
370383 );
371384 }
372385
386+ /**
387+ * @return void
388+ * @throws \PHPUnit\Framework\MockObject\Exception
389+ */
373390 public function testFetch ()
374391 {
375392 $ discountAmount = 100 ;
@@ -391,4 +408,71 @@ public function testFetch()
391408 $ totalMock ->expects ($ this ->once ())->method ('getDiscountDescription ' )->willReturn ($ discountDescription );
392409 $ this ->assertEquals ($ expectedResult , $ this ->discount ->fetch ($ quoteMock , $ totalMock ));
393410 }
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+ }
394478}
0 commit comments