-
Notifications
You must be signed in to change notification settings - Fork 731
/
StatsBucketTest.php
92 lines (74 loc) · 2.45 KB
/
StatsBucketTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace Elastica\Test\Aggregation;
use Elastica\Aggregation\Histogram;
use Elastica\Aggregation\Max;
use Elastica\Aggregation\StatsBucket;
use Elastica\Document;
use Elastica\Index;
use Elastica\Query;
/**
* @internal
*/
class StatsBucketTest extends BaseAggregationTest
{
/**
* @group functional
*/
public function testStatBucketAggregation(): void
{
$bucketScriptAggregation = new StatsBucket('result', 'age_groups>max_weight');
$histogramAggregation = new Histogram('age_groups', 'age');
$histogramAggregation->setFixedInterval(10);
$histogramAggregation->addAggregation((new Max('max_weight'))->setField('weight'));
$query = Query::create([])
->addAggregation($histogramAggregation)
->addAggregation($bucketScriptAggregation)
;
$results = $this->_getIndexForTest()->search($query)->getAggregation('result');
$this->assertEquals(3, $results['count']);
$this->assertEquals(50, $results['min']);
$this->assertEquals(70, $results['max']);
$this->assertEquals(60, $results['avg']);
$this->assertEquals(180, $results['sum']);
}
/**
* @group unit
*/
public function testConstructThroughSetters(): void
{
$serialDiffAgg = new StatsBucket('bucket_part');
$serialDiffAgg
->setBucketsPath('age_groups>max_weight')
->setFormat('test_format')
->setGapPolicy(10)
;
$expected = [
'stats_bucket' => [
'buckets_path' => 'age_groups>max_weight',
'format' => 'test_format',
'gap_policy' => 10,
],
];
$this->assertEquals($expected, $serialDiffAgg->toArray());
}
/**
* @group unit
*/
public function testToArrayInvalidBucketsPath(): void
{
$this->expectException(\Elastica\Exception\InvalidException::class);
$serialDiffAgg = new StatsBucket('bucket_part');
$serialDiffAgg->toArray();
}
protected function _getIndexForTest(): Index
{
$index = $this->_createIndex();
$index->addDocuments([
Document::create(['weight' => 60, 'height' => 180, 'age' => 25]),
Document::create(['weight' => 70, 'height' => 156, 'age' => 32]),
Document::create(['weight' => 50, 'height' => 155, 'age' => 45]),
]);
$index->refresh();
return $index;
}
}