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

Revision 7576, 12.5 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_UpdateTest extends PHPUnit_Framework_TestCase
33{
34
35    protected $_query;
36
37    public function setUp()
38    {
39        $this->_query = new Solarium_Query_Update;
40    }
41
42    public function testGetType()
43    {
44        $this->assertEquals(Solarium_Client::QUERYTYPE_UPDATE, $this->_query->getType());
45    }
46
47    public function testConfigMode()
48    {
49        $options = array(
50            'handler'  => 'myHandler',
51            'resultclass' => 'myResult',
52            'command' => array(
53                'key1' => array(
54                    'type' => 'delete',
55                    'query' => 'population:[* TO 1000]',
56                    'id' => array(1,2),
57                ),
58                'key2' => array(
59                    'type' => 'commit',
60                    'waitflush' => true,
61                    'waitsearcher' => false,
62                    'expungedeletes' => true,
63                ),
64                'key3' => array(
65                    'type' => 'optimize',
66                    'waitflush' => true,
67                    'waitsearcher' => false,
68                    'maxsegments' => 5,
69                ),
70                'key4' => array(
71                    'type' => 'rollback',
72                )
73            )
74        );
75        $this->_query->setOptions($options);
76        $commands = $this->_query->getCommands();
77
78        $this->assertEquals(
79            $options['handler'],
80            $this->_query->getHandler()
81        );
82
83        $this->assertEquals(
84            $options['resultclass'],
85            $this->_query->getResultClass()
86        );
87
88        $delete = $commands['key1'];
89        $this->assertEquals(
90            array(1,2),
91            $delete->getIds()
92        );
93        $this->assertEquals(
94            array('population:[* TO 1000]'),
95            $delete->getQueries()
96        );
97
98        $commit = $commands['key2'];
99        $this->assertEquals(
100            true,
101            $commit->getWaitFlush()
102        );
103        $this->assertEquals(
104            false,
105            $commit->getWaitSearcher()
106        );
107        $this->assertEquals(
108            true,
109            $commit->getExpungeDeletes()
110        );
111
112        $optimize = $commands['key3'];
113        $this->assertEquals(
114            true,
115            $optimize->getWaitFlush()
116        );
117        $this->assertEquals(
118            false,
119            $optimize->getWaitSearcher()
120        );
121        $this->assertEquals(
122            5,
123            $optimize->getMaxSegments()
124        );
125
126        $rollback = $commands['key4'];
127        $this->assertEquals(
128            'Solarium_Query_Update_Command_Rollback',
129            get_class($rollback)
130        );
131    }
132
133    public function testConstructorWithConfigAddCommand()
134    {
135        $config = array(
136            'command' => array(
137                'key1' => array(
138                    'type' => 'add',
139                ),
140            )
141        );
142
143        $this->setExpectedException('Solarium_Exception');
144        new Solarium_Query_Update($config);
145    }
146
147    public function testAddWithoutKey()
148    {
149        $command = new Solarium_Query_Update_Command_Rollback;
150        $this->_query->add(null, $command);
151
152        $this->assertEquals(
153            array($command),
154            $this->_query->getCommands()
155        );
156    }
157   
158    public function testAddWithKey()
159    {
160        $rollback = new Solarium_Query_Update_Command_Rollback;
161        $this->_query->add('rb', $rollback);
162
163        $commit = new Solarium_Query_Update_Command_Commit;
164        $this->_query->add('cm', $commit);
165
166        $this->assertEquals(
167            array('rb' => $rollback, 'cm' => $commit),
168            $this->_query->getCommands()
169        );
170    }
171
172    public function testRemove()
173    {
174        $rollback = new Solarium_Query_Update_Command_Rollback;
175        $this->_query->add('rb', $rollback);
176
177        $commit = new Solarium_Query_Update_Command_Commit;
178        $this->_query->add('cm', $commit);
179       
180        $this->_query->remove('rb');
181
182        $this->assertEquals(
183            array('cm' => $commit),
184            $this->_query->getCommands()
185        );
186    }
187
188    public function testRemoveWithObjectInput()
189    {
190        $rollback = new Solarium_Query_Update_Command_Rollback;
191        $this->_query->add('rb', $rollback);
192
193        $commit = new Solarium_Query_Update_Command_Commit;
194        $this->_query->add('cm', $commit);
195
196        $this->_query->remove($rollback);
197
198        $this->assertEquals(
199            array('cm' => $commit),
200            $this->_query->getCommands()
201        );
202    }
203
204    public function testRemoveInvalidKey()
205    {
206        $rollback = new Solarium_Query_Update_Command_Rollback;
207        $this->_query->add('rb', $rollback);
208
209        $commit = new Solarium_Query_Update_Command_Commit;
210        $this->_query->add('cm', $commit);
211
212        $this->_query->remove('invalidkey'); //should silently ignore
213
214        $this->assertEquals(
215            array('rb' => $rollback, 'cm' => $commit),
216            $this->_query->getCommands()
217        );
218    }
219
220    public function testAddRollback()
221    {
222        $this->_query->addRollback();
223        $commands = $this->_query->getCommands();
224
225        $this->assertEquals(
226            Solarium_Query_Update::COMMAND_ROLLBACK,
227            $commands[0]->getType()
228        );
229    }
230
231    public function testAddDeleteQuery()
232    {
233        $this->_query->addDeleteQuery('*:*');
234        $commands = $this->_query->getCommands();
235
236        $this->assertEquals(
237            Solarium_Query_Update::COMMAND_DELETE,
238            $commands[0]->getType()
239        );
240
241        $this->assertEquals(
242            array('*:*'),
243            $commands[0]->getQueries()
244        );
245    }
246
247    public function testAddDeleteQueries()
248    {
249        $this->_query->addDeleteQueries(array('id:1','id:2'));
250        $commands = $this->_query->getCommands();
251
252        $this->assertEquals(
253            Solarium_Query_Update::COMMAND_DELETE,
254            $commands[0]->getType()
255        );
256
257        $this->assertEquals(
258            array('id:1','id:2'),
259            $commands[0]->getQueries()
260        );
261    }
262
263    public function testAddDeleteById()
264    {
265        $this->_query->addDeleteById(1);
266        $commands = $this->_query->getCommands();
267
268        $this->assertEquals(
269            Solarium_Query_Update::COMMAND_DELETE,
270            $commands[0]->getType()
271        );
272
273        $this->assertEquals(
274            array(1),
275            $commands[0]->getIds()
276        );
277    }
278
279    public function testAddDeleteByIds()
280    {
281        $this->_query->addDeleteByIds(array(1,2));
282        $commands = $this->_query->getCommands();
283
284        $this->assertEquals(
285            Solarium_Query_Update::COMMAND_DELETE,
286            $commands[0]->getType()
287        );
288
289        $this->assertEquals(
290            array(1,2),
291            $commands[0]->getIds()
292        );
293    }
294
295    public function testAddDocument()
296    {
297        $doc = new Solarium_Document_ReadWrite(array('id' => 1));
298
299        $this->_query->addDocument($doc);
300        $commands = $this->_query->getCommands();
301
302        $this->assertEquals(
303            Solarium_Query_Update::COMMAND_ADD,
304            $commands[0]->getType()
305        );
306
307        $this->assertEquals(
308            array($doc),
309            $commands[0]->getDocuments()
310        );
311    }
312
313    public function testAddDocuments()
314    {
315        $doc1 = new Solarium_Document_ReadWrite(array('id' => 1));
316        $doc2 = new Solarium_Document_ReadWrite(array('id' => 1));
317
318        $this->_query->addDocuments(array($doc1,$doc2), true, 100);
319        $commands = $this->_query->getCommands();
320
321        $this->assertEquals(
322            Solarium_Query_Update::COMMAND_ADD,
323            $commands[0]->getType()
324        );
325
326        $this->assertEquals(
327            array($doc1, $doc2),
328            $commands[0]->getDocuments()
329        );
330
331        $this->assertEquals(
332            true,
333            $commands[0]->getOverwrite()
334        );
335
336        $this->assertEquals(
337            100,
338            $commands[0]->getCommitWithin()
339        );
340    }
341
342    public function testAddCommit()
343    {
344        $this->_query->addCommit(true, false, true);
345        $commands = $this->_query->getCommands();
346
347        $this->assertEquals(
348            Solarium_Query_Update::COMMAND_COMMIT,
349            $commands[0]->getType()
350        );
351
352        $this->assertEquals(
353            true,
354            $commands[0]->getWaitFlush()
355        );
356
357        $this->assertEquals(
358            false,
359            $commands[0]->getWaitSearcher()
360        );
361
362        $this->assertEquals(
363            true,
364            $commands[0]->getExpungeDeletes()
365        );
366    }
367
368    public function testAddOptimize()
369    {
370        $this->_query->addOptimize(true, false, 10);
371        $commands = $this->_query->getCommands();
372
373        $this->assertEquals(
374            Solarium_Query_Update::COMMAND_OPTIMIZE,
375            Solarium_Query_Update::COMMAND_OPTIMIZE,
376            $commands[0]->getType()
377        );
378
379        $this->assertEquals(
380            true,
381            $commands[0]->getWaitFlush()
382        );
383
384        $this->assertEquals(
385            false,
386            $commands[0]->getWaitSearcher()
387        );
388
389        $this->assertEquals(
390            10,
391            $commands[0]->getMaxSegments()
392        );
393    }
394
395    public function testCreateCommand()
396    {
397        $type = Solarium_Query_Update::COMMAND_ROLLBACK;
398        $options = array('optionA' => 1, 'optionB' => 2);
399        $command = $this->_query->createCommand($type, $options);
400
401        // check command type
402        $this->assertEquals(
403            $type,
404            $command->getType()
405        );
406
407        // check option forwarding
408        $commandOptions = $command->getOptions();
409        $this->assertEquals(
410            $options['optionB'],
411            $commandOptions['optionB']
412        );
413    }
414
415    public function testCreateCommandWithInvalidQueryType()
416    {
417        $this->setExpectedException('Solarium_Exception');
418        $this->_query->createCommand('invalidtype');
419    }
420
421    public function testSetAndGetDocumentClass()
422    {
423        $this->_query->setDocumentClass('MyDocument');
424        $this->assertEquals('MyDocument', $this->_query->getDocumentClass());
425    }
426
427    public function testCreateDocument()
428    {
429        $doc = $this->_query->createDocument();
430        $this->assertThat($doc, $this->isInstanceOf($this->_query->getDocumentClass()));
431    }
432
433    public function testCreateDocumentWithCustomClass()
434    {
435        $this->_query->setDocumentClass('MyCustomDoc');
436
437        $doc = $this->_query->createDocument();
438        $this->assertThat($doc, $this->isInstanceOf('MyCustomDoc'));
439    }
440
441    public function testCreateDocumentWithFieldsAndBoosts()
442    {
443        $fields = array('id' => 1, 'name' => 'testname');
444        $boosts = array('name' => 2.7);
445
446        $doc = $this->_query->createDocument($fields, $boosts);
447
448        $this->assertThat($doc, $this->isInstanceOf($this->_query->getDocumentClass()));
449
450        $this->assertEquals(
451            $fields,
452            $doc->getFields()
453        );
454
455        $this->assertEquals(
456            2.7,
457            $doc->getFieldBoost('name')
458        );
459    }
460
461}
462
463class MyCustomDoc extends Solarium_Document_ReadWrite{
464
465}
Note: See TracBrowser for help on using the repository browser.