Skip to content

Commit 4159feb

Browse files
committed
[Compat] Replacing/removing PHPUnit withConsecutive
1 parent 6908da6 commit 4159feb

File tree

1 file changed

+54
-10
lines changed

1 file changed

+54
-10
lines changed

src/module-elasticsuite-thesaurus/Test/Unit/Plugin/QueryRewriteTest.php

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,33 @@ public function testMultipleSearchQueryDepthBuilder()
106106
* ie when the queryBuilder calls itself once for every search terms of the provided array.
107107
*/
108108
$thesaurusIndex->method('getQueryRewrites')->willReturn([]);
109+
/*
110+
* withConsecutive removed in PHPUnit 10 without any alternative \o/.
109111
$thesaurusIndex->expects($this->exactly(2))->method('getQueryRewrites')->withConsecutive(
110112
[$containerConfig, 'foo', 1],
111113
[$containerConfig, 'bar', 1],
112114
);
115+
*/
116+
$invokeCount = $this->exactly(2);
117+
$numberOfInvocationsCallback = 'numberOfInvocations';
118+
if (method_exists($invokeCount, 'getInvocationCount')) {
119+
// Method 'numberOfInvocations' only exists starting from PHPUnit 10.
120+
$numberOfInvocationsCallback = 'getInvocationCount';
121+
}
122+
$thesaurusIndex->expects($invokeCount)->method('getQueryRewrites')->willReturnCallback(
123+
function (...$expectedInputParameters) use ($invokeCount, $containerConfig, $numberOfInvocationsCallback) {
124+
if ($invokeCount->$numberOfInvocationsCallback() === 1) {
125+
$this->assertEquals($containerConfig, $expectedInputParameters[0]);
126+
$this->assertEquals('foo', $expectedInputParameters[1]);
127+
$this->assertEquals(1, $expectedInputParameters[2]);
128+
}
129+
if ($invokeCount->$numberOfInvocationsCallback() === 2) {
130+
$this->assertEquals($containerConfig, $expectedInputParameters[0]);
131+
$this->assertEquals('bar', $expectedInputParameters[1]);
132+
$this->assertEquals(1, $expectedInputParameters[2]);
133+
}
134+
}
135+
);
113136
$initialQuery = $queryBuilderInterceptor->create($containerConfig, ['foo', 'bar'], $spellingType);
114137
$this->assertEquals(QueryInterface::TYPE_BOOL, $initialQuery->getType());
115138

@@ -145,16 +168,39 @@ public function testMultipleSearchQueryDepthBuilderWithRewrites()
145168
$queryRewritePlugin = new QueryRewrite($queryFactory, $thesaurusConfigFactory, $thesaurusIndex);
146169
$queryBuilderInterceptor = $this->getQueryBuilderWithPlugin($queryFactory, $queryRewritePlugin);
147170

171+
/*
172+
* withConsecutive removed in PHPUnit 10 without any alternative \o/.
173+
* ----
148174
$thesaurusIndex->expects($this->exactly(2))->method('getQueryRewrites')->withConsecutive(
149175
[$containerConfig, 'foo', 1],
150176
[$containerConfig, 'bar', 1],
151-
)->willReturnMap(
152-
[
153-
[$containerConfig, 'foo', 1, ['foo bar' => 0.1]],
154-
[$containerConfig, 'bar', 1, ['bar fight' => 0.1]],
155-
]
156-
);
177+
*/
178+
$invokeCount = $this->exactly(2);
179+
$numberOfInvocationsCallback = 'numberOfInvocations';
180+
if (method_exists($invokeCount, 'getInvocationCount')) {
181+
// Method 'numberOfInvocations' only exists starting from PHPUnit 10.
182+
$numberOfInvocationsCallback = 'getInvocationCount';
183+
}
184+
$thesaurusIndex->expects($invokeCount)->method('getQueryRewrites')->willReturnCallback(
185+
function (...$expectedInputParameters) use ($invokeCount, $containerConfig, $numberOfInvocationsCallback) {
186+
if ($invokeCount->$numberOfInvocationsCallback() === 1) {
187+
$this->assertEquals($containerConfig, $expectedInputParameters[0]);
188+
$this->assertEquals('foo', $expectedInputParameters[1]);
189+
$this->assertEquals(1, $expectedInputParameters[2]);
190+
191+
return ['foo bar' => 0.1];
192+
}
193+
if ($invokeCount->$numberOfInvocationsCallback() === 2) {
194+
$this->assertEquals($containerConfig, $expectedInputParameters[0]);
195+
$this->assertEquals('bar', $expectedInputParameters[1]);
196+
$this->assertEquals(1, $expectedInputParameters[2]);
157197

198+
return ['bar fight' => 0.1];
199+
}
200+
201+
return [];
202+
}
203+
);
158204
$query = $queryBuilderInterceptor->create($containerConfig, ['foo', 'bar'], $spellingType);
159205
$this->assertEquals(QueryInterface::TYPE_BOOL, $query->getType());
160206
}
@@ -181,14 +227,12 @@ public function testSingleSearchQueryLimitedRewrites()
181227
->disableOriginalConstructor()
182228
->getMock();
183229

184-
// Passing the mock Query Factory to the plugin to count the occurence of calls to 'create'.
230+
// Passing the mock Query Factory to the plugin to count the occurrence of calls to 'create'.
185231
$queryRewritePlugin = new QueryRewrite($queryFactoryFullMock, $thesaurusConfigFactory, $thesaurusIndex);
186232
// But passing the real Query Factory (with mocked factories) to the query builder itself.
187233
$queryBuilderInterceptor = $this->getQueryBuilderWithPlugin($queryFactory, $queryRewritePlugin);
188234

189-
$thesaurusIndex->expects($this->exactly(1))->method('getQueryRewrites')->withConsecutive(
190-
[$containerConfig, 'foo', 1]
191-
)->willReturnMap(
235+
$thesaurusIndex->expects($this->exactly(1))->method('getQueryRewrites')->willReturnMap(
192236
[
193237
[$containerConfig, 'foo', 1, ['foo bar' => 0.1, 'foo light' => 0.1, 'moo' => 0.1, 'moo bar' => 0.01]],
194238
]

0 commit comments

Comments
 (0)