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_Document_ReadWriteTest extends PHPUnit_Framework_TestCase |
---|
33 | { |
---|
34 | |
---|
35 | protected $_doc; |
---|
36 | |
---|
37 | protected $_fields = array( |
---|
38 | 'id' => 123, |
---|
39 | 'name' => 'Test document', |
---|
40 | 'categories' => array(1,2,3) |
---|
41 | ); |
---|
42 | |
---|
43 | protected function setUp() |
---|
44 | { |
---|
45 | $this->_doc = new Solarium_Document_ReadWrite($this->_fields); |
---|
46 | } |
---|
47 | |
---|
48 | public function testConstructorWithFieldsAndBoosts() |
---|
49 | { |
---|
50 | $fields = array('id' => 1, 'name' => 'testname'); |
---|
51 | $boosts = array('name' => 2.7); |
---|
52 | $doc = new Solarium_Document_ReadWrite($fields, $boosts); |
---|
53 | |
---|
54 | $this->assertEquals( |
---|
55 | $fields, |
---|
56 | $doc->getFields() |
---|
57 | ); |
---|
58 | |
---|
59 | $this->assertEquals( |
---|
60 | 2.7, |
---|
61 | $doc->getFieldBoost('name') |
---|
62 | ); |
---|
63 | } |
---|
64 | |
---|
65 | public function testAddFieldNoBoost() |
---|
66 | { |
---|
67 | $this->_doc->addField('myfield', 'myvalue'); |
---|
68 | |
---|
69 | $expectedFields = $this->_fields; |
---|
70 | $expectedFields['myfield'] = 'myvalue'; |
---|
71 | |
---|
72 | $this->assertEquals( |
---|
73 | $expectedFields, |
---|
74 | $this->_doc->getFields() |
---|
75 | ); |
---|
76 | } |
---|
77 | |
---|
78 | public function testAddFieldWithBoost() |
---|
79 | { |
---|
80 | $this->_doc->addField('myfield', 'myvalue', 2.3); |
---|
81 | |
---|
82 | $expectedFields = $this->_fields; |
---|
83 | $expectedFields['myfield'] = 'myvalue'; |
---|
84 | |
---|
85 | $this->assertEquals( |
---|
86 | $expectedFields, |
---|
87 | $this->_doc->getFields() |
---|
88 | ); |
---|
89 | |
---|
90 | $this->assertEquals( |
---|
91 | 2.3, |
---|
92 | $this->_doc->getFieldBoost('myfield') |
---|
93 | ); |
---|
94 | } |
---|
95 | |
---|
96 | public function testAddFieldMultivalue() |
---|
97 | { |
---|
98 | $this->_doc->addField('myfield', 'myvalue'); |
---|
99 | |
---|
100 | $expectedFields = $this->_fields; |
---|
101 | $expectedFields['myfield'] = 'myvalue'; |
---|
102 | |
---|
103 | $this->assertEquals( |
---|
104 | $expectedFields, |
---|
105 | $this->_doc->getFields() |
---|
106 | ); |
---|
107 | |
---|
108 | $this->_doc->addField('myfield', 'mysecondvalue'); |
---|
109 | |
---|
110 | $expectedFields['myfield'] = array('myvalue','mysecondvalue'); |
---|
111 | |
---|
112 | $this->assertEquals( |
---|
113 | $expectedFields, |
---|
114 | $this->_doc->getFields() |
---|
115 | ); |
---|
116 | } |
---|
117 | |
---|
118 | public function testSetField() |
---|
119 | { |
---|
120 | $this->_doc->setField('name', 'newname'); |
---|
121 | |
---|
122 | $expectedFields = $this->_fields; |
---|
123 | $expectedFields['name'] = 'newname'; |
---|
124 | |
---|
125 | $this->assertEquals( |
---|
126 | $expectedFields, |
---|
127 | $this->_doc->getFields() |
---|
128 | ); |
---|
129 | } |
---|
130 | |
---|
131 | public function testSetFieldWithFalsyValue() |
---|
132 | { |
---|
133 | $falsy_value = ''; |
---|
134 | $this->_doc->setField('name', $falsy_value); |
---|
135 | |
---|
136 | $expectedFields = $this->_fields; |
---|
137 | $expectedFields['name'] = $falsy_value; |
---|
138 | |
---|
139 | $this->assertEquals( |
---|
140 | $expectedFields, |
---|
141 | $this->_doc->getFields() |
---|
142 | ); |
---|
143 | } |
---|
144 | |
---|
145 | public function testRemoveField() |
---|
146 | { |
---|
147 | $this->_doc->removeField('name'); |
---|
148 | |
---|
149 | $expectedFields = $this->_fields; |
---|
150 | unset($expectedFields['name']); |
---|
151 | |
---|
152 | $this->assertEquals( |
---|
153 | $expectedFields, |
---|
154 | $this->_doc->getFields() |
---|
155 | ); |
---|
156 | } |
---|
157 | |
---|
158 | public function testRemoveFieldBySettingToNull() |
---|
159 | { |
---|
160 | $this->_doc->setField('name', NULL); |
---|
161 | |
---|
162 | $expectedFields = $this->_fields; |
---|
163 | unset($expectedFields['name']); |
---|
164 | |
---|
165 | $this->assertEquals( |
---|
166 | $expectedFields, |
---|
167 | $this->_doc->getFields() |
---|
168 | ); |
---|
169 | } |
---|
170 | |
---|
171 | public function testRemoveFieldBoostRemoval() |
---|
172 | { |
---|
173 | $this->_doc->setFieldBoost('name',3.2); |
---|
174 | $this->_doc->removeField('name'); |
---|
175 | |
---|
176 | $this->assertEquals( |
---|
177 | null, |
---|
178 | $this->_doc->getFieldBoost('name') |
---|
179 | ); |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | public function testRemoveInvalidField() |
---|
184 | { |
---|
185 | $this->_doc->removeField('invalidname'); //should silently continue... |
---|
186 | |
---|
187 | $this->assertEquals( |
---|
188 | $this->_fields, |
---|
189 | $this->_doc->getFields() |
---|
190 | ); |
---|
191 | } |
---|
192 | |
---|
193 | public function testSetAndGetFieldBoost() |
---|
194 | { |
---|
195 | $this->_doc->setFieldBoost('name',2.5); |
---|
196 | $this->assertEquals( |
---|
197 | 2.5, |
---|
198 | $this->_doc->getFieldBoost('name') |
---|
199 | ); |
---|
200 | } |
---|
201 | |
---|
202 | public function testGetInvalidFieldBoost() |
---|
203 | { |
---|
204 | $this->assertEquals( |
---|
205 | null, |
---|
206 | $this->_doc->getFieldBoost('invalidname') |
---|
207 | ); |
---|
208 | } |
---|
209 | |
---|
210 | public function testSetAndGetBoost() |
---|
211 | { |
---|
212 | $this->_doc->setBoost(2.5); |
---|
213 | $this->assertEquals( |
---|
214 | 2.5, |
---|
215 | $this->_doc->getBoost() |
---|
216 | ); |
---|
217 | } |
---|
218 | |
---|
219 | public function testSetAndGetFieldByProperty() |
---|
220 | { |
---|
221 | $this->_doc->name = 'new name'; |
---|
222 | |
---|
223 | $this->assertEquals( |
---|
224 | 'new name', |
---|
225 | $this->_doc->name |
---|
226 | ); |
---|
227 | } |
---|
228 | |
---|
229 | public function testSetAndGetMultivalueFieldByProperty() |
---|
230 | { |
---|
231 | $values = array('test1', 'test2', 'test3'); |
---|
232 | $this->_doc->multivaluefield = $values; |
---|
233 | |
---|
234 | $this->assertEquals( |
---|
235 | $values, |
---|
236 | $this->_doc->multivaluefield |
---|
237 | ); |
---|
238 | } |
---|
239 | |
---|
240 | public function testSetAndGetMultivalueFieldByPropertyOverwrite() |
---|
241 | { |
---|
242 | $values = array('test1', 'test2', 'test3'); |
---|
243 | $this->_doc->name = $values; |
---|
244 | |
---|
245 | $this->assertEquals( |
---|
246 | $values, |
---|
247 | $this->_doc->name |
---|
248 | ); |
---|
249 | } |
---|
250 | |
---|
251 | public function testUnsetFieldByProperty() |
---|
252 | { |
---|
253 | unset($this->_doc->name); |
---|
254 | |
---|
255 | $expectedFields = $this->_fields; |
---|
256 | unset($expectedFields['name']); |
---|
257 | |
---|
258 | $this->assertEquals( |
---|
259 | $expectedFields, |
---|
260 | $this->_doc->getFields() |
---|
261 | ); |
---|
262 | } |
---|
263 | |
---|
264 | public function testSetFieldAsArray() |
---|
265 | { |
---|
266 | $this->_doc['name'] = 'newname'; |
---|
267 | |
---|
268 | $expectedFields = $this->_fields; |
---|
269 | $expectedFields['name'] = 'newname'; |
---|
270 | |
---|
271 | $this->assertEquals( |
---|
272 | $expectedFields, |
---|
273 | $this->_doc->getFields() |
---|
274 | ); |
---|
275 | } |
---|
276 | |
---|
277 | public function testRemoveFieldAsArray() |
---|
278 | { |
---|
279 | unset($this->_doc['name']); |
---|
280 | |
---|
281 | $expectedFields = $this->_fields; |
---|
282 | unset($expectedFields['name']); |
---|
283 | |
---|
284 | $this->assertEquals( |
---|
285 | $expectedFields, |
---|
286 | $this->_doc->getFields() |
---|
287 | ); |
---|
288 | } |
---|
289 | |
---|
290 | public function testClearFields() |
---|
291 | { |
---|
292 | $this->_doc->clear(); |
---|
293 | |
---|
294 | $expectedFields = array(); |
---|
295 | |
---|
296 | $this->assertEquals( |
---|
297 | $expectedFields, |
---|
298 | $this->_doc->getFields() |
---|
299 | ); |
---|
300 | } |
---|
301 | |
---|
302 | public function testClearFieldsBoostRemoval() |
---|
303 | { |
---|
304 | $this->_doc->setFieldBoost('name', 3.2); |
---|
305 | $this->_doc->clear(); |
---|
306 | |
---|
307 | $expectedFields = array(); |
---|
308 | |
---|
309 | $this->assertEquals( |
---|
310 | null, |
---|
311 | $this->_doc->getFieldBoost('name') |
---|
312 | ); |
---|
313 | } |
---|
314 | |
---|
315 | } |
---|