Completed
Push — facets-EZP-26465 ( 4eac8e...b3ca9c )
by André
14:49
created

testFindFacetedLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
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 test Content with ezcountry field having multiple countries selected.
56
     *
57
     * @return \eZ\Publish\API\Repository\Values\Content\Content
58
     */
59
    protected function createMultipleCountriesContent()
60
    {
61
        $repository = $this->getRepository();
62
        $contentTypeService = $repository->getContentTypeService();
63
        $contentService = $repository->getContentService();
64
65
        $createStruct = $contentTypeService->newContentTypeCreateStruct('countries-multiple');
66
        $createStruct->mainLanguageCode = 'eng-GB';
67
        $createStruct->remoteId = 'countries-multiple-123';
68
        $createStruct->names = array('eng-GB' => 'Multiple countries');
69
        $createStruct->creatorId = 14;
70
        $createStruct->creationDate = new \DateTime();
71
72
        $fieldCreate = $contentTypeService->newFieldDefinitionCreateStruct('countries', 'ezcountry');
73
        $fieldCreate->names = array('eng-GB' => 'Countries');
74
        $fieldCreate->fieldGroup = 'main';
75
        $fieldCreate->position = 1;
76
        $fieldCreate->isTranslatable = false;
77
        $fieldCreate->isSearchable = true;
78
        $fieldCreate->fieldSettings = array('isMultiple' => true);
79
80
        $createStruct->addFieldDefinition($fieldCreate);
81
82
        $contentGroup = $contentTypeService->loadContentTypeGroupByIdentifier('Content');
83
        $contentTypeDraft = $contentTypeService->createContentType($createStruct, array($contentGroup));
84
        $contentTypeService->publishContentTypeDraft($contentTypeDraft);
85
        $contentType = $contentTypeService->loadContentType($contentTypeDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repositor...ContentType\ContentType. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
86
87
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
88
        $createStruct->remoteId = 'countries-multiple-456';
89
        $createStruct->alwaysAvailable = false;
90
        $createStruct->setField(
91
            'countries',
92
            array('BE', 'DE', 'FR', 'HR', 'NO', 'PT', 'RU')
93
        );
94
95
        $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct(2);
96
        $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
97
        $content = $contentService->publishVersion($draft->getVersionInfo());
98
99
        $this->refreshSearch($repository);
100
101
        return $content;
102
    }
103
104
    /**
105
     * Test for the findLocations() method.
106
     *
107
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
108
     */
109 View Code Duplication
    public function testFieldCollectionContains()
110
    {
111
        $testContent = $this->createMultipleCountriesContent();
112
113
        $query = new LocationQuery(
114
            array(
115
                'query' => new Criterion\Field(
116
                    'countries',
117
                    Criterion\Operator::CONTAINS,
118
                    'Belgium'
119
                ),
120
            )
121
        );
122
123
        $repository = $this->getRepository();
124
        $searchService = $repository->getSearchService();
125
        $result = $searchService->findLocations($query);
126
127
        $this->assertEquals(1, $result->totalCount);
128
        $this->assertEquals(
129
            $testContent->contentInfo->mainLocationId,
130
            $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...
131
        );
132
    }
133
134
    /**
135
     * Test for the findLocations() method.
136
     *
137
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
138
     * @depends eZ\Publish\API\Repository\Tests\SearchServiceTest::testFieldCollectionContains
139
     */
140 View Code Duplication
    public function testFieldCollectionContainsNoMatch()
141
    {
142
        $this->createMultipleCountriesContent();
143
        $query = new LocationQuery(
144
            array(
145
                'query' => new Criterion\Field(
146
                    'countries',
147
                    Criterion\Operator::CONTAINS,
148
                    'Netherlands Antilles'
149
                ),
150
            )
151
        );
152
153
        $repository = $this->getRepository();
154
        $searchService = $repository->getSearchService();
155
        $result = $searchService->findLocations($query);
156
157
        $this->assertEquals(0, $result->totalCount);
158
    }
159
160
    /**
161
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
162
     */
163 View Code Duplication
    public function testInvalidFieldIdentifierRange()
164
    {
165
        $repository = $this->getRepository();
166
        $searchService = $repository->getSearchService();
167
168
        $searchService->findLocations(
169
            new LocationQuery(
170
                array(
171
                    'filter' => new Criterion\Field(
172
                        'some_hopefully_unknown_field',
173
                        Criterion\Operator::BETWEEN,
174
                        array(10, 1000)
175
                    ),
176
                    'sortClauses' => array(new SortClause\ContentId()),
177
                )
178
            )
179
        );
180
    }
181
182
    /**
183
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
184
     */
185
    public function testInvalidFieldIdentifierIn()
186
    {
187
        $repository = $this->getRepository();
188
        $searchService = $repository->getSearchService();
189
190
        $searchService->findLocations(
191
            new LocationQuery(
192
                array(
193
                    'filter' => new Criterion\Field(
194
                        'some_hopefully_unknown_field',
195
                        Criterion\Operator::EQ,
196
                        1000
197
                    ),
198
                    'sortClauses' => array(new SortClause\ContentId()),
199
                )
200
            )
201
        );
202
    }
203
204
    /**
205
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
206
     */
207
    public function testFindLocationsWithNonSearchableField()
208
    {
209
        $repository = $this->getRepository();
210
        $searchService = $repository->getSearchService();
211
212
        $searchService->findLocations(
213
            new LocationQuery(
214
                array(
215
                    'filter' => new Criterion\Field(
216
                        'tag_cloud_url',
217
                        Criterion\Operator::EQ,
218
                        'http://nimbus.com'
219
                    ),
220
                    'sortClauses' => array(new SortClause\ContentId()),
221
                )
222
            )
223
        );
224
    }
225
226
    /**
227
     * @param \eZ\Publish\API\Repository\Values\Content\Search\SearchResult $result
228
     *
229
     * @return array
230
     */
231
    protected function mapResultLocationIds(SearchResult $result)
232
    {
233
        return array_map(
234
            function (SearchHit $searchHit) {
235
                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...
236
            },
237
            $result->searchHits
238
        );
239
    }
240
241
    /**
242
     * Test for the findLocations() method.
243
     *
244
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
245
     */
246 View Code Duplication
    public function testQueryCustomField()
247
    {
248
        $query = new LocationQuery(
249
            array(
250
                'query' => new Criterion\CustomField(
251
                    'custom_field',
252
                    Criterion\Operator::EQ,
253
                    'AdMiNiStRaToR'
254
                ),
255
                'offset' => 0,
256
                'limit' => 10,
257
                'sortClauses' => array(new SortClause\ContentId()),
258
            )
259
        );
260
        $this->assertQueryFixture(
261
            $query,
262
            $this->getFixtureDir() . '/QueryCustomField.php',
263
            null,
264
            true
265
        );
266
    }
267
268
    /**
269
     * Test for the findLocations() method.
270
     *
271
     * This tests explicitly queries the first_name while user is contained in
272
     * the last_name of admin and anonymous. This is done to show the custom
273
     * copy field working.
274
     *
275
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
276
     */
277 View Code Duplication
    public function testQueryModifiedField()
278
    {
279
        // Check using get_class since the others extend SetupFactory\Legacy
280
        if (ltrim(get_class($this->getSetupFactory()), '\\') === 'eZ\Publish\API\Repository\Tests\SetupFactory\Legacy') {
281
            $this->markTestIncomplete(
282
                'Custom fields not supported by LegacySE ' .
283
                '(@todo: Legacy should fallback to just querying normal field so this should be tested here)'
284
            );
285
        }
286
287
        $query = new LocationQuery(
288
            array(
289
                'query' => new Criterion\Field(
290
                    'first_name',
291
                    Criterion\Operator::EQ,
292
                    'User'
293
                ),
294
                'offset' => 0,
295
                'limit' => 10,
296
                'sortClauses' => array(new SortClause\ContentId()),
297
            )
298
        );
299
        $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...
300
301
        $this->assertQueryFixture(
302
            $query,
303
            $this->getFixtureDir() . '/QueryModifiedField.php',
304
            null,
305
            true
306
        );
307
    }
308
309
    /**
310
     * @return \eZ\Publish\API\Repository\Values\ContentType\ContentType
311
     */
312 View Code Duplication
    protected function createTestPlaceContentType()
313
    {
314
        $repository = $this->getRepository();
315
        $contentTypeService = $repository->getContentTypeService();
316
317
        $createStruct = $contentTypeService->newContentTypeCreateStruct('testtype');
318
        $createStruct->mainLanguageCode = 'eng-GB';
319
        $createStruct->names = array('eng-GB' => 'Test type');
320
        $createStruct->creatorId = 14;
321
        $createStruct->creationDate = new \DateTime();
322
323
        $translatableFieldCreate = $contentTypeService->newFieldDefinitionCreateStruct('maplocation', 'ezgmaplocation');
324
        $translatableFieldCreate->names = array('eng-GB' => 'Map location field');
325
        $translatableFieldCreate->fieldGroup = 'main';
326
        $translatableFieldCreate->position = 1;
327
        $translatableFieldCreate->isTranslatable = false;
328
        $translatableFieldCreate->isSearchable = true;
329
330
        $createStruct->addFieldDefinition($translatableFieldCreate);
331
332
        $contentGroup = $contentTypeService->loadContentTypeGroupByIdentifier('Content');
333
        $contentTypeDraft = $contentTypeService->createContentType($createStruct, array($contentGroup));
334
        $contentTypeService->publishContentTypeDraft($contentTypeDraft);
335
        $contentType = $contentTypeService->loadContentType($contentTypeDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repositor...ContentType\ContentType. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
336
337
        return $contentType;
338
    }
339
340
    /**
341
     * Test for the findLocations() method.
342
     *
343
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
344
     * @group maplocation
345
     */
346 View Code Duplication
    public function testMapLocationDistanceLessThanOrEqual()
347
    {
348
        $contentType = $this->createTestPlaceContentType();
349
350
        // Create a draft to account for behaviour with ContentType in different states
351
        $repository = $this->getRepository();
352
        $contentTypeService = $repository->getContentTypeService();
353
        $contentService = $repository->getContentService();
354
        $contentTypeService->createContentTypeDraft($contentType);
355
        $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct(2);
356
357
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
358
        $createStruct->alwaysAvailable = false;
359
        $createStruct->mainLanguageCode = 'eng-GB';
360
        $createStruct->setField(
361
            'maplocation',
362
            array(
363
                'latitude' => 45.894877,
364
                'longitude' => 15.972699,
365
                'address' => 'Here be wild boars',
366
            ),
367
            'eng-GB'
368
        );
369
370
        $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
371
        $wildBoars = $contentService->publishVersion($draft->getVersionInfo());
372
373
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
374
        $createStruct->alwaysAvailable = false;
375
        $createStruct->mainLanguageCode = 'eng-GB';
376
        $createStruct->setField(
377
            'maplocation',
378
            array(
379
                'latitude' => 45.927334,
380
                'longitude' => 15.934847,
381
                'address' => 'A lone tree',
382
            ),
383
            'eng-GB'
384
        );
385
386
        $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
387
        $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...
388
389
        $this->refreshSearch($repository);
390
391
        $query = new LocationQuery(
392
            array(
393
                'filter' => new Criterion\LogicalAnd(
394
                    array(
395
                        new Criterion\ContentTypeId($contentType->id),
396
                        new Criterion\MapLocationDistance(
397
                            'maplocation',
398
                            Criterion\Operator::LTE,
399
                            240,
400
                            43.756825,
401
                            15.775074
402
                        ),
403
                    )
404
                ),
405
                'offset' => 0,
406
                'limit' => 10,
407
                'sortClauses' => array(),
408
            )
409
        );
410
411
        $searchService = $repository->getSearchService();
412
        $result = $searchService->findLocations($query);
413
414
        $this->assertEquals(1, $result->totalCount);
415
        $this->assertEquals(
416
            $wildBoars->contentInfo->mainLocationId,
417
            $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...
418
        );
419
    }
420
421
    /**
422
     * Test for the findLocations() method.
423
     *
424
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
425
     * @group maplocation
426
     */
427 View Code Duplication
    public function testMapLocationDistanceGreaterThanOrEqual()
428
    {
429
        $contentType = $this->createTestPlaceContentType();
430
431
        // Create a draft to account for behaviour with ContentType in different states
432
        $repository = $this->getRepository();
433
        $contentTypeService = $repository->getContentTypeService();
434
        $contentService = $repository->getContentService();
435
        $contentTypeService->createContentTypeDraft($contentType);
436
        $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct(2);
437
438
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
439
        $createStruct->alwaysAvailable = false;
440
        $createStruct->mainLanguageCode = 'eng-GB';
441
        $createStruct->setField(
442
            'maplocation',
443
            array(
444
                'latitude' => 45.894877,
445
                'longitude' => 15.972699,
446
                'address' => 'Here be wild boars',
447
            ),
448
            'eng-GB'
449
        );
450
451
        $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
452
        $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...
453
454
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
455
        $createStruct->alwaysAvailable = false;
456
        $createStruct->mainLanguageCode = 'eng-GB';
457
        $createStruct->setField(
458
            'maplocation',
459
            array(
460
                'latitude' => 45.927334,
461
                'longitude' => 15.934847,
462
                'address' => 'A lone tree',
463
            ),
464
            'eng-GB'
465
        );
466
467
        $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
468
        $tree = $contentService->publishVersion($draft->getVersionInfo());
469
470
        $this->refreshSearch($repository);
471
472
        $query = new LocationQuery(
473
            array(
474
                'filter' => new Criterion\LogicalAnd(
475
                    array(
476
                        new Criterion\ContentTypeId($contentType->id),
477
                        new Criterion\MapLocationDistance(
478
                            'maplocation',
479
                            Criterion\Operator::GTE,
480
                            240,
481
                            43.756825,
482
                            15.775074
483
                        ),
484
                    )
485
                ),
486
                'offset' => 0,
487
                'limit' => 10,
488
                'sortClauses' => array(),
489
            )
490
        );
491
492
        $searchService = $repository->getSearchService();
493
        $result = $searchService->findLocations($query);
494
495
        $this->assertEquals(1, $result->totalCount);
496
        $this->assertEquals(
497
            $tree->contentInfo->mainLocationId,
498
            $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...
499
        );
500
    }
501
502
    /**
503
     * Test for the findLocations() method.
504
     *
505
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
506
     * @group maplocation
507
     */
508
    public function testMapLocationDistanceBetween()
509
    {
510
        $contentType = $this->createTestPlaceContentType();
511
512
        // Create a draft to account for behaviour with ContentType in different states
513
        $repository = $this->getRepository();
514
        $contentTypeService = $repository->getContentTypeService();
515
        $contentService = $repository->getContentService();
516
        $contentTypeService->createContentTypeDraft($contentType);
517
        $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct(2);
518
519
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
520
        $createStruct->alwaysAvailable = false;
521
        $createStruct->mainLanguageCode = 'eng-GB';
522
        $createStruct->setField(
523
            'maplocation',
524
            array(
525
                'latitude' => 45.894877,
526
                'longitude' => 15.972699,
527
                'address' => 'Here be wild boars',
528
            ),
529
            'eng-GB'
530
        );
531
532
        $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
533
        $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...
534
535
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
536
        $createStruct->alwaysAvailable = false;
537
        $createStruct->mainLanguageCode = 'eng-GB';
538
        $createStruct->setField(
539
            'maplocation',
540
            array(
541
                'latitude' => 45.927334,
542
                'longitude' => 15.934847,
543
                'address' => 'A lone tree',
544
            ),
545
            'eng-GB'
546
        );
547
548
        $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
549
        $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...
550
551
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
552
        $createStruct->alwaysAvailable = false;
553
        $createStruct->mainLanguageCode = 'eng-GB';
554
        $createStruct->setField(
555
            'maplocation',
556
            array(
557
                'latitude' => 45.903777,
558
                'longitude' => 15.958788,
559
                'address' => 'Meadow with mushrooms',
560
            ),
561
            'eng-GB'
562
        );
563
564
        $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
565
        $mushrooms = $contentService->publishVersion($draft->getVersionInfo());
566
567
        $this->refreshSearch($repository);
568
569
        $query = new LocationQuery(
570
            array(
571
                'filter' => new Criterion\LogicalAnd(
572
                    array(
573
                        new Criterion\ContentTypeId($contentType->id),
574
                        new Criterion\MapLocationDistance(
575
                            'maplocation',
576
                            Criterion\Operator::BETWEEN,
577
                            array(239, 241),
578
                            43.756825,
579
                            15.775074
580
                        ),
581
                    )
582
                ),
583
                'offset' => 0,
584
                'limit' => 10,
585
                'sortClauses' => array(),
586
            )
587
        );
588
589
        $searchService = $repository->getSearchService();
590
        $result = $searchService->findLocations($query);
591
592
        $this->assertEquals(1, $result->totalCount);
593
        $this->assertEquals(
594
            $mushrooms->contentInfo->mainLocationId,
595
            $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...
596
        );
597
    }
598
599
    /**
600
     * Test for the findLocations() method.
601
     *
602
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
603
     * @group maplocation
604
     */
605 View Code Duplication
    public function testMapLocationDistanceSortAscending()
606
    {
607
        $contentType = $this->createTestPlaceContentType();
608
609
        // Create a draft to account for behaviour with ContentType in different states
610
        $repository = $this->getRepository();
611
        $contentTypeService = $repository->getContentTypeService();
612
        $contentService = $repository->getContentService();
613
        $contentTypeService->createContentTypeDraft($contentType);
614
        $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct(2);
615
616
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
617
        $createStruct->alwaysAvailable = false;
618
        $createStruct->mainLanguageCode = 'eng-GB';
619
        $createStruct->setField(
620
            'maplocation',
621
            array(
622
                'latitude' => 45.894877,
623
                'longitude' => 15.972699,
624
                'address' => 'Here be wild boars',
625
            ),
626
            'eng-GB'
627
        );
628
629
        $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
630
        $wildBoars = $contentService->publishVersion($draft->getVersionInfo());
631
632
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
633
        $createStruct->alwaysAvailable = false;
634
        $createStruct->mainLanguageCode = 'eng-GB';
635
        $createStruct->setField(
636
            'maplocation',
637
            array(
638
                'latitude' => 45.927334,
639
                'longitude' => 15.934847,
640
                'address' => 'A lone tree',
641
            ),
642
            'eng-GB'
643
        );
644
645
        $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
646
        $tree = $contentService->publishVersion($draft->getVersionInfo());
647
648
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
649
        $createStruct->alwaysAvailable = false;
650
        $createStruct->mainLanguageCode = 'eng-GB';
651
        $createStruct->setField(
652
            'maplocation',
653
            array(
654
                'latitude' => 45.903777,
655
                'longitude' => 15.958788,
656
                'address' => 'Meadow with mushrooms',
657
            ),
658
            'eng-GB'
659
        );
660
661
        $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
662
        $mushrooms = $contentService->publishVersion($draft->getVersionInfo());
663
664
        $this->refreshSearch($repository);
665
666
        $wellInVodice = array(
667
            'latitude' => 43.756825,
668
            'longitude' => 15.775074,
669
        );
670
671
        $query = new LocationQuery(
672
            array(
673
                'filter' => new Criterion\LogicalAnd(
674
                    array(
675
                        new Criterion\ContentTypeId($contentType->id),
676
                        new Criterion\MapLocationDistance(
677
                            'maplocation',
678
                            Criterion\Operator::GTE,
679
                            235,
680
                            $wellInVodice['latitude'],
681
                            $wellInVodice['longitude']
682
                        ),
683
                    )
684
                ),
685
                'offset' => 0,
686
                'limit' => 10,
687
                'sortClauses' => array(
688
                    new SortClause\MapLocationDistance(
689
                        'testtype',
690
                        'maplocation',
691
                        $wellInVodice['latitude'],
692
                        $wellInVodice['longitude'],
693
                        LocationQuery::SORT_ASC
694
                    ),
695
                ),
696
            )
697
        );
698
699
        $searchService = $repository->getSearchService();
700
        $result = $searchService->findLocations($query);
701
702
        $this->assertEquals(3, $result->totalCount);
703
        $this->assertEquals(
704
            $wildBoars->contentInfo->mainLocationId,
705
            $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...
706
        );
707
        $this->assertEquals(
708
            $mushrooms->contentInfo->mainLocationId,
709
            $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...
710
        );
711
        $this->assertEquals(
712
            $tree->contentInfo->mainLocationId,
713
            $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...
714
        );
715
    }
716
717
    /**
718
     * Test for the findLocations() method.
719
     *
720
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
721
     * @group maplocation
722
     */
723 View Code Duplication
    public function testMapLocationDistanceSortDescending()
724
    {
725
        $contentType = $this->createTestPlaceContentType();
726
727
        // Create a draft to account for behaviour with ContentType in different states
728
        $repository = $this->getRepository();
729
        $contentTypeService = $repository->getContentTypeService();
730
        $contentService = $repository->getContentService();
731
        $contentTypeService->createContentTypeDraft($contentType);
732
        $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct(2);
733
734
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
735
        $createStruct->alwaysAvailable = false;
736
        $createStruct->mainLanguageCode = 'eng-GB';
737
        $createStruct->setField(
738
            'maplocation',
739
            array(
740
                'latitude' => 45.894877,
741
                'longitude' => 15.972699,
742
                'address' => 'Here be wild boars',
743
            ),
744
            'eng-GB'
745
        );
746
747
        $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
748
        $wildBoars = $contentService->publishVersion($draft->getVersionInfo());
749
750
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
751
        $createStruct->alwaysAvailable = false;
752
        $createStruct->mainLanguageCode = 'eng-GB';
753
        $createStruct->setField(
754
            'maplocation',
755
            array(
756
                'latitude' => 45.927334,
757
                'longitude' => 15.934847,
758
                'address' => 'A lone tree',
759
            ),
760
            'eng-GB'
761
        );
762
763
        $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
764
        $tree = $contentService->publishVersion($draft->getVersionInfo());
765
766
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
767
        $createStruct->alwaysAvailable = false;
768
        $createStruct->mainLanguageCode = 'eng-GB';
769
        $createStruct->setField(
770
            'maplocation',
771
            array(
772
                'latitude' => 45.903777,
773
                'longitude' => 15.958788,
774
                'address' => 'Meadow with mushrooms',
775
            ),
776
            'eng-GB'
777
        );
778
779
        $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
780
        $mushrooms = $contentService->publishVersion($draft->getVersionInfo());
781
782
        $this->refreshSearch($repository);
783
784
        $well = array(
785
            'latitude' => 43.756825,
786
            'longitude' => 15.775074,
787
        );
788
789
        $query = new LocationQuery(
790
            array(
791
                'filter' => new Criterion\LogicalAnd(
792
                    array(
793
                        new Criterion\ContentTypeId($contentType->id),
794
                        new Criterion\MapLocationDistance(
795
                            'maplocation',
796
                            Criterion\Operator::GTE,
797
                            235,
798
                            $well['latitude'],
799
                            $well['longitude']
800
                        ),
801
                    )
802
                ),
803
                'offset' => 0,
804
                'limit' => 10,
805
                'sortClauses' => array(
806
                    new SortClause\MapLocationDistance(
807
                        'testtype',
808
                        'maplocation',
809
                        $well['latitude'],
810
                        $well['longitude'],
811
                        LocationQuery::SORT_DESC
812
                    ),
813
                ),
814
            )
815
        );
816
817
        $searchService = $repository->getSearchService();
818
        $result = $searchService->findLocations($query);
819
820
        $this->assertEquals(3, $result->totalCount);
821
        $this->assertEquals(
822
            $wildBoars->contentInfo->mainLocationId,
823
            $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...
824
        );
825
        $this->assertEquals(
826
            $mushrooms->contentInfo->mainLocationId,
827
            $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...
828
        );
829
        $this->assertEquals(
830
            $tree->contentInfo->mainLocationId,
831
            $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...
832
        );
833
    }
834
835
    /**
836
     * Test for the findLocations() method.
837
     *
838
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
839
     * @group maplocation
840
     */
841
    public function testMapLocationDistanceWithCustomField()
842
    {
843
        $contentType = $this->createTestPlaceContentType();
844
845
        // Create a draft to account for behaviour with ContentType in different states
846
        $repository = $this->getRepository();
847
        $contentTypeService = $repository->getContentTypeService();
848
        $contentService = $repository->getContentService();
849
        $contentTypeService->createContentTypeDraft($contentType);
850
        $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct(2);
851
852
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
853
        $createStruct->alwaysAvailable = false;
854
        $createStruct->mainLanguageCode = 'eng-GB';
855
        $createStruct->setField(
856
            'maplocation',
857
            array(
858
                'latitude' => 45.894877,
859
                'longitude' => 15.972699,
860
                'address' => 'Here be wild boars',
861
            ),
862
            'eng-GB'
863
        );
864
865
        $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
866
        $wildBoars = $contentService->publishVersion($draft->getVersionInfo());
867
868
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
869
        $createStruct->alwaysAvailable = false;
870
        $createStruct->mainLanguageCode = 'eng-GB';
871
        $createStruct->setField(
872
            'maplocation',
873
            array(
874
                'latitude' => 45.927334,
875
                'longitude' => 15.934847,
876
                'address' => 'A lone tree',
877
            ),
878
            'eng-GB'
879
        );
880
881
        $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
882
        $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...
883
884
        $this->refreshSearch($repository);
885
886
        $distanceCriterion = new Criterion\MapLocationDistance(
887
            'maplocation',
888
            Criterion\Operator::LTE,
889
            240,
890
            43.756825,
891
            15.775074
892
        );
893
        $distanceCriterion->setCustomField('testtype', 'maplocation', 'custom_geolocation_field');
894
895
        $query = new LocationQuery(
896
            array(
897
                'filter' => new Criterion\LogicalAnd(
898
                    array(
899
                        new Criterion\ContentTypeId($contentType->id),
900
                        $distanceCriterion,
901
                    )
902
                ),
903
                'offset' => 0,
904
                'limit' => 10,
905
                'sortClauses' => array(),
906
            )
907
        );
908
909
        $searchService = $repository->getSearchService();
910
        $result = $searchService->findLocations($query);
911
912
        $this->assertEquals(1, $result->totalCount);
913
        $this->assertEquals(
914
            $wildBoars->contentInfo->mainLocationId,
915
            $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...
916
        );
917
    }
918
919
    /**
920
     * Test for the findLocations() method.
921
     *
922
     * @see \eZ\Publish\API\Repository\SearchService::findLocations()
923
     * @group maplocation
924
     */
925
    public function testMapLocationDistanceWithCustomFieldSort()
926
    {
927
        $contentType = $this->createTestPlaceContentType();
928
929
        // Create a draft to account for behaviour with ContentType in different states
930
        $repository = $this->getRepository();
931
        $contentTypeService = $repository->getContentTypeService();
932
        $contentService = $repository->getContentService();
933
        $contentTypeService->createContentTypeDraft($contentType);
934
        $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct(2);
935
936
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
937
        $createStruct->alwaysAvailable = false;
938
        $createStruct->mainLanguageCode = 'eng-GB';
939
        $createStruct->setField(
940
            'maplocation',
941
            array(
942
                'latitude' => 45.894877,
943
                'longitude' => 15.972699,
944
                'address' => 'Here be wild boars',
945
            ),
946
            'eng-GB'
947
        );
948
949
        $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
950
        $wildBoars = $contentService->publishVersion($draft->getVersionInfo());
951
952
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
953
        $createStruct->alwaysAvailable = false;
954
        $createStruct->mainLanguageCode = 'eng-GB';
955
        $createStruct->setField(
956
            'maplocation',
957
            array(
958
                'latitude' => 45.927334,
959
                'longitude' => 15.934847,
960
                'address' => 'A lone tree',
961
            ),
962
            'eng-GB'
963
        );
964
965
        $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
966
        $tree = $contentService->publishVersion($draft->getVersionInfo());
967
968
        $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
969
        $createStruct->alwaysAvailable = false;
970
        $createStruct->mainLanguageCode = 'eng-GB';
971
        $createStruct->setField(
972
            'maplocation',
973
            array(
974
                'latitude' => 45.903777,
975
                'longitude' => 15.958788,
976
                'address' => 'Meadow with mushrooms',
977
            ),
978
            'eng-GB'
979
        );
980
981
        $draft = $contentService->createContent($createStruct, array($locationCreateStruct));
982
        $mushrooms = $contentService->publishVersion($draft->getVersionInfo());
983
984
        $this->refreshSearch($repository);
985
986
        $well = array(
987
            'latitude' => 43.756825,
988
            'longitude' => 15.775074,
989
        );
990
991
        $sortClause = new SortClause\MapLocationDistance(
992
            'testtype',
993
            'maplocation',
994
            $well['latitude'],
995
            $well['longitude'],
996
            LocationQuery::SORT_DESC
997
        );
998
        $sortClause->setCustomField('testtype', 'maplocation', 'custom_geolocation_field');
999
1000
        $query = new LocationQuery(
1001
            array(
1002
                'filter' => new Criterion\LogicalAnd(
1003
                    array(
1004
                        new Criterion\ContentTypeId($contentType->id),
1005
                        new Criterion\MapLocationDistance(
1006
                            'maplocation',
1007
                            Criterion\Operator::GTE,
1008
                            235,
1009
                            $well['latitude'],
1010
                            $well['longitude']
1011
                        ),
1012
                    )
1013
                ),
1014
                'offset' => 0,
1015
                'limit' => 10,
1016
                'sortClauses' => array(
1017
                    $sortClause,
1018
                ),
1019
            )
1020
        );
1021
1022
        $searchService = $repository->getSearchService();
1023
        $result = $searchService->findLocations($query);
1024
1025
        $this->assertEquals(3, $result->totalCount);
1026
        $this->assertEquals(
1027
            $wildBoars->contentInfo->mainLocationId,
1028
            $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...
1029
        );
1030
        $this->assertEquals(
1031
            $mushrooms->contentInfo->mainLocationId,
1032
            $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...
1033
        );
1034
        $this->assertEquals(
1035
            $tree->contentInfo->mainLocationId,
1036
            $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...
1037
        );
1038
    }
1039
1040
    /**
1041
     * Assert that query result matches the given fixture.
1042
     *
1043
     * @param LocationQuery $query
1044
     * @param string $fixture
1045
     * @param null|callable $closure
1046
     */
1047
    protected function assertQueryFixture(LocationQuery $query, $fixture, $closure = null, $ignoreScore = true)
1048
    {
1049
        $repository = $this->getRepository();
1050
        $searchService = $repository->getSearchService();
1051
1052
        try {
1053
            $result = $searchService->findLocations($query);
1054
            $this->simplifySearchResult($result);
1055
        } catch (NotImplementedException $e) {
1056
            $this->markTestSkipped(
1057
                'This feature is not supported by the current search backend: ' . $e->getMessage()
1058
            );
1059
        }
1060
1061 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...
1062
            if (isset($_ENV['ez_tests_record'])) {
1063
                file_put_contents(
1064
                    $record = $fixture . '.recording',
1065
                    "<?php\n\nreturn " . var_export($result, true) . ";\n\n"
1066
                );
1067
                $this->markTestIncomplete("No fixture available. Result recorded at $record. Result: \n" . $this->printResult($result));
1068
            } else {
1069
                $this->markTestIncomplete("No fixture available. Set \$_ENV['ez_tests_record'] to generate:\n " . $fixture);
1070
            }
1071
        }
1072
1073
        $fixture = include $fixture;
1074
1075
        if ($closure !== null) {
1076
            $closure($result);
1077
        }
1078
1079 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...
1080
            foreach (array($fixture, $result) as $result) {
1081
                $property = new \ReflectionProperty(get_class($result), 'maxScore');
1082
                $property->setAccessible(true);
1083
                $property->setValue($result, 0.0);
1084
1085
                foreach ($result->searchHits as $hit) {
1086
                    $property = new \ReflectionProperty(get_class($hit), 'score');
1087
                    $property->setAccessible(true);
1088
                    $property->setValue($hit, 0.0);
1089
                }
1090
            }
1091
        }
1092
1093
        foreach (array($fixture, $result) as $set) {
1094
            foreach ($set->searchHits as $hit) {
1095
                $property = new \ReflectionProperty(get_class($hit), 'index');
1096
                $property->setAccessible(true);
1097
                $property->setValue($hit, null);
1098
1099
                $property = new \ReflectionProperty(get_class($hit), 'matchedTranslation');
1100
                $property->setAccessible(true);
1101
                $property->setValue($hit, null);
1102
            }
1103
        }
1104
1105
        $this->assertEquals(
1106
            $fixture,
1107
            $result,
1108
            'Search results do not match.',
1109
            .2 // Be quite generous regarding delay -- most important for scores
1110
        );
1111
    }
1112
1113
    /**
1114
     * Show a simplified view of the search result for manual introspection.
1115
     *
1116
     * @param SearchResult $result
1117
     *
1118
     * @return string
1119
     */
1120 View Code Duplication
    protected function printResult(SearchResult $result)
1121
    {
1122
        $printed = '';
1123
        foreach ($result->searchHits as $hit) {
1124
            $printed .= sprintf(" - %s (%s)\n", $hit->valueObject['title'], $hit->valueObject['id']);
1125
        }
1126
1127
        return $printed;
1128
    }
1129
1130
    /**
1131
     * Simplify search result.
1132
     *
1133
     * This leads to saner comparisons of results, since we do not get the full
1134
     * content objects every time.
1135
     *
1136
     * @param SearchResult $result
1137
     */
1138
    protected function simplifySearchResult(SearchResult $result)
1139
    {
1140
        $result->time = 1;
1141
1142
        foreach ($result->searchHits as $hit) {
1143
            switch (true) {
1144 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...
1145
                    $hit->valueObject = array(
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...
1146
                        'id' => $hit->valueObject->contentInfo->id,
0 ignored issues
show
Documentation introduced by
The property $contentInfo is declared protected in eZ\Publish\Core\Repository\Values\Content\Location. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
1147
                        'title' => $hit->valueObject->contentInfo->name,
0 ignored issues
show
Documentation introduced by
The property $contentInfo is declared protected in eZ\Publish\Core\Repository\Values\Content\Location. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
1148
                    );
1149
                    break;
1150
1151
                default:
1152
                    throw new \RuntimeException('Unknown search result hit type: ' . get_class($hit->valueObject));
1153
            }
1154
        }
1155
    }
1156
1157
    /**
1158
     * Get fixture directory.
1159
     *
1160
     * @return string
1161
     */
1162
    protected function getFixtureDir()
1163
    {
1164
        return __DIR__ . '/_fixtures/' . getenv('fixtureDir') . '/';
1165
    }
1166
}
1167