diff --git a/src/Aggregation/AbstractAggregation.php b/src/Aggregation/AbstractAggregation.php index e72c4371be32405b3bbafd49781c6f2ca687764c..8f0ad9c771d23d9f19ee56c41dd0ac50ff3cafcf 100644 --- a/src/Aggregation/AbstractAggregation.php +++ b/src/Aggregation/AbstractAggregation.php @@ -60,14 +60,11 @@ abstract class AbstractAggregation implements NamedBuilderInterface * Inner aggregations container init. * * @param string $name - * @param string $field */ - public function __construct($name, $field = null) + public function __construct($name) { $this->name = $name; - if ($field !== null) { - $this->setField($field); - } + $this->aggregations = new NamedBuilderBag(); } diff --git a/src/Aggregation/ChildrenAggregation.php b/src/Aggregation/ChildrenAggregation.php index f87b1d92afb3ecc50983ea6df28a12e0bd851d4e..6782ce868fa596a349af73068502cc85e0de9955 100644 --- a/src/Aggregation/ChildrenAggregation.php +++ b/src/Aggregation/ChildrenAggregation.php @@ -35,6 +35,17 @@ class ChildrenAggregation extends AbstractAggregation return $this->children; } + /** + * @param string $name + * @param string $children + */ + public function __construct($name, $children = null) + { + parent::__construct($name); + + $this->setChildren($children); + } + /** * Sets children. * diff --git a/src/Aggregation/DateRangeAggregation.php b/src/Aggregation/DateRangeAggregation.php index ff445055b318dce8a2c7cb6ef29135387dcd4a69..fb8f7e375b7cae8b56fb91863e850e5342f8aa97 100644 --- a/src/Aggregation/DateRangeAggregation.php +++ b/src/Aggregation/DateRangeAggregation.php @@ -33,6 +33,25 @@ class DateRangeAggregation extends AbstractAggregation return $this->format; } + /** + * @param string $name + * @param string $field + * @param string $format + * @param array $ranges + */ + public function __construct($name, $field = null, $format = null, array $ranges = []) + { + parent::__construct($name); + + $this->setField($field); + $this->setFormat($format); + foreach ($ranges as $range) { + $from = isset($range['from']) ? $range['from'] : null; + $to = isset($range['to']) ? $range['to'] : null; + $this->addRange($from, $to); + } + } + /** * @param string $format */ diff --git a/src/Aggregation/ExtendedStatsAggregation.php b/src/Aggregation/ExtendedStatsAggregation.php index 2cf6a408f07adeec1026f8f0534e05093c5c11ab..45dcc67bc2f3094012b82b7915dd58c0fe9250c2 100644 --- a/src/Aggregation/ExtendedStatsAggregation.php +++ b/src/Aggregation/ExtendedStatsAggregation.php @@ -22,6 +22,23 @@ class ExtendedStatsAggregation extends AbstractAggregation use MetricTrait; use ScriptAwareTrait; + /** + * Inner aggregations container init. + * + * @param string $name + * @param string $field + * @param int $sigma + * @param string $script + */ + public function __construct($name, $field = null, $sigma = null, $script = null) + { + parent::__construct($name); + + $this->setField($field); + $this->setSigma($sigma); + $this->setScript($script); + } + /** * @var int */ diff --git a/src/Aggregation/FilterAggregation.php b/src/Aggregation/FilterAggregation.php index 3655c31c6f9833519f01af19ba7c1a7a37d8167d..2e512baa6e30bc603fd492bc56c164763b643c1e 100644 --- a/src/Aggregation/FilterAggregation.php +++ b/src/Aggregation/FilterAggregation.php @@ -26,6 +26,21 @@ class FilterAggregation extends AbstractAggregation */ protected $filter; + /** + * Inner aggregations container init. + * + * @param string $name + * @param BuilderInterface $filter + */ + public function __construct($name, BuilderInterface $filter = null) + { + parent::__construct($name); + + if ($filter !== null) { + $this->setFilter($filter); + } + } + /** * Sets a filter. * diff --git a/src/Aggregation/FiltersAggregation.php b/src/Aggregation/FiltersAggregation.php index 4206b5eb9eaeb0eb60500390f30957bb499f6fcc..51e8ba623e8ae6ae42e04a038ae4367fa8162832 100644 --- a/src/Aggregation/FiltersAggregation.php +++ b/src/Aggregation/FiltersAggregation.php @@ -31,6 +31,27 @@ class FiltersAggregation extends AbstractAggregation */ private $anonymous = false; + /** + * Inner aggregations container init. + * + * @param string $name + * @param BuilderInterface[] $filters + * @param bool $anonymous + */ + public function __construct($name, $filters = [], $anonymous = false) + { + parent::__construct($name); + + $this->setAnonymous($anonymous); + foreach ($filters as $name => $filter) { + if ($anonymous) { + $this->addFilter($filter); + } else { + $this->addFilter($filter, $name); + } + } + } + /** * @param bool $anonymous * diff --git a/src/Aggregation/GeoBoundsAggregation.php b/src/Aggregation/GeoBoundsAggregation.php index 1079d7cd8a9a22c09feecd5897a93148b3cc156d..f408fba5df72e7e990d2cd31f66dfa4a2f884fe1 100644 --- a/src/Aggregation/GeoBoundsAggregation.php +++ b/src/Aggregation/GeoBoundsAggregation.php @@ -25,6 +25,21 @@ class GeoBoundsAggregation extends AbstractAggregation */ private $wrapLongitude = true; + /** + * Inner aggregations container init. + * + * @param string $name + * @param string $field + * @param bool $wrapLongitude + */ + public function __construct($name, $field = null, $wrapLongitude = true) + { + parent::__construct($name); + + $this->setField($field); + $this->setWrapLongitude($wrapLongitude); + } + /** * @return bool */ diff --git a/src/Aggregation/GeoDistanceAggregation.php b/src/Aggregation/GeoDistanceAggregation.php index d360bdf4b2a14bf9d39fce728ec84841e5a646d8..b9a0e6fe99a9dfce6e2746cf82ecaf464b565e8f 100644 --- a/src/Aggregation/GeoDistanceAggregation.php +++ b/src/Aggregation/GeoDistanceAggregation.php @@ -40,6 +40,31 @@ class GeoDistanceAggregation extends AbstractAggregation */ private $ranges = []; + /** + * Inner aggregations container init. + * + * @param string $name + * @param string $field + * @param mixed $origin + * @param array $ranges + * @param string $unit + * @param string $distanceType + */ + public function __construct($name, $field = null, $origin = null, $ranges = [], $unit = null, $distanceType = null) + { + parent::__construct($name); + + $this->setField($field); + $this->setOrigin($origin); + foreach ($ranges as $range) { + $from = isset($range['from']) ? $range['from'] : null; + $to = isset($range['to']) ? $range['to'] : null; + $this->addRange($from, $to); + } + $this->setUnit($unit); + $this->setDistanceType($distanceType); + } + /** * @return string */ diff --git a/src/Aggregation/GeoHashGridAggregation.php b/src/Aggregation/GeoHashGridAggregation.php index f85144ee11ae546ea9b59f67e5295326d9ca0f30..875333d33cb2225e2b02588067ca56f80211b55d 100644 --- a/src/Aggregation/GeoHashGridAggregation.php +++ b/src/Aggregation/GeoHashGridAggregation.php @@ -35,6 +35,25 @@ class GeoHashGridAggregation extends AbstractAggregation */ private $shardSize; + /** + * Inner aggregations container init. + * + * @param string $name + * @param string $field + * @param int $precision + * @param int $size + * @param int $shardSize + */ + public function __construct($name, $field = null, $precision = null, $size = null, $shardSize = null) + { + parent::__construct($name); + + $this->setField($field); + $this->setPrecision($precision); + $this->setSize($size); + $this->setShardSize($shardSize); + } + /** * @return int */ diff --git a/src/Aggregation/HistogramAggregation.php b/src/Aggregation/HistogramAggregation.php index 07d70d025cd1dd29480be25e842e01cc6a3bf2c3..3c5aaca426552dc0156a33fdcab1bb0c89349da3 100644 --- a/src/Aggregation/HistogramAggregation.php +++ b/src/Aggregation/HistogramAggregation.php @@ -53,6 +53,40 @@ class HistogramAggregation extends AbstractAggregation */ protected $keyed; + /** + * Inner aggregations container init. + * + * @param string $name + * @param string $field + * @param int $interval + * @param int $minDocCount + * @param string $orderMode + * @param string $orderDirection + * @param int $extendedBoundsMin + * @param int $extendedBoundsMax + * @param bool $keyed + */ + public function __construct( + $name, + $field = null, + $interval = null, + $minDocCount = null, + $orderMode = null, + $orderDirection = self::DIRECTION_ASC, + $extendedBoundsMin = null, + $extendedBoundsMax = null, + $keyed = null + ) { + parent::__construct($name); + + $this->setField($field); + $this->setInterval($interval); + $this->setMinDocCount($minDocCount); + $this->setOrder($orderMode, $orderDirection); + $this->setExtendedBounds($extendedBoundsMin, $extendedBoundsMax); + $this->setKeyed($keyed); + } + /** * @return bool */ diff --git a/src/Aggregation/Ipv4RangeAggregation.php b/src/Aggregation/Ipv4RangeAggregation.php index 917c7a8e23c2e95c9f6cf0da6492bce29c26caf5..9b486b7e83524296a868e1f6373f6069a4fa0c3c 100644 --- a/src/Aggregation/Ipv4RangeAggregation.php +++ b/src/Aggregation/Ipv4RangeAggregation.php @@ -25,6 +25,29 @@ class Ipv4RangeAggregation extends AbstractAggregation */ private $ranges = []; + /** + * Inner aggregations container init. + * + * @param string $name + * @param string $field + * @param array $ranges + */ + public function __construct($name, $field = null, $ranges = []) + { + parent::__construct($name); + + $this->setField($field); + foreach ($ranges as $range) { + if (is_array($range)) { + $from = isset($range['from']) ? $range['from'] : null; + $to = isset($range['to']) ? $range['to'] : null; + $this->addRange($from, $to); + } else { + $this->addMask($range); + } + } + } + /** * Add range to aggregation. * diff --git a/src/Aggregation/MissingAggregation.php b/src/Aggregation/MissingAggregation.php index feec6a68d25af65ad5516c8533b770edce2ee9b9..5330289692f738848f4fbd85ce24b5024df010b6 100644 --- a/src/Aggregation/MissingAggregation.php +++ b/src/Aggregation/MissingAggregation.php @@ -20,6 +20,19 @@ class MissingAggregation extends AbstractAggregation { use BucketingTrait; + /** + * Inner aggregations container init. + * + * @param string $name + * @param string $field + */ + public function __construct($name, $field = null) + { + parent::__construct($name); + + $this->setField($field); + } + /** * {@inheritdoc} */ diff --git a/src/Aggregation/NestedAggregation.php b/src/Aggregation/NestedAggregation.php index ee0bdc125031927590cd91e2b9f07bdb106e4232..f3ace96516ef1ed12467a2f258ca0749b5db9049 100644 --- a/src/Aggregation/NestedAggregation.php +++ b/src/Aggregation/NestedAggregation.php @@ -25,6 +25,19 @@ class NestedAggregation extends AbstractAggregation */ private $path; + /** + * Inner aggregations container init. + * + * @param string $name + * @param string $path + */ + public function __construct($name, $path = null) + { + parent::__construct($name); + + $this->setPath($path); + } + /** * Return path. * diff --git a/src/Aggregation/PercentileRanksAggregation.php b/src/Aggregation/PercentileRanksAggregation.php index bc8a7fb3c6c381210ceb99f087dcaedf741249c1..20f839e1089d1138f947a712aa0575eca2296651 100644 --- a/src/Aggregation/PercentileRanksAggregation.php +++ b/src/Aggregation/PercentileRanksAggregation.php @@ -32,6 +32,25 @@ class PercentileRanksAggregation extends AbstractAggregation */ private $compression; + /** + * Inner aggregations container init. + * + * @param string $name + * @param string $field + * @param array $values + * @param string $script + * @param int $compression + */ + public function __construct($name, $field = null, $values = null, $script = null, $compression = null) + { + parent::__construct($name); + + $this->setField($field); + $this->setValues($values); + $this->setScript($script); + $this->setCompression($compression); + } + /** * @return array */ @@ -98,7 +117,6 @@ class PercentileRanksAggregation extends AbstractAggregation * @param array $a * * @return bool - * * @throws \LogicException */ private function isRequiredParametersSet($a) diff --git a/src/Aggregation/PercentilesAggregation.php b/src/Aggregation/PercentilesAggregation.php index d8890854b7da1ca2f6b461c9d0df7ee00bdb563c..3f203c497d8d84df08f1370b15bde05ce0809d4c 100644 --- a/src/Aggregation/PercentilesAggregation.php +++ b/src/Aggregation/PercentilesAggregation.php @@ -32,6 +32,25 @@ class PercentilesAggregation extends AbstractAggregation */ private $compression; + /** + * Inner aggregations container init. + * + * @param string $name + * @param string $field + * @param array $percents + * @param string $script + * @param int $compression + */ + public function __construct($name, $field = null, $percents = null, $script = null, $compression = null) + { + parent::__construct($name); + + $this->setField($field); + $this->setPercents($percents); + $this->setScript($script); + $this->setCompression($compression); + } + /** * @return array */ diff --git a/src/Aggregation/RangeAggregation.php b/src/Aggregation/RangeAggregation.php index 41e107569998baaf9fd7396005e294e6077ebc48..557e9c49d4a48d39dbece5f413547e65f85d5b5b 100644 --- a/src/Aggregation/RangeAggregation.php +++ b/src/Aggregation/RangeAggregation.php @@ -30,6 +30,28 @@ class RangeAggregation extends AbstractAggregation */ private $keyed = false; + /** + * Inner aggregations container init. + * + * @param string $name + * @param string $field + * @param array $ranges + * @param bool $keyed + */ + public function __construct($name, $field = null, $ranges = [], $keyed = false) + { + parent::__construct($name); + + $this->setField($field); + $this->setKeyed($keyed); + foreach ($ranges as $range) { + $from = isset($range['from']) ? $range['from'] : null; + $to = isset($range['to']) ? $range['to'] : null; + $key = isset($range['key']) ? $range['key'] : null; + $this->addRange($from, $to, $key); + } + } + /** * Sets if result buckets should be keyed. * diff --git a/src/Aggregation/ReverseNestedAggregation.php b/src/Aggregation/ReverseNestedAggregation.php index 3225d74b2e5983c0ccda7e53aeaab2cff208e3c4..c833135ecf128c0b4769429d57e0f3f6e5ab3915 100644 --- a/src/Aggregation/ReverseNestedAggregation.php +++ b/src/Aggregation/ReverseNestedAggregation.php @@ -25,6 +25,19 @@ class ReverseNestedAggregation extends AbstractAggregation */ private $path; + /** + * Inner aggregations container init. + * + * @param string $name + * @param string $path + */ + public function __construct($name, $path = null) + { + parent::__construct($name); + + $this->setPath($path); + } + /** * Return path. * diff --git a/src/Aggregation/StatsAggregation.php b/src/Aggregation/StatsAggregation.php index 515e8a37876d3f5c14e441aa60c740023d21095b..425bea55cc22a61a7272206d9a7af79238ff3f48 100644 --- a/src/Aggregation/StatsAggregation.php +++ b/src/Aggregation/StatsAggregation.php @@ -22,6 +22,21 @@ class StatsAggregation extends AbstractAggregation use MetricTrait; use ScriptAwareTrait; + /** + * Inner aggregations container init. + * + * @param string $name + * @param string $field + * @param string $script + */ + public function __construct($name, $field = null, $script = null) + { + parent::__construct($name); + + $this->setField($field); + $this->setScript($script); + } + /** * {@inheritdoc} */ diff --git a/src/Aggregation/TermsAggregation.php b/src/Aggregation/TermsAggregation.php index 3726089aadabf1aa62a39f74ce783516ebc4fc25..2b65ec144ea45811b495e6d4a6b995ba1028add2 100644 --- a/src/Aggregation/TermsAggregation.php +++ b/src/Aggregation/TermsAggregation.php @@ -22,6 +22,21 @@ class TermsAggregation extends AbstractAggregation use BucketingTrait; use ScriptAwareTrait; + /** + * Inner aggregations container init. + * + * @param string $name + * @param string $field + * @param string $script + */ + public function __construct($name, $field = null, $script = null) + { + parent::__construct($name); + + $this->setField($field); + $this->setScript($script); + } + /** * {@inheritdoc} */ diff --git a/tests/Aggregation/DateRangeAggregationTest.php b/tests/Aggregation/DateRangeAggregationTest.php index 45ec22675d39a8eba2f574f4a82002a849b16b8b..e9050240650d76e9f989b6196186ff2b6a02c04c 100644 --- a/tests/Aggregation/DateRangeAggregationTest.php +++ b/tests/Aggregation/DateRangeAggregationTest.php @@ -66,4 +66,74 @@ class DateRangeAggregationTest extends \PHPUnit_Framework_TestCase $result = $aggregation->getType(); $this->assertEquals('date_range', $result); } + + /** + * Data provider for testDateRangeAggregationConstructor. + * + * @return array + */ + public function testDateRangeAggregationConstructorProvider() + { + return [ + // Case #0. Minimum arguments. + [], + // Case #1. Provide field. + ['field' => 'fieldName'], + // Case #2. Provide format. + ['field' => 'fieldName', 'format' => 'formatString'], + // Case #3. Provide empty ranges. + ['field' => 'fieldName', 'format' => 'formatString', 'ranges' => []], + // Case #4. Provide 1 range. + [ + 'field' => 'fieldName', + 'format' => 'formatString', + 'ranges' => [['from' => 'value']], + ], + // Case #4. Provide 2 ranges. + [ + 'field' => 'fieldName', + 'format' => 'formatString', + 'ranges' => [['from' => 'value'], ['to' => 'value']], + ], + // Case #5. Provide 3 ranges. + [ + 'field' => 'fieldName', + 'format' => 'formatString', + 'ranges' => [['from' => 'value'], ['to' => 'value'], ['from' => 'value', 'to' => 'value2']], + ], + ]; + } + + /** + * Tests constructor method. + * + * @param string $field + * @param string $format + * @param array $ranges + * + * @dataProvider testDateRangeAggregationConstructorProvider + */ + public function testDateRangeAggregationConstructor($field = null, $format = null, array $ranges = null) + { + /** @var DateRangeAggregation|\PHPUnit_Framework_MockObject_MockObject $aggregation */ + $aggregation = $this->getMockBuilder('ONGR\ElasticsearchDSL\Aggregation\DateRangeAggregation') + ->disableOriginalConstructor()->getMock(); + $aggregation->expects($this->once())->method('setField')->with($field); + $aggregation->expects($this->once())->method('setFormat')->with($format); + $aggregation->expects($this->exactly(count($ranges)))->method('addRange'); + + if ($field !== null) { + if ($format !== null) { + if ($ranges !== null) { + $aggregation->__construct('mock', $field, $format, $ranges); + } else { + $aggregation->__construct('mock', $field, $format); + } + } else { + $aggregation->__construct('mock', $field); + } + } else { + $aggregation->__construct('mock'); + } + } } diff --git a/tests/Aggregation/FilterAggregationTest.php b/tests/Aggregation/FilterAggregationTest.php index 385a94b8a9d2ba16bc9e92359d00b7e1ecf67ef2..753551c8de0c67c6c20b327d7b0494675505e7ca 100644 --- a/tests/Aggregation/FilterAggregationTest.php +++ b/tests/Aggregation/FilterAggregationTest.php @@ -12,7 +12,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\DSL\Aggregation; use ONGR\ElasticsearchDSL\Aggregation\FilterAggregation; -use ONGR\ElasticsearchDSL\Filter\AndFilter; +use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\Filter\MissingFilter; class FilterAggregationTest extends \PHPUnit_Framework_TestCase @@ -143,4 +143,22 @@ class FilterAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->setFilter(new MissingFilter('test')); $aggregation->toArray(); } + + /** + * Tests if filter can be passed to constructor. + */ + public function testConstructorFilter() + { + /** @var BuilderInterface|\PHPUnit_Framework_MockObject_MockObject $builderInterface */ + $builderInterface = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); + $aggregation = new FilterAggregation('test', $builderInterface); + $this->assertSame( + [ + 'agg_test' => [ + 'filter' => [null => null], + ], + ], + $aggregation->toArray() + ); + } } diff --git a/tests/Aggregation/FiltersAggregationTest.php b/tests/Aggregation/FiltersAggregationTest.php index 4f541beecd39618d1e80df6344334882e6cbc134..af8c21f149c63ede67bc4be8dcaef1fd7036f8ac 100644 --- a/tests/Aggregation/FiltersAggregationTest.php +++ b/tests/Aggregation/FiltersAggregationTest.php @@ -12,6 +12,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\DSL\Aggregation; use ONGR\ElasticsearchDSL\Aggregation\FiltersAggregation; +use ONGR\ElasticsearchDSL\BuilderInterface; /** * Unit test for filters aggregation. @@ -97,4 +98,62 @@ class FiltersAggregationTest extends \PHPUnit_Framework_TestCase ]; $this->assertEquals($expected, $results); } + + /** + * Tests if filters can be passed to constructor. + */ + public function testConstructorFilter() + { + /** @var BuilderInterface|\PHPUnit_Framework_MockObject_MockObject $builderInterface1 */ + $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); + $builderInterface1->expects($this->any())->method('getType')->willReturn('type1'); + /** @var BuilderInterface|\PHPUnit_Framework_MockObject_MockObject $builderInterface2 */ + $builderInterface2 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); + $builderInterface2->expects($this->any())->method('getType')->willReturn('type2'); + + $aggregation = new FiltersAggregation( + 'test', + [ + 'filter1' => $builderInterface1, + 'filter2' => $builderInterface2, + ] + ); + + $this->assertSame( + [ + 'agg_test' => [ + 'filters' => [ + 'filters' => [ + 'filter1' => ['type1' => null], + 'filter2' => ['type2' => null], + ], + ], + ], + ], + $aggregation->toArray() + ); + + $aggregation = new FiltersAggregation( + 'test', + [ + 'filter1' => $builderInterface1, + 'filter2' => $builderInterface2, + ], + true + ); + + $this->assertSame( + [ + 'agg_test' => [ + 'filters' => [ + 'filters' => [ + ['type1' => null], + ['type2' => null], + ], + ], + ], + ], + $aggregation->toArray() + ); + } } diff --git a/tests/Aggregation/GeoDistanceAggregationTest.php b/tests/Aggregation/GeoDistanceAggregationTest.php index 0379ab08ef0c18bbf7d22c0e03b12a1cc4343d97..19ab6994c388e75d5cff3090665d2a64855f4fa8 100644 --- a/tests/Aggregation/GeoDistanceAggregationTest.php +++ b/tests/Aggregation/GeoDistanceAggregationTest.php @@ -112,4 +112,42 @@ class GeoDistanceAggregationTest extends \PHPUnit_Framework_TestCase $result = $aggregation->getType(); $this->assertEquals('geo_distance', $result); } + + /** + * Tests if parameters can be passed to constructor. + */ + public function testConstructorFilter() + { + $aggregation = new GeoDistanceAggregation( + 'test', + 'fieldName', + 'originValue', + [ + ['from' => 'value'], + ['to' => 'value'], + ['from' => 'value', 'to' => 'value2'], + ], + 'unitValue', + 'distanceTypeValue' + ); + + $this->assertSame( + [ + 'agg_test' => [ + 'geo_distance' => [ + 'field' => 'fieldName', + 'origin' => 'originValue', + 'unit' => 'unitValue', + 'distance_type' => 'distanceTypeValue', + 'ranges' => [ + ['from' => 'value'], + ['to' => 'value'], + ['from' => 'value', 'to' => 'value2'], + ], + ], + ], + ], + $aggregation->toArray() + ); + } } diff --git a/tests/Aggregation/Ipv4RangeAggregationTest.php b/tests/Aggregation/Ipv4RangeAggregationTest.php index eb3a27eccdfae5c950c57f48d816c374c1be69e7..7542584b5e035a454026dfc9d5450ef719af75cf 100644 --- a/tests/Aggregation/Ipv4RangeAggregationTest.php +++ b/tests/Aggregation/Ipv4RangeAggregationTest.php @@ -25,4 +25,36 @@ class Ipv4RangeAggregationTest extends \PHPUnit_Framework_TestCase $agg = new Ipv4RangeAggregation('foo'); $agg->toArray(); } + + /** + * Tests if field and range can be passed to constructor. + */ + public function testConstructorFilter() + { + $aggregation = new Ipv4RangeAggregation('test', 'fieldName', [['from' => 'fromValue']]); + $this->assertSame( + [ + 'agg_test' => [ + 'ip_range' => [ + 'field' => 'fieldName', + 'ranges' => [['from' => 'fromValue']], + ], + ], + ], + $aggregation->toArray() + ); + + $aggregation = new Ipv4RangeAggregation('test', 'fieldName', ['maskValue']); + $this->assertSame( + [ + 'agg_test' => [ + 'ip_range' => [ + 'field' => 'fieldName', + 'ranges' => [['mask' => 'maskValue']], + ], + ], + ], + $aggregation->toArray() + ); + } } diff --git a/tests/Aggregation/PercentileRanksAggregationTest.php b/tests/Aggregation/PercentileRanksAggregationTest.php index 5ac348596cd0e43bbfb10bdf771c720af2cc9441..94ce741f69db060974bc22fc50eff59d94e285f7 100644 --- a/tests/Aggregation/PercentileRanksAggregationTest.php +++ b/tests/Aggregation/PercentileRanksAggregationTest.php @@ -62,4 +62,32 @@ class PercentileRanksAggregationTest extends \PHPUnit_Framework_TestCase $this->agg->setScript('bar'); $this->agg->toArray(); } + + /** + * Test getType method. + */ + public function testGetType() + { + $this->assertEquals('percentile_ranks', $this->agg->getType()); + } + + /** + * Test toArray method. + */ + public function testToArray() + { + $this->agg->setField('bar'); + $this->agg->setValues(['bar']); + $this->assertSame( + [ + 'agg_foo' => [ + 'percentile_ranks' => [ + 'field' => 'bar', + 'values' => ['bar'], + ], + ], + ], + $this->agg->toArray() + ); + } } diff --git a/tests/Aggregation/PercentilesAggregationTest.php b/tests/Aggregation/PercentilesAggregationTest.php index 9ab92e4e6e4260aef7651a1b4e954abcddace258..b4492f42d416b986e7f66425b33f27506a3659c7 100644 --- a/tests/Aggregation/PercentilesAggregationTest.php +++ b/tests/Aggregation/PercentilesAggregationTest.php @@ -26,4 +26,28 @@ class PercentilesAggregationTest extends \PHPUnit_Framework_TestCase $aggregation = new PercentilesAggregation('bar'); $aggregation->getArray(); } + + /** + * Test getType method. + */ + public function testGetType() + { + $aggregation = new PercentilesAggregation('bar'); + $this->assertEquals('percentiles', $aggregation->getType()); + } + + /** + * Test getArray method. + */ + public function testGetArray() + { + $aggregation = new PercentilesAggregation('bar', 'fieldValue', ['percentsValue']); + $this->assertSame( + [ + 'percents' => ['percentsValue'], + 'field' => 'fieldValue', + ], + $aggregation->getArray() + ); + } } diff --git a/tests/Aggregation/RangeAggregationTest.php b/tests/Aggregation/RangeAggregationTest.php index 9414ea5d368bf703172415a1f78878a102e024c8..c66244d0811bdfe58b7ac11e09ad6d84b41a25c9 100644 --- a/tests/Aggregation/RangeAggregationTest.php +++ b/tests/Aggregation/RangeAggregationTest.php @@ -199,4 +199,29 @@ class RangeAggregationTest extends \PHPUnit_Framework_TestCase $result = $aggregation->removeRange(500, 700); $this->assertFalse($result, 'returns false after removing not-existing range'); } + + /** + * Tests if parameter can be passed to constructor. + */ + public function testConstructor() + { + $aggregation = new RangeAggregation('foo', 'fieldValue', [['from' => 'now', 'key' => 'nowkey']], true); + $this->assertSame( + [ + 'agg_foo' => [ + 'range' => [ + 'keyed' => true, + 'ranges' => [ + [ + 'from' => 'now', + 'key' => 'nowkey', + ], + ], + 'field' => 'fieldValue', + ], + ], + ], + $aggregation->toArray() + ); + } } diff --git a/tests/Aggregation/StatsAggregationTest.php b/tests/Aggregation/StatsAggregationTest.php index 621e0cfc4dbf5c243600a57846a3545e3c77c7d3..f826b397310bbb89a08c4cfda0155737f420b8b3 100644 --- a/tests/Aggregation/StatsAggregationTest.php +++ b/tests/Aggregation/StatsAggregationTest.php @@ -31,4 +31,23 @@ class StatsAggregationTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expectedResult, $aggregation->toArray()); } + + /** + * Tests if parameter can be passed to constructor. + */ + public function testConstructor() + { + $aggregation = new StatsAggregation('foo', 'fieldValue', 'scriptValue'); + $this->assertSame( + [ + 'agg_foo' => [ + 'stats' => [ + 'field' => 'fieldValue', + 'script' => 'scriptValue', + ], + ], + ], + $aggregation->toArray() + ); + } }