Skip to content

Commit

Permalink
Merge pull request ongr-io#159 from niels-nijens/fix-boolquery-getque…
Browse files Browse the repository at this point in the history
…ries

Fix notice in BoolQuery::getQueries when nothing is added to bool type
  • Loading branch information
saimaz authored Sep 22, 2016
2 parents d823229 + de17c85 commit ef5c2e1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Query/BoolQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 [];
}

/**
Expand Down
50 changes: 50 additions & 0 deletions tests/Query/BoolQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit ef5c2e1

Please sign in to comment.