From b1b6c980cce2ffc3f278e0d3d02e61b174cee52e Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Thu, 10 Dec 2020 12:55:39 +0100 Subject: [PATCH] Fix Composite::addAfter(null) (#1877) --- CHANGELOG.md | 1 + src/Aggregation/Composite.php | 4 ++++ tests/Aggregation/CompositeTest.php | 37 +++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f795b1caee..69aacdb897 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Replaced `_routing` and `_retry_on_conflict` by `routing` and `retry_on_conflict` in `AbstractUpdateAction` [#1807](https://github.com/ruflin/Elastica/issues/1807) * Fixed `Elastica\Util::toSnakeCase()` with first letter being lower cased [#1831](https://github.com/ruflin/Elastica/pull/1831) * Fixed handling precision as string in `Elastica\Aggregation\GeohashGrid::setPrecision()` [#1884](https://github.com/ruflin/Elastica/pull/1884) +* Fixed calling `Elastica\Aggregation\Composite::addAfter()` with the `null` value [1877](https://github.com/ruflin/Elastica/pull/1877) ### Security diff --git a/src/Aggregation/Composite.php b/src/Aggregation/Composite.php index 86e81707b1..cd0a328f60 100644 --- a/src/Aggregation/Composite.php +++ b/src/Aggregation/Composite.php @@ -25,6 +25,10 @@ public function addSource(AbstractAggregation $aggregation): self */ public function addAfter(?array $checkpoint): self { + if (null === $checkpoint) { + return $this; + } + return $this->setParam('after', $checkpoint); } } diff --git a/tests/Aggregation/CompositeTest.php b/tests/Aggregation/CompositeTest.php index 1d97e34f64..9bb86693f8 100644 --- a/tests/Aggregation/CompositeTest.php +++ b/tests/Aggregation/CompositeTest.php @@ -178,6 +178,43 @@ public function testCompositeWithAfterAggregation(): void $this->assertEquals($expected, $results); } + /** + * @group functional + */ + public function testCompositeWithNullAfter(): void + { + $composite = new Composite('products'); + $composite->setSize(2); + $composite->addSource((new Terms('color'))->setField('color.keyword')); + $composite->addAfter(null); + + $query = new Query(); + $query->addAggregation($composite); + + $results = $this->_getIndexForTest()->search($query)->getAggregation('products'); + $expected = [ + 'after_key' => [ + 'color' => 'green', + ], + 'buckets' => [ + [ + 'key' => [ + 'color' => 'blue', + ], + 'doc_count' => 2, + ], + [ + 'key' => [ + 'color' => 'green', + ], + 'doc_count' => 1, + ], + ], + ]; + + $this->assertEquals($expected, $results); + } + protected function _getIndexForTest(): Index { $index = $this->_createIndex();