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_Plugin_PrefetchIteratorTest extends PHPUnit_Framework_TestCase |
---|
33 | { |
---|
34 | /** |
---|
35 | * @var Solarium_Plugin_PrefetchIterator |
---|
36 | */ |
---|
37 | protected $_plugin; |
---|
38 | |
---|
39 | /** |
---|
40 | * @var Solarium_Client |
---|
41 | */ |
---|
42 | protected $_client; |
---|
43 | |
---|
44 | /** |
---|
45 | * @var Solarium_Query_Select |
---|
46 | */ |
---|
47 | protected $_query; |
---|
48 | |
---|
49 | |
---|
50 | public function setUp() |
---|
51 | { |
---|
52 | $this->_plugin = new Solarium_Plugin_PrefetchIterator(); |
---|
53 | |
---|
54 | $this->_client = new Solarium_Client(); |
---|
55 | $this->_query = $this->_client->createSelect(); |
---|
56 | |
---|
57 | } |
---|
58 | |
---|
59 | public function testSetAndGetPrefetch() |
---|
60 | { |
---|
61 | $this->_plugin->setPrefetch(120); |
---|
62 | $this->assertEquals(120, $this->_plugin->getPrefetch()); |
---|
63 | } |
---|
64 | |
---|
65 | public function testSetAndGetQuery() |
---|
66 | { |
---|
67 | $this->_plugin->setQuery($this->_query); |
---|
68 | $this->assertEquals($this->_query, $this->_plugin->getQuery()); |
---|
69 | } |
---|
70 | |
---|
71 | public function testCount() |
---|
72 | { |
---|
73 | $result = $this->_getResult(); |
---|
74 | $mockClient = $this->getMock('Solarium_Client', array('execute')); |
---|
75 | $mockClient->expects($this->exactly(1))->method('execute')->will($this->returnValue($result)); |
---|
76 | |
---|
77 | $this->_plugin->init($mockClient, array()); |
---|
78 | $this->_plugin->setQuery($this->_query); |
---|
79 | $this->assertEquals(5, count($this->_plugin)); |
---|
80 | } |
---|
81 | |
---|
82 | public function testIteratorAndRewind() |
---|
83 | { |
---|
84 | $result = $this->_getResult(); |
---|
85 | $mockClient = $this->getMock('Solarium_Client', array('execute')); |
---|
86 | $mockClient->expects($this->exactly(1))->method('execute')->will($this->returnValue($result)); |
---|
87 | |
---|
88 | $this->_plugin->init($mockClient, array()); |
---|
89 | $this->_plugin->setQuery($this->_query); |
---|
90 | |
---|
91 | $results1 = array(); |
---|
92 | foreach($this->_plugin as $doc) { |
---|
93 | $results1[] = $doc; |
---|
94 | } |
---|
95 | |
---|
96 | // the second foreach will trigger a rewind, this time include keys |
---|
97 | $results2 = array(); |
---|
98 | foreach($this->_plugin as $key => $doc) { |
---|
99 | $results2[$key] = $doc; |
---|
100 | } |
---|
101 | |
---|
102 | $this->assertEquals($result->getDocuments(), $results1); |
---|
103 | $this->assertEquals($result->getDocuments(), $results2); |
---|
104 | } |
---|
105 | |
---|
106 | public function _getResult() |
---|
107 | { |
---|
108 | $numFound = 5; |
---|
109 | |
---|
110 | $docs = array( |
---|
111 | new Solarium_Document_ReadOnly(array('id'=>1,'title'=>'doc1')), |
---|
112 | new Solarium_Document_ReadOnly(array('id'=>2,'title'=>'doc2')), |
---|
113 | new Solarium_Document_ReadOnly(array('id'=>3,'title'=>'doc3')), |
---|
114 | new Solarium_Document_ReadOnly(array('id'=>4,'title'=>'doc4')), |
---|
115 | new Solarium_Document_ReadOnly(array('id'=>5,'title'=>'doc5')), |
---|
116 | ); |
---|
117 | |
---|
118 | return new Solarium_Result_SelectDummy(1, 12, $numFound, $docs, array()); |
---|
119 | } |
---|
120 | } |
---|