File tree Expand file tree Collapse file tree 2 files changed +70
-2
lines changed
tests/Unit/Query/Compound Expand file tree Collapse file tree 2 files changed +70
-2
lines changed Original file line number Diff line number Diff line change @@ -35,10 +35,18 @@ class BoolQuery implements BuilderInterface
3535
3636 /**
3737 * Constructor to prepare container.
38+ *
39+ * @param array $container
3840 */
39- public function __construct ()
41+ public function __construct (array $ container = [] )
4042 {
41- $ this ->container = [];
43+ foreach ($ container as $ type => $ queries ) {
44+ $ queries = is_array ($ queries ) ? $ queries : [$ queries ];
45+
46+ array_walk ($ queries , function ($ query ) use ($ type ) {
47+ $ this ->add ($ query , $ type );
48+ });
49+ }
4250 }
4351
4452 /**
Original file line number Diff line number Diff line change @@ -32,6 +32,66 @@ public function testBoolAddToBoolException()
3232 $ bool ->add (new MatchAllQuery (), 'acme ' );
3333 }
3434
35+ /**
36+ * Tests constructor builds container
37+ */
38+ public function testBoolConstructor ()
39+ {
40+ $ bool = new BoolQuery ([
41+ BoolQuery::SHOULD => [new TermQuery ('key1 ' , 'value1 ' )],
42+ BoolQuery::MUST => [
43+ new TermQuery ('key2 ' , 'value2 ' ),
44+ new TermQuery ('key3 ' , 'value3 ' ),
45+ ],
46+ BoolQuery::MUST_NOT => new TermQuery ('key4 ' , 'value4 ' )
47+ ]);
48+
49+ $ expected = [
50+ 'bool ' => [
51+ 'should ' => [
52+ [
53+ 'term ' => [
54+ 'key1 ' => 'value1 ' ,
55+ ],
56+ ],
57+ ],
58+ 'must ' => [
59+ [
60+ 'term ' => [
61+ 'key2 ' => 'value2 ' ,
62+ ],
63+ ],
64+ [
65+ 'term ' => [
66+ 'key3 ' => 'value3 ' ,
67+ ],
68+ ],
69+ ],
70+ 'must_not ' => [
71+ [
72+ 'term ' => [
73+ 'key4 ' => 'value4 ' ,
74+ ],
75+ ],
76+ ],
77+ ],
78+ ];
79+ $ this ->assertEquals ($ expected , $ bool ->toArray ());
80+ }
81+
82+ /**
83+ * Tests exception thrown if invalid BoolQuery type key is specified
84+ *
85+ * @expectedException \UnexpectedValueException
86+ * @expectedExceptionMessage The bool operator acme is not supported
87+ */
88+ public function testBoolConstructorException ()
89+ {
90+ new BoolQuery ([
91+ 'acme ' => [new TermQuery ('key1 ' , 'value1 ' )],
92+ ]);
93+ }
94+
3595 /**
3696 * Tests toArray() method.
3797 */
You can’t perform that action at this time.
0 commit comments