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 | |
---|
32 | class Solarium_ClientTest extends PHPUnit_Framework_TestCase |
---|
33 | { |
---|
34 | |
---|
35 | /** |
---|
36 | * @var Solarium_Client |
---|
37 | */ |
---|
38 | protected $_client; |
---|
39 | |
---|
40 | public function setUp() |
---|
41 | { |
---|
42 | $this->_client = new Solarium_Client(); |
---|
43 | } |
---|
44 | |
---|
45 | public function testConfigMode() |
---|
46 | { |
---|
47 | $options = array( |
---|
48 | 'adapter' => 'MyAdapter', |
---|
49 | 'adapteroptions' => array( |
---|
50 | 'host' => 'myhost', |
---|
51 | 'port' => 8080, |
---|
52 | ), |
---|
53 | 'querytype' => array( |
---|
54 | 'myquerytype' => array( |
---|
55 | 'query' => 'MyQuery', |
---|
56 | 'requestbuilder' => 'MyRequestBuilder', |
---|
57 | 'responseparser' => 'MyResponseParser' |
---|
58 | ) |
---|
59 | ), |
---|
60 | 'plugin' => array( |
---|
61 | 'myplugin' => array( |
---|
62 | 'plugin' => 'MyClientPlugin', |
---|
63 | 'options' => array( |
---|
64 | 'option1' => 'value1', |
---|
65 | 'option2' => 'value2', |
---|
66 | ) |
---|
67 | ) |
---|
68 | ), |
---|
69 | ); |
---|
70 | |
---|
71 | $this->_client->setOptions($options); |
---|
72 | |
---|
73 | $adapter = $this->_client->getAdapter(); |
---|
74 | |
---|
75 | $this->assertThat($adapter, $this->isInstanceOf('MyAdapter')); |
---|
76 | $this->assertEquals(8080, $adapter->getPort()); |
---|
77 | |
---|
78 | |
---|
79 | $queryTypes = $this->_client->getQueryTypes(); |
---|
80 | $this->assertEquals( |
---|
81 | $options['querytype']['myquerytype'], |
---|
82 | $queryTypes['myquerytype'] |
---|
83 | ); |
---|
84 | |
---|
85 | $plugin = $this->_client->getPlugin('myplugin'); |
---|
86 | $this->assertThat($plugin, $this->isInstanceOf('MyClientPlugin')); |
---|
87 | $this->assertEquals($options['plugin']['myplugin']['options'], $plugin->getOptions()); |
---|
88 | |
---|
89 | } |
---|
90 | |
---|
91 | public function testConfigModeWithoutKeys() |
---|
92 | { |
---|
93 | $options = array( |
---|
94 | 'adapter' => 'MyAdapter', |
---|
95 | 'adapteroptions' => array( |
---|
96 | 'host' => 'myhost', |
---|
97 | 'port' => 8080, |
---|
98 | ), |
---|
99 | 'querytype' => array( |
---|
100 | array( |
---|
101 | 'type' => 'myquerytype', |
---|
102 | 'query' => 'MyQuery', |
---|
103 | 'requestbuilder' => 'MyRequestBuilder', |
---|
104 | 'responseparser' => 'MyResponseParser', |
---|
105 | ) |
---|
106 | ), |
---|
107 | 'plugin' => array( |
---|
108 | array( |
---|
109 | 'key' => 'myplugin', |
---|
110 | 'plugin' => 'MyClientPlugin', |
---|
111 | 'options' => array( |
---|
112 | 'option1' => 'value1', |
---|
113 | 'option2' => 'value2', |
---|
114 | ) |
---|
115 | ) |
---|
116 | ), |
---|
117 | ); |
---|
118 | |
---|
119 | $this->_client->setOptions($options); |
---|
120 | |
---|
121 | $adapter = $this->_client->getAdapter(); |
---|
122 | |
---|
123 | $this->assertThat($adapter, $this->isInstanceOf('MyAdapter')); |
---|
124 | $this->assertEquals(8080, $adapter->getPort()); |
---|
125 | |
---|
126 | $queryTypes = $this->_client->getQueryTypes(); |
---|
127 | $this->assertEquals( |
---|
128 | array( |
---|
129 | 'requestbuilder' => 'MyRequestBuilder', |
---|
130 | 'responseparser' => 'MyResponseParser', |
---|
131 | 'query' => 'MyQuery', |
---|
132 | ), |
---|
133 | $queryTypes['myquerytype'] |
---|
134 | ); |
---|
135 | |
---|
136 | $plugin = $this->_client->getPlugin('myplugin'); |
---|
137 | $this->assertThat($plugin, $this->isInstanceOf('MyClientPlugin')); |
---|
138 | $this->assertEquals($options['plugin'][0]['options'], $plugin->getOptions()); |
---|
139 | } |
---|
140 | |
---|
141 | public function testSetAndGetAdapterWithDefaultAdapter() |
---|
142 | { |
---|
143 | $defaultAdapter = $this->_client->getOption('adapter'); |
---|
144 | $adapter = $this->_client->getAdapter(); |
---|
145 | $this->assertThat($adapter, $this->isInstanceOf($defaultAdapter)); |
---|
146 | } |
---|
147 | |
---|
148 | public function testSetAndGetAdapterWithString() |
---|
149 | { |
---|
150 | $adapterClass = 'MyAdapter'; |
---|
151 | $this->_client->setAdapter($adapterClass); |
---|
152 | $this->assertThat($this->_client->getAdapter(), $this->isInstanceOf($adapterClass)); |
---|
153 | } |
---|
154 | |
---|
155 | public function testSetAndGetAdapterWithObject() |
---|
156 | { |
---|
157 | $adapterClass = 'MyAdapter'; |
---|
158 | $this->_client->setAdapter(new $adapterClass); |
---|
159 | $this->assertThat($this->_client->getAdapter(), $this->isInstanceOf($adapterClass)); |
---|
160 | } |
---|
161 | |
---|
162 | public function testRegisterQueryTypeAndGetQueryTypes() |
---|
163 | { |
---|
164 | $queryTypes = $this->_client->getQueryTypes(); |
---|
165 | |
---|
166 | $this->_client->registerQueryType('myquerytype','myquery','mybuilder','myparser'); |
---|
167 | |
---|
168 | $queryTypes['myquerytype'] = array( |
---|
169 | 'query' => 'myquery', |
---|
170 | 'requestbuilder' => 'mybuilder', |
---|
171 | 'responseparser' => 'myparser', |
---|
172 | ); |
---|
173 | |
---|
174 | $this->assertEquals( |
---|
175 | $queryTypes, |
---|
176 | $this->_client->getQueryTypes() |
---|
177 | ); |
---|
178 | } |
---|
179 | |
---|
180 | public function testRegisterAndGetPlugin() |
---|
181 | { |
---|
182 | $options = array('option1' => 1); |
---|
183 | $this->_client->registerPlugin('testplugin','MyClientPlugin',$options); |
---|
184 | |
---|
185 | $plugin = $this->_client->getPlugin('testplugin'); |
---|
186 | |
---|
187 | $this->assertThat( |
---|
188 | $plugin, |
---|
189 | $this->isInstanceOf('MyClientPlugin') |
---|
190 | ); |
---|
191 | |
---|
192 | $this->assertEquals( |
---|
193 | $options, |
---|
194 | $plugin->getOptions() |
---|
195 | ); |
---|
196 | } |
---|
197 | |
---|
198 | public function testRegisterInvalidPlugin() |
---|
199 | { |
---|
200 | $this->setExpectedException('Solarium_Exception'); |
---|
201 | $this->_client->registerPlugin('testplugin','StdClass'); |
---|
202 | } |
---|
203 | |
---|
204 | public function testGetInvalidPlugin() |
---|
205 | { |
---|
206 | $this->assertEquals( |
---|
207 | null, |
---|
208 | $this->_client->getPlugin('invalidplugin', false) |
---|
209 | ); |
---|
210 | } |
---|
211 | |
---|
212 | public function testAutoloadPlugin() |
---|
213 | { |
---|
214 | $loadbalancer = $this->_client->getPlugin('loadbalancer'); |
---|
215 | $this->assertThat( |
---|
216 | $loadbalancer, |
---|
217 | $this->isInstanceOf('Solarium_Plugin_Loadbalancer') |
---|
218 | ); |
---|
219 | } |
---|
220 | |
---|
221 | public function testAutoloadInvalidPlugin() |
---|
222 | { |
---|
223 | $this->setExpectedException('Solarium_Exception'); |
---|
224 | $this->_client->getPlugin('invalidpluginname'); |
---|
225 | } |
---|
226 | |
---|
227 | public function testRemoveAndGetPlugins() |
---|
228 | { |
---|
229 | $options = array('option1' => 1); |
---|
230 | $this->_client->registerPlugin('testplugin','MyClientPlugin',$options); |
---|
231 | |
---|
232 | $plugin = $this->_client->getPlugin('testplugin'); |
---|
233 | $plugins = $this->_client->getPlugins(); |
---|
234 | |
---|
235 | $this->assertEquals( |
---|
236 | array('testplugin' => $plugin), |
---|
237 | $plugins |
---|
238 | ); |
---|
239 | |
---|
240 | $this->_client->removePlugin('testplugin'); |
---|
241 | $plugins = $this->_client->getPlugins(); |
---|
242 | |
---|
243 | $this->assertEquals( |
---|
244 | array(), |
---|
245 | $plugins |
---|
246 | ); |
---|
247 | } |
---|
248 | |
---|
249 | public function testRemovePluginAndGetPluginsWithObjectInput() |
---|
250 | { |
---|
251 | $options = array('option1' => 1); |
---|
252 | $this->_client->registerPlugin('testplugin','MyClientPlugin',$options); |
---|
253 | |
---|
254 | $plugin = $this->_client->getPlugin('testplugin'); |
---|
255 | $plugins = $this->_client->getPlugins(); |
---|
256 | |
---|
257 | $this->assertEquals( |
---|
258 | array('testplugin' => $plugin), |
---|
259 | $plugins |
---|
260 | ); |
---|
261 | |
---|
262 | $this->_client->removePlugin($plugin); |
---|
263 | $plugins = $this->_client->getPlugins(); |
---|
264 | |
---|
265 | $this->assertEquals( |
---|
266 | array(), |
---|
267 | $plugins |
---|
268 | ); |
---|
269 | } |
---|
270 | |
---|
271 | public function testCreateRequest() |
---|
272 | { |
---|
273 | $queryStub = $this->getMock('Solarium_Query_Select'); |
---|
274 | $queryStub->expects($this->any()) |
---|
275 | ->method('getType') |
---|
276 | ->will($this->returnValue('testquerytype')); |
---|
277 | |
---|
278 | $observer = $this->getMock('Solarium_Client_RequestBuilder', array('build')); |
---|
279 | $observer->expects($this->once()) |
---|
280 | ->method('build') |
---|
281 | ->with($this->equalTo($queryStub)); |
---|
282 | |
---|
283 | $this->_client->registerQueryType('testquerytype', 'Solarium_Query_Select', $observer, ''); |
---|
284 | $this->_client->createRequest($queryStub); |
---|
285 | } |
---|
286 | |
---|
287 | public function testCreateRequestInvalidQueryType() |
---|
288 | { |
---|
289 | $queryStub = $this->getMock('Solarium_Query_Select'); |
---|
290 | $queryStub->expects($this->any()) |
---|
291 | ->method('getType') |
---|
292 | ->will($this->returnValue('testquerytype')); |
---|
293 | |
---|
294 | $this->setExpectedException('Solarium_Exception'); |
---|
295 | $this->_client->createRequest($queryStub); |
---|
296 | } |
---|
297 | |
---|
298 | public function testCreateRequestPrePlugin() |
---|
299 | { |
---|
300 | $query = new Solarium_Query_Select(); |
---|
301 | |
---|
302 | $observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array())); |
---|
303 | $observer->expects($this->once()) |
---|
304 | ->method('preCreateRequest') |
---|
305 | ->with($this->equalTo($query)); |
---|
306 | |
---|
307 | $this->_client->registerPlugin('testplugin', $observer); |
---|
308 | $this->_client->createRequest($query); |
---|
309 | } |
---|
310 | |
---|
311 | public function testCreateRequestPostPlugin() |
---|
312 | { |
---|
313 | $query = new Solarium_Query_Select(); |
---|
314 | $request = $this->_client->createRequest($query); |
---|
315 | |
---|
316 | $observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array())); |
---|
317 | $observer->expects($this->once()) |
---|
318 | ->method('postCreateRequest') |
---|
319 | ->with($this->equalTo($query),$this->equalTo($request)); |
---|
320 | |
---|
321 | $this->_client->registerPlugin('testplugin', $observer); |
---|
322 | $this->_client->createRequest($query); |
---|
323 | } |
---|
324 | |
---|
325 | public function testCreateRequestWithOverridingPlugin() |
---|
326 | { |
---|
327 | $overrideValue = 'dummyvalue'; |
---|
328 | $query = new Solarium_Query_Select(); |
---|
329 | |
---|
330 | $observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array())); |
---|
331 | $observer->expects($this->once()) |
---|
332 | ->method('preCreateRequest') |
---|
333 | ->with($this->equalTo($query)) |
---|
334 | ->will($this->returnValue($overrideValue)); |
---|
335 | |
---|
336 | $this->_client->registerPlugin('testplugin', $observer); |
---|
337 | $request = $this->_client->createRequest($query); |
---|
338 | |
---|
339 | $this->assertEquals( |
---|
340 | $overrideValue, |
---|
341 | $request |
---|
342 | ); |
---|
343 | } |
---|
344 | |
---|
345 | public function testCreateResult() |
---|
346 | { |
---|
347 | $query = new Solarium_Query_Select(); |
---|
348 | $response = new Solarium_Client_Response('',array('HTTP 1.0 200 OK')); |
---|
349 | $result = $this->_client->createResult($query, $response); |
---|
350 | |
---|
351 | $this->assertThat( |
---|
352 | $result, |
---|
353 | $this->isInstanceOf($query->getResultClass()) |
---|
354 | ); |
---|
355 | } |
---|
356 | |
---|
357 | public function testCreateResultPrePlugin() |
---|
358 | { |
---|
359 | $query = new Solarium_Query_Select(); |
---|
360 | $response = new Solarium_Client_Response('',array('HTTP 1.0 200 OK')); |
---|
361 | |
---|
362 | $observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array())); |
---|
363 | $observer->expects($this->once()) |
---|
364 | ->method('preCreateResult') |
---|
365 | ->with($this->equalTo($query),$this->equalTo($response)); |
---|
366 | |
---|
367 | $this->_client->registerPlugin('testplugin', $observer); |
---|
368 | $this->_client->createResult($query, $response); |
---|
369 | } |
---|
370 | |
---|
371 | public function testCreateResultPostPlugin() |
---|
372 | { |
---|
373 | $query = new Solarium_Query_Select(); |
---|
374 | $response = new Solarium_Client_Response('',array('HTTP 1.0 200 OK')); |
---|
375 | $result = $this->_client->createResult($query, $response); |
---|
376 | |
---|
377 | $observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array())); |
---|
378 | $observer->expects($this->once()) |
---|
379 | ->method('postCreateResult') |
---|
380 | ->with($this->equalTo($query), $this->equalTo($response), $this->equalTo($result)); |
---|
381 | |
---|
382 | $this->_client->registerPlugin('testplugin', $observer); |
---|
383 | $this->_client->createResult($query, $response); |
---|
384 | } |
---|
385 | |
---|
386 | public function testCreateResultWithOverridingPlugin() |
---|
387 | { |
---|
388 | $overrideValue = 'dummyvalue'; |
---|
389 | $query = new Solarium_Query_Select(); |
---|
390 | $response = new Solarium_Client_Response('',array('HTTP 1.0 200 OK')); |
---|
391 | |
---|
392 | $observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array())); |
---|
393 | $observer->expects($this->once()) |
---|
394 | ->method('preCreateResult') |
---|
395 | ->with($this->equalTo($query), $this->equalTo($response)) |
---|
396 | ->will($this->returnValue($overrideValue)); |
---|
397 | |
---|
398 | $this->_client->registerPlugin('testplugin', $observer); |
---|
399 | $result = $this->_client->createResult($query, $response); |
---|
400 | |
---|
401 | $this->assertEquals( |
---|
402 | $overrideValue, |
---|
403 | $result |
---|
404 | ); |
---|
405 | } |
---|
406 | |
---|
407 | public function testExecute() |
---|
408 | { |
---|
409 | $query = new Solarium_Query_Ping(); |
---|
410 | |
---|
411 | $observer = $this->getMock('Solarium_Client', array('createRequest','executeRequest','createResult')); |
---|
412 | |
---|
413 | $observer->expects($this->once()) |
---|
414 | ->method('createRequest') |
---|
415 | ->with($this->equalTo($query)) |
---|
416 | ->will($this->returnValue('dummyrequest')); |
---|
417 | |
---|
418 | $observer->expects($this->once()) |
---|
419 | ->method('executeRequest') |
---|
420 | ->with($this->equalTo('dummyrequest')) |
---|
421 | ->will($this->returnValue('dummyresponse')); |
---|
422 | |
---|
423 | $observer->expects($this->once()) |
---|
424 | ->method('createResult') |
---|
425 | ->with($this->equalTo($query),$this->equalTo('dummyresponse')); |
---|
426 | |
---|
427 | $observer->execute($query); |
---|
428 | } |
---|
429 | |
---|
430 | public function testExecutePrePlugin() |
---|
431 | { |
---|
432 | $query = new Solarium_Query_Ping(); |
---|
433 | |
---|
434 | $mock = $this->getMock('Solarium_Client', array('createRequest','executeRequest','createResult')); |
---|
435 | |
---|
436 | $mock->expects($this->once()) |
---|
437 | ->method('createRequest') |
---|
438 | ->will($this->returnValue('dummyrequest')); |
---|
439 | |
---|
440 | $mock->expects($this->once()) |
---|
441 | ->method('executeRequest') |
---|
442 | ->will($this->returnValue('dummyresponse')); |
---|
443 | |
---|
444 | $mock->expects($this->once()) |
---|
445 | ->method('createResult') |
---|
446 | ->will($this->returnValue('dummyresult')); |
---|
447 | |
---|
448 | $observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array())); |
---|
449 | $observer->expects($this->once()) |
---|
450 | ->method('preExecute') |
---|
451 | ->with($this->equalTo($query)); |
---|
452 | |
---|
453 | $mock->registerPlugin('testplugin', $observer); |
---|
454 | $mock->execute($query); |
---|
455 | } |
---|
456 | |
---|
457 | public function testExecutePostPlugin() |
---|
458 | { |
---|
459 | $query = new Solarium_Query_Ping(); |
---|
460 | |
---|
461 | $mock = $this->getMock('Solarium_Client', array('createRequest','executeRequest','createResult')); |
---|
462 | |
---|
463 | $mock->expects($this->once()) |
---|
464 | ->method('createRequest') |
---|
465 | ->will($this->returnValue('dummyrequest')); |
---|
466 | |
---|
467 | $mock->expects($this->once()) |
---|
468 | ->method('executeRequest') |
---|
469 | ->will($this->returnValue('dummyresponse')); |
---|
470 | |
---|
471 | $mock->expects($this->once()) |
---|
472 | ->method('createResult') |
---|
473 | ->will($this->returnValue('dummyresult')); |
---|
474 | |
---|
475 | $observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array())); |
---|
476 | $observer->expects($this->once()) |
---|
477 | ->method('postExecute') |
---|
478 | ->with($this->equalTo($query), $this->equalTo('dummyresult')); |
---|
479 | |
---|
480 | $mock->registerPlugin('testplugin', $observer); |
---|
481 | $mock->execute($query); |
---|
482 | } |
---|
483 | |
---|
484 | public function testExecuteWithOverridingPlugin() |
---|
485 | { |
---|
486 | $query = new Solarium_Query_Ping(); |
---|
487 | |
---|
488 | $observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array())); |
---|
489 | $observer->expects($this->once()) |
---|
490 | ->method('preExecute') |
---|
491 | ->with($this->equalTo($query)) |
---|
492 | ->will($this->returnValue('dummyoverride')); |
---|
493 | |
---|
494 | $this->_client->registerPlugin('testplugin', $observer); |
---|
495 | $result = $this->_client->execute($query); |
---|
496 | |
---|
497 | $this->assertEquals( |
---|
498 | 'dummyoverride', |
---|
499 | $result |
---|
500 | ); |
---|
501 | } |
---|
502 | |
---|
503 | public function testExecuteRequest() |
---|
504 | { |
---|
505 | $request = new Solarium_Client_Request(); |
---|
506 | $dummyResponse = 'dummyresponse'; |
---|
507 | |
---|
508 | $observer = $this->getMock('Solarium_Client_Adapter', array('execute')); |
---|
509 | $observer->expects($this->once()) |
---|
510 | ->method('execute') |
---|
511 | ->with($this->equalTo($request)) |
---|
512 | ->will($this->returnValue($dummyResponse)); |
---|
513 | |
---|
514 | $this->_client->setAdapter($observer); |
---|
515 | $response = $this->_client->executeRequest($request); |
---|
516 | |
---|
517 | $this->assertEquals( |
---|
518 | $dummyResponse, |
---|
519 | $response |
---|
520 | ); |
---|
521 | } |
---|
522 | |
---|
523 | public function testExecuteRequestPrePlugin() |
---|
524 | { |
---|
525 | $request = new Solarium_Client_Request(); |
---|
526 | $dummyResponse = 'dummyresponse'; |
---|
527 | |
---|
528 | $mockAdapter = $this->getMock('Solarium_Client_Adapter', array('execute')); |
---|
529 | $mockAdapter->expects($this->once()) |
---|
530 | ->method('execute') |
---|
531 | ->with($this->equalTo($request)) |
---|
532 | ->will($this->returnValue($dummyResponse)); |
---|
533 | $this->_client->setAdapter($mockAdapter); |
---|
534 | |
---|
535 | $observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array())); |
---|
536 | $observer->expects($this->once()) |
---|
537 | ->method('preExecuteRequest') |
---|
538 | ->with($this->equalTo($request)); |
---|
539 | |
---|
540 | $this->_client->registerPlugin('testplugin', $observer); |
---|
541 | $this->_client->executeRequest($request); |
---|
542 | } |
---|
543 | |
---|
544 | public function testExecuteRequestPostPlugin() |
---|
545 | { |
---|
546 | $request = new Solarium_Client_Request(); |
---|
547 | $dummyResponse = 'dummyresponse'; |
---|
548 | |
---|
549 | $mockAdapter = $this->getMock('Solarium_Client_Adapter', array('execute')); |
---|
550 | $mockAdapter->expects($this->any()) |
---|
551 | ->method('execute') |
---|
552 | ->with($this->equalTo($request)) |
---|
553 | ->will($this->returnValue($dummyResponse)); |
---|
554 | $this->_client->setAdapter($mockAdapter); |
---|
555 | |
---|
556 | $response = $this->_client->executeRequest($request); |
---|
557 | |
---|
558 | $observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array())); |
---|
559 | $observer->expects($this->once()) |
---|
560 | ->method('postExecuteRequest') |
---|
561 | ->with($this->equalTo($request), $this->equalTo($response)); |
---|
562 | |
---|
563 | $this->_client->registerPlugin('testplugin', $observer); |
---|
564 | $this->_client->executeRequest($request); |
---|
565 | } |
---|
566 | |
---|
567 | public function testExecuteRequestWithOverridingPlugin() |
---|
568 | { |
---|
569 | $request = new Solarium_Client_Request(); |
---|
570 | $dummyOverride = 'dummyoverride'; |
---|
571 | |
---|
572 | $observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array())); |
---|
573 | $observer->expects($this->once()) |
---|
574 | ->method('preExecuteRequest') |
---|
575 | ->with($this->equalTo($request)) |
---|
576 | ->will($this->returnValue($dummyOverride)); |
---|
577 | |
---|
578 | $this->_client->registerPlugin('testplugin', $observer); |
---|
579 | $response = $this->_client->executeRequest($request); |
---|
580 | |
---|
581 | $this->assertEquals( |
---|
582 | $dummyOverride, |
---|
583 | $response |
---|
584 | ); |
---|
585 | } |
---|
586 | |
---|
587 | public function testPing() |
---|
588 | { |
---|
589 | $query = new Solarium_Query_Ping(); |
---|
590 | |
---|
591 | $observer = $this->getMock('Solarium_Client', array('execute')); |
---|
592 | $observer->expects($this->once()) |
---|
593 | ->method('execute') |
---|
594 | ->with($this->equalTo($query)); |
---|
595 | |
---|
596 | $observer->ping($query); |
---|
597 | } |
---|
598 | |
---|
599 | public function testSelect() |
---|
600 | { |
---|
601 | $query = new Solarium_Query_Select(); |
---|
602 | |
---|
603 | $observer = $this->getMock('Solarium_Client', array('execute')); |
---|
604 | $observer->expects($this->once()) |
---|
605 | ->method('execute') |
---|
606 | ->with($this->equalTo($query)); |
---|
607 | |
---|
608 | $observer->select($query); |
---|
609 | } |
---|
610 | |
---|
611 | public function testUpdate() |
---|
612 | { |
---|
613 | $query = new Solarium_Query_Update(); |
---|
614 | |
---|
615 | $observer = $this->getMock('Solarium_Client', array('execute')); |
---|
616 | $observer->expects($this->once()) |
---|
617 | ->method('execute') |
---|
618 | ->with($this->equalTo($query)); |
---|
619 | |
---|
620 | $observer->update($query); |
---|
621 | } |
---|
622 | |
---|
623 | public function testMoreLikeThis() |
---|
624 | { |
---|
625 | $query = new Solarium_Query_MoreLikeThis(); |
---|
626 | |
---|
627 | $observer = $this->getMock('Solarium_Client', array('execute')); |
---|
628 | $observer->expects($this->once()) |
---|
629 | ->method('execute') |
---|
630 | ->with($this->equalTo($query)); |
---|
631 | |
---|
632 | $observer->moreLikeThis($query); |
---|
633 | } |
---|
634 | |
---|
635 | public function testAnalyze() |
---|
636 | { |
---|
637 | $query = new Solarium_Query_Analysis_Field(); |
---|
638 | |
---|
639 | $observer = $this->getMock('Solarium_Client', array('execute')); |
---|
640 | $observer->expects($this->once()) |
---|
641 | ->method('execute') |
---|
642 | ->with($this->equalTo($query)); |
---|
643 | |
---|
644 | $observer->analyze($query); |
---|
645 | } |
---|
646 | |
---|
647 | public function testTerms() |
---|
648 | { |
---|
649 | $query = new Solarium_Query_Terms(); |
---|
650 | |
---|
651 | $observer = $this->getMock('Solarium_Client', array('execute')); |
---|
652 | $observer->expects($this->once()) |
---|
653 | ->method('execute') |
---|
654 | ->with($this->equalTo($query)); |
---|
655 | |
---|
656 | $observer->terms($query); |
---|
657 | } |
---|
658 | |
---|
659 | public function testSuggester() |
---|
660 | { |
---|
661 | $query = new Solarium_Query_Suggester(); |
---|
662 | |
---|
663 | $observer = $this->getMock('Solarium_Client', array('execute')); |
---|
664 | $observer->expects($this->once()) |
---|
665 | ->method('execute') |
---|
666 | ->with($this->equalTo($query)); |
---|
667 | |
---|
668 | $observer->suggester($query); |
---|
669 | } |
---|
670 | |
---|
671 | public function testCreateQuery() |
---|
672 | { |
---|
673 | $options = array('optionA' => 1, 'optionB' => 2); |
---|
674 | $query = $this->_client->createQuery(Solarium_Client::QUERYTYPE_SELECT, $options); |
---|
675 | |
---|
676 | // check class mapping |
---|
677 | $this->assertThat($query, $this->isInstanceOf('Solarium_Query_Select')); |
---|
678 | |
---|
679 | // check option forwarding |
---|
680 | $queryOptions = $query->getOptions(); |
---|
681 | $this->assertEquals( |
---|
682 | $options['optionB'], |
---|
683 | $queryOptions['optionB'] |
---|
684 | ); |
---|
685 | } |
---|
686 | |
---|
687 | public function testCreateQueryWithInvalidQueryType() |
---|
688 | { |
---|
689 | $this->setExpectedException('Solarium_Exception'); |
---|
690 | $this->_client->createQuery('invalidtype'); |
---|
691 | } |
---|
692 | |
---|
693 | public function testCreateQueryPrePlugin() |
---|
694 | { |
---|
695 | $type = Solarium_Client::QUERYTYPE_SELECT; |
---|
696 | $options = array('optionA' => 1, 'optionB' => 2); |
---|
697 | |
---|
698 | $observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array())); |
---|
699 | $observer->expects($this->once()) |
---|
700 | ->method('preCreateQuery') |
---|
701 | ->with($this->equalTo($type), $this->equalTo($options)); |
---|
702 | |
---|
703 | $this->_client->registerPlugin('testplugin', $observer); |
---|
704 | $this->_client->createQuery($type, $options); |
---|
705 | } |
---|
706 | |
---|
707 | public function testCreateQueryWithOverridingPlugin() |
---|
708 | { |
---|
709 | $type = Solarium_Client::QUERYTYPE_SELECT; |
---|
710 | $options = array('optionA' => 1, 'optionB' => 2); |
---|
711 | $dummyvalue = 'test123'; |
---|
712 | |
---|
713 | $observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array())); |
---|
714 | $observer->expects($this->once()) |
---|
715 | ->method('preCreateQuery') |
---|
716 | ->with($this->equalTo($type), $this->equalTo($options)) |
---|
717 | ->will($this->returnValue($dummyvalue)); |
---|
718 | |
---|
719 | $this->_client->registerPlugin('testplugin', $observer); |
---|
720 | $query = $this->_client->createQuery($type, $options); |
---|
721 | |
---|
722 | $this->assertEquals( |
---|
723 | $dummyvalue, |
---|
724 | $query |
---|
725 | ); |
---|
726 | } |
---|
727 | |
---|
728 | public function testCreateQueryPostPlugin() |
---|
729 | { |
---|
730 | $type = Solarium_Client::QUERYTYPE_SELECT; |
---|
731 | $options = array('optionA' => 1, 'optionB' => 2); |
---|
732 | $query = $this->_client->createQuery($type, $options); |
---|
733 | |
---|
734 | $observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array())); |
---|
735 | $observer->expects($this->once()) |
---|
736 | ->method('postCreateQuery') |
---|
737 | ->with($this->equalTo($type), $this->equalTo($options), $this->equalTo($query)); |
---|
738 | |
---|
739 | $this->_client->registerPlugin('testplugin', $observer); |
---|
740 | $this->_client->createQuery($type, $options); |
---|
741 | } |
---|
742 | |
---|
743 | public function testCreateSelect() |
---|
744 | { |
---|
745 | $options = array('optionA' => 1, 'optionB' => 2); |
---|
746 | |
---|
747 | $observer = $this->getMock('Solarium_Client', array('createQuery')); |
---|
748 | $observer->expects($this->once()) |
---|
749 | ->method('createQuery') |
---|
750 | ->with($this->equalTo(Solarium_Client::QUERYTYPE_SELECT), $this->equalTo($options)); |
---|
751 | |
---|
752 | $observer->createSelect($options); |
---|
753 | } |
---|
754 | |
---|
755 | public function testCreateUpdate() |
---|
756 | { |
---|
757 | $options = array('optionA' => 1, 'optionB' => 2); |
---|
758 | |
---|
759 | $observer = $this->getMock('Solarium_Client', array('createQuery')); |
---|
760 | $observer->expects($this->once()) |
---|
761 | ->method('createQuery') |
---|
762 | ->with($this->equalTo(Solarium_Client::QUERYTYPE_UPDATE), $this->equalTo($options)); |
---|
763 | |
---|
764 | $observer->createUpdate($options); |
---|
765 | } |
---|
766 | |
---|
767 | public function testCreatePing() |
---|
768 | { |
---|
769 | $options = array('optionA' => 1, 'optionB' => 2); |
---|
770 | |
---|
771 | $observer = $this->getMock('Solarium_Client', array('createQuery')); |
---|
772 | $observer->expects($this->once()) |
---|
773 | ->method('createQuery') |
---|
774 | ->with($this->equalTo(Solarium_Client::QUERYTYPE_PING), $this->equalTo($options)); |
---|
775 | |
---|
776 | $observer->createPing($options); |
---|
777 | } |
---|
778 | |
---|
779 | public function testCreateMoreLikeThis() |
---|
780 | { |
---|
781 | $options = array('optionA' => 1, 'optionB' => 2); |
---|
782 | |
---|
783 | $observer = $this->getMock('Solarium_Client', array('createQuery')); |
---|
784 | $observer->expects($this->once()) |
---|
785 | ->method('createQuery') |
---|
786 | ->with($this->equalTo(Solarium_Client::QUERYTYPE_MORELIKETHIS), $this->equalTo($options)); |
---|
787 | |
---|
788 | $observer->createMoreLikeThis($options); |
---|
789 | } |
---|
790 | |
---|
791 | public function testCreateAnalysisField() |
---|
792 | { |
---|
793 | $options = array('optionA' => 1, 'optionB' => 2); |
---|
794 | |
---|
795 | $observer = $this->getMock('Solarium_Client', array('createQuery')); |
---|
796 | $observer->expects($this->once()) |
---|
797 | ->method('createQuery') |
---|
798 | ->with($this->equalTo(Solarium_Client::QUERYTYPE_ANALYSIS_FIELD), $this->equalTo($options)); |
---|
799 | |
---|
800 | $observer->createAnalysisField($options); |
---|
801 | } |
---|
802 | |
---|
803 | public function testCreateAnalysisDocument() |
---|
804 | { |
---|
805 | $options = array('optionA' => 1, 'optionB' => 2); |
---|
806 | |
---|
807 | $observer = $this->getMock('Solarium_Client', array('createQuery')); |
---|
808 | $observer->expects($this->once()) |
---|
809 | ->method('createQuery') |
---|
810 | ->with($this->equalTo(Solarium_Client::QUERYTYPE_ANALYSIS_DOCUMENT), $this->equalTo($options)); |
---|
811 | |
---|
812 | $observer->createAnalysisDocument($options); |
---|
813 | } |
---|
814 | |
---|
815 | public function testCreateTerms() |
---|
816 | { |
---|
817 | $options = array('optionA' => 1, 'optionB' => 2); |
---|
818 | |
---|
819 | $observer = $this->getMock('Solarium_Client', array('createQuery')); |
---|
820 | $observer->expects($this->once()) |
---|
821 | ->method('createQuery') |
---|
822 | ->with($this->equalTo(Solarium_Client::QUERYTYPE_TERMS), $this->equalTo($options)); |
---|
823 | |
---|
824 | $observer->createTerms($options); |
---|
825 | } |
---|
826 | |
---|
827 | public function testCreateSuggester() |
---|
828 | { |
---|
829 | $options = array('optionA' => 1, 'optionB' => 2); |
---|
830 | |
---|
831 | $observer = $this->getMock('Solarium_Client', array('createQuery')); |
---|
832 | $observer->expects($this->once()) |
---|
833 | ->method('createQuery') |
---|
834 | ->with($this->equalTo(Solarium_Client::QUERYTYPE_SUGGESTER), $this->equalTo($options)); |
---|
835 | |
---|
836 | $observer->createSuggester($options); |
---|
837 | } |
---|
838 | |
---|
839 | public function testTriggerEvent() |
---|
840 | { |
---|
841 | $eventName = 'Test'; |
---|
842 | $params = array('a', 'b'); |
---|
843 | $override = true; |
---|
844 | |
---|
845 | $clientMock = $this->getMock('Solarium_Client', array('_callPlugins')); |
---|
846 | $clientMock->expects($this->once()) |
---|
847 | ->method('_callPlugins') |
---|
848 | ->with($this->equalTo('event'.$eventName), $this->equalTo($params), $override); |
---|
849 | |
---|
850 | $clientMock->triggerEvent($eventName, $params, $override); |
---|
851 | } |
---|
852 | |
---|
853 | } |
---|
854 | |
---|
855 | class MyAdapter extends Solarium_Client_Adapter_Http{ |
---|
856 | |
---|
857 | public function execute($request) |
---|
858 | { |
---|
859 | $response = new Solarium_Client_Response('{}', array('HTTP/1.1 200 OK')); |
---|
860 | return $response; |
---|
861 | } |
---|
862 | } |
---|
863 | |
---|
864 | class MyClientPlugin extends Solarium_Plugin_Abstract{ |
---|
865 | |
---|
866 | } |
---|