From 729740b06f820302b874551e06977fa5f188bafe Mon Sep 17 00:00:00 2001 From: Niels Nijens Date: Wed, 21 Sep 2016 15:50:48 +0200 Subject: [PATCH 1/2] Add tests for BoolQuery::getQueries --- tests/Query/BoolQueryTest.php | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/Query/BoolQueryTest.php b/tests/Query/BoolQueryTest.php index d08dfe18..738bada3 100644 --- a/tests/Query/BoolQueryTest.php +++ b/tests/Query/BoolQueryTest.php @@ -112,4 +112,54 @@ public function testSingleMust() ]; $this->assertEquals($expected, $bool->toArray()); } + + /** + * Tests if BoolQuery::getQueries returns an empty array. + */ + public function testGetQueriesEmpty() + { + $bool = new BoolQuery(); + + $this->assertInternalType('array', $bool->getQueries()); + } + + /** + * Tests if BoolQuery::getQueries returns an array with the added queries of all bool types. + */ + public function testGetQueries() + { + $query = new TermQuery('key1', 'value1'); + $query2 = new TermQuery('key2', 'value2'); + + $bool = new BoolQuery(); + $bool->add($query, BoolQuery::MUST, 'query'); + $bool->add($query2, BoolQuery::SHOULD, 'query2'); + + $this->assertSame(array('query' => $query, 'query2' => $query2), $bool->getQueries()); + } + + /** + * Tests if BoolQuery::getQueries with specified bool type returns an empty array. + */ + public function testGetQueriesByBoolTypeEmpty() + { + $bool = new BoolQuery(); + + $this->assertInternalType('array', $bool->getQueries(BoolQuery::MUST)); + } + + /** + * Tests if BoolQuery::getQueries with specified bool type returns an array with added queries. + */ + public function testGetQueriesByBoolTypeWithQueryAddedToBoolType() + { + $query = new TermQuery('key1', 'value1'); + $query2 = new TermQuery('key2', 'value2'); + + $bool = new BoolQuery(); + $bool->add($query, BoolQuery::MUST, 'query'); + $bool->add($query2, BoolQuery::SHOULD, 'query2'); + + $this->assertSame(array('query' => $query), $bool->getQueries(BoolQuery::MUST)); + } } From de17c854e17ad577b50dc6f2ac3c7b0e916e7f3b Mon Sep 17 00:00:00 2001 From: Niels Nijens Date: Wed, 21 Sep 2016 15:51:54 +0200 Subject: [PATCH 2/2] Fix notice in BoolQuery::getQueries when nothing is added to bool type --- src/Query/BoolQuery.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Query/BoolQuery.php b/src/Query/BoolQuery.php index 51a0be02..55bd94a9 100644 --- a/src/Query/BoolQuery.php +++ b/src/Query/BoolQuery.php @@ -42,7 +42,10 @@ public function __construct() } /** - * @param null $boolType + * Returns the query instances (by bool type). + * + * @param string|null $boolType + * * @return array */ public function getQueries($boolType = null) @@ -57,7 +60,11 @@ public function getQueries($boolType = null) return $queries; } - return $this->container[$boolType]; + if (isset($this->container[$boolType])) { + return $this->container[$boolType]; + } + + return []; } /**