scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2  
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11  
12 namespace Symfony\Component\DomCrawler\Tests\Field;
13  
14 use Symfony\Component\DomCrawler\Field\ChoiceFormField;
15  
16 class ChoiceFormFieldTest extends FormFieldTestCase
17 {
18 public function testInitialize()
19 {
20 $node = $this->createNode('textarea', '');
21 try {
22 $field = new ChoiceFormField($node);
23 $this->fail('->initialize() throws a \LogicException if the node is not an input or a select');
24 } catch (\LogicException $e) {
25 $this->assertTrue(true, '->initialize() throws a \LogicException if the node is not an input or a select');
26 }
27  
28 $node = $this->createNode('input', '', array('type' => 'text'));
29 try {
30 $field = new ChoiceFormField($node);
31 $this->fail('->initialize() throws a \LogicException if the node is an input with a type different from checkbox or radio');
32 } catch (\LogicException $e) {
33 $this->assertTrue(true, '->initialize() throws a \LogicException if the node is an input with a type different from checkbox or radio');
34 }
35 }
36  
37 public function testGetType()
38 {
39 $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo'));
40 $field = new ChoiceFormField($node);
41  
42 $this->assertEquals('radio', $field->getType(), '->getType() returns radio for radio buttons');
43  
44 $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'value' => 'foo'));
45 $field = new ChoiceFormField($node);
46  
47 $this->assertEquals('checkbox', $field->getType(), '->getType() returns radio for a checkbox');
48  
49 $node = $this->createNode('select', '');
50 $field = new ChoiceFormField($node);
51  
52 $this->assertEquals('select', $field->getType(), '->getType() returns radio for a select');
53 }
54  
55 public function testIsMultiple()
56 {
57 $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo'));
58 $field = new ChoiceFormField($node);
59  
60 $this->assertFalse($field->isMultiple(), '->isMultiple() returns false for radio buttons');
61  
62 $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'value' => 'foo'));
63 $field = new ChoiceFormField($node);
64  
65 $this->assertFalse($field->isMultiple(), '->isMultiple() returns false for checkboxes');
66  
67 $node = $this->createNode('select', '');
68 $field = new ChoiceFormField($node);
69  
70 $this->assertFalse($field->isMultiple(), '->isMultiple() returns false for selects without the multiple attribute');
71  
72 $node = $this->createNode('select', '', array('multiple' => 'multiple'));
73 $field = new ChoiceFormField($node);
74  
75 $this->assertTrue($field->isMultiple(), '->isMultiple() returns true for selects with the multiple attribute');
76  
77 $node = $this->createNode('select', '', array('multiple' => ''));
78 $field = new ChoiceFormField($node);
79  
80 $this->assertTrue($field->isMultiple(), '->isMultiple() returns true for selects with an empty multiple attribute');
81 }
82  
83 public function testSelects()
84 {
85 $node = $this->createSelectNode(array('foo' => false, 'bar' => false));
86 $field = new ChoiceFormField($node);
87  
88 $this->assertTrue($field->hasValue(), '->hasValue() returns true for selects');
89 $this->assertEquals('foo', $field->getValue(), '->getValue() returns the first option if none are selected');
90 $this->assertFalse($field->isMultiple(), '->isMultiple() returns false when no multiple attribute is defined');
91  
92 $node = $this->createSelectNode(array('foo' => false, 'bar' => true));
93 $field = new ChoiceFormField($node);
94  
95 $this->assertEquals('bar', $field->getValue(), '->getValue() returns the selected option');
96  
97 $field->setValue('foo');
98 $this->assertEquals('foo', $field->getValue(), '->setValue() changes the selected option');
99  
100 try {
101 $field->setValue('foobar');
102 $this->fail('->setValue() throws an \InvalidArgumentException if the value is not one of the selected options');
103 } catch (\InvalidArgumentException $e) {
104 $this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is not one of the selected options');
105 }
106  
107 try {
108 $field->setValue(array('foobar'));
109 $this->fail('->setValue() throws an \InvalidArgumentException if the value is an array');
110 } catch (\InvalidArgumentException $e) {
111 $this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is an array');
112 }
113 }
114  
115 public function testSelectWithEmptyBooleanAttribute()
116 {
117 $node = $this->createSelectNode(array('foo' => false, 'bar' => true), array(), '');
118 $field = new ChoiceFormField($node);
119  
120 $this->assertEquals('bar', $field->getValue());
121 }
122  
123 public function testSelectIsDisabled()
124 {
125 $node = $this->createSelectNode(array('foo' => false, 'bar' => true), array('disabled' => 'disabled'));
126 $field = new ChoiceFormField($node);
127  
128 $this->assertTrue($field->isDisabled(), '->isDisabled() returns true for selects with a disabled attribute');
129 }
130  
131 public function testMultipleSelects()
132 {
133 $node = $this->createSelectNode(array('foo' => false, 'bar' => false), array('multiple' => 'multiple'));
134 $field = new ChoiceFormField($node);
135  
136 $this->assertEquals(array(), $field->getValue(), '->setValue() returns an empty array if multiple is true and no option is selected');
137  
138 $field->setValue('foo');
139 $this->assertEquals(array('foo'), $field->getValue(), '->setValue() returns an array of options if multiple is true');
140  
141 $field->setValue('bar');
142 $this->assertEquals(array('bar'), $field->getValue(), '->setValue() returns an array of options if multiple is true');
143  
144 $field->setValue(array('foo', 'bar'));
145 $this->assertEquals(array('foo', 'bar'), $field->getValue(), '->setValue() returns an array of options if multiple is true');
146  
147 $node = $this->createSelectNode(array('foo' => true, 'bar' => true), array('multiple' => 'multiple'));
148 $field = new ChoiceFormField($node);
149  
150 $this->assertEquals(array('foo', 'bar'), $field->getValue(), '->getValue() returns the selected options');
151  
152 try {
153 $field->setValue(array('foobar'));
154 $this->fail('->setValue() throws an \InvalidArgumentException if the value is not one of the options');
155 } catch (\InvalidArgumentException $e) {
156 $this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is not one of the options');
157 }
158 }
159  
160 public function testRadioButtons()
161 {
162 $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo'));
163 $field = new ChoiceFormField($node);
164 $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'bar'));
165 $field->addChoice($node);
166  
167 $this->assertFalse($field->hasValue(), '->hasValue() returns false when no radio button is selected');
168 $this->assertNull($field->getValue(), '->getValue() returns null if no radio button is selected');
169 $this->assertFalse($field->isMultiple(), '->isMultiple() returns false for radio buttons');
170  
171 $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo'));
172 $field = new ChoiceFormField($node);
173 $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'bar', 'checked' => 'checked'));
174 $field->addChoice($node);
175  
176 $this->assertTrue($field->hasValue(), '->hasValue() returns true when a radio button is selected');
177 $this->assertEquals('bar', $field->getValue(), '->getValue() returns the value attribute of the selected radio button');
178  
179 $field->setValue('foo');
180 $this->assertEquals('foo', $field->getValue(), '->setValue() changes the selected radio button');
181  
182 try {
183 $field->setValue('foobar');
184 $this->fail('->setValue() throws an \InvalidArgumentException if the value is not one of the radio button values');
185 } catch (\InvalidArgumentException $e) {
186 $this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is not one of the radio button values');
187 }
188 }
189  
190 public function testRadioButtonsWithEmptyBooleanAttribute()
191 {
192 $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo'));
193 $field = new ChoiceFormField($node);
194 $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'bar', 'checked' => ''));
195 $field->addChoice($node);
196  
197 $this->assertTrue($field->hasValue(), '->hasValue() returns true when a radio button is selected');
198 $this->assertEquals('bar', $field->getValue(), '->getValue() returns the value attribute of the selected radio button');
199 }
200  
201 public function testRadioButtonIsDisabled()
202 {
203 $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo', 'disabled' => 'disabled'));
204 $field = new ChoiceFormField($node);
205 $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'bar'));
206 $field->addChoice($node);
207 $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'baz', 'disabled' => ''));
208 $field->addChoice($node);
209  
210 $field->select('foo');
211 $this->assertEquals('foo', $field->getValue(), '->getValue() returns the value attribute of the selected radio button');
212 $this->assertTrue($field->isDisabled());
213  
214 $field->select('bar');
215 $this->assertEquals('bar', $field->getValue(), '->getValue() returns the value attribute of the selected radio button');
216 $this->assertFalse($field->isDisabled());
217  
218 $field->select('baz');
219 $this->assertEquals('baz', $field->getValue(), '->getValue() returns the value attribute of the selected radio button');
220 $this->assertTrue($field->isDisabled());
221 }
222  
223 public function testCheckboxes()
224 {
225 $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name'));
226 $field = new ChoiceFormField($node);
227  
228 $this->assertFalse($field->hasValue(), '->hasValue() returns false when the checkbox is not checked');
229 $this->assertNull($field->getValue(), '->getValue() returns null if the checkbox is not checked');
230 $this->assertFalse($field->isMultiple(), '->hasValue() returns false for checkboxes');
231 try {
232 $field->addChoice(new \DOMElement('input'));
233 $this->fail('->addChoice() throws a \LogicException for checkboxes');
234 } catch (\LogicException $e) {
235 $this->assertTrue(true, '->initialize() throws a \LogicException for checkboxes');
236 }
237  
238 $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked'));
239 $field = new ChoiceFormField($node);
240  
241 $this->assertTrue($field->hasValue(), '->hasValue() returns true when the checkbox is checked');
242 $this->assertEquals('on', $field->getValue(), '->getValue() returns 1 if the checkbox is checked and has no value attribute');
243  
244 $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked', 'value' => 'foo'));
245 $field = new ChoiceFormField($node);
246  
247 $this->assertEquals('foo', $field->getValue(), '->getValue() returns the value attribute if the checkbox is checked');
248  
249 $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked', 'value' => 'foo'));
250 $field = new ChoiceFormField($node);
251  
252 $field->setValue(false);
253 $this->assertNull($field->getValue(), '->setValue() unchecks the checkbox is value is false');
254  
255 $field->setValue(true);
256 $this->assertEquals('foo', $field->getValue(), '->setValue() checks the checkbox is value is true');
257  
258 try {
259 $field->setValue('bar');
260 $this->fail('->setValue() throws an \InvalidArgumentException if the value is not one from the value attribute');
261 } catch (\InvalidArgumentException $e) {
262 $this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is not one from the value attribute');
263 }
264 }
265  
266 public function testCheckboxWithEmptyBooleanAttribute()
267 {
268 $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'value' => 'foo', 'checked' => ''));
269 $field = new ChoiceFormField($node);
270  
271 $this->assertTrue($field->hasValue(), '->hasValue() returns true when the checkbox is checked');
272 $this->assertEquals('foo', $field->getValue());
273 }
274  
275 public function testTick()
276 {
277 $node = $this->createSelectNode(array('foo' => false, 'bar' => false));
278 $field = new ChoiceFormField($node);
279  
280 try {
281 $field->tick();
282 $this->fail('->tick() throws a \LogicException for select boxes');
283 } catch (\LogicException $e) {
284 $this->assertTrue(true, '->tick() throws a \LogicException for select boxes');
285 }
286  
287 $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name'));
288 $field = new ChoiceFormField($node);
289 $field->tick();
290 $this->assertEquals('on', $field->getValue(), '->tick() ticks checkboxes');
291 }
292  
293 public function testUntick()
294 {
295 $node = $this->createSelectNode(array('foo' => false, 'bar' => false));
296 $field = new ChoiceFormField($node);
297  
298 try {
299 $field->untick();
300 $this->fail('->untick() throws a \LogicException for select boxes');
301 } catch (\LogicException $e) {
302 $this->assertTrue(true, '->untick() throws a \LogicException for select boxes');
303 }
304  
305 $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked'));
306 $field = new ChoiceFormField($node);
307 $field->untick();
308 $this->assertNull($field->getValue(), '->untick() unticks checkboxes');
309 }
310  
311 public function testSelect()
312 {
313 $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked'));
314 $field = new ChoiceFormField($node);
315 $field->select(true);
316 $this->assertEquals('on', $field->getValue(), '->select() changes the value of the field');
317 $field->select(false);
318 $this->assertNull($field->getValue(), '->select() changes the value of the field');
319  
320 $node = $this->createSelectNode(array('foo' => false, 'bar' => false));
321 $field = new ChoiceFormField($node);
322 $field->select('foo');
323 $this->assertEquals('foo', $field->getValue(), '->select() changes the selected option');
324 }
325  
326 public function testOptionWithNoValue()
327 {
328 $node = $this->createSelectNodeWithEmptyOption(array('foo' => false, 'bar' => false));
329 $field = new ChoiceFormField($node);
330 $this->assertEquals('foo', $field->getValue());
331  
332 $node = $this->createSelectNodeWithEmptyOption(array('foo' => false, 'bar' => true));
333 $field = new ChoiceFormField($node);
334 $this->assertEquals('bar', $field->getValue());
335 $field->select('foo');
336 $this->assertEquals('foo', $field->getValue(), '->select() changes the selected option');
337 }
338  
339 public function testDisableValidation()
340 {
341 $node = $this->createSelectNode(array('foo' => false, 'bar' => false));
342 $field = new ChoiceFormField($node);
343 $field->disableValidation();
344 $field->setValue('foobar');
345 $this->assertEquals('foobar', $field->getValue(), '->disableValidation() allows to set a value which is not in the selected options.');
346  
347 $node = $this->createSelectNode(array('foo' => false, 'bar' => false), array('multiple' => 'multiple'));
348 $field = new ChoiceFormField($node);
349 $field->disableValidation();
350 $field->setValue(array('foobar'));
351 $this->assertEquals(array('foobar'), $field->getValue(), '->disableValidation() allows to set a value which is not in the selected options.');
352 }
353  
354 public function testSelectWithEmptyValue()
355 {
356 $node = $this->createSelectNodeWithEmptyOption(array('' => true, 'Female' => false, 'Male' => false));
357 $field = new ChoiceFormField($node);
358  
359 $this->assertSame('', $field->getValue());
360 }
361  
362 protected function createSelectNode($options, $attributes = array(), $selectedAttrText = 'selected')
363 {
364 $document = new \DOMDocument();
365 $node = $document->createElement('select');
366  
367 foreach ($attributes as $name => $value) {
368 $node->setAttribute($name, $value);
369 }
370 $node->setAttribute('name', 'name');
371  
372 foreach ($options as $value => $selected) {
373 $option = $document->createElement('option', $value);
374 $option->setAttribute('value', $value);
375 if ($selected) {
376 $option->setAttribute('selected', $selectedAttrText);
377 }
378 $node->appendChild($option);
379 }
380  
381 return $node;
382 }
383  
384 protected function createSelectNodeWithEmptyOption($options, $attributes = array())
385 {
386 $document = new \DOMDocument();
387 $node = $document->createElement('select');
388  
389 foreach ($attributes as $name => $value) {
390 $node->setAttribute($name, $value);
391 }
392 $node->setAttribute('name', 'name');
393  
394 foreach ($options as $value => $selected) {
395 $option = $document->createElement('option', $value);
396 if ($selected) {
397 $option->setAttribute('selected', 'selected');
398 }
399 $node->appendChild($option);
400 }
401  
402 return $node;
403 }
404 }