From 24ec4d11cb2cac35656810a4bd632e6f748ad952 Mon Sep 17 00:00:00 2001 From: Mahdi Masaeli Date: Mon, 24 Jun 2024 12:29:38 +0330 Subject: [PATCH 1/2] Feat: write avg aggregation class --- src/Aggregation/AvgAggregation.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/Aggregation/AvgAggregation.php diff --git a/src/Aggregation/AvgAggregation.php b/src/Aggregation/AvgAggregation.php new file mode 100644 index 0000000..67633fa --- /dev/null +++ b/src/Aggregation/AvgAggregation.php @@ -0,0 +1,13 @@ + Date: Mon, 24 Jun 2024 12:30:10 +0330 Subject: [PATCH 2/2] Feat: write avg aggregation test --- tests/Aggregation/AvgAggregationTest.php | 76 ++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 tests/Aggregation/AvgAggregationTest.php diff --git a/tests/Aggregation/AvgAggregationTest.php b/tests/Aggregation/AvgAggregationTest.php new file mode 100644 index 0000000..9cb64d2 --- /dev/null +++ b/tests/Aggregation/AvgAggregationTest.php @@ -0,0 +1,76 @@ +assertEquals([ + 'avg' => [ + 'field' => 'avg_price', + ], + ], $query->build()); + } + + public function testItBuildTheAggregationUsingAField(): void + { + $query = new AvgAggregation('avg_price'); + $query->setField('price'); + + $this->assertEquals([ + 'avg' => [ + 'field' => 'price', + ], + ], $query->build()); + } + + public function testItBuildTheAggregationUsingAScript(): void + { + $query = new AvgAggregation('avg_price', new SourceScript('doc.price.value')); + + $this->assertEquals([ + 'avg' => [ + 'script' => [ + 'source' => 'doc.price.value', + ], + ], + ], $query->build()); + } + + public function testItBuildTheAggregationUsingAScriptViaSet(): void + { + $query = new AvgAggregation('avg_price'); + $query->setScript('doc.price.value'); + + $this->assertEquals([ + 'avg' => [ + 'script' => [ + 'source' => 'doc.price.value', + ], + ], + ], $query->build()); + } + + public function testItBuildTheAggregationWithMissingValue(): void + { + $query = new AvgAggregation('avg_price'); + $query->setField('price'); + $query->setMissing(10); + + $this->assertEquals([ + 'avg' => [ + 'field' => 'price', + 'missing' => 10, + ], + ], $query->build()); + } +}