Completed
Pull Request — master (#1875)
by romain
03:31 queued 01:02
created

TermsTest::testTermsWithMissingAggregation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Elastica\Test\Aggregation;
4
5
use Elastica\Aggregation\Terms;
6
use Elastica\Document;
7
use Elastica\Index;
8
use Elastica\Mapping;
9
use Elastica\Query;
10
11
/**
12
 * @group functional
13
 *
14
 * @internal
15
 */
16
class TermsTest extends BaseAggregationTest
17
{
18
    /**
19
     * @group unit
20
     */
21
    public function testIncludePattern(): void
22
    {
23
        $agg = new Terms('terms');
24
        $agg->setInclude('pattern*');
25
26
        $this->assertSame('pattern*', $agg->getParam('include'));
27
    }
28
29
    /**
30
     * @group unit
31
     */
32 View Code Duplication
    public function testIncludeExactMatch(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
33
    {
34
        $agg = new Terms('terms');
35
        $agg->setIncludeAsExactMatch(['first', 'second']);
36
37
        $this->assertSame(['first', 'second'], $agg->getParam('include'));
38
    }
39
40
    /**
41
     * @group unit
42
     */
43 View Code Duplication
    public function testIncludeWithPartitions(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
44
    {
45
        $agg = new Terms('terms');
46
        $agg->setIncludeWithPartitions(1, 23);
47
48
        $this->assertSame([
49
            'partition' => 1,
50
            'num_partitions' => 23,
51
        ], $agg->getParam('include'));
52
    }
53
54
    /**
55
     * @group unit
56
     */
57
    public function testExcludePattern(): void
58
    {
59
        $agg = new Terms('terms');
60
        $agg->setExclude('pattern*');
61
62
        $this->assertSame('pattern*', $agg->getParam('exclude'));
63
    }
64
65
    /**
66
     * @group unit
67
     */
68 View Code Duplication
    public function testExcludeExactMatch(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
69
    {
70
        $agg = new Terms('terms');
71
        $agg->setExcludeAsExactMatch(['first', 'second']);
72
73
        $this->assertSame(['first', 'second'], $agg->getParam('exclude'));
74
    }
75
76 View Code Duplication
    public function testTermsAggregation(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
77
    {
78
        $agg = new Terms('terms');
79
        $agg->setField('color');
80
81
        $query = new Query();
82
        $query->addAggregation($agg);
83
        $results = $this->getIndex()->search($query)->getAggregation('terms');
84
85
        $this->assertEquals(2, $results['buckets'][0]['doc_count']);
86
        $this->assertEquals('blue', $results['buckets'][0]['key']);
87
    }
88
89 View Code Duplication
    public function testTermsWithMissingAggregation(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
90
    {
91
        $agg = new Terms('terms');
92
        $agg->setField('color');
93
        $agg->setMissing('blue');
94
95
        $query = new Query();
96
        $query->addAggregation($agg);
97
        $results = $this->getIndex()->search($query)->getAggregation('terms');
98
99
        $this->assertEquals(3, $results['buckets'][0]['doc_count']);
100
        $this->assertEquals('blue', $results['buckets'][0]['key']);
101
    }
102
103 View Code Duplication
    public function testTermsSetOrder(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
104
    {
105
        $agg = new Terms('terms');
106
        $agg->setField('color');
107
        $agg->setOrder('_count', 'asc');
108
109
        $query = new Query();
110
        $query->addAggregation($agg);
111
        $results = $this->getIndex()->search($query)->getAggregation('terms');
112
113
        $this->assertEquals('blue', $results['buckets'][2]['key']);
114
    }
115
116
    public function testTermsSetOrders(): void
117
    {
118
        $agg = new Terms('terms');
119
        $agg->setField('color');
120
        $agg->setOrders([
121
            ['_count' => 'asc'], // 1. red,   2. green, 3. blue
122
            ['_key' => 'asc'],   // 1. green, 2. red,   3. blue
123
        ]);
124
125
        $query = new Query();
126
        $query->addAggregation($agg);
127
        $results = $this->getIndex()->search($query)->getAggregation('terms');
128
129
        $this->assertSame('green', $results['buckets'][0]['key']);
130
        $this->assertSame('red', $results['buckets'][1]['key']);
131
        $this->assertSame('blue', $results['buckets'][2]['key']);
132
    }
133
134 View Code Duplication
    private function getIndex(): Index
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
135
    {
136
        $index = $this->_createIndex();
137
138
        $mapping = new Mapping([
139
            'color' => ['type' => 'keyword'],
140
        ]);
141
        $index->setMapping($mapping);
142
143
        $index->addDocuments([
144
            new Document(1, ['color' => 'blue']),
145
            new Document(2, ['color' => 'blue']),
146
            new Document(3, ['color' => 'red']),
147
            new Document(4, ['color' => 'green']),
148
            new Document(5, ['anything' => 'anything']),
149
        ]);
150
151
        $index->refresh();
152
153
        return $index;
154
    }
155
}
156