These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Elastica\Test\Aggregation; |
||
4 | |||
5 | use Elastica\Aggregation\Percentiles; |
||
6 | use Elastica\Document; |
||
7 | use Elastica\Query; |
||
8 | |||
9 | /** |
||
10 | * @internal |
||
11 | */ |
||
12 | class PercentilesTest extends BaseAggregationTest |
||
13 | { |
||
14 | /** |
||
15 | * @group functional |
||
16 | */ |
||
17 | public function testConstruct(): void |
||
18 | { |
||
19 | $aggr = new Percentiles('price_percentile'); |
||
20 | $this->assertEquals('price_percentile', $aggr->getName()); |
||
21 | |||
22 | $aggr = new Percentiles('price_percentile', 'price'); |
||
23 | $this->assertEquals('price', $aggr->getParam('field')); |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * @group functional |
||
28 | */ |
||
29 | public function testSetField(): void |
||
30 | { |
||
31 | $aggr = new Percentiles('price_percentile'); |
||
32 | $aggr->setField('price'); |
||
33 | |||
34 | $this->assertEquals('price', $aggr->getParam('field')); |
||
35 | $this->assertInstanceOf(Percentiles::class, $aggr->setField('price')); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * @group unit |
||
40 | */ |
||
41 | View Code Duplication | public function testCompression(): void |
|
42 | { |
||
43 | $expected = [ |
||
44 | 'percentiles' => [ |
||
45 | 'field' => 'price', |
||
46 | 'keyed' => false, |
||
47 | 'tdigest' => [ |
||
48 | 'compression' => 100, |
||
49 | ], |
||
50 | ], |
||
51 | ]; |
||
52 | $aggr = new Percentiles('price_percentile'); |
||
53 | $aggr->setField('price'); |
||
54 | $aggr->setKeyed(false); |
||
55 | $aggr->setCompression(100); |
||
56 | |||
57 | $this->assertEquals($expected, $aggr->toArray()); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @group unit |
||
62 | */ |
||
63 | View Code Duplication | public function testHdr(): void |
|
64 | { |
||
65 | $expected = [ |
||
66 | 'percentiles' => [ |
||
67 | 'field' => 'price', |
||
68 | 'keyed' => false, |
||
69 | 'hdr' => [ |
||
70 | 'number_of_significant_value_digits' => 2.0, |
||
71 | ], |
||
72 | ], |
||
73 | ]; |
||
74 | $aggr = new Percentiles('price_percentile'); |
||
75 | $aggr->setField('price'); |
||
76 | $aggr->setKeyed(false); |
||
77 | $aggr->setHdr('number_of_significant_value_digits', 2); |
||
78 | |||
79 | $this->assertEquals($expected, $aggr->toArray()); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @group functional |
||
84 | */ |
||
85 | public function testSetPercents(): void |
||
86 | { |
||
87 | $percents = [1, 2, 3]; |
||
88 | $aggr = new Percentiles('price_percentile'); |
||
89 | $aggr->setPercents($percents); |
||
90 | $this->assertEquals($percents, $aggr->getParam('percents')); |
||
91 | $this->assertInstanceOf(Percentiles::class, $aggr->setPercents($percents)); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @group functional |
||
96 | */ |
||
97 | public function testAddPercent(): void |
||
98 | { |
||
99 | $percents = [1, 2, 3]; |
||
100 | $aggr = new Percentiles('price_percentile'); |
||
101 | $aggr->setPercents($percents); |
||
102 | $this->assertEquals($percents, $aggr->getParam('percents')); |
||
103 | $aggr->addPercent(4); |
||
104 | $percents[] = 4; |
||
105 | $this->assertEquals($percents, $aggr->getParam('percents')); |
||
106 | $this->assertInstanceOf(Percentiles::class, $aggr->addPercent(4)); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @group functional |
||
111 | */ |
||
112 | View Code Duplication | public function testSetScript(): void |
|
0 ignored issues
–
show
|
|||
113 | { |
||
114 | $script = 'doc["load_time"].value / 20'; |
||
115 | $aggr = new Percentiles('price_percentile'); |
||
116 | $aggr->setScript($script); |
||
117 | $this->assertEquals($script, $aggr->getParam('script')); |
||
118 | $this->assertInstanceOf(Percentiles::class, $aggr->setScript($script)); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @group functional |
||
123 | */ |
||
124 | public function testActualWork(): void |
||
125 | { |
||
126 | // prepare |
||
127 | $index = $this->_createIndex(); |
||
128 | $index->addDocuments([ |
||
129 | new Document(1, ['price' => 100]), |
||
130 | new Document(2, ['price' => 200]), |
||
131 | new Document(3, ['price' => 300]), |
||
132 | new Document(4, ['price' => 400]), |
||
133 | new Document(5, ['price' => 500]), |
||
134 | new Document(6, ['price' => 600]), |
||
135 | new Document(7, ['price' => 700]), |
||
136 | new Document(8, ['price' => 800]), |
||
137 | new Document(9, ['price' => 900]), |
||
138 | new Document(10, ['price' => 1000]), |
||
139 | ]); |
||
140 | $index->refresh(); |
||
141 | |||
142 | // execute |
||
143 | $aggr = new Percentiles('price_percentile'); |
||
144 | $aggr->setField('price'); |
||
145 | |||
146 | $query = new Query(); |
||
147 | $query->addAggregation($aggr); |
||
148 | |||
149 | $resultSet = $index->search($query); |
||
150 | $aggrResult = $resultSet->getAggregation('price_percentile'); |
||
151 | |||
152 | $this->assertEquals(100.0, $aggrResult['values']['1.0']); |
||
153 | $this->assertEquals(100.0, $aggrResult['values']['5.0']); |
||
154 | $this->assertEquals(300.0, $aggrResult['values']['25.0']); |
||
155 | $this->assertEquals(550.0, $aggrResult['values']['50.0']); |
||
156 | $this->assertEquals(800.0, $aggrResult['values']['75.0']); |
||
157 | $this->assertEquals(1000.0, $aggrResult['values']['95.0']); |
||
158 | $this->assertEquals(1000.0, $aggrResult['values']['99.0']); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @group functional |
||
163 | */ |
||
164 | public function testKeyed(): void |
||
165 | { |
||
166 | $expected = [ |
||
167 | 'values' => [ |
||
168 | [ |
||
169 | 'key' => 1, |
||
170 | 'value' => 100, |
||
171 | ], |
||
172 | [ |
||
173 | 'key' => 5, |
||
174 | 'value' => 100, |
||
175 | ], |
||
176 | [ |
||
177 | 'key' => 25, |
||
178 | 'value' => 300, |
||
179 | ], |
||
180 | [ |
||
181 | 'key' => 50, |
||
182 | 'value' => 550, |
||
183 | ], |
||
184 | [ |
||
185 | 'key' => 75, |
||
186 | 'value' => 800, |
||
187 | ], |
||
188 | [ |
||
189 | 'key' => 95, |
||
190 | 'value' => 1000, |
||
191 | ], |
||
192 | [ |
||
193 | 'key' => 99, |
||
194 | 'value' => 1000, |
||
195 | ], |
||
196 | ], |
||
197 | ]; |
||
198 | |||
199 | // prepare |
||
200 | $index = $this->_createIndex(); |
||
201 | $index->addDocuments([ |
||
202 | new Document(1, ['price' => 100]), |
||
203 | new Document(2, ['price' => 200]), |
||
204 | new Document(3, ['price' => 300]), |
||
205 | new Document(4, ['price' => 400]), |
||
206 | new Document(5, ['price' => 500]), |
||
207 | new Document(6, ['price' => 600]), |
||
208 | new Document(7, ['price' => 700]), |
||
209 | new Document(8, ['price' => 800]), |
||
210 | new Document(9, ['price' => 900]), |
||
211 | new Document(10, ['price' => 1000]), |
||
212 | ]); |
||
213 | $index->refresh(); |
||
214 | |||
215 | // execute |
||
216 | $aggr = new Percentiles('price_percentile'); |
||
217 | $aggr->setField('price'); |
||
218 | $aggr->setKeyed(false); |
||
219 | |||
220 | $query = new Query(); |
||
221 | $query->addAggregation($aggr); |
||
222 | |||
223 | $resultSet = $index->search($query); |
||
224 | $aggrResult = $resultSet->getAggregation('price_percentile'); |
||
225 | |||
226 | $this->assertEquals($expected, $aggrResult); |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * @group unit |
||
231 | */ |
||
232 | public function testMissing(): void |
||
233 | { |
||
234 | $expected = [ |
||
235 | 'percentiles' => [ |
||
236 | 'field' => 'price', |
||
237 | 'keyed' => false, |
||
238 | 'hdr' => [ |
||
239 | 'number_of_significant_value_digits' => 2.0, |
||
240 | ], |
||
241 | 'missing' => 10, |
||
242 | ], |
||
243 | ]; |
||
244 | $aggr = new Percentiles('price_percentile'); |
||
245 | $aggr->setField('price'); |
||
246 | $aggr->setKeyed(false); |
||
247 | $aggr->setHdr('number_of_significant_value_digits', 2); |
||
248 | $aggr->setMissing(10); |
||
249 | |||
250 | $this->assertEquals($expected, $aggr->toArray()); |
||
251 | } |
||
252 | } |
||
253 |
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.