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

Revision 7588, 14.2 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_HighlightingTest extends PHPUnit_Framework_TestCase
33{
34
35    /**
36     * @var Solarium_Query_Select_Component_Highlighting
37     */
38    protected $_hlt;
39
40    public function setUp()
41    {
42        $this->_hlt = new Solarium_Query_Select_Component_Highlighting;
43    }
44
45    public function testConfigMode()
46    {
47        $options = array(
48            'field' => array(
49                'fieldA' => array(
50                    'snippets' => 3,
51                    'fragsize' => 25,
52                ),
53                'fieldB'
54            ),
55            'snippets' => 2,
56            'fragsize' => 20,
57            'mergecontiguous' => true,
58            'requirefieldmatch' => false,
59            'maxanalyzedchars' => 40,
60            'alternatefield' => 'text',
61            'maxalternatefieldlength' => 50,
62            'formatter' => 'myFormatter',
63            'simpleprefix' => '<b>',
64            'simplepostfix' => '</b>',
65            'fragmenter' => 'myFragmenter',
66            'fraglistbuilder' => 'regex',
67            'fragmentsbuilder' => 'myBuilder',
68            'usefastvectorhighlighter' => true,
69            'usephrasehighlighter' => false,
70            'highlightmultiterm' => true,
71            'regexslop' => .8,
72            'regexpattern' => 'myPattern',
73            'regexmaxanalyzedchars' => 500,
74            'query' => 'text:myvalue',
75            'phraselimit' => 35,
76
77        );
78
79        $this->_hlt->setOptions($options);
80
81        $this->assertEquals(array('fieldA','fieldB'), array_keys($this->_hlt->getFields()));
82        $this->assertEquals($options['field']['fieldA']['snippets'], $this->_hlt->getField('fieldA')->getSnippets());
83        $this->assertEquals($options['field']['fieldA']['fragsize'], $this->_hlt->getField('fieldA')->getFragSize());
84        $this->assertEquals(null, $this->_hlt->getField('FieldB')->getSnippets());
85        $this->assertEquals($options['snippets'], $this->_hlt->getSnippets());
86        $this->assertEquals($options['fragsize'], $this->_hlt->getFragSize());
87        $this->assertEquals($options['mergecontiguous'], $this->_hlt->getMergeContiguous());
88        $this->assertEquals($options['maxanalyzedchars'], $this->_hlt->getMaxAnalyzedChars());
89        $this->assertEquals($options['alternatefield'], $this->_hlt->getAlternateField());
90        $this->assertEquals($options['maxalternatefieldlength'], $this->_hlt->getMaxAlternateFieldLength());
91        $this->assertEquals($options['formatter'], $this->_hlt->getFormatter());
92        $this->assertEquals($options['simpleprefix'], $this->_hlt->getSimplePrefix());
93        $this->assertEquals($options['simplepostfix'], $this->_hlt->getSimplePostfix());
94        $this->assertEquals($options['fragmenter'], $this->_hlt->getFragmenter());
95        $this->assertEquals($options['fraglistbuilder'], $this->_hlt->getFragListBuilder());
96        $this->assertEquals($options['fragmentsbuilder'], $this->_hlt->getFragmentsBuilder());
97        $this->assertEquals($options['usefastvectorhighlighter'], $this->_hlt->getUseFastVectorHighlighter());
98        $this->assertEquals($options['usephrasehighlighter'], $this->_hlt->getUsePhraseHighlighter());
99        $this->assertEquals($options['highlightmultiterm'], $this->_hlt->getHighlightMultiTerm());
100        $this->assertEquals($options['regexslop'], $this->_hlt->getRegexSlop());
101        $this->assertEquals($options['regexpattern'], $this->_hlt->getRegexPattern());
102        $this->assertEquals($options['regexmaxanalyzedchars'], $this->_hlt->getRegexMaxAnalyzedChars());
103        $this->assertEquals($options['query'], $this->_hlt->getQuery());
104        $this->assertEquals($options['phraselimit'], $this->_hlt->getPhraseLimit());
105    }
106
107    public function testGetType()
108    {
109        $this->assertEquals(Solarium_Query_Select::COMPONENT_HIGHLIGHTING, $this->_hlt->getType());
110    }
111
112    public function testGetFieldAutocreate()
113    {
114        $name = 'test';
115        $field = $this->_hlt->getField($name);
116
117        $this->assertEquals($name, $field->getName());
118    }
119
120    public function testGetFieldNoAutocreate()
121    {
122        $name = 'test';
123        $field = $this->_hlt->getField($name, false);
124
125        $this->assertEquals(null, $field);
126    }
127
128    public function testAddFieldWithObject()
129    {
130        $field = new Solarium_Query_Select_Component_Highlighting_Field;
131        $field->setName('test');
132
133        $this->_hlt->addField($field);
134
135        $this->assertEquals($field, $this->_hlt->getField('test'));
136    }
137
138    public function testAddFieldWithString()
139    {
140        $name = 'test';
141        $this->_hlt->addField($name);
142
143        $this->assertEquals(array($name), array_keys($this->_hlt->getFields()));
144    }
145
146    public function testAddFieldWithArray()
147    {
148        $config = array(
149            'name' => 'fieldA',
150            'snippets' => 6
151        );
152        $this->_hlt->addField($config);
153
154        $this->assertEquals(6, $this->_hlt->getField('fieldA')->getSnippets());
155    }
156
157    public function testAddFieldWithObjectWithoutName()
158    {
159        $field = new Solarium_Query_Select_Component_Highlighting_Field;
160        $this->setExpectedException('Solarium_Exception');
161        $this->_hlt->addField($field);
162    }
163
164    public function testAddsFieldsWithString()
165    {
166        $fields = 'test1,test2';
167        $this->_hlt->addFields($fields);
168
169        $this->assertEquals(array('test1','test2'), array_keys($this->_hlt->getFields()));
170    }
171
172    public function testAddsFieldsWithArray()
173    {
174        $fields = array(
175            'test1' => array('snippets' => 2),
176            'test2' => array('snippets' => 5),
177        );
178        $this->_hlt->addFields($fields);
179
180        $this->assertEquals(2, $this->_hlt->getField('test1')->getSnippets());
181        $this->assertEquals(5, $this->_hlt->getField('test2')->getSnippets());
182    }
183
184    public function testRemoveField()
185    {
186        $fields = array(
187            'test1' => array('snippets' => 2),
188            'test2' => array('snippets' => 5),
189        );
190
191        $this->_hlt->addFields($fields);
192        $this->assertEquals(array('test1','test2'), array_keys($this->_hlt->getFields()));
193
194        $this->_hlt->removeField('test1');
195        $this->assertEquals(array('test2'), array_keys($this->_hlt->getFields()));
196    }
197
198    public function testRemoveFieldWithInvalidName()
199    {
200        $fields = array(
201            'test1' => array('snippets' => 2),
202            'test2' => array('snippets' => 5),
203        );
204
205        $this->_hlt->addFields($fields);
206        $this->assertEquals(array('test1','test2'), array_keys($this->_hlt->getFields()));
207
208        $this->_hlt->removeField('test1=3'); // should fail silently and do nothing
209        $this->assertEquals(array('test1','test2'), array_keys($this->_hlt->getFields()));
210    }
211
212    public function testClearFields()
213    {
214        $fields = array(
215            'test1' => array('snippets' => 2),
216            'test2' => array('snippets' => 5),
217        );
218
219        $this->_hlt->addFields($fields);
220        $this->assertEquals(array('test1','test2'), array_keys($this->_hlt->getFields()));
221
222        $this->_hlt->clearFields();
223        $this->assertEquals(array(), array_keys($this->_hlt->getFields()));
224    }
225
226    public function testSetFields()
227    {
228        $fields = array(
229            'test1' => array('snippets' => 2),
230            'test2' => array('snippets' => 5),
231        );
232
233        $this->_hlt->addFields($fields);
234        $this->assertEquals(array('test1','test2'), array_keys($this->_hlt->getFields()));
235
236        $newFields = array(
237            'test3' => array('snippets' => 4),
238            'test4' => array('snippets' => 6),
239        );
240
241        $this->_hlt->setFields($newFields);
242        $this->assertEquals(array('test3','test4'), array_keys($this->_hlt->getFields()));
243    }
244
245
246    public function testSetAndGetSnippets()
247    {
248        $value = 2;
249        $this->_hlt->setSnippets($value);
250
251        $this->assertEquals(
252            $value,
253            $this->_hlt->getSnippets()
254        );
255    }
256
257    public function testSetAndGetFragSize()
258    {
259        $value = 20;
260        $this->_hlt->setFragsize($value);
261
262        $this->assertEquals(
263            $value,
264            $this->_hlt->getFragSize()
265        );
266    }
267
268    public function testSetAndGetMergeContiguous()
269    {
270        $value = true;
271        $this->_hlt->setMergeContiguous($value);
272
273        $this->assertEquals(
274            $value,
275            $this->_hlt->getMergeContiguous()
276        );
277    }
278
279    public function testSetAndGetRequireFieldMatch()
280    {
281        $value = true;
282        $this->_hlt->setRequireFieldMatch($value);
283
284        $this->assertEquals(
285            $value,
286            $this->_hlt->getRequireFieldMatch()
287        );
288    }
289
290    public function testSetAndGetMaxAnalyzedChars()
291    {
292        $value = 200;
293        $this->_hlt->setMaxAnalyzedChars($value);
294
295        $this->assertEquals(
296            $value,
297            $this->_hlt->getMaxAnalyzedChars()
298        );
299    }
300
301    public function testSetAndGetAlternateField()
302    {
303        $value = 'description';
304        $this->_hlt->setAlternateField($value);
305
306        $this->assertEquals(
307            $value,
308            $this->_hlt->getAlternateField()
309        );
310    }
311
312    public function testSetAndGetMaxAlternateFieldLength()
313    {
314        $value = 150;
315        $this->_hlt->setMaxAlternateFieldLength($value);
316
317        $this->assertEquals(
318            $value,
319            $this->_hlt->getMaxAlternateFieldLength()
320        );
321    }
322
323    public function testSetAndGetFormatter()
324    {
325        $this->_hlt->setFormatter();
326
327        $this->assertEquals(
328            'simple',
329            $this->_hlt->getFormatter()
330        );
331    }
332
333    public function testSetAndGetSimplePrefix()
334    {
335        $value = '<em>';
336        $this->_hlt->setSimplePrefix($value);
337
338        $this->assertEquals(
339            $value,
340            $this->_hlt->getSimplePrefix()
341        );
342    }
343
344    public function testSetAndGetSimplePostfix()
345    {
346        $value = '</em>';
347        $this->_hlt->setSimplePostfix($value);
348
349        $this->assertEquals(
350            $value,
351            $this->_hlt->getSimplePostfix()
352        );
353    }
354
355    public function testSetAndGetFragmenter()
356    {
357        $value = Solarium_Query_Select_Component_Highlighting::FRAGMENTER_REGEX;
358        $this->_hlt->setFragmenter($value);
359
360        $this->assertEquals(
361            $value,
362            $this->_hlt->getFragmenter()
363        );
364    }
365
366    public function testSetAndGetFragListBuilder()
367    {
368        $value = 'myBuilder';
369        $this->_hlt->setFragListBuilder($value);
370
371        $this->assertEquals(
372            $value,
373            $this->_hlt->getFragListBuilder()
374        );
375    }
376
377    public function testSetAndGetFragmentsBuilder()
378    {
379        $value = 'myBuilder';
380        $this->_hlt->setFragmentsBuilder($value);
381
382        $this->assertEquals(
383            $value,
384            $this->_hlt->getFragmentsBuilder()
385        );
386    }
387
388    public function testSetAndGetUseFastVectorHighlighter()
389    {
390        $value = true;
391        $this->_hlt->setUseFastVectorHighlighter($value);
392
393        $this->assertEquals(
394            $value,
395            $this->_hlt->getUseFastVectorHighlighter()
396        );
397    }
398
399    public function testSetAndGetUsePhraseHighlighter()
400    {
401        $value = true;
402        $this->_hlt->setUsePhraseHighlighter($value);
403
404        $this->assertEquals(
405            $value,
406            $this->_hlt->getUsePhraseHighlighter()
407        );
408    }
409
410    public function testSetAndGetHighlightMultiTerm()
411    {
412        $value = true;
413        $this->_hlt->setHighlightMultiTerm($value);
414
415        $this->assertEquals(
416            $value,
417            $this->_hlt->getHighlightMultiTerm()
418        );
419    }
420
421    public function testSetAndGetRegexSlop()
422    {
423        $value = .8;
424        $this->_hlt->setRegexSlop($value);
425
426        $this->assertEquals(
427            $value,
428            $this->_hlt->getRegexSlop()
429        );
430    }
431
432    public function testSetAndGetRegexPattern()
433    {
434        $value = 'myPattern';
435        $this->_hlt->setRegexPattern($value);
436
437        $this->assertEquals(
438            $value,
439            $this->_hlt->getRegexPattern()
440        );
441    }
442
443    public function testSetAndGetRegexMaxAnalyzedChars()
444    {
445        $value = 500;
446        $this->_hlt->setRegexMaxAnalyzedChars($value);
447
448        $this->assertEquals(
449            $value,
450            $this->_hlt->getRegexMaxAnalyzedChars()
451        );
452    }
453
454    public function testSetAndGetQuery()
455    {
456        $value = 'text:myvalue';
457        $this->_hlt->setQuery($value);
458
459        $this->assertEquals(
460            $value,
461            $this->_hlt->getQuery()
462        );
463    }
464
465    public function testSetAndGetPhraseLimit()
466    {
467        $value = 20;
468        $this->_hlt->setPhraseLimit($value);
469
470        $this->assertEquals(
471            $value,
472            $this->_hlt->getPhraseLimit()
473        );
474    }
475
476}
Note: See TracBrowser for help on using the repository browser.