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

Revision 7576, 10.4 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_Client_RequestBuilder_UpdateTest extends PHPUnit_Framework_TestCase
33{
34
35    /**
36     * @var Solarium_Query_Update
37     */
38    protected $_query;
39
40    /**
41     * @var Solarium_Client_RequestBuilder_Update
42     */
43    protected $_builder;
44
45    public function setUp()
46    {
47        $this->_query = new Solarium_Query_Update;
48        $this->_builder = new Solarium_Client_RequestBuilder_Update;
49    }
50
51    public function testGetMethod()
52    {
53        $request = $this->_builder->build($this->_query);
54        $this->assertEquals(
55            Solarium_Client_Request::METHOD_POST,
56            $request->getMethod()
57        );
58    }
59
60    public function testGetUri()
61    {
62        $request = $this->_builder->build($this->_query);
63        $this->assertEquals(
64            'update?wt=json',
65            $request->getUri()
66        );
67    }
68
69    public function testBuildAddXmlNoParamsSingleDocument()
70    {
71        $command = new Solarium_Query_Update_Command_Add;
72        $command->addDocument(new Solarium_Document_ReadWrite(array('id' => 1)));
73       
74        $this->assertEquals(
75            '<add><doc><field name="id">1</field></doc></add>',
76            $this->_builder->buildAddXml($command)
77        );
78    }
79
80    public function testBuildAddXmlWithParams()
81    {
82        $command = new Solarium_Query_Update_Command_Add(array('overwrite' => true,'commitwithin' => 100));
83        $command->addDocument(new Solarium_Document_ReadWrite(array('id' => 1)));
84
85        $this->assertEquals(
86            '<add overwrite="true" commitWithin="100"><doc><field name="id">1</field></doc></add>',
87            $this->_builder->buildAddXml($command)
88        );
89    }
90
91    public function testBuildAddXmlSpecialCharacters()
92    {
93        $command = new Solarium_Query_Update_Command_Add;
94        $command->addDocument(new Solarium_Document_ReadWrite(array('id' => 1, 'text' => 'test < 123 > test')));
95
96        $this->assertEquals(
97            '<add><doc><field name="id">1</field><field name="text">test &lt; 123 &gt; test</field></doc></add>',
98            $this->_builder->buildAddXml($command)
99        );
100    }
101
102    public function testBuildAddXmlMultivalueField()
103    {
104        $command = new Solarium_Query_Update_Command_Add;
105        $command->addDocument(new Solarium_Document_ReadWrite(array('id' => array(1,2,3), 'text' => 'test < 123 > test')));
106
107        $this->assertEquals(
108            '<add><doc><field name="id">1</field><field name="id">2</field><field name="id">3</field><field name="text">test &lt; 123 &gt; test</field></doc></add>',
109            $this->_builder->buildAddXml($command)
110        );
111    }
112
113    public function testBuildAddXmlSingleDocumentWithBoost()
114    {
115        $doc = new Solarium_Document_ReadWrite(array('id' => 1));
116        $doc->setBoost(2.5);
117        $command = new Solarium_Query_Update_Command_Add;
118        $command->addDocument($doc);
119
120        $this->assertEquals(
121            '<add><doc boost="2.5"><field name="id">1</field></doc></add>',
122            $this->_builder->buildAddXml($command)
123        );
124    }
125
126    public function testBuildAddXmlSingleDocumentWithFieldBoost()
127    {
128        $doc = new Solarium_Document_ReadWrite(array('id' => 1));
129        $doc->setFieldBoost('id',2.1);
130        $command = new Solarium_Query_Update_Command_Add;
131        $command->addDocument($doc);
132
133        $this->assertEquals(
134            '<add><doc><field name="id" boost="2.1">1</field></doc></add>',
135            $this->_builder->buildAddXml($command)
136        );
137    }
138
139    public function testBuildAddXmlMultipleDocuments()
140    {
141        $command = new Solarium_Query_Update_Command_Add;
142        $command->addDocument(new Solarium_Document_ReadWrite(array('id' => 1)));
143        $command->addDocument(new Solarium_Document_ReadWrite(array('id' => 2)));
144
145        $this->assertEquals(
146            '<add><doc><field name="id">1</field></doc><doc><field name="id">2</field></doc></add>',
147            $this->_builder->buildAddXml($command)
148        );
149    }
150
151    public function testBuildDeleteXml()
152    {
153        $command = new Solarium_Query_Update_Command_Delete;
154
155        $this->assertEquals(
156            '<delete></delete>',
157            $this->_builder->buildDeleteXml($command)
158        );
159    }
160
161    public function testBuildDeleteXmlSingleId()
162    {
163        $command = new Solarium_Query_Update_Command_Delete;
164        $command->addId(123);
165
166        $this->assertEquals(
167            '<delete><id>123</id></delete>',
168            $this->_builder->buildDeleteXml($command)
169        );
170    }
171
172    public function testBuildDeleteXmlMultipleIds()
173    {
174        $command = new Solarium_Query_Update_Command_Delete;
175        $command->addId(123);
176        $command->addId(456);
177
178        $this->assertEquals(
179            '<delete><id>123</id><id>456</id></delete>',
180            $this->_builder->buildDeleteXml($command)
181        );
182    }
183
184    public function testBuildDeleteXmlSingleQuery()
185    {
186        $command = new Solarium_Query_Update_Command_Delete;
187        $command->addQuery('*:*');
188
189        $this->assertEquals(
190            '<delete><query>*:*</query></delete>',
191            $this->_builder->buildDeleteXml($command)
192        );
193    }
194
195    public function testBuildDeleteXmlMultipleQueries()
196    {
197        $command = new Solarium_Query_Update_Command_Delete;
198        $command->addQuery('published:false');
199        $command->addQuery('id:[10 TO 20]');
200
201        $this->assertEquals(
202            '<delete><query>published:false</query><query>id:[10 TO 20]</query></delete>',
203            $this->_builder->buildDeleteXml($command)
204        );
205    }
206
207    public function testBuildDeleteXmlIdsAndQueries()
208    {
209        $command = new Solarium_Query_Update_Command_Delete;
210        $command->addId(123);
211        $command->addId(456);
212        $command->addQuery('published:false');
213        $command->addQuery('id:[10 TO 20]');
214
215        $this->assertEquals(
216            '<delete><id>123</id><id>456</id><query>published:false</query><query>id:[10 TO 20]</query></delete>',
217            $this->_builder->buildDeleteXml($command)
218        );
219    }
220
221    public function testBuildDeleteXmlIdAndQuerySpecialChars()
222    {
223        $command = new Solarium_Query_Update_Command_Delete;
224        $command->addId('special<char>id');
225        $command->addQuery('id:special<char>id');
226
227        $this->assertEquals(
228            '<delete><id>special&lt;char&gt;id</id><query>id:special&lt;char&gt;id</query></delete>',
229            $this->_builder->buildDeleteXml($command)
230        );
231    }
232
233    public function testBuildOptimizeXml()
234    {
235        $command = new Solarium_Query_Update_Command_Optimize;
236
237        $this->assertEquals(
238            '<optimize/>',
239            $this->_builder->buildOptimizeXml($command)
240        );
241    }
242
243    public function testBuildOptimizeXmlWithParams()
244    {
245        $command = new Solarium_Query_Update_Command_Optimize(array('waitflush'=>true,'waitsearcher'=>false,'maxsegments'=>10));
246
247        $this->assertEquals(
248            '<optimize waitFlush="true" waitSearcher="false" maxSegments="10"/>',
249            $this->_builder->buildOptimizeXml($command)
250        );
251    }
252
253    public function testBuildCommitXml()
254    {
255        $command = new Solarium_Query_Update_Command_Commit;
256
257        $this->assertEquals(
258            '<commit/>',
259            $this->_builder->buildCommitXml($command)
260        );
261    }
262
263    public function testBuildCommitXmlWithParams()
264    {
265        $command = new Solarium_Query_Update_Command_Commit(array('waitflush'=>true,'waitsearcher'=>false,'expungedeletes'=>true));
266
267        $this->assertEquals(
268            '<commit waitFlush="true" waitSearcher="false" expungeDeletes="true"/>',
269            $this->_builder->buildCommitXml($command)
270        );
271    }
272
273    public function testBuildRollbackXml()
274    {
275        $command = new Solarium_Query_Update_Command_Rollback;
276
277        $this->assertEquals(
278            '<rollback/>',
279            $this->_builder->buildRollbackXml($command)
280        );
281    }
282
283    public function testCompleteRequest()
284    {
285        $this->_query->addDeleteById(1);
286        $this->_query->addRollback();
287        $this->_query->addDeleteQuery('*:*');
288        $this->_query->addDocument(new Solarium_Document_ReadWrite(array('id' => 1)));
289        $this->_query->addCommit();
290        $this->_query->addOptimize();
291
292        $this->assertEquals(
293            '<update>'
294            . '<delete><id>1</id></delete>'
295            . '<rollback/>'
296            . '<delete><query>*:*</query></delete>'
297            . '<add><doc><field name="id">1</field></doc></add>'
298            . '<commit/>'
299            . '<optimize/>'
300            . '</update>',
301            $this->_builder->getRawData($this->_query)
302        );
303    }
304
305    public function testInvalidCommandInRequest()
306    {
307        $this->_query->add('invalidcommand',new InvalidCommand);
308
309        $this->setExpectedException('Solarium_Exception');
310        $this->_builder->build($this->_query);
311    }
312}
313
314
315class InvalidCommand extends StdClass
316{
317    public function getType()
318    {
319        return 'invalid';
320    }
321}
Note: See TracBrowser for help on using the repository browser.