Completed
Push — ezp_31440 ( 1f3ef2...0d0002 )
by
unknown
13:18
created

testRenderContentQueryActionWithMinOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
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);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\eZ\Pu...y\SearchService::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<eZ\Publish\API\Repository\SearchService> of property $searchService.

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..

Loading history...
66
        $this->queryTypeRegistry = $this->createMock(QueryTypeRegistry::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\eZ\Pu...eryTypeRegistry::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<eZ\Publish\Core\Q...Type\QueryTypeRegistry> of property $queryTypeRegistry.

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..

Loading history...
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));
0 ignored issues
show
Compatibility introduced by
$query of type object<eZ\Publish\API\Re...y\Values\Content\Query> is not a sub-type of object<eZ\Publish\API\Re...\Content\LocationQuery>. It seems like you assume a child class of the class eZ\Publish\API\Repository\Values\Content\Query to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
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));
0 ignored issues
show
Compatibility introduced by
$query of type object<eZ\Publish\API\Re...y\Values\Content\Query> is not a sub-type of object<eZ\Publish\API\Re...\Content\LocationQuery>. It seems like you assume a child class of the class eZ\Publish\API\Repository\Values\Content\Query to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
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));
0 ignored issues
show
Compatibility introduced by
$query of type object<eZ\Publish\API\Re...y\Values\Content\Query> is not a sub-type of object<eZ\Publish\API\Re...\Content\LocationQuery>. It seems like you assume a child class of the class eZ\Publish\API\Repository\Values\Content\Query to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
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
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
181
            ->method('getQuery')
182
            ->with($options['query']['parameters'] ?? [])
183
            ->willReturn($query);
184
185
        $this->queryTypeRegistry
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<eZ\Publish\Core\Q...Type\QueryTypeRegistry>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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