1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace eZ\Publish\Core\Pagination\Pagerfanta\Tests\AdapterFactory; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\SearchService; |
12
|
|
|
use eZ\Publish\API\Repository\Values\Content\LocationQuery; |
13
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query; |
14
|
|
|
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit; |
15
|
|
|
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult; |
16
|
|
|
use eZ\Publish\Core\Pagination\Pagerfanta\AdapterFactory\SearchHitAdapterFactory; |
17
|
|
|
use eZ\Publish\Core\Pagination\Pagerfanta\ContentSearchHitAdapter; |
18
|
|
|
use eZ\Publish\Core\Pagination\Pagerfanta\LocationSearchHitAdapter; |
19
|
|
|
use Pagerfanta\Adapter\FixedAdapter; |
20
|
|
|
use PHPUnit\Framework\TestCase; |
21
|
|
|
|
22
|
|
|
final class SearchHitAdapterFactoryTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
private const EXAMPLE_LANGUAGE_FILTER = [ |
25
|
|
|
'language' => 'eng-GB', |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** @var \eZ\Publish\API\Repository\SearchService|\PHPUnit\Framework\MockObject\MockObject */ |
29
|
|
|
private $searchService; |
30
|
|
|
|
31
|
|
|
/** @var \eZ\Publish\Core\Pagination\Pagerfanta\AdapterFactory\SearchHitAdapterFactory */ |
32
|
|
|
private $searchHitAdapterFactory; |
33
|
|
|
|
34
|
|
|
protected function setUp(): void |
35
|
|
|
{ |
36
|
|
|
$this->searchService = $this->createMock(SearchService::class); |
37
|
|
|
$this->searchHitAdapterFactory = new SearchHitAdapterFactory($this->searchService); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testCreateAdapterForContentQuery(): void |
41
|
|
|
{ |
42
|
|
|
$query = new Query(); |
43
|
|
|
|
44
|
|
|
$this->assertEquals( |
45
|
|
|
new ContentSearchHitAdapter( |
46
|
|
|
$query, |
47
|
|
|
$this->searchService, |
|
|
|
|
48
|
|
|
self::EXAMPLE_LANGUAGE_FILTER |
49
|
|
|
), |
50
|
|
|
$this->searchHitAdapterFactory->createAdapter($query, self::EXAMPLE_LANGUAGE_FILTER) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testCreateAdapterForLocationQuery(): void |
55
|
|
|
{ |
56
|
|
|
$query = new LocationQuery(); |
57
|
|
|
|
58
|
|
|
$this->assertEquals( |
59
|
|
|
new LocationSearchHitAdapter( |
60
|
|
|
$query, |
61
|
|
|
$this->searchService, |
|
|
|
|
62
|
|
|
self::EXAMPLE_LANGUAGE_FILTER |
63
|
|
|
), |
64
|
|
|
$this->searchHitAdapterFactory->createAdapter($query, self::EXAMPLE_LANGUAGE_FILTER) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @dataProvider dataProviderForCreateFixedAdapter |
70
|
|
|
*/ |
71
|
|
|
public function testCreateFixedAdapter(Query $query, string $expectedSearchMethod): void |
72
|
|
|
{ |
73
|
|
|
$hits = [ |
74
|
|
|
new SearchHit(), |
75
|
|
|
new SearchHit(), |
76
|
|
|
new SearchHit(), |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
$this->searchService |
80
|
|
|
->expects($this->once()) |
81
|
|
|
->method($expectedSearchMethod) |
82
|
|
|
->with($query, self::EXAMPLE_LANGUAGE_FILTER) |
83
|
|
|
->willReturn( |
84
|
|
|
new SearchResult([ |
85
|
|
|
'searchHits' => $hits, |
86
|
|
|
'totalCount' => count($hits), |
87
|
|
|
]) |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$this->assertEquals( |
91
|
|
|
new FixedAdapter(count($hits), $hits), |
92
|
|
|
$this->searchHitAdapterFactory->createFixedAdapter($query, self::EXAMPLE_LANGUAGE_FILTER) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function dataProviderForCreateFixedAdapter(): iterable |
97
|
|
|
{ |
98
|
|
|
yield 'content query' => [ |
99
|
|
|
new Query(), |
100
|
|
|
'findContent', |
101
|
|
|
]; |
102
|
|
|
|
103
|
|
|
yield 'location query' => [ |
104
|
|
|
new LocationQuery(), |
105
|
|
|
'findLocations', |
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.