forked from ongr-io/ElasticsearchDSL
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; | ||
|
||
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; | ||
use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait; | ||
|
||
/** | ||
* Class representing ChildrenAggregation. | ||
* | ||
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-children-aggregation.html | ||
*/ | ||
class ParentAggregation extends AbstractAggregation | ||
{ | ||
use BucketingTrait; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $parent; | ||
|
||
/** | ||
* Return children. | ||
* | ||
* @return string | ||
*/ | ||
public function getParent() | ||
{ | ||
return $this->parent; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @param string $parent | ||
*/ | ||
public function __construct($name, $parent = null) | ||
{ | ||
parent::__construct($name); | ||
|
||
$this->setParent($parent); | ||
} | ||
|
||
/** | ||
* @param string $parent | ||
* | ||
* @return $this | ||
*/ | ||
public function setParent($parent) | ||
{ | ||
$this->parent = $parent; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getType() | ||
{ | ||
return 'parent'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getArray() | ||
{ | ||
if (count($this->getAggregations()) == 0) { | ||
throw new \LogicException("Parent aggregation `{$this->getName()}` has no aggregations added"); | ||
} | ||
|
||
return ['type' => $this->getParent()]; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
tests/Unit/Aggregation/Bucketing/ParentAggregationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Bucketing; | ||
|
||
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\ParentAggregation; | ||
|
||
/** | ||
* Unit test for parent aggregation. | ||
*/ | ||
class ParentAggregationTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* Tests if ParentAggregation#getArray throws exception when expected. | ||
*/ | ||
public function testGetArrayException() | ||
{ | ||
$this->expectException(\LogicException::class); | ||
$aggregation = new ParentAggregation('foo'); | ||
$aggregation->getArray(); | ||
} | ||
|
||
/** | ||
* Tests getType method. | ||
*/ | ||
public function testParentAggregationGetType() | ||
{ | ||
$aggregation = new ParentAggregation('foo'); | ||
$result = $aggregation->getType(); | ||
$this->assertEquals('parent', $result); | ||
} | ||
|
||
/** | ||
* Tests getArray method. | ||
*/ | ||
public function testParentAggregationGetArray() | ||
{ | ||
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation') | ||
->disableOriginalConstructor() | ||
->getMockForAbstractClass(); | ||
$aggregation = new ParentAggregation('foo'); | ||
$aggregation->addAggregation($mock); | ||
$aggregation->setParent('question'); | ||
$result = $aggregation->getArray(); | ||
$expected = ['type' => 'question']; | ||
$this->assertEquals($expected, $result); | ||
} | ||
} |