Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
27 | class SearchServiceFulltextTest extends BaseTest |
||
28 | { |
||
29 | protected function setUp() |
||
41 | |||
42 | /** |
||
43 | * Create test Content and return Content ID map for subsequent testing. |
||
44 | */ |
||
45 | public function testPrepareContent() |
||
92 | |||
93 | /** |
||
94 | * Return pairs of arguments: |
||
95 | * - search string for testing |
||
96 | * - an array of corresponding Content keys as defined in testPrepareContent() method, |
||
97 | * ordered and grouped by relevancy. |
||
98 | * |
||
99 | * @see testPrepareContent |
||
100 | */ |
||
101 | View Code Duplication | public function providerForTestFulltextSearchSolr6(): array |
|
174 | |||
175 | /** |
||
176 | * Return pairs of arguments: |
||
177 | * - search string for testing |
||
178 | * - an array of corresponding Content keys as defined in testPrepareContent() method, |
||
179 | * ordered and grouped by relevancy. |
||
180 | * |
||
181 | * @see testPrepareContent |
||
182 | */ |
||
183 | View Code Duplication | public function providerForTestFulltextSearchSolr7(): array |
|
256 | |||
257 | /** |
||
258 | * Test for the findContent() method on Solr 6. |
||
259 | * |
||
260 | * @param string $searchString |
||
261 | * @param array $expectedKeys |
||
262 | * @param array $idMap |
||
263 | * |
||
264 | * @depends testPrepareContent |
||
265 | * @dataProvider providerForTestFulltextSearchSolr6 |
||
266 | */ |
||
267 | View Code Duplication | public function testFulltextContentSearchSolr6(string $searchString, array $expectedKeys, array $idMap): void |
|
275 | |||
276 | /** |
||
277 | * Test for the findContent() method on Solr >= 7. |
||
278 | * |
||
279 | * @param string $searchString |
||
280 | * @param array $expectedKeys |
||
281 | * @param array $idMap |
||
282 | * |
||
283 | * @depends testPrepareContent |
||
284 | * @dataProvider providerForTestFulltextSearchSolr7 |
||
285 | */ |
||
286 | View Code Duplication | public function testFulltextContentSearchSolr7(string $searchString, array $expectedKeys, array $idMap): void |
|
294 | |||
295 | private function doTestFulltextContentSearch(string $searchString, array $expectedKeys, array $idMap): void |
||
305 | |||
306 | /** |
||
307 | * Test for the findLocations() method on Solr 6. |
||
308 | * |
||
309 | * @param $searchString |
||
310 | * @param array $expectedKeys |
||
311 | * @param array $idMap |
||
312 | * |
||
313 | * @depends testPrepareContent |
||
314 | * @dataProvider providerForTestFulltextSearchSolr6 |
||
315 | */ |
||
316 | public function testFulltextLocationSearchSolr6($searchString, array $expectedKeys, array $idMap): void |
||
317 | { |
||
318 | if (!$this->isSolrMajorVersionInRange('6.0.0', '7.0.0')) { |
||
319 | $this->markTestSkipped('This test is only relevant for Solr 6'); |
||
320 | } |
||
321 | |||
322 | $this->doTestFulltextLocationSearch($searchString, $expectedKeys, $idMap); |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Test for the findLocations() method on Solr >= 7. |
||
327 | * |
||
328 | * @param $searchString |
||
329 | * @param array $expectedKeys |
||
330 | * @param array $idMap |
||
331 | * |
||
332 | * @depends testPrepareContent |
||
333 | * @dataProvider providerForTestFulltextSearchSolr7 |
||
334 | */ |
||
335 | View Code Duplication | public function testFulltextLocationSearchSolr7($searchString, array $expectedKeys, array $idMap): void |
|
343 | |||
344 | private function doTestFulltextLocationSearch($searchString, array $expectedKeys, array $idMap): void |
||
354 | |||
355 | /** |
||
356 | * Assert given $searchResult using $expectedKeys and $idMap. |
||
357 | * |
||
358 | * @param \eZ\Publish\API\Repository\Values\Content\Search\SearchResult $searchResult |
||
359 | * @param array $expectedKeys |
||
360 | * @param array $idMap |
||
361 | */ |
||
362 | public function assertFulltextSearch(SearchResult $searchResult, array $expectedKeys, array $idMap) |
||
382 | |||
383 | /** |
||
384 | * Map given array of $expectedKeys to Content IDs, using $idMap. |
||
385 | * |
||
386 | * @param array $expectedKeys |
||
387 | * @param array $idMap |
||
388 | * |
||
389 | * @return array |
||
390 | */ |
||
391 | private function mapKeysToIds(array $expectedKeys, array $idMap) |
||
416 | |||
417 | /** |
||
418 | * Map given $searchResult to an array of Content IDs, ordered and grouped by relevancy score. |
||
419 | * |
||
420 | * @param \eZ\Publish\API\Repository\Values\Content\Search\SearchResult $searchResult |
||
421 | * |
||
422 | * @return array |
||
423 | */ |
||
424 | private function mapSearchResultToIds(SearchResult $searchResult) |
||
453 | |||
454 | /** |
||
455 | * Checks if Solr version is in the given range. |
||
456 | * |
||
457 | * @param string $minVersion |
||
458 | * @param string $maxVersion |
||
459 | * |
||
460 | * @return bool |
||
461 | */ |
||
462 | private function isSolrMajorVersionInRange(string $minVersion, string $maxVersion): bool |
||
471 | } |
||
472 |