1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elastica\Test\Query; |
4
|
|
|
|
5
|
|
|
use Elastica\Document; |
6
|
|
|
use Elastica\Query; |
7
|
|
|
use Elastica\Query\MatchQuery; |
8
|
|
|
use Elastica\Query\Term; |
9
|
|
|
use Elastica\Test\Base as BaseTest; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @internal |
13
|
|
|
*/ |
14
|
|
|
class PostFilterTest extends BaseTest |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @group unit |
18
|
|
|
*/ |
19
|
|
View Code Duplication |
public function testToArray(): void |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$query = new Query(); |
22
|
|
|
|
23
|
|
|
$postFilter = new Term(['color' => 'green']); |
24
|
|
|
$query->setPostFilter($postFilter); |
25
|
|
|
|
26
|
|
|
$data = $query->toArray(); |
27
|
|
|
|
28
|
|
|
$this->assertArrayHasKey('post_filter', $data); |
29
|
|
|
$this->assertEquals(['term' => ['color' => 'green']], $data['post_filter']); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @group functional |
34
|
|
|
*/ |
35
|
|
|
public function testQuery(): void |
36
|
|
|
{ |
37
|
|
|
$query = new Query(); |
38
|
|
|
|
39
|
|
|
$match = new MatchQuery(); |
40
|
|
|
$match->setField('make', 'ford'); |
41
|
|
|
|
42
|
|
|
$query->setQuery($match); |
43
|
|
|
|
44
|
|
|
$filter = new Term(); |
45
|
|
|
$filter->setTerm('color', 'green'); |
46
|
|
|
|
47
|
|
|
$query->setPostFilter($filter); |
48
|
|
|
|
49
|
|
|
$this->assertEquals(1, $this->_getTestIndex()->count($query)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
View Code Duplication |
protected function _getTestIndex() |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$index = $this->_createIndex(); |
55
|
|
|
$docs = [ |
56
|
|
|
new Document(1, ['color' => 'green', 'make' => 'ford']), |
57
|
|
|
new Document(2, ['color' => 'blue', 'make' => 'volvo']), |
58
|
|
|
new Document(3, ['color' => 'red', 'make' => 'ford']), |
59
|
|
|
new Document(4, ['color' => 'green', 'make' => 'renault']), |
60
|
|
|
]; |
61
|
|
|
$index->addDocuments($docs); |
62
|
|
|
$index->refresh(); |
63
|
|
|
|
64
|
|
|
return $index; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
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.