diff --git a/src/Aggregation/AbstractAggregation.php b/src/Aggregation/AbstractAggregation.php index 938a41e3e2e6947dc52b92b8279b6bce487f0ba7..1f4a7c1a53a44bdb795d08eb915ea72699de50e9 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 ac48255155654e00321bb3e470e0b40ae4455b07..09beb1c87f2dafe0e28c1106266f792592e3ce42 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 c833135ecf128c0b4769429d57e0f3f6e5ab3915..79529610b06a2cf0c6dac10d3b4a686a65625a79 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 6529ce44296c319d920c8292bb4ac5f2d3fe5a88..71d9ab0a5c19e05f6df4d43789c1bf94152fe034 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 0ec3582fb1fbd0948847872c87bfd0b1592c2c15..ff008e59ddf5567c67fdb028a2194b7fdf4417d3 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 56c2c508bba2450c6bbdff54e75ecaf495318fec..2f39291e9b8aeb0d6e3fc066f7c437577bb4b830 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 45d9c9fe44ac4077833f08c90fba6c7d0599a704..f2b8d8e9836c1e81efdce451c3a0b8352f5197b6 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 7ee71789666e6a88eeee7cb15379119a8e10fccc..e899f31423657a25d46d26deba4e2ca358e18b79 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 d13fdbdf3254578beb95c973be8e4fcee93fe4ca..ce5111b9f8b8a387c891b1169fff03a61e9c366b 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 9d914094c96deea887ced473c6e144611b6b85f9..3a6a9025116bbb43a5637ad52da0c767fff85da3 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 aec9a1ff3b8c616a6bc8c024e5f4b1a7f72eec2e..ac1ebfaad3f84df7eef908e5e04b8769392e0dc4 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 7b7463fb55125cfd74d3a157907a865a11751eac..b4ec0f3359ea5ec355e1be3ea53f21a1d72c3ac5 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 e6d841e2d4d7b461e1af9ad553270be5f85abf65..48fb372f59d9b2554de0db90037196a3f41d0eda 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 266a2d24e2ae742acf38b57c604f41219187053b..846c4dda34281566857ceb7a59f3e7b9ca7aab27 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 7a4f7f96c04c3dd551373124d182aa208a774b91..fd592944d69898c0771b61df63e0e172e5aa24e2 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 fa51283c62ef04b4b0ec0eb09ea7118b2d9af03b..95d93d6508a580fa3d712c76c7867a239bac6cde 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 61d71a66650b2bbf500f4761dc0d366f67399097..87c5be36acf5e1485c22872eff53c8488c796cbc 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 cba7c8fc080d8c17d291688e3a743bd6cf2b1d55..531c150102c3de63247cb7d80778a498d20166d4 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 6b00d6566f7e4d15b2e6bff80295f5f65b0b6f9a..cd85cd986d9d645d2babb2acffbabd6a8b3428ee 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 148e52ced0ac2d02fcd35240da6ec8ba5ec217db..52f724a4dfe41e69ff99a4b18d78fca0cd166edd 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 0d97a7d8433870dfa29f78be5267785b49e9be92..02abe2d8c9e5e33e5e31fd7fd078f7259e749585 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'], ], ], ];