File tree Expand file tree Collapse file tree 2 files changed +89
-0
lines changed
Expand file tree Collapse file tree 2 files changed +89
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Erichard \ElasticQueryBuilder \Aggregation ;
6+
7+ class AvgAggregation extends MetricAggregation
8+ {
9+ protected function getType (): string
10+ {
11+ return 'avg ' ;
12+ }
13+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Tests \Erichard \ElasticQueryBuilder \Aggregation ;
6+
7+ use Erichard \ElasticQueryBuilder \Aggregation \AvgAggregation ;
8+ use Erichard \ElasticQueryBuilder \Options \SourceScript ;
9+ use PHPUnit \Framework \TestCase ;
10+
11+ class AvgAggregationTest extends TestCase
12+ {
13+ public function testBuildWithNameOnly (): void
14+ {
15+ $ query = new AvgAggregation ('avg_price ' );
16+
17+ $ this ->assertEquals ([
18+ 'avg ' => [
19+ 'field ' => 'avg_price ' ,
20+ ],
21+ ], $ query ->build ());
22+ }
23+
24+ public function testItBuildTheAggregationUsingAField (): void
25+ {
26+ $ query = new AvgAggregation ('avg_price ' );
27+ $ query ->setField ('price ' );
28+
29+ $ this ->assertEquals ([
30+ 'avg ' => [
31+ 'field ' => 'price ' ,
32+ ],
33+ ], $ query ->build ());
34+ }
35+
36+ public function testItBuildTheAggregationUsingAScript (): void
37+ {
38+ $ query = new AvgAggregation ('avg_price ' , new SourceScript ('doc.price.value ' ));
39+
40+ $ this ->assertEquals ([
41+ 'avg ' => [
42+ 'script ' => [
43+ 'source ' => 'doc.price.value ' ,
44+ ],
45+ ],
46+ ], $ query ->build ());
47+ }
48+
49+ public function testItBuildTheAggregationUsingAScriptViaSet (): void
50+ {
51+ $ query = new AvgAggregation ('avg_price ' );
52+ $ query ->setScript ('doc.price.value ' );
53+
54+ $ this ->assertEquals ([
55+ 'avg ' => [
56+ 'script ' => [
57+ 'source ' => 'doc.price.value ' ,
58+ ],
59+ ],
60+ ], $ query ->build ());
61+ }
62+
63+ public function testItBuildTheAggregationWithMissingValue (): void
64+ {
65+ $ query = new AvgAggregation ('avg_price ' );
66+ $ query ->setField ('price ' );
67+ $ query ->setMissing (10 );
68+
69+ $ this ->assertEquals ([
70+ 'avg ' => [
71+ 'field ' => 'price ' ,
72+ 'missing ' => 10 ,
73+ ],
74+ ], $ query ->build ());
75+ }
76+ }
You can’t perform that action at this time.
0 commit comments