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_Query_Select_Component_StatsTest extends PHPUnit_Framework_TestCase |
---|
33 | { |
---|
34 | |
---|
35 | /** |
---|
36 | * @var Solarium_Query_Select_Component_Stats |
---|
37 | */ |
---|
38 | protected $_stats; |
---|
39 | |
---|
40 | public function setUp() |
---|
41 | { |
---|
42 | $this->_stats = new Solarium_Query_Select_Component_Stats; |
---|
43 | } |
---|
44 | |
---|
45 | public function testGetType() |
---|
46 | { |
---|
47 | $this->assertEquals(Solarium_Query_Select::COMPONENT_STATS, $this->_stats->getType()); |
---|
48 | } |
---|
49 | |
---|
50 | public function testConfigMode() |
---|
51 | { |
---|
52 | $options = array( |
---|
53 | 'facet' => 'field1, field2', |
---|
54 | 'field' => array( |
---|
55 | 'f1' => array(), |
---|
56 | 'f2' => array(), |
---|
57 | ) |
---|
58 | ); |
---|
59 | |
---|
60 | $this->_stats->setOptions($options); |
---|
61 | |
---|
62 | $this->assertEquals(array('field1','field2'), $this->_stats->getFacets()); |
---|
63 | $this->assertEquals(array('f1','f2'), array_keys($this->_stats->getFields())); |
---|
64 | } |
---|
65 | |
---|
66 | public function testCreateFieldWithKey() |
---|
67 | { |
---|
68 | $field = $this->_stats->createField('mykey'); |
---|
69 | |
---|
70 | // check class |
---|
71 | $this->assertThat($field, $this->isInstanceOf('Solarium_Query_Select_Component_Stats_Field')); |
---|
72 | |
---|
73 | $this->assertEquals( |
---|
74 | 'mykey', |
---|
75 | $field->getKey() |
---|
76 | ); |
---|
77 | } |
---|
78 | |
---|
79 | public function testCreateFieldWithOptions() |
---|
80 | { |
---|
81 | $options = array('key' => 'testkey'); |
---|
82 | $field = $this->_stats->createField($options); |
---|
83 | |
---|
84 | // check class |
---|
85 | $this->assertThat($field, $this->isInstanceOf('Solarium_Query_Select_Component_Stats_Field')); |
---|
86 | |
---|
87 | // check option forwarding |
---|
88 | $fieldOptions = $field->getOptions(); |
---|
89 | $this->assertEquals( |
---|
90 | $options['key'], |
---|
91 | $field->getKey() |
---|
92 | ); |
---|
93 | } |
---|
94 | |
---|
95 | public function testAddAndGetField() |
---|
96 | { |
---|
97 | $field = new Solarium_Query_Select_Component_Stats_Field; |
---|
98 | $field->setKey('f1'); |
---|
99 | $this->_stats->addField($field); |
---|
100 | |
---|
101 | $this->assertEquals( |
---|
102 | $field, |
---|
103 | $this->_stats->getField('f1') |
---|
104 | ); |
---|
105 | } |
---|
106 | |
---|
107 | public function testAddFieldWithOptions() |
---|
108 | { |
---|
109 | $this->_stats->addField(array('key' => 'f1')); |
---|
110 | |
---|
111 | $this->assertEquals( |
---|
112 | 'f1', |
---|
113 | $this->_stats->getField('f1')->getKey() |
---|
114 | ); |
---|
115 | } |
---|
116 | |
---|
117 | public function testAddAndGetFieldWithKey() |
---|
118 | { |
---|
119 | $key = 'f1'; |
---|
120 | |
---|
121 | $fld = $this->_stats->createField($key, true); |
---|
122 | |
---|
123 | $this->assertEquals( |
---|
124 | $key, |
---|
125 | $fld->getKey() |
---|
126 | ); |
---|
127 | |
---|
128 | $this->assertEquals( |
---|
129 | $fld, |
---|
130 | $this->_stats->getField('f1') |
---|
131 | ); |
---|
132 | } |
---|
133 | |
---|
134 | public function testAddFieldWithoutKey() |
---|
135 | { |
---|
136 | $fld = new Solarium_Query_Select_Component_Stats_Field; |
---|
137 | |
---|
138 | $this->setExpectedException('Solarium_Exception'); |
---|
139 | $this->_stats->addField($fld); |
---|
140 | } |
---|
141 | |
---|
142 | public function testAddFieldWithUsedKey() |
---|
143 | { |
---|
144 | $f1 = new Solarium_Query_Select_Component_Stats_Field; |
---|
145 | $f1->setKey('f1'); |
---|
146 | |
---|
147 | $f2 = new Solarium_Query_Select_Component_Stats_Field; |
---|
148 | $f2->setKey('f1'); |
---|
149 | |
---|
150 | $this->_stats->addField($f1); |
---|
151 | $this->setExpectedException('Solarium_Exception'); |
---|
152 | $this->_stats->addField($f2); |
---|
153 | } |
---|
154 | |
---|
155 | public function testGetInvalidField() |
---|
156 | { |
---|
157 | $this->assertEquals( |
---|
158 | null, |
---|
159 | $this->_stats->getField('invalidkey') |
---|
160 | ); |
---|
161 | } |
---|
162 | |
---|
163 | public function testAddFields() |
---|
164 | { |
---|
165 | $f1 = new Solarium_Query_Select_Component_Stats_Field; |
---|
166 | $f1->setKey('f1'); |
---|
167 | |
---|
168 | $f2 = new Solarium_Query_Select_Component_Stats_Field; |
---|
169 | $f2->setKey('f2'); |
---|
170 | |
---|
171 | $fields = array('f1' => $f1, 'f2' => $f2); |
---|
172 | |
---|
173 | $this->_stats->addFields($fields); |
---|
174 | $this->assertEquals( |
---|
175 | $fields, |
---|
176 | $this->_stats->getFields() |
---|
177 | ); |
---|
178 | } |
---|
179 | |
---|
180 | public function testAddFieldsWithOptions() |
---|
181 | { |
---|
182 | $fields = array( |
---|
183 | 'f1' => array(''), |
---|
184 | array('key' => 'f2') |
---|
185 | ); |
---|
186 | |
---|
187 | $this->_stats->addFields($fields); |
---|
188 | $fields = $this->_stats->getFields(); |
---|
189 | |
---|
190 | $this->assertEquals( array('f1', 'f2'), array_keys($fields)); |
---|
191 | } |
---|
192 | |
---|
193 | public function testRemoveField() |
---|
194 | { |
---|
195 | $f1 = new Solarium_Query_Select_Component_Stats_Field; |
---|
196 | $f1->setKey('f1'); |
---|
197 | |
---|
198 | $f2 = new Solarium_Query_Select_Component_Stats_Field; |
---|
199 | $f2->setKey('f2'); |
---|
200 | |
---|
201 | $fields = array('f1' => $f1, 'f2' => $f2); |
---|
202 | |
---|
203 | $this->_stats->addFields($fields); |
---|
204 | $this->_stats->removeField('f1'); |
---|
205 | $this->assertEquals( |
---|
206 | array('f2' => $f2), |
---|
207 | $this->_stats->getFields() |
---|
208 | ); |
---|
209 | } |
---|
210 | |
---|
211 | public function testRemoveFieldWithObjectInput() |
---|
212 | { |
---|
213 | $f1 = new Solarium_Query_Select_Component_Stats_Field; |
---|
214 | $f1->setKey('f1'); |
---|
215 | |
---|
216 | $f2 = new Solarium_Query_Select_Component_Stats_Field; |
---|
217 | $f2->setKey('f2'); |
---|
218 | |
---|
219 | $fields = array($f1, $f2); |
---|
220 | |
---|
221 | $this->_stats->addFields($fields); |
---|
222 | $this->_stats->removeField($f1); |
---|
223 | $this->assertEquals( |
---|
224 | array('f2' => $f2), |
---|
225 | $this->_stats->getFields() |
---|
226 | ); |
---|
227 | } |
---|
228 | |
---|
229 | public function testRemoveInvalidField() |
---|
230 | { |
---|
231 | $f1 = new Solarium_Query_Select_Component_Stats_Field; |
---|
232 | $f1->setKey('f1'); |
---|
233 | |
---|
234 | $f2 = new Solarium_Query_Select_Component_Stats_Field; |
---|
235 | $f2->setKey('f2'); |
---|
236 | |
---|
237 | $fields = array('f1' => $f1, 'f2' => $f2); |
---|
238 | |
---|
239 | $this->_stats->addFields($fields); |
---|
240 | $this->_stats->removeField('f3'); //continue silently |
---|
241 | $this->assertEquals( |
---|
242 | $fields, |
---|
243 | $this->_stats->getFields() |
---|
244 | ); |
---|
245 | } |
---|
246 | |
---|
247 | public function testClearFields() |
---|
248 | { |
---|
249 | $f1 = new Solarium_Query_Select_Component_Stats_Field; |
---|
250 | $f1->setKey('f1'); |
---|
251 | |
---|
252 | $f2 = new Solarium_Query_Select_Component_Stats_Field; |
---|
253 | $f2->setKey('f2'); |
---|
254 | |
---|
255 | $fields = array($f1, $f2); |
---|
256 | |
---|
257 | $this->_stats->addFields($fields); |
---|
258 | $this->_stats->clearFields(); |
---|
259 | $this->assertEquals( |
---|
260 | array(), |
---|
261 | $this->_stats->getFields() |
---|
262 | ); |
---|
263 | } |
---|
264 | |
---|
265 | public function testSetFields() |
---|
266 | { |
---|
267 | $f1 = new Solarium_Query_Select_Component_Stats_Field; |
---|
268 | $f1->setKey('f1'); |
---|
269 | |
---|
270 | $f2 = new Solarium_Query_Select_Component_Stats_Field; |
---|
271 | $f2->setKey('f2'); |
---|
272 | |
---|
273 | $fields = array($f1, $f2); |
---|
274 | |
---|
275 | $this->_stats->addFields($fields); |
---|
276 | |
---|
277 | $f3 = new Solarium_Query_Select_Component_Stats_Field; |
---|
278 | $f3->setKey('f3'); |
---|
279 | |
---|
280 | $f4 = new Solarium_Query_Select_Component_Stats_Field; |
---|
281 | $f4->setKey('f4'); |
---|
282 | |
---|
283 | $fields2 = array('f3' => $f3, 'f4' => $f4); |
---|
284 | |
---|
285 | $this->_stats->setFields($fields2); |
---|
286 | |
---|
287 | $this->assertEquals( |
---|
288 | $fields2, |
---|
289 | $this->_stats->getFields() |
---|
290 | ); |
---|
291 | } |
---|
292 | |
---|
293 | public function testAddFacet() |
---|
294 | { |
---|
295 | $expectedFacets = $this->_stats->getFacets(); |
---|
296 | $expectedFacets[] = 'newfacet'; |
---|
297 | $this->_stats->addFacet('newfacet'); |
---|
298 | $this->assertEquals($expectedFacets, $this->_stats->getFacets()); |
---|
299 | } |
---|
300 | |
---|
301 | public function testClearFacets() |
---|
302 | { |
---|
303 | $this->_stats->addFacet('newfacet'); |
---|
304 | $this->_stats->clearFacets(); |
---|
305 | $this->assertEquals(array(), $this->_stats->getFacets()); |
---|
306 | } |
---|
307 | |
---|
308 | public function testAddFacets() |
---|
309 | { |
---|
310 | $facets = array('facet1','facet2'); |
---|
311 | |
---|
312 | $this->_stats->clearFacets(); |
---|
313 | $this->_stats->addFacets($facets); |
---|
314 | $this->assertEquals($facets, $this->_stats->getFacets()); |
---|
315 | } |
---|
316 | |
---|
317 | public function testAddFacetsAsStringWithTrim() |
---|
318 | { |
---|
319 | $this->_stats->clearFacets(); |
---|
320 | $this->_stats->addFacets('facet1, facet2'); |
---|
321 | $this->assertEquals(array('facet1','facet2'), $this->_stats->getFacets()); |
---|
322 | } |
---|
323 | |
---|
324 | public function testRemoveFacet() |
---|
325 | { |
---|
326 | $this->_stats->clearFacets(); |
---|
327 | $this->_stats->addFacets(array('facet1','facet2')); |
---|
328 | $this->_stats->removeFacet('facet1'); |
---|
329 | $this->assertEquals(array('facet2'), $this->_stats->getFacets()); |
---|
330 | } |
---|
331 | |
---|
332 | public function testSetFacets() |
---|
333 | { |
---|
334 | $this->_stats->clearFacets(); |
---|
335 | $this->_stats->addFacets(array('facet1','facet2')); |
---|
336 | $this->_stats->setFacets(array('facet3','facet4')); |
---|
337 | $this->assertEquals(array('facet3','facet4'), $this->_stats->getFacets()); |
---|
338 | } |
---|
339 | |
---|
340 | } |
---|