1 | <?php |
---|
2 | /** |
---|
3 | * Copyright 2011 Bas de Nooijer. All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions are met: |
---|
7 | * |
---|
8 | * 1. Redistributions of source code must retain the above copyright notice, |
---|
9 | * this list of conditions and the following disclaimer. |
---|
10 | * |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
12 | * this listof conditions and the following disclaimer in the documentation |
---|
13 | * and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" |
---|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
26 | * |
---|
27 | * The views and conclusions contained in the software and documentation are |
---|
28 | * those of the authors and should not be interpreted as representing official |
---|
29 | * policies, either expressed or implied, of the copyright holder. |
---|
30 | */ |
---|
31 | |
---|
32 | class Solarium_Query_Select_Component_Facet_MultiQueryTest extends PHPUnit_Framework_TestCase |
---|
33 | { |
---|
34 | |
---|
35 | /** |
---|
36 | * @var Solarium_Query_Select_Component_Facet_MultiQuery |
---|
37 | */ |
---|
38 | protected $_facet; |
---|
39 | |
---|
40 | public function setUp() |
---|
41 | { |
---|
42 | $this->_facet = new Solarium_Query_Select_Component_Facet_MultiQuery; |
---|
43 | } |
---|
44 | |
---|
45 | public function testConfigMode() |
---|
46 | { |
---|
47 | $options = array( |
---|
48 | 'key' => 'myKey', |
---|
49 | 'exclude' => array('e1','e2'), |
---|
50 | 'query' => array( |
---|
51 | array( |
---|
52 | 'key' => 'k1', |
---|
53 | 'query' => 'category:1', |
---|
54 | 'exclude' => array('fq1','fq2') |
---|
55 | ), |
---|
56 | 'k2' => array( |
---|
57 | 'query' => 'category:2' |
---|
58 | ), |
---|
59 | ) |
---|
60 | ); |
---|
61 | |
---|
62 | $this->_facet->setOptions($options); |
---|
63 | |
---|
64 | $this->assertEquals($options['key'], $this->_facet->getKey()); |
---|
65 | $this->assertEquals($options['exclude'], $this->_facet->getExcludes()); |
---|
66 | |
---|
67 | $query1 = $this->_facet->getQuery('k1'); |
---|
68 | $this->assertEquals('k1', $query1->getKey()); |
---|
69 | $this->assertEquals('category:1', $query1->getQuery()); |
---|
70 | $this->assertEquals(array('fq1','fq2', 'e1', 'e2'), $query1->getExcludes()); |
---|
71 | |
---|
72 | $query2 = $this->_facet->getQuery('k2'); |
---|
73 | $this->assertEquals('k2', $query2->getKey()); |
---|
74 | $this->assertEquals('category:2', $query2->getQuery()); |
---|
75 | } |
---|
76 | |
---|
77 | public function testGetType() |
---|
78 | { |
---|
79 | $this->assertEquals( |
---|
80 | Solarium_Query_Select_Component_FacetSet::FACET_MULTIQUERY, |
---|
81 | $this->_facet->getType() |
---|
82 | ); |
---|
83 | } |
---|
84 | |
---|
85 | public function testCreateAndGetQuery() |
---|
86 | { |
---|
87 | $key = 'k1'; |
---|
88 | $query = 'category:1'; |
---|
89 | $excludes = array('fq1','fq2'); |
---|
90 | |
---|
91 | $facetQuery = new Solarium_Query_Select_Component_Facet_Query; |
---|
92 | $facetQuery->setKey($key); |
---|
93 | $facetQuery->setQuery($query); |
---|
94 | $facetQuery->setExcludes($excludes); |
---|
95 | |
---|
96 | $this->_facet->createQuery($key, $query, $excludes); |
---|
97 | |
---|
98 | $this->assertEquals( |
---|
99 | $facetQuery, |
---|
100 | $this->_facet->getQuery($key) |
---|
101 | ); |
---|
102 | } |
---|
103 | |
---|
104 | public function testAddAndGetQuery() |
---|
105 | { |
---|
106 | $key = 'k1'; |
---|
107 | $query = 'category:1'; |
---|
108 | $excludes = array('fq1','fq2'); |
---|
109 | |
---|
110 | $facetQuery = new Solarium_Query_Select_Component_Facet_Query; |
---|
111 | $facetQuery->setKey($key); |
---|
112 | $facetQuery->setQuery($query); |
---|
113 | $facetQuery->setExcludes($excludes); |
---|
114 | |
---|
115 | $this->_facet->addQuery($facetQuery); |
---|
116 | |
---|
117 | $this->assertEquals( |
---|
118 | $facetQuery, |
---|
119 | $this->_facet->getQuery($key) |
---|
120 | ); |
---|
121 | } |
---|
122 | |
---|
123 | public function testAddQueryWithConfig() |
---|
124 | { |
---|
125 | $config = array( |
---|
126 | 'key' => 'k1', |
---|
127 | 'query' => 'category:1', |
---|
128 | 'excludes' => array('fq1','fq2') |
---|
129 | ); |
---|
130 | |
---|
131 | $facetQuery = new Solarium_Query_Select_Component_Facet_Query($config); |
---|
132 | |
---|
133 | $this->_facet->addQuery($config); |
---|
134 | |
---|
135 | $this->assertEquals( |
---|
136 | $facetQuery, |
---|
137 | $this->_facet->getQuery($config['key']) |
---|
138 | ); |
---|
139 | } |
---|
140 | |
---|
141 | public function testAddQueryNoKey() |
---|
142 | { |
---|
143 | $query = 'category:1'; |
---|
144 | $excludes = array('fq1','fq2'); |
---|
145 | |
---|
146 | $facetQuery = new Solarium_Query_Select_Component_Facet_Query; |
---|
147 | $facetQuery->setQuery($query); |
---|
148 | $facetQuery->setExcludes($excludes); |
---|
149 | |
---|
150 | $this->setExpectedException('Solarium_Exception'); |
---|
151 | $this->_facet->addQuery($facetQuery); |
---|
152 | } |
---|
153 | |
---|
154 | public function testAddQueryNoUniqueKey() |
---|
155 | { |
---|
156 | $facetQuery1 = new Solarium_Query_Select_Component_Facet_Query; |
---|
157 | $facetQuery1->setKey('k1'); |
---|
158 | $facetQuery1->setQuery('category:1'); |
---|
159 | |
---|
160 | $facetQuery2 = new Solarium_Query_Select_Component_Facet_Query; |
---|
161 | $facetQuery2->setKey('k1'); |
---|
162 | $facetQuery2->setQuery('category:2'); |
---|
163 | |
---|
164 | $this->_facet->addQuery($facetQuery1); |
---|
165 | |
---|
166 | $this->setExpectedException('Solarium_Exception'); |
---|
167 | $this->_facet->addQuery($facetQuery2); |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | public function testAddQueryExcludeForwarding() |
---|
172 | { |
---|
173 | $this->_facet->addExclude('fq1'); |
---|
174 | |
---|
175 | $facetQuery = new Solarium_Query_Select_Component_Facet_Query; |
---|
176 | $facetQuery->setKey('k1'); |
---|
177 | $facetQuery->setQuery('category:1'); |
---|
178 | |
---|
179 | $this->_facet->addQuery($facetQuery); |
---|
180 | |
---|
181 | $this->assertEquals( |
---|
182 | array('fq1'), |
---|
183 | $facetQuery->getExcludes() |
---|
184 | ); |
---|
185 | } |
---|
186 | |
---|
187 | public function testAddAndGetQueries() |
---|
188 | { |
---|
189 | $facetQuery1 = new Solarium_Query_Select_Component_Facet_Query; |
---|
190 | $facetQuery1->setKey('k1'); |
---|
191 | $facetQuery1->setQuery('category:1'); |
---|
192 | |
---|
193 | $facetQuery2 = new Solarium_Query_Select_Component_Facet_Query; |
---|
194 | $facetQuery2->setKey('k2'); |
---|
195 | $facetQuery2->setQuery('category:2'); |
---|
196 | |
---|
197 | $this->_facet->addQueries(array($facetQuery1, $facetQuery2)); |
---|
198 | |
---|
199 | $this->assertEquals( |
---|
200 | array('k1' => $facetQuery1, 'k2' => $facetQuery2), |
---|
201 | $this->_facet->getQueries() |
---|
202 | ); |
---|
203 | } |
---|
204 | |
---|
205 | public function testAddQueriesWithConfig() |
---|
206 | { |
---|
207 | $facetQuery1 = new Solarium_Query_Select_Component_Facet_Query; |
---|
208 | $facetQuery1->setKey('k1'); |
---|
209 | $facetQuery1->setQuery('category:1'); |
---|
210 | $facetQuery1->addExcludes(array('fq1','fq2')); |
---|
211 | |
---|
212 | $facetQuery2 = new Solarium_Query_Select_Component_Facet_Query; |
---|
213 | $facetQuery2->setKey('k2'); |
---|
214 | $facetQuery2->setQuery('category:2'); |
---|
215 | |
---|
216 | $facetQueries = array('k1' => $facetQuery1, 'k2' => $facetQuery2); |
---|
217 | |
---|
218 | $config = array( |
---|
219 | array( |
---|
220 | 'key' => 'k1', |
---|
221 | 'query' => 'category:1', |
---|
222 | 'exclude' => array('fq1','fq2') |
---|
223 | ), |
---|
224 | 'k2' => array( |
---|
225 | 'query' => 'category:2' |
---|
226 | ), |
---|
227 | ); |
---|
228 | $this->_facet->addQueries($config); |
---|
229 | |
---|
230 | $this->assertEquals( |
---|
231 | $facetQueries, |
---|
232 | $this->_facet->getQueries() |
---|
233 | ); |
---|
234 | } |
---|
235 | |
---|
236 | public function testGetInvalidQuery() |
---|
237 | { |
---|
238 | $this->assertEquals( |
---|
239 | null, |
---|
240 | $this->_facet->getQuery('invalidkey') |
---|
241 | ); |
---|
242 | } |
---|
243 | |
---|
244 | public function testRemoveQuery() |
---|
245 | { |
---|
246 | $facetQuery = new Solarium_Query_Select_Component_Facet_Query; |
---|
247 | $facetQuery->setKey('k1'); |
---|
248 | $facetQuery->setQuery('category:1'); |
---|
249 | |
---|
250 | $this->_facet->addQuery($facetQuery); |
---|
251 | $this->assertEquals( |
---|
252 | array('k1' => $facetQuery), |
---|
253 | $this->_facet->getQueries() |
---|
254 | ); |
---|
255 | |
---|
256 | $this->_facet->removeQuery('k1'); |
---|
257 | $this->assertEquals( |
---|
258 | array(), |
---|
259 | $this->_facet->getQueries() |
---|
260 | ); |
---|
261 | } |
---|
262 | |
---|
263 | public function testRemoveQueryWithObjectInput() |
---|
264 | { |
---|
265 | $facetQuery = new Solarium_Query_Select_Component_Facet_Query; |
---|
266 | $facetQuery->setKey('k1'); |
---|
267 | $facetQuery->setQuery('category:1'); |
---|
268 | |
---|
269 | $this->_facet->addQuery($facetQuery); |
---|
270 | $this->assertEquals( |
---|
271 | array('k1' => $facetQuery), |
---|
272 | $this->_facet->getQueries() |
---|
273 | ); |
---|
274 | |
---|
275 | $this->_facet->removeQuery($facetQuery); |
---|
276 | $this->assertEquals( |
---|
277 | array(), |
---|
278 | $this->_facet->getQueries() |
---|
279 | ); |
---|
280 | } |
---|
281 | |
---|
282 | public function testRemoveInvalidQuery() |
---|
283 | { |
---|
284 | $facetQuery = new Solarium_Query_Select_Component_Facet_Query; |
---|
285 | $facetQuery->setKey('k1'); |
---|
286 | $facetQuery->setQuery('category:1'); |
---|
287 | $this->_facet->addQuery($facetQuery); |
---|
288 | |
---|
289 | $before = $this->_facet->getQueries(); |
---|
290 | $this->_facet->removeQuery('invalidkey'); |
---|
291 | $after = $this->_facet->getQueries(); |
---|
292 | |
---|
293 | $this->assertEquals( |
---|
294 | $before, |
---|
295 | $after |
---|
296 | ); |
---|
297 | } |
---|
298 | |
---|
299 | public function testClearQueries() |
---|
300 | { |
---|
301 | $facetQuery1 = new Solarium_Query_Select_Component_Facet_Query; |
---|
302 | $facetQuery1->setKey('k1'); |
---|
303 | $facetQuery1->setQuery('category:1'); |
---|
304 | |
---|
305 | $facetQuery2 = new Solarium_Query_Select_Component_Facet_Query; |
---|
306 | $facetQuery2->setKey('k2'); |
---|
307 | $facetQuery2->setQuery('category:2'); |
---|
308 | |
---|
309 | $this->_facet->addQueries(array($facetQuery1, $facetQuery2)); |
---|
310 | $this->_facet->clearQueries(); |
---|
311 | $this->assertEquals( |
---|
312 | array(), |
---|
313 | $this->_facet->getQueries() |
---|
314 | ); |
---|
315 | } |
---|
316 | |
---|
317 | public function testSetQueries() |
---|
318 | { |
---|
319 | $facetQuery1 = new Solarium_Query_Select_Component_Facet_Query; |
---|
320 | $facetQuery1->setKey('k1'); |
---|
321 | $facetQuery1->setQuery('category:1'); |
---|
322 | $this->_facet->addQuery($facetQuery1); |
---|
323 | |
---|
324 | $facetQuery2 = new Solarium_Query_Select_Component_Facet_Query; |
---|
325 | $facetQuery2->setKey('k2'); |
---|
326 | $facetQuery2->setQuery('category:2'); |
---|
327 | |
---|
328 | $facetQuery3 = new Solarium_Query_Select_Component_Facet_Query; |
---|
329 | $facetQuery3->setKey('k3'); |
---|
330 | $facetQuery3->setQuery('category:3'); |
---|
331 | |
---|
332 | $this->_facet->setQueries(array($facetQuery2, $facetQuery3)); |
---|
333 | $this->assertEquals( |
---|
334 | array('k2' => $facetQuery2, 'k3' => $facetQuery3), |
---|
335 | $this->_facet->getQueries() |
---|
336 | ); |
---|
337 | } |
---|
338 | |
---|
339 | public function testAddExcludeForwarding() |
---|
340 | { |
---|
341 | $facetQuery = new Solarium_Query_Select_Component_Facet_Query; |
---|
342 | $facetQuery->setKey('k1'); |
---|
343 | $facetQuery->setQuery('category:1'); |
---|
344 | $this->_facet->addQuery($facetQuery); |
---|
345 | |
---|
346 | $this->_facet->addExclude('fq1'); |
---|
347 | |
---|
348 | $this->assertEquals( |
---|
349 | array('fq1'), |
---|
350 | $facetQuery->getExcludes() |
---|
351 | ); |
---|
352 | } |
---|
353 | |
---|
354 | public function testRemoveExcludeForwarding() |
---|
355 | { |
---|
356 | $this->_facet->addExclude('fq1'); |
---|
357 | |
---|
358 | $facetQuery = new Solarium_Query_Select_Component_Facet_Query; |
---|
359 | $facetQuery->setKey('k1'); |
---|
360 | $facetQuery->setQuery('category:1'); |
---|
361 | $this->_facet->addQuery($facetQuery); |
---|
362 | |
---|
363 | $this->assertEquals( |
---|
364 | array('fq1'), |
---|
365 | $facetQuery->getExcludes() |
---|
366 | ); |
---|
367 | |
---|
368 | $this->_facet->removeExclude('fq1'); |
---|
369 | |
---|
370 | $this->assertEquals( |
---|
371 | array(), |
---|
372 | $facetQuery->getExcludes() |
---|
373 | ); |
---|
374 | } |
---|
375 | |
---|
376 | public function testClearExcludesForwarding() |
---|
377 | { |
---|
378 | $this->_facet->addExclude('fq1'); |
---|
379 | $this->_facet->addExclude('fq2'); |
---|
380 | |
---|
381 | $facetQuery = new Solarium_Query_Select_Component_Facet_Query; |
---|
382 | $facetQuery->setKey('k1'); |
---|
383 | $facetQuery->setQuery('category:1'); |
---|
384 | $this->_facet->addQuery($facetQuery); |
---|
385 | |
---|
386 | $this->assertEquals( |
---|
387 | array('fq1','fq2'), |
---|
388 | $facetQuery->getExcludes() |
---|
389 | ); |
---|
390 | |
---|
391 | $this->_facet->clearExcludes(); |
---|
392 | |
---|
393 | $this->assertEquals( |
---|
394 | array(), |
---|
395 | $facetQuery->getExcludes() |
---|
396 | ); |
---|
397 | } |
---|
398 | |
---|
399 | |
---|
400 | } |
---|