ChoiceFormFieldTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Components\DomCrawler\Field;
  11. require_once __DIR__.'/FormFieldTestCase.php';
  12. use Symfony\Components\DomCrawler\Field\ChoiceFormField;
  13. class ChoiceFormFieldTest extends FormFieldTestCase
  14. {
  15. public function testInitialize()
  16. {
  17. $node = $this->createNode('textarea', '');
  18. try
  19. {
  20. $field = new ChoiceFormField($node);
  21. $this->fail('->initialize() throws a \LogicException if the node is not an input or a select');
  22. }
  23. catch (\LogicException $e)
  24. {
  25. $this->assertTrue(true, '->initialize() throws a \LogicException if the node is not an input or a select');
  26. }
  27. $node = $this->createNode('input', '', array('type' => 'text'));
  28. try
  29. {
  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. }
  33. catch (\LogicException $e)
  34. {
  35. $this->assertTrue(true, '->initialize() throws a \LogicException if the node is an input with a type different from checkbox or radio');
  36. }
  37. }
  38. public function testGetType()
  39. {
  40. $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo'));
  41. $field = new ChoiceFormField($node);
  42. $this->assertEquals('radio', $field->getType(), '->getType() returns radio for radio buttons');
  43. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'value' => 'foo'));
  44. $field = new ChoiceFormField($node);
  45. $this->assertEquals('checkbox', $field->getType(), '->getType() returns radio for a checkbox');
  46. $node = $this->createNode('select', '');
  47. $field = new ChoiceFormField($node);
  48. $this->assertEquals('select', $field->getType(), '->getType() returns radio for a select');
  49. }
  50. public function testIsMultiple()
  51. {
  52. $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo'));
  53. $field = new ChoiceFormField($node);
  54. $this->assertEquals(false, $field->isMultiple(), '->isMultiple() returns false for radio buttons');
  55. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'value' => 'foo'));
  56. $field = new ChoiceFormField($node);
  57. $this->assertEquals(false, $field->isMultiple(), '->isMultiple() returns false for checkboxes');
  58. $node = $this->createNode('select', '');
  59. $field = new ChoiceFormField($node);
  60. $this->assertEquals(false, $field->isMultiple(), '->isMultiple() returns false for selects without the multiple attribute');
  61. $node = $this->createNode('select', '', array('multiple' => 'multiple'));
  62. $field = new ChoiceFormField($node);
  63. $this->assertEquals(true, $field->isMultiple(), '->isMultiple() returns true for selects with the multiple attribute');
  64. }
  65. public function testSelects()
  66. {
  67. $node = $this->createSelectNode(array('foo' => false, 'bar' => false));
  68. $field = new ChoiceFormField($node);
  69. $this->assertTrue($field->hasValue(), '->hasValue() returns true for selects');
  70. $this->assertEquals('foo', $field->getValue(), '->getValue() returns the first option if none are selected');
  71. $this->assertFalse($field->isMultiple(), '->isMultiple() returns false when no multiple attribute is defined');
  72. $node = $this->createSelectNode(array('foo' => false, 'bar' => true));
  73. $field = new ChoiceFormField($node);
  74. $this->assertEquals('bar', $field->getValue(), '->getValue() returns the selected option');
  75. $field->setValue('foo');
  76. $this->assertEquals('foo', $field->getValue(), '->setValue() changes the selected option');
  77. try
  78. {
  79. $field->setValue('foobar');
  80. $this->fail('->setValue() throws an \InvalidArgumentException if the value is not one of the selected options');
  81. }
  82. catch (\InvalidArgumentException $e)
  83. {
  84. $this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is not one of the selected options');
  85. }
  86. try
  87. {
  88. $field->setValue(array('foobar'));
  89. $this->fail('->setValue() throws an \InvalidArgumentException if the value is an array');
  90. }
  91. catch (\InvalidArgumentException $e)
  92. {
  93. $this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is an array');
  94. }
  95. }
  96. public function testMultipleSelects()
  97. {
  98. $node = $this->createSelectNode(array('foo' => false, 'bar' => false), array('multiple' => 'multiple'));
  99. $field = new ChoiceFormField($node);
  100. $this->assertEquals(array(), $field->getValue(), '->setValue() returns an empty array if multiple is true and no option is selected');
  101. $field->setValue('foo');
  102. $this->assertEquals(array('foo'), $field->getValue(), '->setValue() returns an array of options if multiple is true');
  103. $field->setValue('bar');
  104. $this->assertEquals(array('bar'), $field->getValue(), '->setValue() returns an array of options if multiple is true');
  105. $field->setValue(array('foo', 'bar'));
  106. $this->assertEquals(array('foo', 'bar'), $field->getValue(), '->setValue() returns an array of options if multiple is true');
  107. $node = $this->createSelectNode(array('foo' => true, 'bar' => true), array('multiple' => 'multiple'));
  108. $field = new ChoiceFormField($node);
  109. $this->assertEquals(array('foo', 'bar'), $field->getValue(), '->getValue() returns the selected options');
  110. try
  111. {
  112. $field->setValue(array('foobar'));
  113. $this->fail('->setValue() throws an \InvalidArgumentException if the value is not one of the options');
  114. }
  115. catch (\InvalidArgumentException $e)
  116. {
  117. $this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is not one of the options');
  118. }
  119. }
  120. public function testRadioButtons()
  121. {
  122. $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo'));
  123. $field = new ChoiceFormField($node);
  124. $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'bar'));
  125. $field->addChoice($node);
  126. $this->assertFalse($field->hasValue(), '->hasValue() returns false when no radio button is selected');
  127. $this->assertEquals(null, $field->getValue(), '->getValue() returns null if no radio button is selected');
  128. $this->assertFalse($field->isMultiple(), '->isMultiple() returns false for radio buttons');
  129. $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo'));
  130. $field = new ChoiceFormField($node);
  131. $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'bar', 'checked' => 'checked'));
  132. $field->addChoice($node);
  133. $this->assertTrue($field->hasValue(), '->hasValue() returns true when a radio button is selected');
  134. $this->assertEquals('bar', $field->getValue(), '->getValue() returns the value attribute of the selected radio button');
  135. $field->setValue('foo');
  136. $this->assertEquals('foo', $field->getValue(), '->setValue() changes the selected radio button');
  137. try
  138. {
  139. $field->setValue('foobar');
  140. $this->fail('->setValue() throws an \InvalidArgumentException if the value is not one of the radio button values');
  141. }
  142. catch (\InvalidArgumentException $e)
  143. {
  144. $this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is not one of the radio button values');
  145. }
  146. }
  147. public function testCheckboxes()
  148. {
  149. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name'));
  150. $field = new ChoiceFormField($node);
  151. $this->assertFalse($field->hasValue(), '->hasValue() returns false when the checkbox is not checked');
  152. $this->assertEquals(null, $field->getValue(), '->getValue() returns null if the checkbox is not checked');
  153. $this->assertFalse($field->isMultiple(), '->hasValue() returns false for checkboxes');
  154. try
  155. {
  156. $field->addChoice(new \DOMNode());
  157. $this->fail('->addChoice() throws a \LogicException for checkboxes');
  158. }
  159. catch (\LogicException $e)
  160. {
  161. $this->assertTrue(true, '->initialize() throws a \LogicException for checkboxes');
  162. }
  163. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked'));
  164. $field = new ChoiceFormField($node);
  165. $this->assertTrue($field->hasValue(), '->hasValue() returns true when the checkbox is checked');
  166. $this->assertEquals('1', $field->getValue(), '->getValue() returns 1 if the checkbox is checked and has no value attribute');
  167. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked', 'value' => 'foo'));
  168. $field = new ChoiceFormField($node);
  169. $this->assertEquals('foo', $field->getValue(), '->getValue() returns the value attribute if the checkbox is checked');
  170. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked', 'value' => 'foo'));
  171. $field = new ChoiceFormField($node);
  172. $field->setValue(false);
  173. $this->assertEquals(null, $field->getValue(), '->setValue() unchecks the checkbox is value is false');
  174. $field->setValue(true);
  175. $this->assertEquals('foo', $field->getValue(), '->setValue() checks the checkbox is value is true');
  176. try
  177. {
  178. $field->setValue('bar');
  179. $this->fail('->setValue() throws an \InvalidArgumentException if the value is not one from the value attribute');
  180. }
  181. catch (\InvalidArgumentException $e)
  182. {
  183. $this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is not one from the value attribute');
  184. }
  185. }
  186. protected function createSelectNode($options, $attributes = array())
  187. {
  188. $document = new \DOMDocument();
  189. $node = $document->createElement('select');
  190. foreach ($attributes as $name => $value)
  191. {
  192. $node->setAttribute($name, $value);
  193. }
  194. $node->setAttribute('name', 'name');
  195. foreach ($options as $value => $selected)
  196. {
  197. $option = $document->createElement('option', $value);
  198. $option->setAttribute('value', $value);
  199. if ($selected)
  200. {
  201. $option->setAttribute('selected', 'selected');
  202. }
  203. $node->appendChild($option);
  204. }
  205. return $node;
  206. }
  207. }