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_CustomizeRequestTest extends PHPUnit_Framework_TestCase |
---|
33 | { |
---|
34 | /** |
---|
35 | * @var Solarium_Plugin_CustomizeRequest |
---|
36 | */ |
---|
37 | protected $_plugin; |
---|
38 | |
---|
39 | public function setUp() |
---|
40 | { |
---|
41 | $this->_plugin = new Solarium_Plugin_CustomizeRequest(); |
---|
42 | } |
---|
43 | |
---|
44 | public function testConfigMode() |
---|
45 | { |
---|
46 | $options = array( |
---|
47 | 'customization' => array( |
---|
48 | array( |
---|
49 | 'key' => 'auth', |
---|
50 | 'type' => 'header', |
---|
51 | 'name' => 'X-my-auth', |
---|
52 | 'value' => 'mypassword', |
---|
53 | 'persistent' => true |
---|
54 | ), |
---|
55 | 'id' => array( |
---|
56 | 'type' => 'param', |
---|
57 | 'name' => 'id', |
---|
58 | 'value' => '1234', |
---|
59 | 'persistent' => false, |
---|
60 | 'overwrite' => false, |
---|
61 | ), |
---|
62 | ) |
---|
63 | ); |
---|
64 | |
---|
65 | $this->_plugin->setOptions($options); |
---|
66 | |
---|
67 | $auth = $this->_plugin->getCustomization('auth'); |
---|
68 | $id = $this->_plugin->getCustomization('id'); |
---|
69 | |
---|
70 | $this->assertThat($auth, $this->isInstanceOf('Solarium_Plugin_CustomizeRequest_Customization')); |
---|
71 | $this->assertEquals('auth', $auth->getKey()); |
---|
72 | $this->assertEquals('header', $auth->getType()); |
---|
73 | $this->assertEquals('X-my-auth', $auth->getName()); |
---|
74 | $this->assertEquals('mypassword', $auth->getValue()); |
---|
75 | $this->assertEquals(true, $auth->getPersistent()); |
---|
76 | |
---|
77 | $this->assertThat($id, $this->isInstanceOf('Solarium_Plugin_CustomizeRequest_Customization')); |
---|
78 | $this->assertEquals('id', $id->getKey()); |
---|
79 | $this->assertEquals('param', $id->getType()); |
---|
80 | $this->assertEquals('id', $id->getName()); |
---|
81 | $this->assertEquals('1234', $id->getValue()); |
---|
82 | $this->assertEquals(false, $id->getPersistent()); |
---|
83 | $this->assertEquals(false, $id->getOverwrite()); |
---|
84 | } |
---|
85 | |
---|
86 | public function testCreateCustomization() |
---|
87 | { |
---|
88 | $customization = $this->_plugin->createCustomization('id1'); |
---|
89 | |
---|
90 | $this->assertEquals( |
---|
91 | $customization, |
---|
92 | $this->_plugin->getCustomization('id1') |
---|
93 | ); |
---|
94 | } |
---|
95 | |
---|
96 | public function testCreateCustomizationWithArray() |
---|
97 | { |
---|
98 | $input = array( |
---|
99 | 'key' => 'auth', |
---|
100 | 'type' => 'header', |
---|
101 | 'name' => 'X-my-auth', |
---|
102 | 'value' => 'mypassword', |
---|
103 | 'persistent' => true |
---|
104 | ); |
---|
105 | $customization = $this->_plugin->createCustomization($input); |
---|
106 | |
---|
107 | $this->assertEquals($customization, $this->_plugin->getCustomization('auth')); |
---|
108 | $this->assertEquals($input['key'], $customization->getKey()); |
---|
109 | $this->assertEquals($input['type'], $customization->getType()); |
---|
110 | $this->assertEquals($input['name'], $customization->getName()); |
---|
111 | $this->assertEquals($input['value'], $customization->getValue()); |
---|
112 | $this->assertEquals($input['persistent'], $customization->getPersistent()); |
---|
113 | } |
---|
114 | |
---|
115 | public function testAddAndGetCustomization() |
---|
116 | { |
---|
117 | $customization = new Solarium_Plugin_CustomizeRequest_Customization; |
---|
118 | $customization->setKey('id1'); |
---|
119 | $this->_plugin->addCustomization($customization); |
---|
120 | |
---|
121 | $this->assertEquals( |
---|
122 | $customization, |
---|
123 | $this->_plugin->getCustomization('id1') |
---|
124 | ); |
---|
125 | } |
---|
126 | |
---|
127 | public function testAddAndGetCustomizationWithKey() |
---|
128 | { |
---|
129 | $key = 'id1'; |
---|
130 | |
---|
131 | $customization = $this->_plugin->createCustomization($key); |
---|
132 | |
---|
133 | $this->assertEquals( |
---|
134 | $key, |
---|
135 | $customization->getKey() |
---|
136 | ); |
---|
137 | |
---|
138 | $this->assertEquals( |
---|
139 | $customization, |
---|
140 | $this->_plugin->getCustomization($key) |
---|
141 | ); |
---|
142 | } |
---|
143 | |
---|
144 | public function testAddCustomizationWithoutKey() |
---|
145 | { |
---|
146 | $customization = new Solarium_Plugin_CustomizeRequest_Customization;; |
---|
147 | |
---|
148 | $this->setExpectedException('Solarium_Exception'); |
---|
149 | $this->_plugin->addCustomization($customization); |
---|
150 | } |
---|
151 | |
---|
152 | public function testAddCustomizationWithUsedKey() |
---|
153 | { |
---|
154 | $customization1 = new Solarium_Plugin_CustomizeRequest_Customization; |
---|
155 | $customization1->setKey('id1')->setName('test1'); |
---|
156 | |
---|
157 | $customization2 = new Solarium_Plugin_CustomizeRequest_Customization; |
---|
158 | $customization2->setKey('id1')->setName('test2'); |
---|
159 | |
---|
160 | $this->_plugin->addCustomization($customization1); |
---|
161 | $this->setExpectedException('Solarium_Exception'); |
---|
162 | $this->_plugin->addCustomization($customization2); |
---|
163 | } |
---|
164 | |
---|
165 | public function testAddDuplicateCustomizationWith() |
---|
166 | { |
---|
167 | $customization = new Solarium_Plugin_CustomizeRequest_Customization; |
---|
168 | $customization->setKey('id1')->setName('test1'); |
---|
169 | |
---|
170 | $this->_plugin->addCustomization($customization); |
---|
171 | $this->_plugin->addCustomization($customization); |
---|
172 | |
---|
173 | $this->assertEquals( |
---|
174 | $customization, |
---|
175 | $this->_plugin->getCustomization('id1') |
---|
176 | ); |
---|
177 | } |
---|
178 | |
---|
179 | public function testGetInvalidCustomization() |
---|
180 | { |
---|
181 | $this->assertEquals( |
---|
182 | null, |
---|
183 | $this->_plugin->getCustomization('invalidkey') |
---|
184 | ); |
---|
185 | } |
---|
186 | |
---|
187 | public function testAddCustomizations() |
---|
188 | { |
---|
189 | $customization1 = new Solarium_Plugin_CustomizeRequest_Customization; |
---|
190 | $customization1->setKey('id1')->setName('test1'); |
---|
191 | |
---|
192 | $customization2 = new Solarium_Plugin_CustomizeRequest_Customization; |
---|
193 | $customization2->setKey('id2')->setName('test2'); |
---|
194 | |
---|
195 | $customizations = array('id1' => $customization1, 'id2' => $customization2); |
---|
196 | |
---|
197 | $this->_plugin->addCustomizations($customizations); |
---|
198 | $this->assertEquals( |
---|
199 | $customizations, |
---|
200 | $this->_plugin->getCustomizations() |
---|
201 | ); |
---|
202 | } |
---|
203 | |
---|
204 | public function testRemoveCustomization() |
---|
205 | { |
---|
206 | $customization1 = new Solarium_Plugin_CustomizeRequest_Customization; |
---|
207 | $customization1->setKey('id1')->setName('test1'); |
---|
208 | |
---|
209 | $customization2 = new Solarium_Plugin_CustomizeRequest_Customization; |
---|
210 | $customization2->setKey('id2')->setName('test2'); |
---|
211 | |
---|
212 | $customizations = array($customization1, $customization2); |
---|
213 | |
---|
214 | $this->_plugin->addCustomizations($customizations); |
---|
215 | $this->_plugin->removeCustomization('id1'); |
---|
216 | $this->assertEquals( |
---|
217 | array('id2' => $customization2), |
---|
218 | $this->_plugin->getCustomizations() |
---|
219 | ); |
---|
220 | } |
---|
221 | |
---|
222 | public function testRemoveCustomizationWithObjectInput() |
---|
223 | { |
---|
224 | $customization1 = new Solarium_Plugin_CustomizeRequest_Customization; |
---|
225 | $customization1->setKey('id1')->setName('test1'); |
---|
226 | |
---|
227 | $customization2 = new Solarium_Plugin_CustomizeRequest_Customization; |
---|
228 | $customization2->setKey('id2')->setName('test2'); |
---|
229 | |
---|
230 | $customizations = array($customization1, $customization2); |
---|
231 | |
---|
232 | $this->_plugin->addCustomizations($customizations); |
---|
233 | $this->_plugin->removeCustomization($customization1); |
---|
234 | $this->assertEquals( |
---|
235 | array('id2' => $customization2), |
---|
236 | $this->_plugin->getCustomizations() |
---|
237 | ); |
---|
238 | } |
---|
239 | |
---|
240 | public function testRemoveInvalidCustomization() |
---|
241 | { |
---|
242 | $customization1 = new Solarium_Plugin_CustomizeRequest_Customization; |
---|
243 | $customization1->setKey('id1')->setName('test1'); |
---|
244 | |
---|
245 | $customization2 = new Solarium_Plugin_CustomizeRequest_Customization; |
---|
246 | $customization2->setKey('id2')->setName('test2'); |
---|
247 | |
---|
248 | $customizations = array('id1' => $customization1, 'id2' => $customization2); |
---|
249 | |
---|
250 | $this->_plugin->addCustomizations($customizations); |
---|
251 | $this->_plugin->removeCustomization('id3'); //continue silently |
---|
252 | $this->assertEquals( |
---|
253 | $customizations, |
---|
254 | $this->_plugin->getCustomizations() |
---|
255 | ); |
---|
256 | } |
---|
257 | |
---|
258 | public function testClearCustomizations() |
---|
259 | { |
---|
260 | $customization1 = new Solarium_Plugin_CustomizeRequest_Customization; |
---|
261 | $customization1->setKey('id1')->setName('test1'); |
---|
262 | |
---|
263 | $customization2 = new Solarium_Plugin_CustomizeRequest_Customization; |
---|
264 | $customization2->setKey('id2')->setName('test2'); |
---|
265 | |
---|
266 | $customizations = array($customization1, $customization2); |
---|
267 | |
---|
268 | $this->_plugin->addCustomizations($customizations); |
---|
269 | $this->_plugin->clearCustomizations(); |
---|
270 | $this->assertEquals( |
---|
271 | array(), |
---|
272 | $this->_plugin->getCustomizations() |
---|
273 | ); |
---|
274 | } |
---|
275 | |
---|
276 | public function testSetCustomizations() |
---|
277 | { |
---|
278 | $customization1 = new Solarium_Plugin_CustomizeRequest_Customization; |
---|
279 | $customization1->setKey('id1')->setName('test1'); |
---|
280 | |
---|
281 | $customization2 = new Solarium_Plugin_CustomizeRequest_Customization; |
---|
282 | $customization2->setKey('id2')->setName('test2'); |
---|
283 | |
---|
284 | $customizations1 = array('id1' => $customization1, 'id2' => $customization2); |
---|
285 | |
---|
286 | $this->_plugin->addCustomizations($customizations1); |
---|
287 | |
---|
288 | $customization3 = new Solarium_Plugin_CustomizeRequest_Customization; |
---|
289 | $customization3->setKey('id3')->setName('test3'); |
---|
290 | |
---|
291 | $customization4 = new Solarium_Plugin_CustomizeRequest_Customization; |
---|
292 | $customization4->setKey('id4')->setName('test4'); |
---|
293 | |
---|
294 | $customizations2 = array('id3' => $customization3, 'id4' => $customization4); |
---|
295 | |
---|
296 | $this->_plugin->setCustomizations($customizations2); |
---|
297 | |
---|
298 | $this->assertEquals( |
---|
299 | $customizations2, |
---|
300 | $this->_plugin->getCustomizations() |
---|
301 | ); |
---|
302 | } |
---|
303 | |
---|
304 | public function testPostCreateRequestWithHeaderAndParam() |
---|
305 | { |
---|
306 | $input = array( |
---|
307 | 'key' => 'xid', |
---|
308 | 'type' => 'param', |
---|
309 | 'name' => 'xid', |
---|
310 | 'value' => 123, |
---|
311 | ); |
---|
312 | $this->_plugin->addCustomization($input); |
---|
313 | |
---|
314 | $input = array( |
---|
315 | 'key' => 'auth', |
---|
316 | 'type' => 'header', |
---|
317 | 'name' => 'X-my-auth', |
---|
318 | 'value' => 'mypassword', |
---|
319 | 'persistent' => true |
---|
320 | ); |
---|
321 | $this->_plugin->addCustomization($input); |
---|
322 | |
---|
323 | $request = new Solarium_Client_Request(); |
---|
324 | $this->_plugin->postCreateRequest(null, $request); |
---|
325 | |
---|
326 | $this->assertEquals( |
---|
327 | 123, |
---|
328 | $request->getParam('xid') |
---|
329 | ); |
---|
330 | |
---|
331 | $this->assertEquals( |
---|
332 | array('X-my-auth: mypassword'), |
---|
333 | $request->getHeaders() |
---|
334 | ); |
---|
335 | } |
---|
336 | |
---|
337 | public function testPostCreateRequestWithInvalidCustomization() |
---|
338 | { |
---|
339 | $input = array( |
---|
340 | 'key' => 'xid', |
---|
341 | 'type' => 'invalid', |
---|
342 | 'name' => 'xid', |
---|
343 | 'value' => 123, |
---|
344 | ); |
---|
345 | $this->_plugin->addCustomization($input); |
---|
346 | |
---|
347 | $request = new Solarium_Client_Request(); |
---|
348 | |
---|
349 | $this->setExpectedException('Solarium_Exception'); |
---|
350 | $this->_plugin->postCreateRequest(null, $request); |
---|
351 | } |
---|
352 | |
---|
353 | public function testPostCreateRequestWithoutCustomizations() |
---|
354 | { |
---|
355 | $request = new Solarium_Client_Request(); |
---|
356 | $originalRequest = clone $request; |
---|
357 | |
---|
358 | $this->_plugin->postCreateRequest(null, $request); |
---|
359 | |
---|
360 | $this->assertEquals( |
---|
361 | $originalRequest, |
---|
362 | $request |
---|
363 | ); |
---|
364 | } |
---|
365 | |
---|
366 | public function testPostCreateRequestWithPersistentAndNonPersistentCustomizations() |
---|
367 | { |
---|
368 | $input = array( |
---|
369 | 'key' => 'xid', |
---|
370 | 'type' => 'param', |
---|
371 | 'name' => 'xid', |
---|
372 | 'value' => 123, |
---|
373 | ); |
---|
374 | $this->_plugin->addCustomization($input); |
---|
375 | |
---|
376 | $input = array( |
---|
377 | 'key' => 'auth', |
---|
378 | 'type' => 'header', |
---|
379 | 'name' => 'X-my-auth', |
---|
380 | 'value' => 'mypassword', |
---|
381 | 'persistent' => true |
---|
382 | ); |
---|
383 | $this->_plugin->addCustomization($input); |
---|
384 | |
---|
385 | $request = new Solarium_Client_Request(); |
---|
386 | $this->_plugin->postCreateRequest(null, $request); |
---|
387 | |
---|
388 | $this->assertEquals( |
---|
389 | 123, |
---|
390 | $request->getParam('xid') |
---|
391 | ); |
---|
392 | |
---|
393 | $this->assertEquals( |
---|
394 | array('X-my-auth: mypassword'), |
---|
395 | $request->getHeaders() |
---|
396 | ); |
---|
397 | |
---|
398 | // second use, only the header should be persistent |
---|
399 | $request = new Solarium_Client_Request(); |
---|
400 | $this->_plugin->postCreateRequest(null, $request); |
---|
401 | |
---|
402 | $this->assertEquals( |
---|
403 | null, |
---|
404 | $request->getParam('xid') |
---|
405 | ); |
---|
406 | |
---|
407 | $this->assertEquals( |
---|
408 | array('X-my-auth: mypassword'), |
---|
409 | $request->getHeaders() |
---|
410 | ); |
---|
411 | } |
---|
412 | |
---|
413 | } |
---|