|
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\MVC\Symfony\Controller\Tests; |
|
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\Core\MVC\Symfony\Controller\QueryRenderController; |
|
15
|
|
|
use eZ\Publish\Core\MVC\Symfony\View\QueryView; |
|
16
|
|
|
use eZ\Publish\Core\Pagination\Pagerfanta\ContentSearchHitAdapter; |
|
17
|
|
|
use eZ\Publish\Core\Pagination\Pagerfanta\LocationSearchHitAdapter; |
|
18
|
|
|
use eZ\Publish\Core\QueryType\QueryType; |
|
19
|
|
|
use eZ\Publish\Core\QueryType\QueryTypeRegistry; |
|
20
|
|
|
use Pagerfanta\Pagerfanta; |
|
21
|
|
|
use PHPUnit\Framework\TestCase; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
23
|
|
|
|
|
24
|
|
|
final class QueryRenderControllerTest extends TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
private const EXAMPLE_CURRENT_PAGE = 3; |
|
27
|
|
|
private const EXAMPLE_MAX_PER_PAGE = 100; |
|
28
|
|
|
|
|
29
|
|
|
private const MIN_OPTIONS = [ |
|
30
|
|
|
'query' => [ |
|
31
|
|
|
'query_type' => 'ExampleQuery', |
|
32
|
|
|
], |
|
33
|
|
|
'template' => 'example.html.twig', |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
private const ALL_OPTIONS = [ |
|
37
|
|
|
'query' => [ |
|
38
|
|
|
'query_type' => 'ExampleQuery', |
|
39
|
|
|
'parameters' => [ |
|
40
|
|
|
'foo' => 'foo', |
|
41
|
|
|
'bar' => 'bar', |
|
42
|
|
|
'baz' => 'baz', |
|
43
|
|
|
], |
|
44
|
|
|
'assign_results_to' => 'results', |
|
45
|
|
|
], |
|
46
|
|
|
'template' => 'example.html.twig', |
|
47
|
|
|
'pagination' => [ |
|
48
|
|
|
'enabled' => true, |
|
49
|
|
|
'limit' => self::EXAMPLE_MAX_PER_PAGE, |
|
50
|
|
|
'page_param' => 'p', |
|
51
|
|
|
], |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
|
|
/** @var \eZ\Publish\API\Repository\SearchService */ |
|
55
|
|
|
private $searchService; |
|
56
|
|
|
|
|
57
|
|
|
/** @var \eZ\Publish\Core\QueryType\QueryTypeRegistry */ |
|
58
|
|
|
private $queryTypeRegistry; |
|
59
|
|
|
|
|
60
|
|
|
/** @var \eZ\Publish\Core\MVC\Symfony\Controller\QueryRenderController */ |
|
61
|
|
|
private $controller; |
|
62
|
|
|
|
|
63
|
|
|
protected function setUp(): void |
|
64
|
|
|
{ |
|
65
|
|
|
$this->searchService = $this->createMock(SearchService::class); |
|
|
|
|
|
|
66
|
|
|
$this->queryTypeRegistry = $this->createMock(QueryTypeRegistry::class); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
$this->controller = new QueryRenderController( |
|
69
|
|
|
$this->searchService, |
|
70
|
|
|
$this->queryTypeRegistry |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testRenderContentQueryActionWithMinOptions(): void |
|
75
|
|
|
{ |
|
76
|
|
|
$query = $this->configureQueryTypeRegistryMock(self::MIN_OPTIONS); |
|
77
|
|
|
|
|
78
|
|
|
$items = new Pagerfanta(new ContentSearchHitAdapter($query, $this->searchService)); |
|
79
|
|
|
$items->setAllowOutOfRangePages(true); |
|
80
|
|
|
|
|
81
|
|
|
$this->assertContentQueryRenderResult( |
|
82
|
|
|
new QueryView('example.html.twig', [ |
|
83
|
|
|
'items' => $items, |
|
84
|
|
|
]), |
|
85
|
|
|
self::MIN_OPTIONS |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testRenderContentQueryActionWithAllOptions(): void |
|
90
|
|
|
{ |
|
91
|
|
|
$query = $this->configureQueryTypeRegistryMock(self::ALL_OPTIONS); |
|
92
|
|
|
|
|
93
|
|
|
$items = new Pagerfanta(new ContentSearchHitAdapter($query, $this->searchService)); |
|
94
|
|
|
$items->setAllowOutOfRangePages(true); |
|
95
|
|
|
$items->setCurrentPage(self::EXAMPLE_CURRENT_PAGE); |
|
96
|
|
|
$items->setMaxPerPage(self::EXAMPLE_MAX_PER_PAGE); |
|
97
|
|
|
|
|
98
|
|
|
$this->assertContentQueryRenderResult( |
|
99
|
|
|
new QueryView('example.html.twig', [ |
|
100
|
|
|
'results' => $items, |
|
101
|
|
|
]), |
|
102
|
|
|
self::ALL_OPTIONS, |
|
103
|
|
|
new Request(['p' => self::EXAMPLE_CURRENT_PAGE]) |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function testRenderContentInfoQueryActionWithMinOptions(): void |
|
108
|
|
|
{ |
|
109
|
|
|
$query = $this->configureQueryTypeRegistryMock(self::MIN_OPTIONS); |
|
110
|
|
|
|
|
111
|
|
|
$items = new Pagerfanta(new ContentSearchHitAdapter($query, $this->searchService)); |
|
112
|
|
|
$items->setAllowOutOfRangePages(true); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertContentInfoQueryRenderResult( |
|
115
|
|
|
new QueryView('example.html.twig', [ |
|
116
|
|
|
'items' => $items, |
|
117
|
|
|
]), |
|
118
|
|
|
self::MIN_OPTIONS |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function testRenderContentInfoQueryActionWithAllOptions(): void |
|
123
|
|
|
{ |
|
124
|
|
|
$query = $this->configureQueryTypeRegistryMock(self::ALL_OPTIONS, new LocationQuery()); |
|
125
|
|
|
|
|
126
|
|
|
$items = new Pagerfanta(new LocationSearchHitAdapter($query, $this->searchService)); |
|
|
|
|
|
|
127
|
|
|
$items->setAllowOutOfRangePages(true); |
|
128
|
|
|
$items->setCurrentPage(self::EXAMPLE_CURRENT_PAGE); |
|
129
|
|
|
$items->setMaxPerPage(self::EXAMPLE_MAX_PER_PAGE); |
|
130
|
|
|
|
|
131
|
|
|
$this->assertContentInfoQueryRenderResult( |
|
132
|
|
|
new QueryView('example.html.twig', [ |
|
133
|
|
|
'results' => $items, |
|
134
|
|
|
]), |
|
135
|
|
|
self::ALL_OPTIONS, |
|
136
|
|
|
new Request(['p' => self::EXAMPLE_CURRENT_PAGE]) |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function testRenderLocationQueryActionWithMinOptions(): void |
|
141
|
|
|
{ |
|
142
|
|
|
$query = $this->configureQueryTypeRegistryMock(self::MIN_OPTIONS, new LocationQuery()); |
|
143
|
|
|
|
|
144
|
|
|
$items = new Pagerfanta(new LocationSearchHitAdapter($query, $this->searchService)); |
|
|
|
|
|
|
145
|
|
|
$items->setAllowOutOfRangePages(true); |
|
146
|
|
|
|
|
147
|
|
|
$this->assertContentInfoQueryRenderResult( |
|
148
|
|
|
new QueryView('example.html.twig', [ |
|
149
|
|
|
'items' => $items, |
|
150
|
|
|
]), |
|
151
|
|
|
self::MIN_OPTIONS |
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function testRenderLocationQueryActionWithAllOptions(): void |
|
156
|
|
|
{ |
|
157
|
|
|
$query = $this->configureQueryTypeRegistryMock(self::ALL_OPTIONS, new LocationQuery()); |
|
158
|
|
|
|
|
159
|
|
|
$items = new Pagerfanta(new LocationSearchHitAdapter($query, $this->searchService)); |
|
|
|
|
|
|
160
|
|
|
$items->setAllowOutOfRangePages(true); |
|
161
|
|
|
$items->setCurrentPage(self::EXAMPLE_CURRENT_PAGE); |
|
162
|
|
|
$items->setMaxPerPage(self::EXAMPLE_MAX_PER_PAGE); |
|
163
|
|
|
|
|
164
|
|
|
$this->assertLocationQueryRenderResult( |
|
165
|
|
|
new QueryView('example.html.twig', [ |
|
166
|
|
|
'results' => $items, |
|
167
|
|
|
]), |
|
168
|
|
|
self::ALL_OPTIONS, |
|
169
|
|
|
new Request(['p' => self::EXAMPLE_CURRENT_PAGE]) |
|
170
|
|
|
); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
private function configureQueryTypeRegistryMock(array $options, ?Query $query = null): Query |
|
174
|
|
|
{ |
|
175
|
|
|
if ($query === null) { |
|
176
|
|
|
$query = new Query(); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
$queryType = $this->createMock(QueryType::class); |
|
180
|
|
|
$queryType |
|
|
|
|
|
|
181
|
|
|
->method('getQuery') |
|
182
|
|
|
->with($options['query']['parameters'] ?? []) |
|
183
|
|
|
->willReturn($query); |
|
184
|
|
|
|
|
185
|
|
|
$this->queryTypeRegistry |
|
|
|
|
|
|
186
|
|
|
->method('getQueryType') |
|
187
|
|
|
->with($options['query']['query_type'] ?? null) |
|
188
|
|
|
->willReturn($queryType); |
|
189
|
|
|
|
|
190
|
|
|
return $query; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
private function assertContentQueryRenderResult( |
|
194
|
|
|
QueryView $expectedView, |
|
195
|
|
|
array $options, |
|
196
|
|
|
Request $request = null |
|
197
|
|
|
): void { |
|
198
|
|
|
$this->assertEquals( |
|
199
|
|
|
$expectedView, |
|
200
|
|
|
$this->controller->renderContentQueryAction( |
|
201
|
|
|
$request ?? new Request(), |
|
202
|
|
|
$options |
|
203
|
|
|
) |
|
204
|
|
|
); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
private function assertContentInfoQueryRenderResult( |
|
208
|
|
|
QueryView $expectedView, |
|
209
|
|
|
array $options, |
|
210
|
|
|
Request $request = null |
|
211
|
|
|
): void { |
|
212
|
|
|
$this->assertEquals( |
|
213
|
|
|
$expectedView, |
|
214
|
|
|
$this->controller->renderContentInfoQueryAction( |
|
215
|
|
|
$request ?? new Request(), |
|
216
|
|
|
$options |
|
217
|
|
|
) |
|
218
|
|
|
); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
private function assertLocationQueryRenderResult( |
|
222
|
|
|
QueryView $expectedView, |
|
223
|
|
|
array $options, |
|
224
|
|
|
Request $request = null |
|
225
|
|
|
): void { |
|
226
|
|
|
$this->assertEquals( |
|
227
|
|
|
$expectedView, |
|
228
|
|
|
$this->controller->renderLocationQueryAction( |
|
229
|
|
|
$request ?? new Request(), |
|
230
|
|
|
$options |
|
231
|
|
|
) |
|
232
|
|
|
); |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..