source: sandbox/expresso-solr/expressoMail1_2/inc/solrclient/tests/Solarium/Query/SelectTest.php @ 7576

Revision 7576, 18.8 KB checked in by adir, 11 years ago (diff)

Ticket #000 - Adicionando a integracao de buscas com Solr na base a ser isnerida na comunidade

Line 
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
32class Solarium_Query_SelectTest extends PHPUnit_Framework_TestCase
33{
34
35    /**
36     * @var Solarium_Query_Select
37     */
38    protected $_query;
39
40    public function setUp()
41    {
42        $this->_query = new Solarium_Query_Select;
43    }
44
45    public function testGetType()
46    {
47        $this->assertEquals(Solarium_Client::QUERYTYPE_SELECT, $this->_query->getType());
48    }
49
50    public function testSetAndGetStart()
51    {
52        $this->_query->setStart(234);
53        $this->assertEquals(234, $this->_query->getStart());
54    }
55
56    public function testSetAndGetQueryWithTrim()
57    {
58        $this->_query->setQuery(' *:* ');
59        $this->assertEquals('*:*', $this->_query->getQuery());
60    }
61
62    public function testSetAndGetQueryWithBind()
63    {
64        $this->_query->setQuery('id:%1%', array(678));
65        $this->assertEquals('id:678', $this->_query->getQuery());
66    }
67
68    public function testSetAndGetQueryDefaultOperator()
69    {
70        $value = Solarium_Query_Select::QUERY_OPERATOR_AND;
71
72        $this->_query->setQueryDefaultOperator($value);
73        $this->assertEquals($value, $this->_query->getQueryDefaultOperator());
74    }
75
76    public function testSetAndGetQueryDefaultField()
77    {
78        $value = 'mydefault';
79
80        $this->_query->setQueryDefaultField($value);
81        $this->assertEquals($value, $this->_query->getQueryDefaultField());
82    }
83
84    public function testSetAndGetResultClass()
85    {
86        $this->_query->setResultClass('MyResult');
87        $this->assertEquals('MyResult', $this->_query->getResultClass());
88    }
89
90    public function testSetAndGetDocumentClass()
91    {
92        $this->_query->setDocumentClass('MyDocument');
93        $this->assertEquals('MyDocument', $this->_query->getDocumentClass());
94    }
95
96    public function testSetAndGetRows()
97    {
98        $this->_query->setRows(100);
99        $this->assertEquals(100, $this->_query->getRows());
100    }
101
102    public function testAddField()
103    {
104        $expectedFields = $this->_query->getFields();
105        $expectedFields[] = 'newfield';
106        $this->_query->addField('newfield');
107        $this->assertEquals($expectedFields, $this->_query->getFields());
108    }
109
110    public function testClearFields()
111    {
112        $this->_query->addField('newfield');
113        $this->_query->clearFields();
114        $this->assertEquals(array(), $this->_query->getFields());
115    }
116
117    public function testAddFields()
118    {
119        $fields = array('field1','field2');
120
121        $this->_query->clearFields();
122        $this->_query->addFields($fields);
123        $this->assertEquals($fields, $this->_query->getFields());
124    }
125
126    public function testAddFieldsAsStringWithTrim()
127    {
128        $this->_query->clearFields();
129        $this->_query->addFields('field1, field2');
130        $this->assertEquals(array('field1','field2'), $this->_query->getFields());
131    }
132
133    public function testRemoveField()
134    {
135        $this->_query->clearFields();
136        $this->_query->addFields(array('field1','field2'));
137        $this->_query->removeField('field1');
138        $this->assertEquals(array('field2'), $this->_query->getFields());
139    }
140
141    public function testSetFields()
142    {
143        $this->_query->clearFields();
144        $this->_query->addFields(array('field1','field2'));
145        $this->_query->setFields(array('field3','field4'));
146        $this->assertEquals(array('field3','field4'), $this->_query->getFields());
147    }
148
149    public function testAddSort()
150    {
151        $this->_query->addSort('field1', Solarium_Query_Select::SORT_DESC);
152        $this->assertEquals(
153            array('field1' => Solarium_Query_Select::SORT_DESC),
154            $this->_query->getSorts()
155        );
156    }
157
158    public function testAddSorts()
159    {
160        $sorts = array(
161            'field1' => Solarium_Query_Select::SORT_DESC,
162            'field2' => Solarium_Query_Select::SORT_ASC
163        );
164
165        $this->_query->addSorts($sorts);
166        $this->assertEquals(
167            $sorts,
168            $this->_query->getSorts()
169        );
170    }
171
172    public function testRemoveSort()
173    {
174        $sorts = array(
175            'field1' => Solarium_Query_Select::SORT_DESC,
176            'field2' => Solarium_Query_Select::SORT_ASC
177        );
178
179        $this->_query->addSorts($sorts);
180        $this->_query->removeSort('field1');
181        $this->assertEquals(
182            array('field2' => Solarium_Query_Select::SORT_ASC),
183            $this->_query->getSorts()
184        );
185    }
186
187    public function testRemoveInvalidSort()
188    {
189        $sorts = array(
190            'field1' => Solarium_Query_Select::SORT_DESC,
191            'field2' => Solarium_Query_Select::SORT_ASC
192        );
193
194        $this->_query->addSorts($sorts);
195        $this->_query->removeSort('invalidfield'); //continue silently
196        $this->assertEquals(
197            $sorts,
198            $this->_query->getSorts()
199        );
200    }
201
202    public function testClearSorts()
203    {
204        $sorts = array(
205            'field1' => Solarium_Query_Select::SORT_DESC,
206            'field2' => Solarium_Query_Select::SORT_ASC
207        );
208
209        $this->_query->addSorts($sorts);
210        $this->_query->clearSorts();
211        $this->assertEquals(
212            array(),
213            $this->_query->getSorts()
214        );
215    }
216
217    public function testSetSorts()
218    {
219        $sorts = array(
220            'field1' => Solarium_Query_Select::SORT_DESC,
221            'field2' => Solarium_Query_Select::SORT_ASC
222        );
223
224        $this->_query->addSorts($sorts);
225        $this->_query->setSorts(array('field3' => Solarium_Query_Select::SORT_ASC));
226        $this->assertEquals(
227            array('field3' => Solarium_Query_Select::SORT_ASC),
228            $this->_query->getSorts()
229        );
230    }
231
232    public function testAddAndGetFilterQuery()
233    {
234        $fq = new Solarium_Query_Select_FilterQuery;
235        $fq->setKey('fq1')->setQuery('category:1');
236        $this->_query->addFilterQuery($fq);
237
238        $this->assertEquals(
239            $fq,
240            $this->_query->getFilterQuery('fq1')
241        );
242    }
243
244    public function testAddAndGetFilterQueryWithKey()
245    {
246        $key = 'fq1';
247
248        $fq = $this->_query->createFilterQuery($key, true);
249        $fq->setQuery('category:1');
250
251        $this->assertEquals(
252            $key,
253            $fq->getKey()
254        );
255
256        $this->assertEquals(
257            $fq,
258            $this->_query->getFilterQuery('fq1')
259        );
260    }
261
262    public function testAddFilterQueryWithoutKey()
263    {
264        $fq = new Solarium_Query_Select_FilterQuery;
265        $fq->setQuery('category:1');
266
267        $this->setExpectedException('Solarium_Exception');
268        $this->_query->addFilterQuery($fq);
269    }
270
271    public function testAddFilterQueryWithUsedKey()
272    {
273        $fq1 = new Solarium_Query_Select_FilterQuery;
274        $fq1->setKey('fq1')->setQuery('category:1');
275
276        $fq2 = new Solarium_Query_Select_FilterQuery;
277        $fq2->setKey('fq1')->setQuery('category:2');
278
279        $this->_query->addFilterQuery($fq1);
280        $this->setExpectedException('Solarium_Exception');
281        $this->_query->addFilterQuery($fq2);
282    }
283
284    public function testGetInvalidFilterQuery()
285    {
286        $this->assertEquals(
287            null,
288            $this->_query->getFilterQuery('invalidtag')
289        );
290    }
291
292    public function testAddFilterQueries()
293    {
294        $fq1 = new Solarium_Query_Select_FilterQuery;
295        $fq1->setKey('fq1')->setQuery('category:1');
296
297        $fq2 = new Solarium_Query_Select_FilterQuery;
298        $fq2->setKey('fq2')->setQuery('category:2');
299
300        $filterQueries = array('fq1' => $fq1, 'fq2' => $fq2);
301
302        $this->_query->addFilterQueries($filterQueries);
303        $this->assertEquals(
304            $filterQueries,
305            $this->_query->getFilterQueries()
306        );
307    }
308
309    public function testRemoveFilterQuery()
310    {
311        $fq1 = new Solarium_Query_Select_FilterQuery;
312        $fq1->setKey('fq1')->setQuery('category:1');
313
314        $fq2 = new Solarium_Query_Select_FilterQuery;
315        $fq2->setKey('fq2')->setQuery('category:2');
316
317        $filterQueries = array($fq1, $fq2);
318
319        $this->_query->addFilterQueries($filterQueries);
320        $this->_query->removeFilterQuery('fq1');
321        $this->assertEquals(
322            array('fq2' => $fq2),
323            $this->_query->getFilterQueries()
324        );
325    }
326
327    public function testRemoveFilterQueryWithObjectInput()
328    {
329        $fq1 = new Solarium_Query_Select_FilterQuery;
330        $fq1->setKey('fq1')->setQuery('category:1');
331
332        $fq2 = new Solarium_Query_Select_FilterQuery;
333        $fq2->setKey('fq2')->setQuery('category:2');
334
335        $filterQueries = array($fq1, $fq2);
336
337        $this->_query->addFilterQueries($filterQueries);
338        $this->_query->removeFilterQuery($fq1);
339        $this->assertEquals(
340            array('fq2' => $fq2),
341            $this->_query->getFilterQueries()
342        );
343    }
344
345    public function testRemoveInvalidFilterQuery()
346    {
347        $fq1 = new Solarium_Query_Select_FilterQuery;
348        $fq1->setKey('fq1')->setQuery('category:1');
349
350        $fq2 = new Solarium_Query_Select_FilterQuery;
351        $fq2->setKey('fq2')->setQuery('category:2');
352
353        $filterQueries = array('fq1' => $fq1, 'fq2' => $fq2);
354
355        $this->_query->addFilterQueries($filterQueries);
356        $this->_query->removeFilterQuery('fq3'); //continue silently
357        $this->assertEquals(
358            $filterQueries,
359            $this->_query->getFilterQueries()
360        );
361    }
362
363    public function testClearFilterQueries()
364    {
365        $fq1 = new Solarium_Query_Select_FilterQuery;
366        $fq1->setKey('fq1')->setQuery('category:1');
367
368        $fq2 = new Solarium_Query_Select_FilterQuery;
369        $fq2->setKey('fq2')->setQuery('category:2');
370
371        $filterQueries = array($fq1, $fq2);
372
373        $this->_query->addFilterQueries($filterQueries);
374        $this->_query->clearFilterQueries();
375        $this->assertEquals(
376            array(),
377            $this->_query->getFilterQueries()
378        );
379    }
380
381    public function testSetFilterQueries()
382    {
383        $fq1 = new Solarium_Query_Select_FilterQuery;
384        $fq1->setKey('fq1')->setQuery('category:1');
385
386        $fq2 = new Solarium_Query_Select_FilterQuery;
387        $fq2->setKey('fq2')->setQuery('category:2');
388
389        $filterQueries1 = array('fq1' => $fq1, 'fq2' => $fq2);
390
391        $this->_query->addFilterQueries($filterQueries1);
392
393        $fq3 = new Solarium_Query_Select_FilterQuery;
394        $fq3->setKey('fq3')->setQuery('category:3');
395
396        $fq4 = new Solarium_Query_Select_FilterQuery;
397        $fq4->setKey('fq4')->setQuery('category:4');
398
399        $filterQueries2 = array('fq3' => $fq3, 'fq4' => $fq4);
400
401        $this->_query->setFilterQueries($filterQueries2);
402
403        $this->assertEquals(
404            $filterQueries2,
405            $this->_query->getFilterQueries()
406        );
407    }
408
409    public function testConfigMode()
410    {
411        $config = array(
412            'query'  => 'text:mykeyword',
413            'sort'   => array('score' => 'asc'),
414            'fields' => array('id','title','category'),
415            'rows'   => 100,
416            'start'  => 200,
417            'filterquery' => array(
418                array('key' => 'pub', 'tag' => array('pub'),'query' => 'published:true'),
419                'online' => array('tag' => 'onl','query' => 'online:true')
420            ),
421            'component' => array(
422                'facetset' => array(
423                    'facet' => array(
424                        array('type' => 'field', 'key' => 'categories', 'field' => 'category'),
425                        'category13' => array('type' => 'query', 'query' => 'category:13')
426                    )
427                ),
428            ),
429            'resultclass' => 'MyResultClass',
430            'documentclass' => 'MyDocumentClass',
431        );
432        $query = new Solarium_Query_Select($config);
433
434        $this->assertEquals($config['query'], $query->getQuery());
435        $this->assertEquals($config['sort'], $query->getSorts());
436        $this->assertEquals($config['fields'], $query->getFields());
437        $this->assertEquals($config['rows'], $query->getRows());
438        $this->assertEquals($config['start'], $query->getStart());
439        $this->assertEquals($config['documentclass'], $query->getDocumentClass());
440        $this->assertEquals($config['resultclass'], $query->getResultClass());
441        $this->assertEquals('published:true', $query->getFilterQuery('pub')->getQuery());
442        $this->assertEquals('online:true', $query->getFilterQuery('online')->getQuery());
443
444        $facets = $query->getFacetSet()->getFacets();
445        $this->assertEquals(
446            'category',
447            $facets['categories']->getField()
448        );
449        $this->assertEquals(
450            'category:13',
451            $facets['category13']->getQuery()
452        );
453
454        $components = $query->getComponents();
455        $this->assertEquals(1, count($components));
456        $this->assertThat(array_pop($components), $this->isInstanceOf('Solarium_Query_Select_Component_FacetSet'));
457    }
458
459    public function testSetAndGetComponents()
460    {
461        $mlt = new Solarium_Query_Select_Component_MoreLikeThis;
462        $this->_query->setComponent('mlt',$mlt);
463
464        $this->assertEquals(
465            array('mlt' => $mlt),
466            $this->_query->getComponents()
467        );
468    }
469
470    public function testSetAndGetComponent()
471    {
472        $mlt = new Solarium_Query_Select_Component_MoreLikeThis;
473        $this->_query->setComponent('mlt',$mlt);
474
475        $this->assertEquals(
476            $mlt,
477            $this->_query->getComponent('mlt')
478        );
479    }
480
481    public function testGetInvalidComponent()
482    {
483        $this->assertEquals(
484            null,
485            $this->_query->getComponent('invalid')
486        );
487    }
488
489    public function testGetInvalidComponentAutoload()
490    {
491        $this->setExpectedException('Solarium_Exception');
492        $this->_query->getComponent('invalid', true);
493    }
494
495    public function testRemoveComponent()
496    {
497        $mlt = new Solarium_Query_Select_Component_MoreLikeThis;
498        $this->_query->setComponent('mlt',$mlt);
499
500        $this->assertEquals(
501            array('mlt' => $mlt),
502            $this->_query->getComponents()
503        );
504
505        $this->_query->removeComponent('mlt');
506
507        $this->assertEquals(
508            array(),
509            $this->_query->getComponents()
510        );
511    }
512
513    public function testRemoveComponentWithObjectInput()
514    {
515        $mlt = new Solarium_Query_Select_Component_MoreLikeThis;
516        $this->_query->setComponent('mlt',$mlt);
517
518        $this->assertEquals(
519            array('mlt' => $mlt),
520            $this->_query->getComponents()
521        );
522
523        $this->_query->removeComponent($mlt);
524
525        $this->assertEquals(
526            array(),
527            $this->_query->getComponents()
528        );
529    }
530
531    public function testGetMoreLikeThis()
532    {
533        $mlt = $this->_query->getMoreLikeThis();
534
535        $this->assertEquals(
536            'Solarium_Query_Select_Component_MoreLikeThis',
537            get_class($mlt)
538        );
539    }
540
541    public function testGetDisMax()
542    {
543        $dismax = $this->_query->getDisMax();
544
545        $this->assertEquals(
546            'Solarium_Query_Select_Component_DisMax',
547            get_class($dismax)
548        );
549    }
550
551    public function testGetHighlighting()
552    {
553        $hlt = $this->_query->getHighlighting();
554
555        $this->assertEquals(
556            'Solarium_Query_Select_Component_Highlighting',
557            get_class($hlt)
558        );
559    }
560
561    public function testGetGrouping()
562    {
563        $grouping = $this->_query->getGrouping();
564
565        $this->assertEquals(
566            'Solarium_Query_Select_Component_Grouping',
567            get_class($grouping)
568        );
569    }
570
571    public function testRegisterComponentType()
572    {
573        $components = $this->_query->getComponentTypes();
574        $components['mykey'] = array(
575            'component' => 'mycomponent',
576            'requestbuilder' => 'mybuilder',
577            'responseparser' => 'myparser',
578        );
579
580        $this->_query->registerComponentType('mykey','mycomponent','mybuilder','myparser');
581
582        $this->assertEquals(
583            $components,
584            $this->_query->getComponentTypes()
585        );
586    }
587
588    public function testCreateFilterQuery()
589    {
590        $options = array('optionA' => 1, 'optionB' => 2);
591        $fq = $this->_query->createFilterQuery($options);
592
593        // check class
594       $this->assertThat($fq, $this->isInstanceOf('Solarium_Query_Select_FilterQuery'));
595
596        // check option forwarding
597        $fqOptions = $fq->getOptions();
598        $this->assertEquals(
599            $options['optionB'],
600            $fqOptions['optionB']
601        );
602    }
603
604    public function testGetSpellcheck()
605    {
606        $spellcheck = $this->_query->getSpellcheck();
607
608        $this->assertEquals(
609            'Solarium_Query_Select_Component_Spellcheck',
610            get_class($spellcheck)
611        );
612    }
613
614    public function testGetDistributedSearch()
615    {
616        $spellcheck = $this->_query->getDistributedSearch();
617
618        $this->assertEquals(
619            'Solarium_Query_Select_Component_DistributedSearch',
620            get_class($spellcheck)
621        );
622    }
623
624    public function testGetStats()
625    {
626        $stats = $this->_query->getStats();
627
628        $this->assertEquals(
629            'Solarium_Query_Select_Component_Stats',
630            get_class($stats)
631        );
632    }
633
634    public function testGetDebug()
635    {
636        $stats = $this->_query->getDebug();
637
638        $this->assertEquals(
639            'Solarium_Query_Select_Component_Debug',
640            get_class($stats)
641        );
642    }
643}
Note: See TracBrowser for help on using the repository browser.