source: sandbox/expresso-solr/expressoMail1_2/solrclient/tests/Solarium/Query/Select/Component/FacetSetTest.php @ 7588

Revision 7588, 11.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_Select_Component_FacetSetTest extends PHPUnit_Framework_TestCase
33{
34
35    /**
36     * @var Solarium_Query_Select_Component_FacetSet
37     */
38    protected $_facetSet;
39
40    public function setUp()
41    {
42        $this->_facetSet = new Solarium_Query_Select_Component_FacetSet;
43    }
44
45    public function testConfigMode()
46    {
47        $options = array(
48            'facet' => array(
49                array('type' => 'query', 'key' => 'f1', 'query' => 'category:1'),
50                'f2' => array('type' => 'query', 'query' => 'category:2')
51            ),
52            'prefix' => 'pr',
53            'sort' => 'index',
54            'mincount' => 10,
55            'missing' => 5,
56        );
57
58        $this->_facetSet->setOptions($options);
59        $facets = $this->_facetSet->getFacets();
60
61        $this->assertEquals(2, count($facets));
62        $this->assertEquals($options['prefix'], $this->_facetSet->getPrefix());
63        $this->assertEquals($options['sort'], $this->_facetSet->getSort());
64        $this->assertEquals($options['mincount'], $this->_facetSet->getMincount());
65        $this->assertEquals($options['missing'], $this->_facetSet->getMissing());
66    }
67
68    public function testGetType()
69    {
70        $this->assertEquals(Solarium_Query_Select::COMPONENT_FACETSET, $this->_facetSet->getType());
71    }
72
73    public function testSetAndGetSort()
74    {
75        $this->_facetSet->setSort('index');
76        $this->assertEquals('index', $this->_facetSet->getSort());
77    }
78
79    public function testSetAndGetPrefix()
80    {
81        $this->_facetSet->setPrefix('xyz');
82        $this->assertEquals('xyz', $this->_facetSet->getPrefix());
83    }
84
85    public function testSetAndGetLimit()
86    {
87        $this->_facetSet->setLimit(12);
88        $this->assertEquals(12, $this->_facetSet->getLimit());
89    }
90
91    public function testSetAndGetMinCount()
92    {
93        $this->_facetSet->setMincount(100);
94        $this->assertEquals(100, $this->_facetSet->getMincount());
95    }
96
97    public function testSetAndGetMissing()
98    {
99        $this->_facetSet->setMissing(true);
100        $this->assertEquals(true, $this->_facetSet->getMissing());
101    }
102
103    public function testAddAndGetFacet()
104    {
105        $fq = new Solarium_Query_Select_Component_Facet_Query;
106        $fq->setKey('f1')->setQuery('category:1');
107        $this->_facetSet->addFacet($fq);
108
109        $this->assertEquals(
110            $fq,
111            $this->_facetSet->getFacet('f1')
112        );
113    }
114
115    public function testAddFacetWithoutKey()
116    {
117        $fq = new Solarium_Query_Select_Component_Facet_Query;
118        $fq->setQuery('category:1');
119
120        $this->setExpectedException('Solarium_Exception');
121        $this->_facetSet->addFacet($fq);
122    }
123
124    public function testAddFacetWithUsedKey()
125    {
126        $fq1 = new Solarium_Query_Select_Component_Facet_Query;
127        $fq1->setKey('f1')->setQuery('category:1');
128
129        $fq2 = new Solarium_Query_Select_Component_Facet_Query;
130        $fq2->setKey('f1')->setQuery('category:2');
131
132        $this->_facetSet->addFacet($fq1);
133        $this->setExpectedException('Solarium_Exception');
134        $this->_facetSet->addFacet($fq2);
135    }
136
137    public function testGetInvalidFacet()
138    {
139        $this->assertEquals(
140            null,
141            $this->_facetSet->getFacet('invalidtag')
142        );
143    }
144
145    public function testAddFacets()
146    {
147        $fq1 = new Solarium_Query_Select_Component_Facet_Query;
148        $fq1->setKey('f1')->setQuery('category:1');
149
150        $fq2 = new Solarium_Query_Select_Component_Facet_Query;
151        $fq2->setKey('f2')->setQuery('category:2');
152
153        $facets = array('f1' => $fq1, 'f2' => $fq2);
154
155        $this->_facetSet->addFacets($facets);
156        $this->assertEquals(
157            $facets,
158            $this->_facetSet->getFacets()
159        );
160    }
161
162    public function testAddFacetsWithConfig()
163    {
164        $facets = array(
165            array('type' => 'query', 'key' => 'f1', 'query' => 'category:1'),
166            'f2' => array('type' => 'query', 'query' => 'category:2')
167        );
168
169        $this->_facetSet->addFacets($facets);
170
171        $this->assertEquals(
172            2,
173            count($this->_facetSet->getFacets())
174        );
175    }
176
177    public function testRemoveFacet()
178    {
179        $fq1 = new Solarium_Query_Select_Component_Facet_Query;
180        $fq1->setKey('f1')->setQuery('category:1');
181
182        $fq2 = new Solarium_Query_Select_Component_Facet_Query;
183        $fq2->setKey('f2')->setQuery('category:2');
184
185        $facets = array('f1' => $fq1, 'f2' => $fq2);
186
187        $this->_facetSet->addFacets($facets);
188        $this->_facetSet->removeFacet('f1');
189        $this->assertEquals(
190            array('f2' => $fq2),
191            $this->_facetSet->getFacets()
192        );
193    }
194
195    public function testRemoveFacetWithObjectInput()
196    {
197        $fq1 = new Solarium_Query_Select_Component_Facet_Query;
198        $fq1->setKey('f1')->setQuery('category:1');
199
200        $fq2 = new Solarium_Query_Select_Component_Facet_Query;
201        $fq2->setKey('f2')->setQuery('category:2');
202
203        $facets = array('f1' => $fq1, 'f2' => $fq2);
204
205        $this->_facetSet->addFacets($facets);
206        $this->_facetSet->removeFacet($fq1);
207        $this->assertEquals(
208            array('f2' => $fq2),
209            $this->_facetSet->getFacets()
210        );
211    }
212
213    public function testRemoveInvalidFacet()
214    {
215        $fq1 = new Solarium_Query_Select_Component_Facet_Query;
216        $fq1->setKey('f1')->setQuery('category:1');
217
218        $fq2 = new Solarium_Query_Select_Component_Facet_Query;
219        $fq2->setKey('f2')->setQuery('category:2');
220
221        $facets = array('f1' => $fq1, 'f2' => $fq2);
222
223        $this->_facetSet->addFacets($facets);
224        $this->_facetSet->removeFacet('f3'); //continue silently
225        $this->assertEquals(
226            $facets,
227            $this->_facetSet->getFacets()
228        );
229    }
230
231    public function testClearFacets()
232    {
233        $fq1 = new Solarium_Query_Select_Component_Facet_Query;
234        $fq1->setKey('f1')->setQuery('category:1');
235
236        $fq2 = new Solarium_Query_Select_Component_Facet_Query;
237        $fq2->setKey('f2')->setQuery('category:2');
238
239        $facets = array('f1' => $fq1, 'f2' => $fq2);
240
241        $this->_facetSet->addFacets($facets);
242        $this->_facetSet->clearFacets();
243        $this->assertEquals(
244            array(),
245            $this->_facetSet->getFacets()
246        );
247    }
248
249    public function testSetFacets()
250    {
251        $fq1 = new Solarium_Query_Select_Component_Facet_Query;
252        $fq1->setKey('f1')->setQuery('category:1');
253
254        $fq2 = new Solarium_Query_Select_Component_Facet_Query;
255        $fq2->setKey('f2')->setQuery('category:2');
256
257        $facets = array('f1' => $fq1, 'f2' => $fq2);
258
259        $this->_facetSet->addFacets($facets);
260
261        $fq3 = new Solarium_Query_Select_Component_Facet_Query;
262        $fq3->setKey('f3')->setQuery('category:3');
263
264        $fq4 = new Solarium_Query_Select_Component_Facet_Query;
265        $fq4->setKey('f4')->setQuery('category:4');
266
267        $facets = array('f3' => $fq3, 'f4' => $fq4);
268
269        $this->_facetSet->setFacets($facets);
270
271        $this->assertEquals(
272            $facets,
273            $this->_facetSet->getFacets()
274        );
275    }
276
277    public function testCreateFacet()
278    {
279        $type = Solarium_Query_Select_Component_FacetSet::FACET_FIELD;
280        $options = array('optionA' => 1, 'optionB' => 2);
281        $facet = $this->_facetSet->createFacet($type, $options);
282
283        // check class mapping
284        $this->assertEquals(
285            $type,
286            $facet->getType()
287        );
288
289        // check option forwarding
290        $facetOptions = $facet->getOptions();
291        $this->assertEquals(
292            $options['optionB'],
293            $facetOptions['optionB']
294        );
295    }
296
297    public function testCreateFacetAdd()
298    {
299        $type = Solarium_Query_Select_Component_FacetSet::FACET_FIELD;
300        $options = array('key' => 'mykey','optionA' => 1, 'optionB' => 2);
301        $facet = $this->_facetSet->createFacet($type, $options);
302
303        $this->assertEquals($facet, $this->_facetSet->getFacet('mykey'));
304    }
305
306    public function testCreateFacetAddWithString()
307    {
308        $type = Solarium_Query_Select_Component_FacetSet::FACET_FIELD;
309        $options = 'mykey';
310        $facet = $this->_facetSet->createFacet($type, $options);
311
312        $this->assertEquals($facet, $this->_facetSet->getFacet('mykey'));
313    }
314
315    public function testCreateFacetWithInvalidType()
316    {
317        $this->setExpectedException('Solarium_Exception');
318        $this->_facetSet->createFacet('invalidtype');
319    }
320
321    public function testCreateFacetField()
322    {
323        $options = array('optionA' => 1, 'optionB' => 2);
324
325        $observer = $this->getMock('Solarium_Query_Select_Component_FacetSet', array('createFacet'));
326        $observer->expects($this->once())
327                 ->method('createFacet')
328                 ->with($this->equalTo(Solarium_Query_Select_Component_FacetSet::FACET_FIELD), $this->equalTo($options));
329
330        $observer->createFacetField($options);
331    }
332
333    public function testCreateFacetQuery()
334    {
335        $options = array('optionA' => 1, 'optionB' => 2);
336
337        $observer = $this->getMock('Solarium_Query_Select_Component_FacetSet', array('createFacet'));
338        $observer->expects($this->once())
339                 ->method('createFacet')
340                 ->with($this->equalTo(Solarium_Query_Select_Component_FacetSet::FACET_QUERY), $this->equalTo($options));
341
342        $observer->createFacetQuery($options);
343    }
344
345    public function testCreateFacetMultiQuery()
346    {
347        $options = array('optionA' => 1, 'optionB' => 2);
348
349        $observer = $this->getMock('Solarium_Query_Select_Component_FacetSet', array('createFacet'));
350        $observer->expects($this->once())
351                 ->method('createFacet')
352                 ->with($this->equalTo(Solarium_Query_Select_Component_FacetSet::FACET_MULTIQUERY), $this->equalTo($options));
353
354        $observer->createFacetMultiQuery($options);
355    }
356
357    public function testCreateFacetRange()
358    {
359        $options = array('optionA' => 1, 'optionB' => 2);
360
361        $observer = $this->getMock('Solarium_Query_Select_Component_FacetSet', array('createFacet'));
362        $observer->expects($this->once())
363                 ->method('createFacet')
364                 ->with($this->equalTo(Solarium_Query_Select_Component_FacetSet::FACET_RANGE), $this->equalTo($options));
365
366        $observer->createFacetRange($options);
367    }
368   
369}
Note: See TracBrowser for help on using the repository browser.