4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DomCrawler\Tests\Field;
14 use Symfony\Component\DomCrawler\Field\ChoiceFormField;
16 class ChoiceFormFieldTest extends FormFieldTestCase
18 public function testInitialize()
20 $node = $this->createNode('textarea', '');
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');
28 $node = $this->createNode('input', '', array('type' => 'text'));
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');
37 public function testGetType()
39 $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo'));
40 $field = new ChoiceFormField($node);
42 $this->assertEquals('radio', $field->getType(), '->getType() returns radio for radio buttons');
44 $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'value' => 'foo'));
45 $field = new ChoiceFormField($node);
47 $this->assertEquals('checkbox', $field->getType(), '->getType() returns radio for a checkbox');
49 $node = $this->createNode('select', '');
50 $field = new ChoiceFormField($node);
52 $this->assertEquals('select', $field->getType(), '->getType() returns radio for a select');
55 public function testIsMultiple()
57 $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo'));
58 $field = new ChoiceFormField($node);
60 $this->assertFalse($field->isMultiple(), '->isMultiple() returns false for radio buttons');
62 $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'value' => 'foo'));
63 $field = new ChoiceFormField($node);
65 $this->assertFalse($field->isMultiple(), '->isMultiple() returns false for checkboxes');
67 $node = $this->createNode('select', '');
68 $field = new ChoiceFormField($node);
70 $this->assertFalse($field->isMultiple(), '->isMultiple() returns false for selects without the multiple attribute');
72 $node = $this->createNode('select', '', array('multiple' => 'multiple'));
73 $field = new ChoiceFormField($node);
75 $this->assertTrue($field->isMultiple(), '->isMultiple() returns true for selects with the multiple attribute');
77 $node = $this->createNode('select', '', array('multiple' => ''));
78 $field = new ChoiceFormField($node);
80 $this->assertTrue($field->isMultiple(), '->isMultiple() returns true for selects with an empty multiple attribute');
83 public function testSelects()
85 $node = $this->createSelectNode(array('foo' => false, 'bar' => false));
86 $field = new ChoiceFormField($node);
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');
92 $node = $this->createSelectNode(array('foo' => false, 'bar' => true));
93 $field = new ChoiceFormField($node);
95 $this->assertEquals('bar', $field->getValue(), '->getValue() returns the selected option');
97 $field->setValue('foo');
98 $this->assertEquals('foo', $field->getValue(), '->setValue() changes the selected option');
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');
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');
115 public function testSelectWithEmptyBooleanAttribute()
117 $node = $this->createSelectNode(array('foo' => false, 'bar' => true), array(), '');
118 $field = new ChoiceFormField($node);
120 $this->assertEquals('bar', $field->getValue());
123 public function testSelectIsDisabled()
125 $node = $this->createSelectNode(array('foo' => false, 'bar' => true), array('disabled' => 'disabled'));
126 $field = new ChoiceFormField($node);
128 $this->assertTrue($field->isDisabled(), '->isDisabled() returns true for selects with a disabled attribute');
131 public function testMultipleSelects()
133 $node = $this->createSelectNode(array('foo' => false, 'bar' => false), array('multiple' => 'multiple'));
134 $field = new ChoiceFormField($node);
136 $this->assertEquals(array(), $field->getValue(), '->setValue() returns an empty array if multiple is true and no option is selected');
138 $field->setValue('foo');
139 $this->assertEquals(array('foo'), $field->getValue(), '->setValue() returns an array of options if multiple is true');
141 $field->setValue('bar');
142 $this->assertEquals(array('bar'), $field->getValue(), '->setValue() returns an array of options if multiple is true');
144 $field->setValue(array('foo', 'bar'));
145 $this->assertEquals(array('foo', 'bar'), $field->getValue(), '->setValue() returns an array of options if multiple is true');
147 $node = $this->createSelectNode(array('foo' => true, 'bar' => true), array('multiple' => 'multiple'));
148 $field = new ChoiceFormField($node);
150 $this->assertEquals(array('foo', 'bar'), $field->getValue(), '->getValue() returns the selected options');
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');
160 public function testRadioButtons()
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);
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');
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);
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');
179 $field->setValue('foo');
180 $this->assertEquals('foo', $field->getValue(), '->setValue() changes the selected radio button');
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');
190 public function testRadioButtonsWithEmptyBooleanAttribute()
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);
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');
201 public function testRadioButtonIsDisabled()
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);
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());
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());
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());
223 public function testCheckboxes()
225 $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name'));
226 $field = new ChoiceFormField($node);
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');
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');
238 $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked'));
239 $field = new ChoiceFormField($node);
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');
244 $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked', 'value' => 'foo'));
245 $field = new ChoiceFormField($node);
247 $this->assertEquals('foo', $field->getValue(), '->getValue() returns the value attribute if the checkbox is checked');
249 $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked', 'value' => 'foo'));
250 $field = new ChoiceFormField($node);
252 $field->setValue(false);
253 $this->assertNull($field->getValue(), '->setValue() unchecks the checkbox is value is false');
255 $field->setValue(true);
256 $this->assertEquals('foo', $field->getValue(), '->setValue() checks the checkbox is value is true');
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');
266 public function testCheckboxWithEmptyBooleanAttribute()
268 $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'value' => 'foo', 'checked' => ''));
269 $field = new ChoiceFormField($node);
271 $this->assertTrue($field->hasValue(), '->hasValue() returns true when the checkbox is checked');
272 $this->assertEquals('foo', $field->getValue());
275 public function testTick()
277 $node = $this->createSelectNode(array('foo' => false, 'bar' => false));
278 $field = new ChoiceFormField($node);
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');
287 $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name'));
288 $field = new ChoiceFormField($node);
290 $this->assertEquals('on', $field->getValue(), '->tick() ticks checkboxes');
293 public function testUntick()
295 $node = $this->createSelectNode(array('foo' => false, 'bar' => false));
296 $field = new ChoiceFormField($node);
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');
305 $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked'));
306 $field = new ChoiceFormField($node);
308 $this->assertNull($field->getValue(), '->untick() unticks checkboxes');
311 public function testSelect()
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');
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');
326 public function testOptionWithNoValue()
328 $node = $this->createSelectNodeWithEmptyOption(array('foo' => false, 'bar' => false));
329 $field = new ChoiceFormField($node);
330 $this->assertEquals('foo', $field->getValue());
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');
339 public function testDisableValidation()
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.');
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.');
354 public function testSelectWithEmptyValue()
356 $node = $this->createSelectNodeWithEmptyOption(array('' => true, 'Female' => false, 'Male' => false));
357 $field = new ChoiceFormField($node);
359 $this->assertSame('', $field->getValue());
362 protected function createSelectNode($options, $attributes = array(), $selectedAttrText = 'selected')
364 $document = new \DOMDocument();
365 $node = $document->createElement('select');
367 foreach ($attributes as $name => $value) {
368 $node->setAttribute($name, $value);
370 $node->setAttribute('name', 'name');
372 foreach ($options as $value => $selected) {
373 $option = $document->createElement('option', $value);
374 $option->setAttribute('value', $value);
376 $option->setAttribute('selected', $selectedAttrText);
378 $node->appendChild($option);
384 protected function createSelectNodeWithEmptyOption($options, $attributes = array())
386 $document = new \DOMDocument();
387 $node = $document->createElement('select');
389 foreach ($attributes as $name => $value) {
390 $node->setAttribute($name, $value);
392 $node->setAttribute('name', 'name');
394 foreach ($options as $value => $selected) {
395 $option = $document->createElement('option', $value);
397 $option->setAttribute('selected', 'selected');
399 $node->appendChild($option);