diff --git a/src/Query/BoolQuery.php b/src/Query/BoolQuery.php index 51a0be0258c728fd5c228116437a374d4e555749..55bd94a94fb35494f49f6be0ae517852bbe4b8e9 100644 --- a/src/Query/BoolQuery.php +++ b/src/Query/BoolQuery.php @@ -42,7 +42,10 @@ class BoolQuery implements BuilderInterface } /** - * @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 @@ class BoolQuery implements BuilderInterface return $queries; } - return $this->container[$boolType]; + if (isset($this->container[$boolType])) { + return $this->container[$boolType]; + } + + return []; } /** diff --git a/tests/Query/BoolQueryTest.php b/tests/Query/BoolQueryTest.php index d08dfe181152f35de42da220a2bb7fd1ee850246..738bada3c021f0a05fc23216c201cd14a0a70735 100644 --- a/tests/Query/BoolQueryTest.php +++ b/tests/Query/BoolQueryTest.php @@ -112,4 +112,54 @@ class BoolQueryTest extends \PHPUnit_Framework_TestCase ]; $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)); + } }