Completed
Push — 7.5 ( 120086...b4ca1d )
by Łukasz
21:58
created

SearchServiceLocationTest::printResult()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the SearchServiceLocationTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\API\Repository\Tests;
10
11
use eZ\Publish\API\Repository\Tests\SetupFactory\LegacyElasticsearch;
12
use eZ\Publish\Core\Repository\Values\Content\Location;
13
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
14
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
15
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
16
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
17
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
18
use eZ\Publish\API\Repository\Exceptions\NotImplementedException;
19
20
/**
21
 * Test case for Location operations in the SearchService.
22
 *
23
 * @see eZ\Publish\API\Repository\SearchService
24
 * @group integration
25
 * @group search
26
 */
27
class SearchServiceLocationTest extends BaseTest
28
{
29
    const QUERY_CLASS = LocationQuery::class;
30
31
    use Common\FacetedSearchProvider;
32
33
    protected function setUp()
34
    {
35
        $setupFactory = $this->getSetupFactory();
36
        if ($setupFactory instanceof LegacyElasticsearch) {
37
            $this->markTestSkipped('Field Location search is not yet implemented Elasticsearch search engine');
38
        }
39
40
        parent::setUp();
41
    }
42
43
    /**
44
     * Test for the findLocation() method.
45
     *
46
     * @dataProvider getFacetedSearches
47
     * @see \eZ\Publish\API\Repository\SearchService::findLoctions()
48
     */
49
    public function testFindFacetedLocation(LocationQuery $query, $fixture)
50
    {
51
        $this->assertQueryFixture($query, $fixture);
52
    }
53
54
    /**
55
     * Create movie Content with subtitle field set to null.
56
     *
57
     * @return \eZ\Publish\API\Repository\Values\Content\Content[]
58
     */
59
    protected function createMovieContent(): array
60
    {
61
        $movies = [];
62
63
        $repository = $this->getRepository();
64
        $contentTypeService = $repository->getContentTypeService();
65
        $contentService = $repository->getContentService();
66
67
        $createStruct = $contentTypeService->newContentTypeCreateStruct('movie');
68
        $createStruct->mainLanguageCode = 'eng-GB';
69
        $createStruct->remoteId = 'movie-123';
70
        $createStruct->names = ['eng-GB' => 'Movie'];
71
        $createStruct->creatorId = 14;
72
        $createStruct->creationDate = new \DateTime();
73
74
        $fieldTitle = $contentTypeService->newFieldDefinitionCreateStruct('title', 'ezstring');
75
        $fieldTitle->names = ['eng-GB' => 'Title'];
76
        $fieldTitle->fieldGroup = 'main';
77
        $fieldTitle->position = 1;
78
        $fieldTitle->isTranslatable = false;
79
        $fieldTitle->isSearchable = true;
80
        $fieldTitle->isRequired = true;
81
        $createStruct->addFieldDefinition($fieldTitle);
82
83
        $fieldSubtitle = $contentTypeService->newFieldDefinitionCreateStruct('subtitle', 'ezstring');
84
        $fieldSubtitle->names = ['eng-GB' => 'Subtitle'];
85
        $fieldSubtitle->fieldGroup = 'main';
86
        $fieldSubtitle->position = 2;
87
        $fieldSubtitle->isTranslatable = false;
88
        $fieldSubtitle->isSearchable = true;
89
        $fieldSubtitle->isRequired = false;
90
        $createStruct->addFieldDefinition($fieldSubtitle);
91
92
        $contentTypeGroup = $contentTypeService->loadContentTypeGroupByIdentifier('Content');
93
        $contentTypeDraft = $contentTypeService->createContentType($createStruct, [$contentTypeGroup]);
94
        $contentTypeService->publishContentTypeDraft($contentTypeDraft);
95
        $contentType = $contentTypeService->loadContentType($contentTypeDraft->id);
96
97
        $createStructRambo = $contentService->newContentCreateStruct($contentType, 'eng-GB');
98
        $createStructRambo->remoteId = 'movie-456';
99
        $createStructRambo->alwaysAvailable = false;
100
        $createStructRambo->setField('title', 'Rambo');
101
        $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct(2);
102
103
        $ramboDraft = $contentService->createContent($createStructRambo, [$locationCreateStruct]);
104
        $movies[] = $contentService->publishVersion($ramboDraft->getVersionInfo());
105
        $this->refreshSearch($repository);
106
107
        $createStructRobocop = $contentService->newContentCreateStruct($contentType, 'eng-GB');
108
        $createStructRobocop->remoteId = 'movie-789';
109
        $createStructRobocop->alwaysAvailable = false;
110
        $createStructRobocop->setField('title', 'Robocop');
111
        $createStructRobocop->setField('subtitle', '');
112
        $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct(2);
113
114
        $robocopDraft = $contentService->createContent($createStructRobocop, [$locationCreateStruct]);
115
        $movies[] = $contentService->publishVersion($robocopDraft->getVersionInfo());
116
        $this->refreshSearch($repository);
117
118
        $createStructLastHope = $contentService->newContentCreateStruct($contentType, 'eng-GB');
119
        $createStructLastHope->remoteId = 'movie-101112';
120
        $createStructLastHope->alwaysAvailable = false;
121
        $createStructLastHope->setField('title', 'Star Wars');
122
        $createStructLastHope->setField('subtitle', 'Last Hope');
123
        $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct(2);
124
125
        $lastHopeDraft = $contentService->createContent($createStructLastHope, [$locationCreateStruct]);
126
        $movies[] = $contentService->publishVersion($lastHopeDraft->getVersionInfo());
127
        $this->refreshSearch($repository);
128
129
        return $movies;
130
    }
131
132
    /**
133
     * Create test Content with ezcountry field having multiple countries selected.
134
     *
135
     * @return \eZ\Publish\API\Repository\Values\Content\Content
136
     */
137
    protected function createMultipleCountriesContent()
138
    {
139
        $repository = $this->getRepository();
140
        $contentTypeService = $repository->getContentTypeService();
141
        $contentService = $repository->getContentService();
142
143
        $createStruct = $contentTypeService->newContentTypeCreateStruct('countries-multiple');
144
        $createStruct->mainLanguageCode = 'eng-GB';
145
        $createStruct->remoteId = 'countries-multiple-123';
146
        $createStruct->names = ['eng-GB' => 'Multiple countries'];
147
        $createStruct->creatorId = 14;
148
        $createStruct->creationDate = new \DateTime();
149
150
        $fieldCreate = $contentTypeService->newFieldDefinitionCreateStruct('countries', 'ezcountry');
151
        $fieldCreate->names = ['eng-GB' => 'Countries'];
152
        $fieldCreate->fieldGroup = 'main';
153
        $fieldCreate->position = 1;
154
        $fieldCreate->isTranslatable = false;
155
        $fieldCreate->isSearchable = true;
156
        $fieldCreate->fieldSettings = ['isMultiple' => true];
157
158
        $createStruct->addFieldDefinition($fieldCreate);
159
160
        $contentGroup = $contentTypeService->loadContentTypeGroupByIdentifier('Content');
161
        $contentTypeDraft = $contentTypeService->createContentType($createStruct, [$contentGroup]);
162
        $contentTypeService->publishContentTypeDraft($contentTypeDraft);
163
        $contentType = $contentTypeService->loadContentType($contentTypeDraft->id);
164
165
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
166
        $createStruct->remoteId = 'countries-multiple-456';
167
        $createStruct->alwaysAvailable = false;
168
        $createStruct->setField(
169
            'countries',
170
            ['BE', 'DE', 'FR', 'HR', 'NO', 'PT', 'RU']
171
        );
172
173
        $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct(2);
174
        $draft = $contentService->createContent($createStruct, [$locationCreateStruct]);
175
        $content = $contentService->publishVersion($draft->getVersionInfo());
176
177
        $this->refreshSearch($repository);
178
179
        return $content;
180
    }
181
182
    /**
183
     * Test for the findLocations() method.
184
     *
185
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
186
     */
187 View Code Duplication
    public function testFieldIsEmptyInLocation()
188
    {
189
        $testContents = $this->createMovieContent();
190
191
        $query = new LocationQuery(
192
            [
193
                'query' => new Criterion\IsFieldEmpty('subtitle'),
194
            ]
195
        );
196
197
        $repository = $this->getRepository();
198
        $searchService = $repository->getSearchService();
199
        $result = $searchService->findLocations($query);
200
201
        $this->assertEquals(2, $result->totalCount);
202
203
        $this->assertEquals(
204
            $testContents[0]->contentInfo->mainLocationId,
205
            $result->searchHits[0]->valueObject->id
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
206
        );
207
208
        $this->assertEquals(
209
            $testContents[1]->contentInfo->mainLocationId,
210
            $result->searchHits[1]->valueObject->id
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
211
        );
212
    }
213
214
    /**
215
     * Test for the findLocations() method.
216
     *
217
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
218
     */
219 View Code Duplication
    public function testFieldIsNotEmptyInLocation()
220
    {
221
        $testContents = $this->createMovieContent();
222
223
        $query = new LocationQuery(
224
            [
225
                'query' => new Criterion\IsFieldEmpty(
226
                    'subtitle',
227
                    false
228
                ),
229
            ]
230
        );
231
232
        $repository = $this->getRepository();
233
        $searchService = $repository->getSearchService();
234
        $result = $searchService->findLocations($query);
235
236
        $this->assertEquals(1, $result->totalCount);
237
238
        $this->assertEquals(
239
            $testContents[2]->contentInfo->mainLocationId,
240
            $result->searchHits[0]->valueObject->id
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
241
        );
242
    }
243
244
    /**
245
     * Test for the findLocations() method.
246
     *
247
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
248
     */
249 View Code Duplication
    public function testFieldCollectionContains()
250
    {
251
        $testContent = $this->createMultipleCountriesContent();
252
253
        $query = new LocationQuery(
254
            [
255
                'query' => new Criterion\Field(
256
                    'countries',
257
                    Criterion\Operator::CONTAINS,
258
                    'Belgium'
259
                ),
260
            ]
261
        );
262
263
        $repository = $this->getRepository();
264
        $searchService = $repository->getSearchService();
265
        $result = $searchService->findLocations($query);
266
267
        $this->assertEquals(1, $result->totalCount);
268
        $this->assertEquals(
269
            $testContent->contentInfo->mainLocationId,
270
            $result->searchHits[0]->valueObject->id
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
271
        );
272
    }
273
274
    /**
275
     * Test for the findLocations() method.
276
     *
277
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
278
     * @depends eZ\Publish\API\Repository\Tests\SearchServiceTest::testFieldCollectionContains
279
     */
280 View Code Duplication
    public function testFieldCollectionContainsNoMatch()
281
    {
282
        $this->createMultipleCountriesContent();
283
        $query = new LocationQuery(
284
            [
285
                'query' => new Criterion\Field(
286
                    'countries',
287
                    Criterion\Operator::CONTAINS,
288
                    'Netherlands Antilles'
289
                ),
290
            ]
291
        );
292
293
        $repository = $this->getRepository();
294
        $searchService = $repository->getSearchService();
295
        $result = $searchService->findLocations($query);
296
297
        $this->assertEquals(0, $result->totalCount);
298
    }
299
300
    /**
301
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
302
     */
303 View Code Duplication
    public function testInvalidFieldIdentifierRange()
304
    {
305
        $repository = $this->getRepository();
306
        $searchService = $repository->getSearchService();
307
308
        $searchService->findLocations(
309
            new LocationQuery(
310
                [
311
                    'filter' => new Criterion\Field(
312
                        'some_hopefully_unknown_field',
313
                        Criterion\Operator::BETWEEN,
314
                        [10, 1000]
315
                    ),
316
                    'sortClauses' => [new SortClause\ContentId()],
317
                ]
318
            )
319
        );
320
    }
321
322
    /**
323
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
324
     */
325
    public function testInvalidFieldIdentifierIn()
326
    {
327
        $repository = $this->getRepository();
328
        $searchService = $repository->getSearchService();
329
330
        $searchService->findLocations(
331
            new LocationQuery(
332
                [
333
                    'filter' => new Criterion\Field(
334
                        'some_hopefully_unknown_field',
335
                        Criterion\Operator::EQ,
336
                        1000
337
                    ),
338
                    'sortClauses' => [new SortClause\ContentId()],
339
                ]
340
            )
341
        );
342
    }
343
344
    /**
345
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
346
     */
347
    public function testFindLocationsWithNonSearchableField()
348
    {
349
        $repository = $this->getRepository();
350
        $searchService = $repository->getSearchService();
351
352
        $searchService->findLocations(
353
            new LocationQuery(
354
                [
355
                    'filter' => new Criterion\Field(
356
                        'tag_cloud_url',
357
                        Criterion\Operator::EQ,
358
                        'http://nimbus.com'
359
                    ),
360
                    'sortClauses' => [new SortClause\ContentId()],
361
                ]
362
            )
363
        );
364
    }
365
366
    /**
367
     * @param \eZ\Publish\API\Repository\Values\Content\Search\SearchResult $result
368
     *
369
     * @return array
370
     */
371
    protected function mapResultLocationIds(SearchResult $result)
372
    {
373
        return array_map(
374
            function (SearchHit $searchHit) {
375
                return $searchHit->valueObject->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
376
            },
377
            $result->searchHits
378
        );
379
    }
380
381
    /**
382
     * Test for the findLocations() method.
383
     *
384
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
385
     */
386 View Code Duplication
    public function testQueryCustomField()
387
    {
388
        $query = new LocationQuery(
389
            [
390
                'query' => new Criterion\CustomField(
391
                    'custom_field',
392
                    Criterion\Operator::EQ,
393
                    'AdMiNiStRaToR'
394
                ),
395
                'offset' => 0,
396
                'limit' => 10,
397
                'sortClauses' => [new SortClause\ContentId()],
398
            ]
399
        );
400
        $this->assertQueryFixture(
401
            $query,
402
            $this->getFixtureDir() . '/QueryCustomField.php',
403
            null,
404
            true
405
        );
406
    }
407
408
    /**
409
     * Test for the findLocations() method.
410
     *
411
     * This tests explicitly queries the first_name while user is contained in
412
     * the last_name of admin and anonymous. This is done to show the custom
413
     * copy field working.
414
     *
415
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
416
     */
417 View Code Duplication
    public function testQueryModifiedField()
418
    {
419
        // Check using get_class since the others extend SetupFactory\Legacy
420
        if (ltrim(get_class($this->getSetupFactory()), '\\') === 'eZ\Publish\API\Repository\Tests\SetupFactory\Legacy') {
421
            $this->markTestIncomplete(
422
                'Custom fields not supported by LegacySE ' .
423
                '(@todo: Legacy should fallback to just querying normal field so this should be tested here)'
424
            );
425
        }
426
427
        $query = new LocationQuery(
428
            [
429
                'query' => new Criterion\Field(
430
                    'first_name',
431
                    Criterion\Operator::EQ,
432
                    'User'
433
                ),
434
                'offset' => 0,
435
                'limit' => 10,
436
                'sortClauses' => [new SortClause\ContentId()],
437
            ]
438
        );
439
        $query->query->setCustomField('user', 'first_name', 'custom_field');
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class eZ\Publish\API\Repositor...Content\Query\Criterion as the method setCustomField() does only exist in the following sub-classes of eZ\Publish\API\Repositor...Content\Query\Criterion: eZ\Publish\API\Repositor...t\Query\Criterion\Field, eZ\Publish\API\Repositor...uery\Criterion\FullText, eZ\Publish\API\Repositor...ion\MapLocationDistance. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
440
441
        $this->assertQueryFixture(
442
            $query,
443
            $this->getFixtureDir() . '/QueryModifiedField.php',
444
            null,
445
            true
446
        );
447
    }
448
449
    /**
450
     * @return \eZ\Publish\API\Repository\Values\ContentType\ContentType
451
     */
452 View Code Duplication
    protected function createTestPlaceContentType()
453
    {
454
        $repository = $this->getRepository();
455
        $contentTypeService = $repository->getContentTypeService();
456
457
        $createStruct = $contentTypeService->newContentTypeCreateStruct('testtype');
458
        $createStruct->mainLanguageCode = 'eng-GB';
459
        $createStruct->names = ['eng-GB' => 'Test type'];
460
        $createStruct->creatorId = 14;
461
        $createStruct->creationDate = new \DateTime();
462
463
        $translatableFieldCreate = $contentTypeService->newFieldDefinitionCreateStruct('maplocation', 'ezgmaplocation');
464
        $translatableFieldCreate->names = ['eng-GB' => 'Map location field'];
465
        $translatableFieldCreate->fieldGroup = 'main';
466
        $translatableFieldCreate->position = 1;
467
        $translatableFieldCreate->isTranslatable = false;
468
        $translatableFieldCreate->isSearchable = true;
469
470
        $createStruct->addFieldDefinition($translatableFieldCreate);
471
472
        $contentGroup = $contentTypeService->loadContentTypeGroupByIdentifier('Content');
473
        $contentTypeDraft = $contentTypeService->createContentType($createStruct, [$contentGroup]);
474
        $contentTypeService->publishContentTypeDraft($contentTypeDraft);
475
        $contentType = $contentTypeService->loadContentType($contentTypeDraft->id);
476
477
        return $contentType;
478
    }
479
480
    /**
481
     * Test for the findLocations() method.
482
     *
483
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
484
     * @group maplocation
485
     */
486 View Code Duplication
    public function testMapLocationDistanceLessThanOrEqual()
487
    {
488
        $contentType = $this->createTestPlaceContentType();
489
490
        // Create a draft to account for behaviour with ContentType in different states
491
        $repository = $this->getRepository();
492
        $contentTypeService = $repository->getContentTypeService();
493
        $contentService = $repository->getContentService();
494
        $contentTypeService->createContentTypeDraft($contentType);
495
        $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct(2);
496
497
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
498
        $createStruct->alwaysAvailable = false;
499
        $createStruct->mainLanguageCode = 'eng-GB';
500
        $createStruct->setField(
501
            'maplocation',
502
            [
503
                'latitude' => 45.894877,
504
                'longitude' => 15.972699,
505
                'address' => 'Here be wild boars',
506
            ],
507
            'eng-GB'
508
        );
509
510
        $draft = $contentService->createContent($createStruct, [$locationCreateStruct]);
511
        $wildBoars = $contentService->publishVersion($draft->getVersionInfo());
512
513
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
514
        $createStruct->alwaysAvailable = false;
515
        $createStruct->mainLanguageCode = 'eng-GB';
516
        $createStruct->setField(
517
            'maplocation',
518
            [
519
                'latitude' => 45.927334,
520
                'longitude' => 15.934847,
521
                'address' => 'A lone tree',
522
            ],
523
            'eng-GB'
524
        );
525
526
        $draft = $contentService->createContent($createStruct, [$locationCreateStruct]);
527
        $tree = $contentService->publishVersion($draft->getVersionInfo());
0 ignored issues
show
Unused Code introduced by
$tree is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
528
529
        $this->refreshSearch($repository);
530
531
        $query = new LocationQuery(
532
            [
533
                'filter' => new Criterion\LogicalAnd(
534
                    [
535
                        new Criterion\ContentTypeId($contentType->id),
536
                        new Criterion\MapLocationDistance(
537
                            'maplocation',
538
                            Criterion\Operator::LTE,
539
                            240,
540
                            43.756825,
541
                            15.775074
542
                        ),
543
                    ]
544
                ),
545
                'offset' => 0,
546
                'limit' => 10,
547
                'sortClauses' => [],
548
            ]
549
        );
550
551
        $searchService = $repository->getSearchService();
552
        $result = $searchService->findLocations($query);
553
554
        $this->assertEquals(1, $result->totalCount);
555
        $this->assertEquals(
556
            $wildBoars->contentInfo->mainLocationId,
557
            $result->searchHits[0]->valueObject->id
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
558
        );
559
    }
560
561
    /**
562
     * Test for the findLocations() method.
563
     *
564
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
565
     * @group maplocation
566
     */
567 View Code Duplication
    public function testMapLocationDistanceGreaterThanOrEqual()
568
    {
569
        $contentType = $this->createTestPlaceContentType();
570
571
        // Create a draft to account for behaviour with ContentType in different states
572
        $repository = $this->getRepository();
573
        $contentTypeService = $repository->getContentTypeService();
574
        $contentService = $repository->getContentService();
575
        $contentTypeService->createContentTypeDraft($contentType);
576
        $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct(2);
577
578
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
579
        $createStruct->alwaysAvailable = false;
580
        $createStruct->mainLanguageCode = 'eng-GB';
581
        $createStruct->setField(
582
            'maplocation',
583
            [
584
                'latitude' => 45.894877,
585
                'longitude' => 15.972699,
586
                'address' => 'Here be wild boars',
587
            ],
588
            'eng-GB'
589
        );
590
591
        $draft = $contentService->createContent($createStruct, [$locationCreateStruct]);
592
        $wildBoars = $contentService->publishVersion($draft->getVersionInfo());
0 ignored issues
show
Unused Code introduced by
$wildBoars is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
593
594
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
595
        $createStruct->alwaysAvailable = false;
596
        $createStruct->mainLanguageCode = 'eng-GB';
597
        $createStruct->setField(
598
            'maplocation',
599
            [
600
                'latitude' => 45.927334,
601
                'longitude' => 15.934847,
602
                'address' => 'A lone tree',
603
            ],
604
            'eng-GB'
605
        );
606
607
        $draft = $contentService->createContent($createStruct, [$locationCreateStruct]);
608
        $tree = $contentService->publishVersion($draft->getVersionInfo());
609
610
        $this->refreshSearch($repository);
611
612
        $query = new LocationQuery(
613
            [
614
                'filter' => new Criterion\LogicalAnd(
615
                    [
616
                        new Criterion\ContentTypeId($contentType->id),
617
                        new Criterion\MapLocationDistance(
618
                            'maplocation',
619
                            Criterion\Operator::GTE,
620
                            240,
621
                            43.756825,
622
                            15.775074
623
                        ),
624
                    ]
625
                ),
626
                'offset' => 0,
627
                'limit' => 10,
628
                'sortClauses' => [],
629
            ]
630
        );
631
632
        $searchService = $repository->getSearchService();
633
        $result = $searchService->findLocations($query);
634
635
        $this->assertEquals(1, $result->totalCount);
636
        $this->assertEquals(
637
            $tree->contentInfo->mainLocationId,
638
            $result->searchHits[0]->valueObject->id
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
639
        );
640
    }
641
642
    /**
643
     * Test for the findLocations() method.
644
     *
645
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
646
     * @group maplocation
647
     */
648
    public function testMapLocationDistanceBetween()
649
    {
650
        $contentType = $this->createTestPlaceContentType();
651
652
        // Create a draft to account for behaviour with ContentType in different states
653
        $repository = $this->getRepository();
654
        $contentTypeService = $repository->getContentTypeService();
655
        $contentService = $repository->getContentService();
656
        $contentTypeService->createContentTypeDraft($contentType);
657
        $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct(2);
658
659
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
660
        $createStruct->alwaysAvailable = false;
661
        $createStruct->mainLanguageCode = 'eng-GB';
662
        $createStruct->setField(
663
            'maplocation',
664
            [
665
                'latitude' => 45.894877,
666
                'longitude' => 15.972699,
667
                'address' => 'Here be wild boars',
668
            ],
669
            'eng-GB'
670
        );
671
672
        $draft = $contentService->createContent($createStruct, [$locationCreateStruct]);
673
        $wildBoars = $contentService->publishVersion($draft->getVersionInfo());
0 ignored issues
show
Unused Code introduced by
$wildBoars is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
674
675
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
676
        $createStruct->alwaysAvailable = false;
677
        $createStruct->mainLanguageCode = 'eng-GB';
678
        $createStruct->setField(
679
            'maplocation',
680
            [
681
                'latitude' => 45.927334,
682
                'longitude' => 15.934847,
683
                'address' => 'A lone tree',
684
            ],
685
            'eng-GB'
686
        );
687
688
        $draft = $contentService->createContent($createStruct, [$locationCreateStruct]);
689
        $tree = $contentService->publishVersion($draft->getVersionInfo());
0 ignored issues
show
Unused Code introduced by
$tree is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
690
691
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
692
        $createStruct->alwaysAvailable = false;
693
        $createStruct->mainLanguageCode = 'eng-GB';
694
        $createStruct->setField(
695
            'maplocation',
696
            [
697
                'latitude' => 45.903777,
698
                'longitude' => 15.958788,
699
                'address' => 'Meadow with mushrooms',
700
            ],
701
            'eng-GB'
702
        );
703
704
        $draft = $contentService->createContent($createStruct, [$locationCreateStruct]);
705
        $mushrooms = $contentService->publishVersion($draft->getVersionInfo());
706
707
        $this->refreshSearch($repository);
708
709
        $query = new LocationQuery(
710
            [
711
                'filter' => new Criterion\LogicalAnd(
712
                    [
713
                        new Criterion\ContentTypeId($contentType->id),
714
                        new Criterion\MapLocationDistance(
715
                            'maplocation',
716
                            Criterion\Operator::BETWEEN,
717
                            [239, 241],
718
                            43.756825,
719
                            15.775074
720
                        ),
721
                    ]
722
                ),
723
                'offset' => 0,
724
                'limit' => 10,
725
                'sortClauses' => [],
726
            ]
727
        );
728
729
        $searchService = $repository->getSearchService();
730
        $result = $searchService->findLocations($query);
731
732
        $this->assertEquals(1, $result->totalCount);
733
        $this->assertEquals(
734
            $mushrooms->contentInfo->mainLocationId,
735
            $result->searchHits[0]->valueObject->id
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
736
        );
737
    }
738
739
    /**
740
     * Test for the findLocations() method.
741
     *
742
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
743
     * @group maplocation
744
     */
745 View Code Duplication
    public function testMapLocationDistanceSortAscending()
746
    {
747
        $contentType = $this->createTestPlaceContentType();
748
749
        // Create a draft to account for behaviour with ContentType in different states
750
        $repository = $this->getRepository();
751
        $contentTypeService = $repository->getContentTypeService();
752
        $contentService = $repository->getContentService();
753
        $contentTypeService->createContentTypeDraft($contentType);
754
        $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct(2);
755
756
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
757
        $createStruct->alwaysAvailable = false;
758
        $createStruct->mainLanguageCode = 'eng-GB';
759
        $createStruct->setField(
760
            'maplocation',
761
            [
762
                'latitude' => 45.894877,
763
                'longitude' => 15.972699,
764
                'address' => 'Here be wild boars',
765
            ],
766
            'eng-GB'
767
        );
768
769
        $draft = $contentService->createContent($createStruct, [$locationCreateStruct]);
770
        $wildBoars = $contentService->publishVersion($draft->getVersionInfo());
771
772
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
773
        $createStruct->alwaysAvailable = false;
774
        $createStruct->mainLanguageCode = 'eng-GB';
775
        $createStruct->setField(
776
            'maplocation',
777
            [
778
                'latitude' => 45.927334,
779
                'longitude' => 15.934847,
780
                'address' => 'A lone tree',
781
            ],
782
            'eng-GB'
783
        );
784
785
        $draft = $contentService->createContent($createStruct, [$locationCreateStruct]);
786
        $tree = $contentService->publishVersion($draft->getVersionInfo());
787
788
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
789
        $createStruct->alwaysAvailable = false;
790
        $createStruct->mainLanguageCode = 'eng-GB';
791
        $createStruct->setField(
792
            'maplocation',
793
            [
794
                'latitude' => 45.903777,
795
                'longitude' => 15.958788,
796
                'address' => 'Meadow with mushrooms',
797
            ],
798
            'eng-GB'
799
        );
800
801
        $draft = $contentService->createContent($createStruct, [$locationCreateStruct]);
802
        $mushrooms = $contentService->publishVersion($draft->getVersionInfo());
803
804
        $this->refreshSearch($repository);
805
806
        $wellInVodice = [
807
            'latitude' => 43.756825,
808
            'longitude' => 15.775074,
809
        ];
810
811
        $query = new LocationQuery(
812
            [
813
                'filter' => new Criterion\LogicalAnd(
814
                    [
815
                        new Criterion\ContentTypeId($contentType->id),
816
                        new Criterion\MapLocationDistance(
817
                            'maplocation',
818
                            Criterion\Operator::GTE,
819
                            235,
820
                            $wellInVodice['latitude'],
821
                            $wellInVodice['longitude']
822
                        ),
823
                    ]
824
                ),
825
                'offset' => 0,
826
                'limit' => 10,
827
                'sortClauses' => [
828
                    new SortClause\MapLocationDistance(
829
                        'testtype',
830
                        'maplocation',
831
                        $wellInVodice['latitude'],
832
                        $wellInVodice['longitude'],
833
                        LocationQuery::SORT_ASC
834
                    ),
835
                ],
836
            ]
837
        );
838
839
        $searchService = $repository->getSearchService();
840
        $result = $searchService->findLocations($query);
841
842
        $this->assertEquals(3, $result->totalCount);
843
        $this->assertEquals(
844
            $wildBoars->contentInfo->mainLocationId,
845
            $result->searchHits[0]->valueObject->id
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
846
        );
847
        $this->assertEquals(
848
            $mushrooms->contentInfo->mainLocationId,
849
            $result->searchHits[1]->valueObject->id
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
850
        );
851
        $this->assertEquals(
852
            $tree->contentInfo->mainLocationId,
853
            $result->searchHits[2]->valueObject->id
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
854
        );
855
    }
856
857
    /**
858
     * Test for the findLocations() method.
859
     *
860
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
861
     * @group maplocation
862
     */
863 View Code Duplication
    public function testMapLocationDistanceSortDescending()
864
    {
865
        $contentType = $this->createTestPlaceContentType();
866
867
        // Create a draft to account for behaviour with ContentType in different states
868
        $repository = $this->getRepository();
869
        $contentTypeService = $repository->getContentTypeService();
870
        $contentService = $repository->getContentService();
871
        $contentTypeService->createContentTypeDraft($contentType);
872
        $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct(2);
873
874
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
875
        $createStruct->alwaysAvailable = false;
876
        $createStruct->mainLanguageCode = 'eng-GB';
877
        $createStruct->setField(
878
            'maplocation',
879
            [
880
                'latitude' => 45.894877,
881
                'longitude' => 15.972699,
882
                'address' => 'Here be wild boars',
883
            ],
884
            'eng-GB'
885
        );
886
887
        $draft = $contentService->createContent($createStruct, [$locationCreateStruct]);
888
        $wildBoars = $contentService->publishVersion($draft->getVersionInfo());
889
890
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
891
        $createStruct->alwaysAvailable = false;
892
        $createStruct->mainLanguageCode = 'eng-GB';
893
        $createStruct->setField(
894
            'maplocation',
895
            [
896
                'latitude' => 45.927334,
897
                'longitude' => 15.934847,
898
                'address' => 'A lone tree',
899
            ],
900
            'eng-GB'
901
        );
902
903
        $draft = $contentService->createContent($createStruct, [$locationCreateStruct]);
904
        $tree = $contentService->publishVersion($draft->getVersionInfo());
905
906
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
907
        $createStruct->alwaysAvailable = false;
908
        $createStruct->mainLanguageCode = 'eng-GB';
909
        $createStruct->setField(
910
            'maplocation',
911
            [
912
                'latitude' => 45.903777,
913
                'longitude' => 15.958788,
914
                'address' => 'Meadow with mushrooms',
915
            ],
916
            'eng-GB'
917
        );
918
919
        $draft = $contentService->createContent($createStruct, [$locationCreateStruct]);
920
        $mushrooms = $contentService->publishVersion($draft->getVersionInfo());
921
922
        $this->refreshSearch($repository);
923
924
        $well = [
925
            'latitude' => 43.756825,
926
            'longitude' => 15.775074,
927
        ];
928
929
        $query = new LocationQuery(
930
            [
931
                'filter' => new Criterion\LogicalAnd(
932
                    [
933
                        new Criterion\ContentTypeId($contentType->id),
934
                        new Criterion\MapLocationDistance(
935
                            'maplocation',
936
                            Criterion\Operator::GTE,
937
                            235,
938
                            $well['latitude'],
939
                            $well['longitude']
940
                        ),
941
                    ]
942
                ),
943
                'offset' => 0,
944
                'limit' => 10,
945
                'sortClauses' => [
946
                    new SortClause\MapLocationDistance(
947
                        'testtype',
948
                        'maplocation',
949
                        $well['latitude'],
950
                        $well['longitude'],
951
                        LocationQuery::SORT_DESC
952
                    ),
953
                ],
954
            ]
955
        );
956
957
        $searchService = $repository->getSearchService();
958
        $result = $searchService->findLocations($query);
959
960
        $this->assertEquals(3, $result->totalCount);
961
        $this->assertEquals(
962
            $wildBoars->contentInfo->mainLocationId,
963
            $result->searchHits[2]->valueObject->id
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
964
        );
965
        $this->assertEquals(
966
            $mushrooms->contentInfo->mainLocationId,
967
            $result->searchHits[1]->valueObject->id
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
968
        );
969
        $this->assertEquals(
970
            $tree->contentInfo->mainLocationId,
971
            $result->searchHits[0]->valueObject->id
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
972
        );
973
    }
974
975
    /**
976
     * Test for the findLocations() method.
977
     *
978
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
979
     * @group maplocation
980
     */
981
    public function testMapLocationDistanceWithCustomField()
982
    {
983
        $contentType = $this->createTestPlaceContentType();
984
985
        // Create a draft to account for behaviour with ContentType in different states
986
        $repository = $this->getRepository();
987
        $contentTypeService = $repository->getContentTypeService();
988
        $contentService = $repository->getContentService();
989
        $contentTypeService->createContentTypeDraft($contentType);
990
        $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct(2);
991
992
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
993
        $createStruct->alwaysAvailable = false;
994
        $createStruct->mainLanguageCode = 'eng-GB';
995
        $createStruct->setField(
996
            'maplocation',
997
            [
998
                'latitude' => 45.894877,
999
                'longitude' => 15.972699,
1000
                'address' => 'Here be wild boars',
1001
            ],
1002
            'eng-GB'
1003
        );
1004
1005
        $draft = $contentService->createContent($createStruct, [$locationCreateStruct]);
1006
        $wildBoars = $contentService->publishVersion($draft->getVersionInfo());
1007
1008
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
1009
        $createStruct->alwaysAvailable = false;
1010
        $createStruct->mainLanguageCode = 'eng-GB';
1011
        $createStruct->setField(
1012
            'maplocation',
1013
            [
1014
                'latitude' => 45.927334,
1015
                'longitude' => 15.934847,
1016
                'address' => 'A lone tree',
1017
            ],
1018
            'eng-GB'
1019
        );
1020
1021
        $draft = $contentService->createContent($createStruct, [$locationCreateStruct]);
1022
        $tree = $contentService->publishVersion($draft->getVersionInfo());
0 ignored issues
show
Unused Code introduced by
$tree is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1023
1024
        $this->refreshSearch($repository);
1025
1026
        $distanceCriterion = new Criterion\MapLocationDistance(
1027
            'maplocation',
1028
            Criterion\Operator::LTE,
1029
            240,
1030
            43.756825,
1031
            15.775074
1032
        );
1033
        $distanceCriterion->setCustomField('testtype', 'maplocation', 'custom_geolocation_field');
1034
1035
        $query = new LocationQuery(
1036
            [
1037
                'filter' => new Criterion\LogicalAnd(
1038
                    [
1039
                        new Criterion\ContentTypeId($contentType->id),
1040
                        $distanceCriterion,
1041
                    ]
1042
                ),
1043
                'offset' => 0,
1044
                'limit' => 10,
1045
                'sortClauses' => [],
1046
            ]
1047
        );
1048
1049
        $searchService = $repository->getSearchService();
1050
        $result = $searchService->findLocations($query);
1051
1052
        $this->assertEquals(1, $result->totalCount);
1053
        $this->assertEquals(
1054
            $wildBoars->contentInfo->mainLocationId,
1055
            $result->searchHits[0]->valueObject->id
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1056
        );
1057
    }
1058
1059
    /**
1060
     * Test for the findLocations() method.
1061
     *
1062
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
1063
     * @group maplocation
1064
     */
1065
    public function testMapLocationDistanceWithCustomFieldSort()
1066
    {
1067
        $contentType = $this->createTestPlaceContentType();
1068
1069
        // Create a draft to account for behaviour with ContentType in different states
1070
        $repository = $this->getRepository();
1071
        $contentTypeService = $repository->getContentTypeService();
1072
        $contentService = $repository->getContentService();
1073
        $contentTypeService->createContentTypeDraft($contentType);
1074
        $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct(2);
1075
1076
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
1077
        $createStruct->alwaysAvailable = false;
1078
        $createStruct->mainLanguageCode = 'eng-GB';
1079
        $createStruct->setField(
1080
            'maplocation',
1081
            [
1082
                'latitude' => 45.894877,
1083
                'longitude' => 15.972699,
1084
                'address' => 'Here be wild boars',
1085
            ],
1086
            'eng-GB'
1087
        );
1088
1089
        $draft = $contentService->createContent($createStruct, [$locationCreateStruct]);
1090
        $wildBoars = $contentService->publishVersion($draft->getVersionInfo());
1091
1092
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
1093
        $createStruct->alwaysAvailable = false;
1094
        $createStruct->mainLanguageCode = 'eng-GB';
1095
        $createStruct->setField(
1096
            'maplocation',
1097
            [
1098
                'latitude' => 45.927334,
1099
                'longitude' => 15.934847,
1100
                'address' => 'A lone tree',
1101
            ],
1102
            'eng-GB'
1103
        );
1104
1105
        $draft = $contentService->createContent($createStruct, [$locationCreateStruct]);
1106
        $tree = $contentService->publishVersion($draft->getVersionInfo());
1107
1108
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
1109
        $createStruct->alwaysAvailable = false;
1110
        $createStruct->mainLanguageCode = 'eng-GB';
1111
        $createStruct->setField(
1112
            'maplocation',
1113
            [
1114
                'latitude' => 45.903777,
1115
                'longitude' => 15.958788,
1116
                'address' => 'Meadow with mushrooms',
1117
            ],
1118
            'eng-GB'
1119
        );
1120
1121
        $draft = $contentService->createContent($createStruct, [$locationCreateStruct]);
1122
        $mushrooms = $contentService->publishVersion($draft->getVersionInfo());
1123
1124
        $this->refreshSearch($repository);
1125
1126
        $well = [
1127
            'latitude' => 43.756825,
1128
            'longitude' => 15.775074,
1129
        ];
1130
1131
        $sortClause = new SortClause\MapLocationDistance(
1132
            'testtype',
1133
            'maplocation',
1134
            $well['latitude'],
1135
            $well['longitude'],
1136
            LocationQuery::SORT_DESC
1137
        );
1138
        $sortClause->setCustomField('testtype', 'maplocation', 'custom_geolocation_field');
1139
1140
        $query = new LocationQuery(
1141
            [
1142
                'filter' => new Criterion\LogicalAnd(
1143
                    [
1144
                        new Criterion\ContentTypeId($contentType->id),
1145
                        new Criterion\MapLocationDistance(
1146
                            'maplocation',
1147
                            Criterion\Operator::GTE,
1148
                            235,
1149
                            $well['latitude'],
1150
                            $well['longitude']
1151
                        ),
1152
                    ]
1153
                ),
1154
                'offset' => 0,
1155
                'limit' => 10,
1156
                'sortClauses' => [
1157
                    $sortClause,
1158
                ],
1159
            ]
1160
        );
1161
1162
        $searchService = $repository->getSearchService();
1163
        $result = $searchService->findLocations($query);
1164
1165
        $this->assertEquals(3, $result->totalCount);
1166
        $this->assertEquals(
1167
            $wildBoars->contentInfo->mainLocationId,
1168
            $result->searchHits[2]->valueObject->id
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1169
        );
1170
        $this->assertEquals(
1171
            $mushrooms->contentInfo->mainLocationId,
1172
            $result->searchHits[1]->valueObject->id
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1173
        );
1174
        $this->assertEquals(
1175
            $tree->contentInfo->mainLocationId,
1176
            $result->searchHits[0]->valueObject->id
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1177
        );
1178
    }
1179
1180
    /**
1181
     * Test for the findLocations() method.
1182
     *
1183
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
1184
     */
1185
    public function testVisibilityCriterionWithHiddenContent()
1186
    {
1187
        $repository = $this->getRepository();
1188
        $contentTypeService = $repository->getContentTypeService();
1189
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
1190
1191
        $contentService = $repository->getContentService();
1192
        $locationService = $repository->getLocationService();
1193
        $searchService = $repository->getSearchService();
1194
1195
        $testRootContentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
1196
        $testRootContentCreate->setField('name', 'Root for test');
1197
1198
        $rootContent = $contentService->createContent(
1199
            $testRootContentCreate,
1200
            [
1201
                $locationService->newLocationCreateStruct(
1202
                    $this->generateId('location', 2)
1203
                ),
1204
            ]
1205
        );
1206
1207
        $publishedRootContent = $contentService->publishVersion($rootContent->versionInfo);
1208
1209
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
1210
        $contentCreate->setField('name', 'To Hide');
1211
1212
        $content = $contentService->createContent(
1213
            $contentCreate,
1214
            [
1215
                $locationService->newLocationCreateStruct(
1216
                    $publishedRootContent->contentInfo->mainLocationId
1217
                ),
1218
            ]
1219
        );
1220
        $publishedContent = $contentService->publishVersion($content->versionInfo);
1221
1222
        $childContentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
1223
        $childContentCreate->setField('name', 'Invisible Child');
1224
1225
        $childContent = $contentService->createContent(
1226
            $childContentCreate,
1227
            [
1228
                $locationService->newLocationCreateStruct(
1229
                    $publishedContent->contentInfo->mainLocationId
1230
                ),
1231
            ]
1232
        );
1233
        $rootLocation = $locationService->loadLocation($publishedRootContent->contentInfo->mainLocationId);
1234
1235
        $contentService->publishVersion($childContent->versionInfo);
1236
        $this->refreshSearch($repository);
1237
1238
        $query = new LocationQuery([
1239
            'query' => new Criterion\LogicalAnd([
1240
                new Criterion\Visibility(
1241
                    Criterion\Visibility::VISIBLE
1242
                ),
1243
                new Criterion\Subtree(
1244
                    $rootLocation->pathString
1245
                ),
1246
            ]),
1247
        ]);
1248
1249
        //Sanity check for visible locations
1250
        $result = $searchService->findLocations($query);
1251
        $this->assertEquals(3, $result->totalCount);
1252
1253
        //Hide main content
1254
        $contentService->hideContent($publishedContent->contentInfo);
1255
        $this->refreshSearch($repository);
1256
1257
        $result = $searchService->findLocations($query);
1258
        $this->assertEquals(1, $result->totalCount);
1259
1260
        //Query for invisible content
1261
        $hiddenQuery = new LocationQuery([
1262
            'query' => new Criterion\LogicalAnd([
1263
                new Criterion\Visibility(
1264
                    Criterion\Visibility::HIDDEN
1265
                ),
1266
                new Criterion\Subtree(
1267
                    $rootLocation->pathString
1268
                ),
1269
            ]),
1270
        ]);
1271
1272
        $result = $searchService->findLocations($hiddenQuery);
1273
        $this->assertEquals(2, $result->totalCount);
1274
    }
1275
1276
    /**
1277
     * Assert that query result matches the given fixture.
1278
     *
1279
     * @param LocationQuery $query
1280
     * @param string $fixture
1281
     * @param null|callable $closure
1282
     */
1283
    protected function assertQueryFixture(LocationQuery $query, $fixture, $closure = null, $ignoreScore = true)
1284
    {
1285
        $repository = $this->getRepository();
1286
        $searchService = $repository->getSearchService();
1287
1288
        try {
1289
            $result = $searchService->findLocations($query);
1290
            $this->simplifySearchResult($result);
1291
        } catch (NotImplementedException $e) {
1292
            $this->markTestSkipped(
1293
                'This feature is not supported by the current search backend: ' . $e->getMessage()
1294
            );
1295
        }
1296
1297 View Code Duplication
        if (!is_file($fixture)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1298
            if (isset($_ENV['ez_tests_record'])) {
1299
                file_put_contents(
1300
                    $record = $fixture . '.recording',
1301
                    "<?php\n\nreturn " . var_export($result, true) . ";\n\n"
1302
                );
1303
                $this->markTestIncomplete("No fixture available. Result recorded at $record. Result: \n" . $this->printResult($result));
1304
            } else {
1305
                $this->markTestIncomplete("No fixture available. Set \$_ENV['ez_tests_record'] to generate:\n " . $fixture);
1306
            }
1307
        }
1308
1309
        $fixture = include $fixture;
1310
1311
        if ($closure !== null) {
1312
            $closure($result);
1313
        }
1314
1315 View Code Duplication
        if ($ignoreScore) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1316
            foreach ([$fixture, $result] as $result) {
1317
                $property = new \ReflectionProperty(get_class($result), 'maxScore');
1318
                $property->setAccessible(true);
1319
                $property->setValue($result, 0.0);
1320
1321
                foreach ($result->searchHits as $hit) {
1322
                    $property = new \ReflectionProperty(get_class($hit), 'score');
1323
                    $property->setAccessible(true);
1324
                    $property->setValue($hit, 0.0);
1325
                }
1326
            }
1327
        }
1328
1329
        foreach ([$fixture, $result] as $set) {
1330
            foreach ($set->searchHits as $hit) {
1331
                $property = new \ReflectionProperty(get_class($hit), 'index');
1332
                $property->setAccessible(true);
1333
                $property->setValue($hit, null);
1334
1335
                $property = new \ReflectionProperty(get_class($hit), 'matchedTranslation');
1336
                $property->setAccessible(true);
1337
                $property->setValue($hit, null);
1338
            }
1339
        }
1340
1341
        $this->assertEquals(
1342
            $fixture,
1343
            $result,
1344
            'Search results do not match.',
1345
            .2 // Be quite generous regarding delay -- most important for scores
1346
        );
1347
    }
1348
1349
    /**
1350
     * Show a simplified view of the search result for manual introspection.
1351
     *
1352
     * @param SearchResult $result
1353
     *
1354
     * @return string
1355
     */
1356 View Code Duplication
    protected function printResult(SearchResult $result)
1357
    {
1358
        $printed = '';
1359
        foreach ($result->searchHits as $hit) {
1360
            $printed .= sprintf(" - %s (%s)\n", $hit->valueObject['title'], $hit->valueObject['id']);
1361
        }
1362
1363
        return $printed;
1364
    }
1365
1366
    /**
1367
     * Simplify search result.
1368
     *
1369
     * This leads to saner comparisons of results, since we do not get the full
1370
     * content objects every time.
1371
     *
1372
     * @param SearchResult $result
1373
     */
1374
    protected function simplifySearchResult(SearchResult $result)
1375
    {
1376
        $result->time = 1;
1377
1378
        foreach ($result->searchHits as $hit) {
1379
            switch (true) {
1380 View Code Duplication
                case $hit->valueObject instanceof Location:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1381
                    $hit->valueObject = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('id' => $hit->valu...ect->contentInfo->name) of type array<string,*,{"id":"*","title":"string"}> is incompatible with the declared type object<eZ\Publish\API\Re...ory\Values\ValueObject> of property $valueObject.

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...
1382
                        'id' => $hit->valueObject->contentInfo->id,
1383
                        'title' => $hit->valueObject->contentInfo->name,
1384
                    ];
1385
                    break;
1386
1387
                default:
1388
                    throw new \RuntimeException('Unknown search result hit type: ' . get_class($hit->valueObject));
1389
            }
1390
        }
1391
    }
1392
1393
    /**
1394
     * Get fixture directory.
1395
     *
1396
     * @return string
1397
     */
1398
    protected function getFixtureDir()
1399
    {
1400
        return __DIR__ . '/_fixtures/' . getenv('fixtureDir') . '/';
1401
    }
1402
}
1403