From 1a500955629c66c08f9ad67697b6ae804a370564 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simonas=20=C5=A0erlinskas?= <simonas.serlinskas@nfq.com> Date: Tue, 11 Aug 2015 14:19:46 +0300 Subject: [PATCH] refactored aggs implementation --- src/Aggregation/AbstractAggregation.php | 9 +- src/Aggregation/GlobalAggregation.php | 2 +- src/Aggregation/ReverseNestedAggregation.php | 4 +- src/Search.php | 86 ++++++++++++++++-- src/SearchEndpoint/AbstractSearchEndpoint.php | 8 ++ src/SearchEndpoint/AggregationsEndpoint.php | 6 +- src/SearchEndpoint/HighlightEndpoint.php | 8 ++ .../SearchEndpointInterface.php | 7 ++ tests/Aggregation/FilterAggregationTest.php | 55 +++--------- tests/Aggregation/FiltersAggregationTest.php | 38 ++++---- .../Aggregation/GeoBoundsAggregationTest.php | 16 ++-- .../GeoDistanceAggregationTest.php | 21 ++--- tests/Aggregation/GlobalAggregationTest.php | 15 +--- .../Aggregation/Ipv4RangeAggregationTest.php | 16 ++-- tests/Aggregation/NestedAggregationTest.php | 21 ++--- .../PercentileRanksAggregationTest.php | 8 +- tests/Aggregation/RangeAggregationTest.php | 90 +++++++++---------- .../ReverseNestedAggregationTest.php | 45 +++------- tests/Aggregation/StatsAggregationTest.php | 12 +-- tests/Aggregation/TermsAggregationTest.php | 77 +++++++--------- tests/Aggregation/TopHitsAggregationTest.php | 24 +++-- 21 files changed, 273 insertions(+), 295 deletions(-) diff --git a/src/Aggregation/AbstractAggregation.php b/src/Aggregation/AbstractAggregation.php index 938a41e..1f4a7c1 100644 --- a/src/Aggregation/AbstractAggregation.php +++ b/src/Aggregation/AbstractAggregation.php @@ -124,16 +124,14 @@ abstract class AbstractAggregation implements BuilderInterface { $array = $this->getArray(); $result = [ - $this->getName() => [ - $this->getType() => is_array($array) ? $this->processArray($array) : $array, - ], + $this->getType() => is_array($array) ? $this->processArray($array) : $array, ]; if ($this->supportsNesting()) { $nestedResult = $this->collectNestedAggregations(); if (!empty($nestedResult)) { - $result[$this->getName()]['aggregations'] = $nestedResult; + $result['aggregations'] = $nestedResult; } } @@ -148,8 +146,9 @@ abstract class AbstractAggregation implements BuilderInterface protected function collectNestedAggregations() { $result = []; + /** @var AbstractAggregation $aggregation */ foreach ($this->getAggregations() as $aggregation) { - $result = array_merge($result, $aggregation->toArray()); + $result[$aggregation->getName()] = $aggregation->toArray(); } return $result; diff --git a/src/Aggregation/GlobalAggregation.php b/src/Aggregation/GlobalAggregation.php index ac48255..09beb1c 100644 --- a/src/Aggregation/GlobalAggregation.php +++ b/src/Aggregation/GlobalAggregation.php @@ -41,6 +41,6 @@ class GlobalAggregation extends AbstractAggregation */ public function getArray() { - return new \stdClass(); + return []; } } diff --git a/src/Aggregation/ReverseNestedAggregation.php b/src/Aggregation/ReverseNestedAggregation.php index c833135..7952961 100644 --- a/src/Aggregation/ReverseNestedAggregation.php +++ b/src/Aggregation/ReverseNestedAggregation.php @@ -75,9 +75,9 @@ class ReverseNestedAggregation extends AbstractAggregation throw new \LogicException("Reverse Nested aggregation `{$this->getName()}` has no aggregations added"); } - $data = new \stdClass(); + $data = []; if ($this->getPath()) { - $data = ['path' => $this->getPath()]; + $data['path'] = $this->getPath(); } return $data; diff --git a/src/Search.php b/src/Search.php index 6529ce4..71d9ab0 100644 --- a/src/Search.php +++ b/src/Search.php @@ -25,6 +25,7 @@ use ONGR\ElasticsearchDSL\SearchEndpoint\SearchEndpointInterface; use ONGR\ElasticsearchDSL\SearchEndpoint\SortEndpoint; use ONGR\ElasticsearchDSL\Serializer\Normalizer\CustomReferencedNormalizer; use ONGR\ElasticsearchDSL\Serializer\OrderedSerializer; +use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Serializer\Normalizer\CustomNormalizer; /** @@ -117,7 +118,7 @@ class Search * * @return SearchEndpointInterface */ - public function getEndpoint($type) + private function getEndpoint($type) { if (!array_key_exists($type, $this->endpoints)) { $this->endpoints[$type] = SearchEndpointFactory::get($type); @@ -153,6 +154,18 @@ class Search return $this; } + /** + * Returns queries inside BoolQuery instance. + * + * @return BuilderInterface + */ + public function getQueries() + { + $endpoint = $this->getEndpoint(QueryEndpoint::NAME); + + return $endpoint->getBool(); + } + /** * Adds a filter to the search. * @@ -175,17 +188,15 @@ class Search } /** - * Adds aggregation into search. - * - * @param AbstractAggregation $aggregation + * Returns queries inside BoolFilter instance. * - * @return $this; + * @return BuilderInterface */ - public function addAggregation(AbstractAggregation $aggregation) + public function getFilters() { - $this->getEndpoint(AggregationsEndpoint::NAME)->add($aggregation, $aggregation->getName()); + $endpoint = $this->getEndpoint(FilterEndpoint::NAME); - return $this; + return $endpoint->getBool(); } /** @@ -209,6 +220,42 @@ class Search return $this; } + /** + * Returns queries inside BoolFilter instance. + * + * @return BuilderInterface + */ + public function getPostFilters() + { + $endpoint = $this->getEndpoint(FilterEndpoint::NAME); + + return $endpoint->getBool(); + } + + /** + * Adds aggregation into search. + * + * @param AbstractAggregation $aggregation + * + * @return $this; + */ + public function addAggregation(AbstractAggregation $aggregation) + { + $this->getEndpoint(AggregationsEndpoint::NAME)->add($aggregation, $aggregation->getName()); + + return $this; + } + + /** + * Returns all aggregations. + * + * @return BuilderInterface[] + */ + public function getAggregations() + { + return $this->getEndpoint(AggregationsEndpoint::NAME)->getAll(); + } + /** * Adds sort to search. * @@ -223,6 +270,16 @@ class Search return $this; } + /** + * Returns all set sorts. + * + * @return BuilderInterface[] + */ + public function getSorts() + { + return $this->getEndpoint(SortEndpoint::NAME)->getAll(); + } + /** * Allows to highlight search results on one or more fields. * @@ -237,6 +294,19 @@ class Search return $this; } + /** + * Returns highlight builder. + * + * @return BuilderInterface + */ + public function getHighlight() + { + /** @var HighlightEndpoint $highlightEndpoint */ + $highlightEndpoint = $this->getEndpoint(HighlightEndpoint::NAME); + + return $highlightEndpoint->getHighlight(); + } + /** * Exclude documents which have a _score less than the minimum specified. * diff --git a/src/SearchEndpoint/AbstractSearchEndpoint.php b/src/SearchEndpoint/AbstractSearchEndpoint.php index 0ec3582..ff008e5 100644 --- a/src/SearchEndpoint/AbstractSearchEndpoint.php +++ b/src/SearchEndpoint/AbstractSearchEndpoint.php @@ -96,4 +96,12 @@ abstract class AbstractSearchEndpoint extends AbstractNormalizable implements Se { return $this->container; } + + /** + * {@inheritdoc} + */ + public function getBool() + { + throw new \BadFunctionCallException(sprintf("Endpoint %s doesn't support bool statements", static::NAME)); + } } diff --git a/src/SearchEndpoint/AggregationsEndpoint.php b/src/SearchEndpoint/AggregationsEndpoint.php index 56c2c50..2f39291 100644 --- a/src/SearchEndpoint/AggregationsEndpoint.php +++ b/src/SearchEndpoint/AggregationsEndpoint.php @@ -11,6 +11,7 @@ namespace ONGR\ElasticsearchDSL\SearchEndpoint; +use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; use ONGR\ElasticsearchDSL\BuilderBag; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; @@ -29,10 +30,11 @@ class AggregationsEndpoint extends AbstractSearchEndpoint */ public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) { + $output = []; if (count($this->getAll()) > 0) { - $output = []; + /** @var AbstractAggregation $aggregation */ foreach ($this->getAll() as $aggregation) { - $output[] = $aggregation->toArray(); + $output[$aggregation->getName()] = $aggregation->toArray(); } } diff --git a/src/SearchEndpoint/HighlightEndpoint.php b/src/SearchEndpoint/HighlightEndpoint.php index 45d9c9f..f2b8d8e 100644 --- a/src/SearchEndpoint/HighlightEndpoint.php +++ b/src/SearchEndpoint/HighlightEndpoint.php @@ -66,4 +66,12 @@ class HighlightEndpoint extends AbstractSearchEndpoint { return [$this->key => $this->highlight]; } + + /** + * @return BuilderInterface + */ + public function getHighlight() + { + return $this->highlight; + } } diff --git a/src/SearchEndpoint/SearchEndpointInterface.php b/src/SearchEndpoint/SearchEndpointInterface.php index 7ee7178..e899f31 100644 --- a/src/SearchEndpoint/SearchEndpointInterface.php +++ b/src/SearchEndpoint/SearchEndpointInterface.php @@ -68,4 +68,11 @@ interface SearchEndpointInterface extends NormalizableInterface * @return array */ public function getAll($boolType = null); + + /** + * Returns Bool filter or query instance with all builder objects inside. + * + * @return BuilderInterface + */ + public function getBool(); } diff --git a/tests/Aggregation/FilterAggregationTest.php b/tests/Aggregation/FilterAggregationTest.php index d13fdbd..ce5111b 100644 --- a/tests/Aggregation/FilterAggregationTest.php +++ b/tests/Aggregation/FilterAggregationTest.php @@ -13,7 +13,9 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\DSL\Aggregation; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; use ONGR\ElasticsearchDSL\Aggregation\FilterAggregation; +use ONGR\ElasticsearchDSL\Aggregation\HistogramAggregation; use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\Filter\MatchAllFilter; use ONGR\ElasticsearchDSL\Filter\MissingFilter; class FilterAggregationTest extends \PHPUnit_Framework_TestCase @@ -29,26 +31,13 @@ class FilterAggregationTest extends \PHPUnit_Framework_TestCase // Case #0 filter aggregation. $aggregation = new FilterAggregation('test_agg'); - - $filter = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface') - ->setMethods(['toArray', 'getType']) - ->getMockForAbstractClass(); - $filter->expects($this->any()) - ->method('getType') - ->willReturn('test_filter'); - $filter->expects($this->any()) - ->method('toArray') - ->willReturn(['test_field' => ['test_value' => 'test']]); + $filter = new MatchAllFilter(); $aggregation->setFilter($filter); $result = [ - AbstractAggregation::PREFIX.'test_agg' => [ - 'filter' => [ - 'test_filter' => [ - 'test_field' => ['test_value' => 'test'], - ], - ], + 'filter' => [ + $filter->getType() => $filter->toArray(), ], ]; @@ -61,31 +50,15 @@ class FilterAggregationTest extends \PHPUnit_Framework_TestCase $aggregation = new FilterAggregation('test_agg'); $aggregation->setFilter($filter); - $aggregation2 = $this->getMockBuilder('ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation') - ->disableOriginalConstructor() - ->setMethods(['toArray', 'getName']) - ->getMockForAbstractClass(); - $aggregation2->expects($this->any()) - ->method('toArray') - ->willReturn([AbstractAggregation::PREFIX.'test_agg2' => ['avg' => []]]); - $aggregation2->expects($this->any()) - ->method('getName') - ->willReturn('test_agg2'); - - $aggregation->addAggregation($aggregation2); + $histogramAgg = new HistogramAggregation('acme', 'bar', 10); + $aggregation->addAggregation($histogramAgg); $result = [ - AbstractAggregation::PREFIX.'test_agg' => [ - 'filter' => [ - 'test_filter' => [ - 'test_field' => ['test_value' => 'test'], - ], - ], - 'aggregations' => [ - AbstractAggregation::PREFIX.'test_agg2' => [ - 'avg' => [], - ], - ], + 'filter' => [ + $filter->getType() => $filter->toArray(), + ], + 'aggregations' => [ + $histogramAgg->getName() => $histogramAgg->toArray(), ], ]; @@ -155,9 +128,7 @@ class FilterAggregationTest extends \PHPUnit_Framework_TestCase $aggregation = new FilterAggregation('test', $builderInterface); $this->assertSame( [ - AbstractAggregation::PREFIX.'test' => [ - 'filter' => [null => null], - ], + 'filter' => [null => null], ], $aggregation->toArray() ); diff --git a/tests/Aggregation/FiltersAggregationTest.php b/tests/Aggregation/FiltersAggregationTest.php index 9d91409..3a6a902 100644 --- a/tests/Aggregation/FiltersAggregationTest.php +++ b/tests/Aggregation/FiltersAggregationTest.php @@ -76,21 +76,19 @@ class FiltersAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addFilter($filter, 'second'); $results = $aggregation->toArray(); $expected = [ - AbstractAggregation::PREFIX.'test_agg' => [ + 'filters' => [ 'filters' => [ - 'filters' => [ - 'first' => [ - 'test_filter' => [ - 'test_field' => [ - 'test_value' => 'test', - ], + 'first' => [ + 'test_filter' => [ + 'test_field' => [ + 'test_value' => 'test', ], ], - 'second' => [ - 'test_filter' => [ - 'test_field' => [ - 'test_value' => 'test', - ], + ], + 'second' => [ + 'test_filter' => [ + 'test_field' => [ + 'test_value' => 'test', ], ], ], @@ -122,12 +120,10 @@ class FiltersAggregationTest extends \PHPUnit_Framework_TestCase $this->assertSame( [ - AbstractAggregation::PREFIX.'test' => [ + 'filters' => [ 'filters' => [ - 'filters' => [ - 'filter1' => ['type1' => null], - 'filter2' => ['type2' => null], - ], + 'filter1' => ['type1' => null], + 'filter2' => ['type2' => null], ], ], ], @@ -145,12 +141,10 @@ class FiltersAggregationTest extends \PHPUnit_Framework_TestCase $this->assertSame( [ - AbstractAggregation::PREFIX.'test' => [ + 'filters' => [ 'filters' => [ - 'filters' => [ - ['type1' => null], - ['type2' => null], - ], + ['type1' => null], + ['type2' => null], ], ], ], diff --git a/tests/Aggregation/GeoBoundsAggregationTest.php b/tests/Aggregation/GeoBoundsAggregationTest.php index aec9a1f..ac1ebfa 100644 --- a/tests/Aggregation/GeoBoundsAggregationTest.php +++ b/tests/Aggregation/GeoBoundsAggregationTest.php @@ -49,22 +49,18 @@ class GeoBoundsAggregationTest extends \PHPUnit_Framework_TestCase $agg->setField('bar'); $agg->setWrapLongitude(true); $result = [ - AbstractAggregation::PREFIX.'foo' => [ - 'geo_bounds' => [ - 'field' => 'bar', - 'wrap_longitude' => true, - ], + 'geo_bounds' => [ + 'field' => 'bar', + 'wrap_longitude' => true, ], ]; $this->assertEquals($result, $agg->toArray(), 'when wraplongitude is true'); $agg->setWrapLongitude(false); $result = [ - AbstractAggregation::PREFIX.'foo' => [ - 'geo_bounds' => [ - 'field' => 'bar', - 'wrap_longitude' => false, - ], + 'geo_bounds' => [ + 'field' => 'bar', + 'wrap_longitude' => false, ], ]; $this->assertEquals($result, $agg->toArray(), 'when wraplongitude is false'); diff --git a/tests/Aggregation/GeoDistanceAggregationTest.php b/tests/Aggregation/GeoDistanceAggregationTest.php index 7b7463f..b4ec0f3 100644 --- a/tests/Aggregation/GeoDistanceAggregationTest.php +++ b/tests/Aggregation/GeoDistanceAggregationTest.php @@ -11,7 +11,6 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\DSL\Aggregation; -use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; use ONGR\ElasticsearchDSL\Aggregation\GeoDistanceAggregation; class GeoDistanceAggregationTest extends \PHPUnit_Framework_TestCase @@ -134,17 +133,15 @@ class GeoDistanceAggregationTest extends \PHPUnit_Framework_TestCase $this->assertSame( [ - AbstractAggregation::PREFIX.'test' => [ - 'geo_distance' => [ - 'field' => 'fieldName', - 'origin' => 'originValue', - 'unit' => 'unitValue', - 'distance_type' => 'distanceTypeValue', - 'ranges' => [ - ['from' => 'value'], - ['to' => 'value'], - ['from' => 'value', 'to' => 'value2'], - ], + 'geo_distance' => [ + 'field' => 'fieldName', + 'origin' => 'originValue', + 'unit' => 'unitValue', + 'distance_type' => 'distanceTypeValue', + 'ranges' => [ + ['from' => 'value'], + ['to' => 'value'], + ['from' => 'value', 'to' => 'value2'], ], ], ], diff --git a/tests/Aggregation/GlobalAggregationTest.php b/tests/Aggregation/GlobalAggregationTest.php index e6d841e..48fb372 100644 --- a/tests/Aggregation/GlobalAggregationTest.php +++ b/tests/Aggregation/GlobalAggregationTest.php @@ -11,7 +11,6 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\DSL\Aggregation; -use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; use ONGR\ElasticsearchDSL\Aggregation\GlobalAggregation; class GlobalAggregationTest extends \PHPUnit_Framework_TestCase @@ -29,9 +28,7 @@ class GlobalAggregationTest extends \PHPUnit_Framework_TestCase $aggregation = new GlobalAggregation('test_agg'); $result = [ - AbstractAggregation::PREFIX.'test_agg' => [ - 'global' => new \stdClass(), - ], + 'global' => [], ]; $out[] = [ @@ -45,13 +42,9 @@ class GlobalAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addAggregation($aggregation2); $result = [ - AbstractAggregation::PREFIX.'test_agg' => [ - 'global' => new \stdClass(), - 'aggregations' => [ - AbstractAggregation::PREFIX.'test_agg_2' => [ - 'global' => new \stdClass(), - ], - ], + 'global' => [], + 'aggregations' => [ + $aggregation2->getName() => $aggregation2->toArray(), ], ]; diff --git a/tests/Aggregation/Ipv4RangeAggregationTest.php b/tests/Aggregation/Ipv4RangeAggregationTest.php index 266a2d2..846c4dd 100644 --- a/tests/Aggregation/Ipv4RangeAggregationTest.php +++ b/tests/Aggregation/Ipv4RangeAggregationTest.php @@ -35,11 +35,9 @@ class Ipv4RangeAggregationTest extends \PHPUnit_Framework_TestCase $aggregation = new Ipv4RangeAggregation('test', 'fieldName', [['from' => 'fromValue']]); $this->assertSame( [ - AbstractAggregation::PREFIX.'test' => [ - 'ip_range' => [ - 'field' => 'fieldName', - 'ranges' => [['from' => 'fromValue']], - ], + 'ip_range' => [ + 'field' => 'fieldName', + 'ranges' => [['from' => 'fromValue']], ], ], $aggregation->toArray() @@ -48,11 +46,9 @@ class Ipv4RangeAggregationTest extends \PHPUnit_Framework_TestCase $aggregation = new Ipv4RangeAggregation('test', 'fieldName', ['maskValue']); $this->assertSame( [ - AbstractAggregation::PREFIX.'test' => [ - 'ip_range' => [ - 'field' => 'fieldName', - 'ranges' => [['mask' => 'maskValue']], - ], + 'ip_range' => [ + 'field' => 'fieldName', + 'ranges' => [['mask' => 'maskValue']], ], ], $aggregation->toArray() diff --git a/tests/Aggregation/NestedAggregationTest.php b/tests/Aggregation/NestedAggregationTest.php index 7a4f7f9..fd59294 100644 --- a/tests/Aggregation/NestedAggregationTest.php +++ b/tests/Aggregation/NestedAggregationTest.php @@ -13,6 +13,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\DSL\Aggregation; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; use ONGR\ElasticsearchDSL\Aggregation\NestedAggregation; +use ONGR\ElasticsearchDSL\Aggregation\TermsAggregation; class NestedAggregationTest extends \PHPUnit_Framework_TestCase { @@ -40,26 +41,16 @@ class NestedAggregationTest extends \PHPUnit_Framework_TestCase */ public function testToArray() { - $termMock = $this - ->getMockBuilder('ONGR\ElasticsearchDSL\Aggregation\TermsAggregation') - ->disableOriginalConstructor() - ->getMock(); - - $termMock - ->expects($this->once()) - ->method('toArray') - ->will($this->returnValue(['terms' => []])); + $termAggregation = new TermsAggregation('acme'); $aggregation = new NestedAggregation('test_nested_agg'); $aggregation->setPath('test_path'); - $aggregation->addAggregation($termMock); + $aggregation->addAggregation($termAggregation); $expectedResult = [ - AbstractAggregation::PREFIX.'test_nested_agg' => [ - 'nested' => ['path' => 'test_path'], - 'aggregations' => [ - 'terms' => [], - ], + 'nested' => ['path' => 'test_path'], + 'aggregations' => [ + $termAggregation->getName() => $termAggregation->toArray(), ], ]; diff --git a/tests/Aggregation/PercentileRanksAggregationTest.php b/tests/Aggregation/PercentileRanksAggregationTest.php index fa51283..95d93d6 100644 --- a/tests/Aggregation/PercentileRanksAggregationTest.php +++ b/tests/Aggregation/PercentileRanksAggregationTest.php @@ -81,11 +81,9 @@ class PercentileRanksAggregationTest extends \PHPUnit_Framework_TestCase $this->agg->setValues(['bar']); $this->assertSame( [ - AbstractAggregation::PREFIX.'foo' => [ - 'percentile_ranks' => [ - 'field' => 'bar', - 'values' => ['bar'], - ], + 'percentile_ranks' => [ + 'field' => 'bar', + 'values' => ['bar'], ], ], $this->agg->toArray() diff --git a/tests/Aggregation/RangeAggregationTest.php b/tests/Aggregation/RangeAggregationTest.php index 61d71a6..87c5be3 100644 --- a/tests/Aggregation/RangeAggregationTest.php +++ b/tests/Aggregation/RangeAggregationTest.php @@ -26,17 +26,15 @@ class RangeAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addRange('10', 20); $result = [ - AbstractAggregation::PREFIX.'test_agg' => [ - 'range' => [ - 'field' => 'test_field', - 'ranges' => [ - [ - 'from' => '10', - 'to' => 20, - ], + 'range' => [ + 'field' => 'test_field', + 'ranges' => [ + [ + 'from' => '10', + 'to' => 20, ], - 'keyed' => false, ], + 'keyed' => false, ], ]; @@ -55,21 +53,19 @@ class RangeAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addRange(null, '20', 'range_2'); $result = [ - AbstractAggregation::PREFIX.'test_agg' => [ - 'range' => [ - 'field' => 'test_field', - 'ranges' => [ - [ - 'from' => '10', - 'key' => 'range_1', - ], - [ - 'to' => '20', - 'key' => 'range_2', - ], + 'range' => [ + 'field' => 'test_field', + 'ranges' => [ + [ + 'from' => '10', + 'key' => 'range_1', + ], + [ + 'to' => '20', + 'key' => 'range_2', ], - 'keyed' => true, ], + 'keyed' => true, ], ]; @@ -91,28 +87,26 @@ class RangeAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addAggregation($aggregation2); $result = [ - AbstractAggregation::PREFIX.'test_agg' => [ - 'range' => [ - 'field' => 'test_field', - 'ranges' => [ - [ - 'from' => '10', - 'to' => '10', - ], + 'range' => [ + 'field' => 'test_field', + 'ranges' => [ + [ + 'from' => '10', + 'to' => '10', ], - 'keyed' => false, ], - 'aggregations' => [ - AbstractAggregation::PREFIX.'test_agg_2' => [ - 'range' => [ - 'ranges' => [ - [ - 'from' => '20', - 'to' => '20', - ], + 'keyed' => false, + ], + 'aggregations' => [ + AbstractAggregation::PREFIX.'test_agg_2' => [ + 'range' => [ + 'ranges' => [ + [ + 'from' => '20', + 'to' => '20', ], - 'keyed' => false, ], + 'keyed' => false, ], ], ], @@ -209,17 +203,15 @@ class RangeAggregationTest extends \PHPUnit_Framework_TestCase $aggregation = new RangeAggregation('foo', 'fieldValue', [['from' => 'now', 'key' => 'nowkey']], true); $this->assertSame( [ - AbstractAggregation::PREFIX.'foo' => [ - 'range' => [ - 'keyed' => true, - 'ranges' => [ - [ - 'from' => 'now', - 'key' => 'nowkey', - ], + 'range' => [ + 'keyed' => true, + 'ranges' => [ + [ + 'from' => 'now', + 'key' => 'nowkey', ], - 'field' => 'fieldValue', ], + 'field' => 'fieldValue', ], ], $aggregation->toArray() diff --git a/tests/Aggregation/ReverseNestedAggregationTest.php b/tests/Aggregation/ReverseNestedAggregationTest.php index cba7c8f..531c150 100644 --- a/tests/Aggregation/ReverseNestedAggregationTest.php +++ b/tests/Aggregation/ReverseNestedAggregationTest.php @@ -13,6 +13,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\DSL\Aggregation; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; use ONGR\ElasticsearchDSL\Aggregation\ReverseNestedAggregation; +use ONGR\ElasticsearchDSL\Aggregation\TermsAggregation; class ReverseNestedAggregationTest extends \PHPUnit_Framework_TestCase { @@ -27,9 +28,7 @@ class ReverseNestedAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->setPath('test_path'); $expectedResult = [ - AbstractAggregation::PREFIX.'test_agg' => [ - 'reverse_nested' => ['path' => 'test_path'], - ], + 'reverse_nested' => ['path' => 'test_path'], ]; $this->assertEquals($expectedResult, $aggregation->toArray()); @@ -40,26 +39,16 @@ class ReverseNestedAggregationTest extends \PHPUnit_Framework_TestCase */ public function testToArray() { - $termMock = $this - ->getMockBuilder('ONGR\ElasticsearchDSL\Aggregation\TermsAggregation') - ->disableOriginalConstructor() - ->getMock(); - - $termMock - ->expects($this->once()) - ->method('toArray') - ->will($this->returnValue(['terms' => []])); + $termAggregation = new TermsAggregation('acme'); $aggregation = new ReverseNestedAggregation('test_nested_agg'); $aggregation->setPath('test_path'); - $aggregation->addAggregation($termMock); + $aggregation->addAggregation($termAggregation); $expectedResult = [ - AbstractAggregation::PREFIX.'test_nested_agg' => [ - 'reverse_nested' => ['path' => 'test_path'], - 'aggregations' => [ - 'terms' => [], - ], + 'reverse_nested' => ['path' => 'test_path'], + 'aggregations' => [ + $termAggregation->getName() => $termAggregation->toArray(), ], ]; @@ -71,25 +60,15 @@ class ReverseNestedAggregationTest extends \PHPUnit_Framework_TestCase */ public function testToArrayNoPath() { - $termMock = $this - ->getMockBuilder('ONGR\ElasticsearchDSL\Aggregation\TermsAggregation') - ->disableOriginalConstructor() - ->getMock(); - - $termMock - ->expects($this->once()) - ->method('toArray') - ->will($this->returnValue(['terms' => []])); + $termAggregation = new TermsAggregation('acme'); $aggregation = new ReverseNestedAggregation('test_nested_agg'); - $aggregation->addAggregation($termMock); + $aggregation->addAggregation($termAggregation); $expectedResult = [ - AbstractAggregation::PREFIX.'test_nested_agg' => [ - 'reverse_nested' => new \stdClass(), - 'aggregations' => [ - 'terms' => [], - ], + 'reverse_nested' => [], + 'aggregations' => [ + $termAggregation->getName() => $termAggregation->toArray(), ], ]; diff --git a/tests/Aggregation/StatsAggregationTest.php b/tests/Aggregation/StatsAggregationTest.php index 6b00d65..cd85cd9 100644 --- a/tests/Aggregation/StatsAggregationTest.php +++ b/tests/Aggregation/StatsAggregationTest.php @@ -25,9 +25,7 @@ class StatsAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->setField('test_field'); $expectedResult = [ - AbstractAggregation::PREFIX.'test_agg' => [ - 'stats' => ['field' => 'test_field'], - ], + 'stats' => ['field' => 'test_field'], ]; $this->assertEquals($expectedResult, $aggregation->toArray()); @@ -41,11 +39,9 @@ class StatsAggregationTest extends \PHPUnit_Framework_TestCase $aggregation = new StatsAggregation('foo', 'fieldValue', 'scriptValue'); $this->assertSame( [ - AbstractAggregation::PREFIX.'foo' => [ - 'stats' => [ - 'field' => 'fieldValue', - 'script' => 'scriptValue', - ], + 'stats' => [ + 'field' => 'fieldValue', + 'script' => 'scriptValue', ], ], $aggregation->toArray() diff --git a/tests/Aggregation/TermsAggregationTest.php b/tests/Aggregation/TermsAggregationTest.php index 148e52c..52f724a 100644 --- a/tests/Aggregation/TermsAggregationTest.php +++ b/tests/Aggregation/TermsAggregationTest.php @@ -26,9 +26,7 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->setField('test_field'); $result = [ - AbstractAggregation::PREFIX.'test_agg' => [ - 'terms' => ['field' => 'test_field'], - ], + 'terms' => ['field' => 'test_field'], ]; $this->assertEquals($aggregation->toArray(), $result); @@ -45,11 +43,10 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addParameter('size', 1); $result = [ - AbstractAggregation::PREFIX.'test_agg' => [ - 'terms' => [ - 'field' => 'test_field', - 'size' => 1, - ], + 'terms' => [ + 'field' => 'test_field', + 'size' => 1, + ], ]; @@ -61,11 +58,9 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addParameter('size', 0); $result = [ - AbstractAggregation::PREFIX.'test_agg' => [ - 'terms' => [ - 'field' => 'test_field', - 'size' => 0, - ], + 'terms' => [ + 'field' => 'test_field', + 'size' => 0, ], ]; @@ -84,12 +79,10 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addParameter('min_doc_count', 10); $result = [ - AbstractAggregation::PREFIX.'test_agg' => [ - 'terms' => [ - 'field' => 'test_field', - 'size' => 1, - 'min_doc_count' => 10, - ], + 'terms' => [ + 'field' => 'test_field', + 'size' => 1, + 'min_doc_count' => 10, ], ]; @@ -108,12 +101,10 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addParameter('exclude', 'pizza_.*'); $result = [ - AbstractAggregation::PREFIX.'test_agg' => [ - 'terms' => [ - 'field' => 'test_field', - 'include' => 'test_.*', - 'exclude' => 'pizza_.*', - ], + 'terms' => [ + 'field' => 'test_field', + 'include' => 'test_.*', + 'exclude' => 'pizza_.*', ], ]; @@ -144,17 +135,15 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase ); $result = [ - AbstractAggregation::PREFIX.'test_agg' => [ - 'terms' => [ - 'field' => 'test_field', - 'include' => [ - 'pattern' => 'test_.*', - 'flags' => 'CANON_EQ|CASE_INSENSITIVE', - ], - 'exclude' => [ - 'pattern' => 'pizza_.*', - 'flags' => 'CASE_INSENSITIVE', - ], + 'terms' => [ + 'field' => 'test_field', + 'include' => [ + 'pattern' => 'test_.*', + 'flags' => 'CANON_EQ|CASE_INSENSITIVE', + ], + 'exclude' => [ + 'pattern' => 'pizza_.*', + 'flags' => 'CASE_INSENSITIVE', ], ], ]; @@ -173,11 +162,9 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addParameter('order', ['_count' => 'asc']); $result = [ - AbstractAggregation::PREFIX.'test_agg' => [ - 'terms' => [ - 'field' => 'test_field', - 'order' => ['_count' => 'asc'], - ], + 'terms' => [ + 'field' => 'test_field', + 'order' => ['_count' => 'asc'], ], ]; @@ -195,11 +182,9 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addParameter('order', ['_term' => 'desc']); $result = [ - AbstractAggregation::PREFIX.'test_agg' => [ - 'terms' => [ - 'field' => 'test_field', - 'order' => ['_term' => 'desc'], - ], + 'terms' => [ + 'field' => 'test_field', + 'order' => ['_term' => 'desc'], ], ]; diff --git a/tests/Aggregation/TopHitsAggregationTest.php b/tests/Aggregation/TopHitsAggregationTest.php index 0d97a7d..02abe2d 100644 --- a/tests/Aggregation/TopHitsAggregationTest.php +++ b/tests/Aggregation/TopHitsAggregationTest.php @@ -29,14 +29,12 @@ class TopHitsAggregationTest extends \PHPUnit_Framework_TestCase $aggregation = new TopHitsAggregation('acme', 1, 1, $sort); $expected = [ - AbstractAggregation::PREFIX.'acme' => [ - 'top_hits' => [ - 'sort' => [ - 'acme' => [], - ], - 'size' => 1, - 'from' => 1, + 'top_hits' => [ + 'sort' => [ + 'acme' => [], ], + 'size' => 1, + 'from' => 1, ], ]; @@ -52,13 +50,11 @@ class TopHitsAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addParameter('_source', ['include' => ['title']]); $expected = [ - AbstractAggregation::PREFIX.'acme' => [ - 'top_hits' => [ - 'size' => 0, - 'from' => 1, - '_source' => [ - 'include' => ['title'], - ], + 'top_hits' => [ + 'size' => 0, + 'from' => 1, + '_source' => [ + 'include' => ['title'], ], ], ]; -- GitLab