Skip to content

Commit 987d2d9

Browse files
committed
[Core] Extra unit tests/deprecation fixes
1 parent b5e2f7d commit 987d2d9

1 file changed

Lines changed: 217 additions & 21 deletions

File tree

src/module-elasticsuite-core/Test/Unit/Indexer/GenericIndexerHandlerTest.php

Lines changed: 217 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -380,21 +380,58 @@ public function testDeleteIndexWithMultipleDimensionsEachHavingDifferentStoreIds
380380
$typeName
381381
);
382382

383-
$mockIndexOperation->expects($this->exactly(2))
383+
$invokeCount = $this->exactly(2);
384+
$invocationsCountCallback = 'numberOfInvocations';
385+
if (method_exists($invokeCount, 'getInvocationCount')) {
386+
// Method 'numberOfInvocations' only exists starting from PHPUnit 10.
387+
$invocationsCountCallback = 'getInvocationCount';
388+
}
389+
$mockIndexOperation->expects($invokeCount)
384390
->method('indexExists')
385-
->withConsecutive(
386-
[$indexName, 1],
387-
[$indexName, 2]
388-
)
389-
->willReturn(true);
390-
391-
$mockIndexOperation->expects($this->exactly(2))
391+
/*
392+
* withConsecutive removed in PHPUnit 10 without any alternative \o/.
393+
* ---
394+
* ->withConsecutive(
395+
* [$indexName, 1],
396+
* [$indexName, 2]
397+
* )
398+
* ->willReturn(true);
399+
*/
400+
->willReturnCallback(function (...$expectedParameters) use ($invokeCount, $invocationsCountCallback, $indexName) {
401+
$this->assertEquals($indexName, $expectedParameters[0]);
402+
if ($invokeCount->$invocationsCountCallback() === 1) {
403+
$this->assertEquals(1, $expectedParameters[1]);
404+
}
405+
if ($invokeCount->$invocationsCountCallback() === 2) {
406+
$this->assertEquals(2, $expectedParameters[1]);
407+
}
408+
409+
return true;
410+
});
411+
412+
$invokeCount = $this->exactly(2);
413+
$mockIndexOperation->expects($invokeCount)
392414
->method('getIndexByName')
415+
/*
393416
->withConsecutive(
394417
[$indexName, 1],
395418
[$indexName, 2]
396419
)
397420
->willReturnOnConsecutiveCalls($mockIndex1, $mockIndex2);
421+
*/
422+
->willReturnCallback(
423+
function (...$expectedParameters) use ($invokeCount, $invocationsCountCallback, $indexName, $mockIndex1, $mockIndex2) {
424+
$this->assertEquals($indexName, $expectedParameters[0]);
425+
if ($invokeCount->$invocationsCountCallback() === 1) {
426+
$this->assertEquals(1, $expectedParameters[1]);
427+
}
428+
if ($invokeCount->$invocationsCountCallback() === 2) {
429+
$this->assertEquals(2, $expectedParameters[1]);
430+
}
431+
432+
return ($expectedParameters[1] === 1) ? $mockIndex1 : $mockIndex2;
433+
}
434+
);
398435

399436
$mockIndexOperation->expects($this->exactly(2))
400437
->method('getBatchIndexingSize')
@@ -421,13 +458,37 @@ public function testDeleteIndexWithMultipleDimensionsEachHavingDifferentStoreIds
421458
->method('createBulk')
422459
->willReturnOnConsecutiveCalls($mockBulk1, $mockBulk2);
423460

424-
$mockIndexOperation->expects($this->exactly(2))
461+
$invokeCount = $this->exactly(2);
462+
$mockIndexOperation->expects($invokeCount)
425463
->method('executeBulk')
426-
->withConsecutive([$mockBulk1], [$mockBulk2]);
464+
// ->withConsecutive([$mockBulk1], [$mockBulk2]);
465+
->willReturnCallback(function (...$expectedParameters) use ($invokeCount, $invocationsCountCallback, $mockBulk1, $mockBulk2) {
466+
if ($invokeCount->$invocationsCountCallback() === 1) {
467+
$this->assertEquals($mockBulk1, $expectedParameters[0]);
468+
}
469+
if ($invokeCount->$invocationsCountCallback() === 2) {
470+
$this->assertEquals($mockBulk2, $expectedParameters[0]);
471+
}
427472

428-
$mockIndexOperation->expects($this->exactly(2))
473+
return true;
474+
});
475+
476+
477+
$invokeCount = $this->exactly(2);
478+
$mockIndexOperation->expects($invokeCount)
429479
->method('refreshIndex')
430-
->withConsecutive([$mockIndex1], [$mockIndex2]);
480+
// ->withConsecutive([$mockIndex1], [$mockIndex2]);
481+
->willReturnCallback(function (...$expectedParameters) use ($invokeCount, $invocationsCountCallback, $mockIndex1, $mockIndex2) {
482+
if ($invokeCount->$invocationsCountCallback() === 1) {
483+
$this->assertEquals($mockIndex1, $expectedParameters[0]);
484+
}
485+
if ($invokeCount->$invocationsCountCallback() === 2) {
486+
$this->assertEquals($mockIndex2, $expectedParameters[0]);
487+
}
488+
489+
return true;
490+
});
491+
431492

432493
$result = $genericIndexerHandler->deleteIndex([$dimension1, $dimension2], $documents);
433494

@@ -569,22 +630,68 @@ public function testDeleteIndexSkipsProcessingForSpecificDimensionIfIndexDoesNot
569630
$typeName
570631
);
571632

572-
$mockIndexOperation->expects($this->exactly(3))
633+
$invokeCount = $this->exactly(3);
634+
$invocationsCountCallback = 'numberOfInvocations';
635+
if (method_exists($invokeCount, 'getInvocationCount')) {
636+
// Method 'numberOfInvocations' only exists starting from PHPUnit 10.
637+
$invocationsCountCallback = 'getInvocationCount';
638+
}
639+
$mockIndexOperation->expects($invokeCount)
573640
->method('indexExists')
641+
/*
574642
->withConsecutive(
575643
[$indexName, 1],
576644
[$indexName, 2],
577645
[$indexName, 3]
578646
)
579647
->willReturnOnConsecutiveCalls(true, false, true);
580-
581-
$mockIndexOperation->expects($this->exactly(2))
648+
*/
649+
->willReturnCallback(function (...$expectedParameters) use ($invokeCount, $invocationsCountCallback, $indexName) {
650+
$this->assertEquals($indexName, $expectedParameters[0], 'Index name');
651+
if ($invokeCount->$invocationsCountCallback() === 1) {
652+
$this->assertEquals(1, $expectedParameters[1], 'Store ID');
653+
654+
return true;
655+
}
656+
if ($invokeCount->$invocationsCountCallback() === 2) {
657+
$this->assertEquals(2, $expectedParameters[1], 'Store ID');
658+
659+
return false;
660+
}
661+
if ($invokeCount->$invocationsCountCallback() === 3) {
662+
$this->assertEquals(3, $expectedParameters[1], 'Store ID');
663+
664+
return true;
665+
}
666+
667+
return false;
668+
});
669+
670+
$invokeCount = $this->exactly(2);
671+
$mockIndexOperation->expects($invokeCount)
582672
->method('getIndexByName')
673+
/*
583674
->withConsecutive(
584675
[$indexName, 1],
585676
[$indexName, 3]
586677
)
587678
->willReturnOnConsecutiveCalls($mockIndex1, $mockIndex3);
679+
*/
680+
->willReturnCallback(function (...$expectedParameters) use ($invokeCount, $invocationsCountCallback, $indexName, $mockIndex1, $mockIndex3) {
681+
$this->assertEquals($indexName, $expectedParameters[0], 'Index name');
682+
if ($invokeCount->$invocationsCountCallback() === 1) {
683+
$this->assertEquals(1, $expectedParameters[1], 'Store ID');
684+
685+
return $mockIndex1;
686+
}
687+
if ($invokeCount->$invocationsCountCallback() === 2) {
688+
$this->assertEquals(3, $expectedParameters[1], 'Store ID');
689+
690+
return $mockIndex3;
691+
}
692+
693+
return null;
694+
});
588695

589696
$mockIndexOperation->expects($this->exactly(2))
590697
->method('getBatchIndexingSize')
@@ -611,13 +718,52 @@ public function testDeleteIndexSkipsProcessingForSpecificDimensionIfIndexDoesNot
611718
->method('createBulk')
612719
->willReturnOnConsecutiveCalls($mockBulk1, $mockBulk3);
613720

614-
$mockIndexOperation->expects($this->exactly(2))
721+
$invokeCount = $this->exactly(2);
722+
$mockIndexOperation->expects($invokeCount)
615723
->method('executeBulk')
616-
->withConsecutive([$mockBulk1], [$mockBulk3]);
724+
// ->withConsecutive([$mockBulk1], [$mockBulk3]);
725+
->willReturnCallback(function (...$expectedParameters) use ($invokeCount, $invocationsCountCallback, $mockBulk1, $mockBulk3) {
726+
if ($invokeCount->$invocationsCountCallback() === 1) {
727+
$this->assertEquals($mockBulk1, $expectedParameters[0]);
617728

618-
$mockIndexOperation->expects($this->exactly(2))
729+
return true;
730+
}
731+
if ($invokeCount->$invocationsCountCallback() === 2) {
732+
$this->assertEquals($mockBulk3, $expectedParameters[0]);
733+
734+
return true;
735+
}
736+
737+
return false;
738+
});
739+
740+
/*
741+
$mockIndexOperation->expects($this->once())
742+
->method('refreshIndex')
743+
->with($mockIndex1);
744+
745+
$result = $genericIndexerHandler->deleteIndex([$dimension1, $dimension2, $dimension3], $documents);
746+
747+
$this->assertSame($genericIndexerHandler, $result);
748+
*/
749+
$invokeCount = $this->exactly(2);
750+
$mockIndexOperation->expects($invokeCount)
619751
->method('refreshIndex')
620-
->withConsecutive([$mockIndex1], [$mockIndex3]);
752+
// ->withConsecutive([$mockIndex1], [$mockIndex3]);
753+
->willReturnCallback(function (...$expectedParameters) use ($invokeCount, $invocationsCountCallback, $mockIndex1, $mockIndex3) {
754+
if ($invokeCount->$invocationsCountCallback() === 1) {
755+
$this->assertEquals($mockIndex1, $expectedParameters[0]);
756+
757+
return true;
758+
}
759+
if ($invokeCount->$invocationsCountCallback() === 2) {
760+
$this->assertEquals($mockIndex3, $expectedParameters[0]);
761+
762+
return true;
763+
}
764+
765+
return false;
766+
});
621767

622768
$result = $genericIndexerHandler->deleteIndex([$dimension1, $dimension2, $dimension3], $documents);
623769

@@ -668,14 +814,42 @@ public function testCleanIndexCallsCreateIndexWithCorrectIndexNameAndDimensionVa
668814
$mockIndex2 = $this->getMockIndex();
669815
$mockIndex3 = $this->getMockIndex();
670816

671-
$mockIndexOperation->expects($this->exactly(3))
817+
$invokeCount = $this->exactly(3);
818+
$invocationsCountCallback = 'numberOfInvocations';
819+
if (method_exists($invokeCount, 'getInvocationCount')) {
820+
// Method 'numberOfInvocations' only exists starting from PHPUnit 10.
821+
$invocationsCountCallback = 'getInvocationCount';
822+
}
823+
$mockIndexOperation->expects($invokeCount)
672824
->method('createIndex')
825+
/*
673826
->withConsecutive(
674827
[$indexName, 1],
675828
[$indexName, 2],
676829
[$indexName, 3]
677830
)
678831
->willReturnOnConsecutiveCalls($mockIndex1, $mockIndex2, $mockIndex3);
832+
*/
833+
->willReturnCallback(function (...$expectedParameters) use ($invokeCount, $invocationsCountCallback, $indexName, $mockIndex1, $mockIndex2, $mockIndex3) {
834+
$this->assertEquals($indexName, $expectedParameters[0], 'Index name');
835+
if ($invokeCount->$invocationsCountCallback() === 1) {
836+
$this->assertEquals(1, $expectedParameters[1], 'Store ID');
837+
838+
return $mockIndex1;
839+
}
840+
if ($invokeCount->$invocationsCountCallback() === 2) {
841+
$this->assertEquals(2, $expectedParameters[1], 'Store ID');
842+
843+
return $mockIndex2;
844+
}
845+
if ($invokeCount->$invocationsCountCallback() === 3) {
846+
$this->assertEquals(3, $expectedParameters[1], 'Store ID');
847+
848+
return $mockIndex3;
849+
}
850+
851+
return null;
852+
});
679853

680854
$result = $genericIndexerHandler->cleanIndex([$dimension1, $dimension2, $dimension3]);
681855

@@ -719,8 +893,15 @@ public function testCleanIndexThrowsExceptionWhenCreateIndexFails()
719893
);
720894

721895
$mockIndex1 = $this->getMockIndex();
722-
$mockIndexOperation->expects($this->exactly(2))
896+
$invokeCount = $this->exactly(2);
897+
$invocationsCountCallback = 'numberOfInvocations';
898+
if (method_exists($invokeCount, 'getInvocationCount')) {
899+
// Method 'numberOfInvocations' only exists starting from PHPUnit 10.
900+
$invocationsCountCallback = 'getInvocationCount';
901+
}
902+
$mockIndexOperation->expects($invokeCount)
723903
->method('createIndex')
904+
/*
724905
->withConsecutive(
725906
[$indexName, 1],
726907
[$indexName, 2]
@@ -729,6 +910,21 @@ public function testCleanIndexThrowsExceptionWhenCreateIndexFails()
729910
$mockIndex1,
730911
$this->throwException(new \Exception('Failed to create index'))
731912
);
913+
*/
914+
->willReturnCallback(function (...$expectedParameters) use ($invokeCount, $invocationsCountCallback, $indexName, $mockIndex1) {
915+
$this->assertEquals($indexName, $expectedParameters[0], 'Index name');
916+
if ($invokeCount->$invocationsCountCallback() === 1) {
917+
$this->assertEquals(1, $expectedParameters[1], 'Store ID');
918+
919+
return $mockIndex1;
920+
}
921+
if ($invokeCount->$invocationsCountCallback() === 2) {
922+
$this->assertEquals(2, $expectedParameters[1], 'Store ID');
923+
throw new \Exception('Failed to create index');
924+
}
925+
926+
return null;
927+
});
732928

733929
$this->expectException(\Exception::class);
734930
$this->expectExceptionMessage('Failed to create index');

0 commit comments

Comments
 (0)