Skip to content

Commit 8239e98

Browse files
committed
[BUGFIX] If no aggregations are set make sure getAggregations won't brake
If you don't request aggregations in your Query but you do access the aggregations in Fluid it results in an Exception. This change will return an empty array if there are no aggregations Fixes: IN-7
1 parent d67949f commit 8239e98

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

Classes/Flowpack/ElasticSearch/ContentRepositoryAdaptor/Eel/ElasticSearchQueryResult.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ public function getAccessibleCount()
192192
public function getAggregations()
193193
{
194194
$this->initialize();
195-
return $this->result['aggregations'];
195+
if (array_key_exists("aggregations", $this->result)) {
196+
return $this->result['aggregations'];
197+
}
198+
return array();
196199
}
197200

198201
/**
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Tests\Unit\Eel;
3+
4+
/* *
5+
* This script belongs to the TYPO3 Flow package "Flowpack.ElasticSearch.ContentRepositoryAdaptor". *
6+
* *
7+
* It is free software; you can redistribute it and/or modify it under *
8+
* the terms of the GNU Lesser General Public License, either version 3 *
9+
* of the License, or (at your option) any later version. *
10+
* *
11+
* The TYPO3 project - inspiring people to share! *
12+
* */
13+
14+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Eel\ElasticSearchQueryBuilder;
15+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Eel\ElasticSearchQueryResult;
16+
17+
/**
18+
* Testcase for ElasticSearchQueryBuilder
19+
*/
20+
class ElasticSearchQueryBuilderResultTest extends \TYPO3\Flow\Tests\UnitTestCase
21+
{
22+
23+
/**
24+
* @test
25+
*/
26+
public function ifNoAggregationsAreSetInTheQueyBuilderResultAnEmptyArrayWillBeReturnedIfYouFetchTheAggregations()
27+
{
28+
$resultArrayWithoutAggregations = array(
29+
"nodes" => array("some", "nodes")
30+
);
31+
32+
$queryBuilder = $this->getMock(ElasticSearchQueryBuilder::class, array("fetch"));
33+
$queryBuilder->method("fetch")->will($this->returnValue($resultArrayWithoutAggregations));
34+
35+
$esQuery = new \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Eel\ElasticSearchQuery($queryBuilder);
36+
37+
$queryResult = new ElasticSearchQueryResult($esQuery);
38+
39+
$actual = $queryResult->getAggregations();
40+
41+
$this->assertTrue(is_array($actual));
42+
$this->assertEmpty($actual);
43+
}
44+
}

0 commit comments

Comments
 (0)