Skip to content

Commit ef5c2e1

Browse files
authored
Merge pull request #159 from niels-nijens/fix-boolquery-getqueries
Fix notice in BoolQuery::getQueries when nothing is added to bool type
2 parents d823229 + de17c85 commit ef5c2e1

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

Diff for: src/Query/BoolQuery.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ public function __construct()
4242
}
4343

4444
/**
45-
* @param null $boolType
45+
* Returns the query instances (by bool type).
46+
*
47+
* @param string|null $boolType
48+
*
4649
* @return array
4750
*/
4851
public function getQueries($boolType = null)
@@ -57,7 +60,11 @@ public function getQueries($boolType = null)
5760
return $queries;
5861
}
5962

60-
return $this->container[$boolType];
63+
if (isset($this->container[$boolType])) {
64+
return $this->container[$boolType];
65+
}
66+
67+
return [];
6168
}
6269

6370
/**

Diff for: tests/Query/BoolQueryTest.php

+50
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,54 @@ public function testSingleMust()
112112
];
113113
$this->assertEquals($expected, $bool->toArray());
114114
}
115+
116+
/**
117+
* Tests if BoolQuery::getQueries returns an empty array.
118+
*/
119+
public function testGetQueriesEmpty()
120+
{
121+
$bool = new BoolQuery();
122+
123+
$this->assertInternalType('array', $bool->getQueries());
124+
}
125+
126+
/**
127+
* Tests if BoolQuery::getQueries returns an array with the added queries of all bool types.
128+
*/
129+
public function testGetQueries()
130+
{
131+
$query = new TermQuery('key1', 'value1');
132+
$query2 = new TermQuery('key2', 'value2');
133+
134+
$bool = new BoolQuery();
135+
$bool->add($query, BoolQuery::MUST, 'query');
136+
$bool->add($query2, BoolQuery::SHOULD, 'query2');
137+
138+
$this->assertSame(array('query' => $query, 'query2' => $query2), $bool->getQueries());
139+
}
140+
141+
/**
142+
* Tests if BoolQuery::getQueries with specified bool type returns an empty array.
143+
*/
144+
public function testGetQueriesByBoolTypeEmpty()
145+
{
146+
$bool = new BoolQuery();
147+
148+
$this->assertInternalType('array', $bool->getQueries(BoolQuery::MUST));
149+
}
150+
151+
/**
152+
* Tests if BoolQuery::getQueries with specified bool type returns an array with added queries.
153+
*/
154+
public function testGetQueriesByBoolTypeWithQueryAddedToBoolType()
155+
{
156+
$query = new TermQuery('key1', 'value1');
157+
$query2 = new TermQuery('key2', 'value2');
158+
159+
$bool = new BoolQuery();
160+
$bool->add($query, BoolQuery::MUST, 'query');
161+
$bool->add($query2, BoolQuery::SHOULD, 'query2');
162+
163+
$this->assertSame(array('query' => $query), $bool->getQueries(BoolQuery::MUST));
164+
}
115165
}

0 commit comments

Comments
 (0)