Completed
Push — master ( 9146c8...39ecb4 )
by Nicolas
04:52
created

FiltersTest::testMixAnonymousAndNamedFilters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
c 0
b 0
f 0
rs 9.9332
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Elastica\Test\Aggregation;
4
5
use Elastica\Aggregation\Avg;
6
use Elastica\Aggregation\Filters;
7
use Elastica\Document;
8
use Elastica\Index;
9
use Elastica\Query;
10
use Elastica\Query\Term;
11
12
/**
13
 * @internal
14
 */
15
class FiltersTest extends BaseAggregationTest
16
{
17
    /**
18
     * @group unit
19
     */
20 View Code Duplication
    public function testToArrayUsingNamedFilters(): 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...
21
    {
22
        $expected = [
23
            'filters' => [
24
                'filters' => [
25
                    '' => [
26
                        'term' => ['color' => ''],
27
                    ],
28
                    '0' => [
29
                        'term' => ['color' => '0'],
30
                    ],
31
                    'blue' => [
32
                        'term' => ['color' => 'blue'],
33
                    ],
34
                    'red' => [
35
                        'term' => ['color' => 'red'],
36
                    ],
37
                ],
38
            ],
39
            'aggs' => [
40
                'avg_price' => ['avg' => ['field' => 'price']],
41
            ],
42
        ];
43
44
        $agg = new Filters('by_color');
45
46
        $agg->addFilter(new Term(['color' => '']), '');
47
        $agg->addFilter(new Term(['color' => '0']), '0');
48
        $agg->addFilter(new Term(['color' => 'blue']), 'blue');
49
        $agg->addFilter(new Term(['color' => 'red']), 'red');
50
51
        $avg = new Avg('avg_price');
52
        $avg->setField('price');
53
        $agg->addAggregation($avg);
54
55
        $this->assertEquals($expected, $agg->toArray());
56
    }
57
58
    /**
59
     * @group unit
60
     */
61 View Code Duplication
    public function testMixNamedAndAnonymousFilters(): 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...
62
    {
63
        $this->expectException(\Elastica\Exception\InvalidException::class);
64
        $this->expectExceptionMessage('Mix named and anonymous keys are not allowed');
65
66
        $agg = new Filters('by_color');
67
        $agg->addFilter(new Term(['color' => '0']), '0');
68
        $agg->addFilter(new Term(['color' => '0']));
69
    }
70
71
    /**
72
     * @group unit
73
     */
74 View Code Duplication
    public function testMixAnonymousAndNamedFilters(): 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...
75
    {
76
        $this->expectException(\Elastica\Exception\InvalidException::class);
77
        $this->expectExceptionMessage('Mix named and anonymous keys are not allowed');
78
79
        $agg = new Filters('by_color');
80
81
        $agg->addFilter(new Term(['color' => '0']));
82
        $agg->addFilter(new Term(['color' => '0']), '0');
83
    }
84
85
    /**
86
     * @group unit
87
     */
88
    public function testToArrayUsingAnonymousFilters(): void
89
    {
90
        $expected = [
91
            'filters' => [
92
                'filters' => [
93
                    [
94
                        'term' => ['color' => 'blue'],
95
                    ],
96
                    [
97
                        'term' => ['color' => 'red'],
98
                    ],
99
                ],
100
            ],
101
            'aggs' => [
102
                'avg_price' => ['avg' => ['field' => 'price']],
103
            ],
104
        ];
105
106
        $agg = new Filters('by_color');
107
108
        $agg->addFilter(new Term(['color' => 'blue']));
109
        $agg->addFilter(new Term(['color' => 'red']));
110
111
        $avg = new Avg('avg_price');
112
        $avg->setField('price');
113
        $agg->addAggregation($avg);
114
115
        $this->assertEquals($expected, $agg->toArray());
116
    }
117
118
    /**
119
     * @group unit
120
     */
121
    public function testToArrayUsingOtherBucket(): void
122
    {
123
        $expected = [
124
            'filters' => [
125
                'filters' => [
126
                    [
127
                        'term' => ['color' => 'blue'],
128
                    ],
129
                ],
130
                'other_bucket' => true,
131
                'other_bucket_key' => 'other',
132
            ],
133
        ];
134
135
        $agg = new Filters('by_color');
136
137
        $agg->addFilter(new Term(['color' => 'blue']));
138
139
        $agg->setOtherBucket(true);
140
        $agg->setOtherBucketKey('other');
141
142
        $this->assertEquals($expected, $agg->toArray());
143
    }
144
145
    /**
146
     * @group functional
147
     */
148 View Code Duplication
    public function testFilterAggregation(): 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...
149
    {
150
        $agg = new Filters('by_color');
151
        $agg->addFilter(new Term(['color' => 'blue']), 'blue');
152
        $agg->addFilter(new Term(['color' => 'red']), 'red');
153
154
        $avg = new Avg('avg_price');
155
        $avg->setField('price');
156
        $agg->addAggregation($avg);
157
158
        $query = new Query();
159
        $query->addAggregation($agg);
160
161
        $results = $this->_getIndexForTest()->search($query)->getAggregation('by_color');
162
163
        $resultsForBlue = $results['buckets']['blue'];
164
        $resultsForRed = $results['buckets']['red'];
165
166
        $this->assertEquals(2, $resultsForBlue['doc_count']);
167
        $this->assertEquals(1, $resultsForRed['doc_count']);
168
169
        $this->assertEquals((5 + 8) / 2, $resultsForBlue['avg_price']['value']);
170
        $this->assertEquals(1, $resultsForRed['avg_price']['value']);
171
    }
172
173
    /**
174
     * @group functional
175
     */
176 View Code Duplication
    public function testSetOtherBucket(): 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...
177
    {
178
        $agg = new Filters('by_color');
179
        $agg->addFilter(new Term(['color' => 'red']), 'red');
180
        $agg->setOtherBucket(true);
181
182
        $avg = new Avg('avg_price');
183
        $avg->setField('price');
184
        $agg->addAggregation($avg);
185
186
        $query = new Query();
187
        $query->addAggregation($agg);
188
189
        $results = $this->_getIndexForTest()->search($query)->getAggregation('by_color');
190
191
        $resultsForRed = $results['buckets']['red'];
192
        $resultsForOtherBucket = $results['buckets']['_other_'];
193
194
        $this->assertEquals(1, $resultsForRed['doc_count']);
195
        $this->assertEquals(3, $resultsForOtherBucket['doc_count']);
196
197
        $this->assertEquals(1, $resultsForRed['avg_price']['value']);
198
        $this->assertEquals((5 + 8 + 3) / 3, $resultsForOtherBucket['avg_price']['value']);
199
    }
200
201
    /**
202
     * @group functional
203
     */
204 View Code Duplication
    public function testSetOtherBucketKey(): 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...
205
    {
206
        $agg = new Filters('by_color');
207
        $agg->addFilter(new Term(['color' => 'red']), 'red');
208
        $agg->setOtherBucket(true);
209
        $agg->setOtherBucketKey('other_colors');
210
211
        $avg = new Avg('avg_price');
212
        $avg->setField('price');
213
        $agg->addAggregation($avg);
214
215
        $query = new Query();
216
        $query->addAggregation($agg);
217
218
        $results = $this->_getIndexForTest()->search($query)->getAggregation('by_color');
219
220
        $resultsForRed = $results['buckets']['red'];
221
        $resultsForOtherBucket = $results['buckets']['other_colors'];
222
223
        $this->assertEquals(1, $resultsForRed['doc_count']);
224
        $this->assertEquals(3, $resultsForOtherBucket['doc_count']);
225
226
        $this->assertEquals(1, $resultsForRed['avg_price']['value']);
227
        $this->assertEquals((5 + 8 + 3) / 3, $resultsForOtherBucket['avg_price']['value']);
228
    }
229
230 View Code Duplication
    protected function _getIndexForTest(): 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...
231
    {
232
        $index = $this->_createIndex('filter');
233
234
        $index->addDocuments([
235
            new Document(1, ['price' => 5, 'color' => 'blue']),
236
            new Document(2, ['price' => 8, 'color' => 'blue']),
237
            new Document(3, ['price' => 1, 'color' => 'red']),
238
            new Document(4, ['price' => 3, 'color' => 'green']),
239
        ]);
240
241
        $index->refresh();
242
243
        return $index;
244
    }
245
}
246