From ac9a2d0ae66d99f5e3e847243427ac60c83494d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simonas=20=C5=A0erlinskas?= <simonas.serlinskas@nfq.com> Date: Mon, 10 Aug 2015 09:58:25 +0300 Subject: [PATCH] fixed top hits aggregation creation --- src/Aggregation/TopHitsAggregation.php | 21 ++--------- tests/Aggregation/TopHitsAggregationTest.php | 39 ++++++++++++++++---- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/Aggregation/TopHitsAggregation.php b/src/Aggregation/TopHitsAggregation.php index 3ee3098..70a0ff1 100644 --- a/src/Aggregation/TopHitsAggregation.php +++ b/src/Aggregation/TopHitsAggregation.php @@ -124,27 +124,10 @@ class TopHitsAggregation extends AbstractAggregation * {@inheritdoc} */ public function getArray() - { - $data = new \stdClass(); - - $filteredData = $this->getFilteredData(); - foreach ($filteredData as $key => $value) { - $data->{$key} = $value; - } - - return $data; - } - - /** - * Filters the data. - * - * @return array - */ - private function getFilteredData() { $output = array_filter( [ - 'sort' => $this->getSort() ? $this->getSort()->toArray() : [], + 'sort' => $this->getSort() ? $this->getSort()->toArray() : null, 'size' => $this->getSize(), 'from' => $this->getFrom(), ], @@ -153,6 +136,8 @@ class TopHitsAggregation extends AbstractAggregation } ); + $output = $this->processArray($output); + return $output; } } diff --git a/tests/Aggregation/TopHitsAggregationTest.php b/tests/Aggregation/TopHitsAggregationTest.php index cd63b50..6e4ca21 100644 --- a/tests/Aggregation/TopHitsAggregationTest.php +++ b/tests/Aggregation/TopHitsAggregationTest.php @@ -26,18 +26,43 @@ class TopHitsAggregationTest extends \PHPUnit_Framework_TestCase public function testToArray() { $sort = new FieldSort('acme'); - $aggregation = new TopHitsAggregation('acme', 0, 1, $sort); + $aggregation = new TopHitsAggregation('acme', 1, 1, $sort); - $expectedAgg = new \stdClass(); - $expectedAgg->size = 0; - $expectedAgg->from = 1; - $expectedAgg->sort = $sort->toArray(); $expected = [ 'acme' => [ - 'top_hits' => $expectedAgg, + 'top_hits' => [ + 'sort' => [ + 'acme' => [], + ], + 'size' => 1, + 'from' => 1, + ], ], ]; - $this->assertEquals($expected, $aggregation->toArray()); + $this->assertSame($expected, $aggregation->toArray()); + } + + /** + * Check if parameters can be set to agg. + */ + public function testParametersAddition() + { + $aggregation = new TopHitsAggregation('acme', 0, 1); + $aggregation->addParameter('_source', ['include' => ['title']]); + + $expected = [ + 'acme' => [ + 'top_hits' => [ + 'size' => 0, + 'from' => 1, + '_source' => [ + 'include' => ['title'], + ] + ], + ], + ]; + + $this->assertSame($expected, $aggregation->toArray()); } } -- GitLab