<?php /* * This file is part of the ONGR package. * * (c) NFQ Technologies UAB <info@nfq.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace ONGR\ElasticsearchDSL\Tests\Aggregation; use ONGR\ElasticsearchDSL\Aggregation\FilterAggregation; use ONGR\ElasticsearchDSL\Aggregation\HistogramAggregation; use ONGR\ElasticsearchDSL\Query\BoolQuery; use ONGR\ElasticsearchDSL\Query\MatchAllQuery; use ONGR\ElasticsearchDSL\Query\MissingQuery; use ONGR\ElasticsearchDSL\Query\TermQuery; class FilterAggregationTest extends \PHPUnit_Framework_TestCase { /** * Data provider for testToArray. * * @return array */ public function getToArrayData() { $out = []; // Case #0 filter aggregation. $aggregation = new FilterAggregation('test_agg'); $filter = new MatchAllQuery(); $aggregation->setFilter($filter); $result = [ 'filter' => $filter->toArray(), ]; $out[] = [ $aggregation, $result, ]; // Case #1 nested filter aggregation. $aggregation = new FilterAggregation('test_agg'); $aggregation->setFilter($filter); $histogramAgg = new HistogramAggregation('acme', 'bar', 10); $aggregation->addAggregation($histogramAgg); $result = [ 'filter' => $filter->toArray(), 'aggregations' => [ $histogramAgg->getName() => $histogramAgg->toArray(), ], ]; $out[] = [ $aggregation, $result, ]; // Case #2 testing bool filter. $aggregation = new FilterAggregation('test_agg'); $matchAllFilter = new MatchAllQuery(); $termFilter = new TermQuery('acme', 'foo'); $boolFilter = new BoolQuery(); $boolFilter->add($matchAllFilter); $boolFilter->add($termFilter); $aggregation->setFilter($boolFilter); $result = [ 'filter' => $boolFilter->toArray(), ]; $out[] = [ $aggregation, $result, ]; return $out; } /** * Test for filter aggregation toArray() method. * * @param FilterAggregation $aggregation * @param array $expectedResult * * @dataProvider getToArrayData */ public function testToArray($aggregation, $expectedResult) { $this->assertEquals($expectedResult, $aggregation->toArray()); } /** * Test for setField(). * * @expectedException \LogicException * @expectedExceptionMessage doesn't support `field` parameter */ public function testSetField() { $aggregation = new FilterAggregation('test_agg'); $aggregation->setField('test_field'); } /** * Test for toArray() without setting a filter. * * @expectedException \LogicException * @expectedExceptionMessage has no filter added */ public function testToArrayNoFilter() { $aggregation = new FilterAggregation('test_agg'); $aggregation->toArray(); } /** * Test for toArray() with setting a filter. */ public function testToArrayWithFilter() { $aggregation = new FilterAggregation('test_agg'); $aggregation->setFilter(new MissingQuery('test')); $aggregation->toArray(); } /** * Tests if filter can be passed to constructor. */ public function testConstructorFilter() { $matchAllFilter = new MatchAllQuery(); $aggregation = new FilterAggregation('test', $matchAllFilter); $this->assertEquals( [ 'filter' => $matchAllFilter->toArray(), ], $aggregation->toArray() ); } }