Skip to content
Snippets Groups Projects
Commit a9ed693e authored by Aivaras Gotovskis's avatar Aivaras Gotovskis
Browse files

Add constructor arguments to DateRangeAggregation.

parent 59af1398
Branches
No related tags found
No related merge requests found
...@@ -41,8 +41,9 @@ class DateRangeAggregation extends AbstractAggregation ...@@ -41,8 +41,9 @@ class DateRangeAggregation extends AbstractAggregation
*/ */
public function __construct($name, $field = null, $format = null, array $ranges = []) public function __construct($name, $field = null, $format = null, array $ranges = [])
{ {
parent::__construct($name, $field); parent::__construct($name);
$this->setField($field);
$this->setFormat($format); $this->setFormat($format);
foreach ($ranges as $range) { foreach ($ranges as $range) {
$from = isset($range['from']) ? $range['from'] : null; $from = isset($range['from']) ? $range['from'] : null;
......
...@@ -66,4 +66,74 @@ class DateRangeAggregationTest extends \PHPUnit_Framework_TestCase ...@@ -66,4 +66,74 @@ class DateRangeAggregationTest extends \PHPUnit_Framework_TestCase
$result = $aggregation->getType(); $result = $aggregation->getType();
$this->assertEquals('date_range', $result); $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');
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment