ChoiceFormFieldTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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\Component\DomCrawler\Field;
  11. require_once __DIR__.'/FormFieldTestCase.php';
  12. use Symfony\Component\DomCrawler\Field\ChoiceFormField;
  13. class ChoiceFormFieldTest extends FormFieldTestCase
  14. {
  15. public function testInitialize()
  16. {
  17. $node = $this->createNode('textarea', '');
  18. try {
  19. $field = new ChoiceFormField($node);
  20. $this->fail('->initialize() throws a \LogicException if the node is not an input or a select');
  21. } catch (\LogicException $e) {
  22. $this->assertTrue(true, '->initialize() throws a \LogicException if the node is not an input or a select');
  23. }
  24. $node = $this->createNode('input', '', array('type' => 'text'));
  25. try {
  26. $field = new ChoiceFormField($node);
  27. $this->fail('->initialize() throws a \LogicException if the node is an input with a type different from checkbox or radio');
  28. } catch (\LogicException $e) {
  29. $this->assertTrue(true, '->initialize() throws a \LogicException if the node is an input with a type different from checkbox or radio');
  30. }
  31. }
  32. public function testGetType()
  33. {
  34. $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo'));
  35. $field = new ChoiceFormField($node);
  36. $this->assertEquals('radio', $field->getType(), '->getType() returns radio for radio buttons');
  37. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'value' => 'foo'));
  38. $field = new ChoiceFormField($node);
  39. $this->assertEquals('checkbox', $field->getType(), '->getType() returns radio for a checkbox');
  40. $node = $this->createNode('select', '');
  41. $field = new ChoiceFormField($node);
  42. $this->assertEquals('select', $field->getType(), '->getType() returns radio for a select');
  43. }
  44. public function testIsMultiple()
  45. {
  46. $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo'));
  47. $field = new ChoiceFormField($node);
  48. $this->assertEquals(false, $field->isMultiple(), '->isMultiple() returns false for radio buttons');
  49. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'value' => 'foo'));
  50. $field = new ChoiceFormField($node);
  51. $this->assertEquals(false, $field->isMultiple(), '->isMultiple() returns false for checkboxes');
  52. $node = $this->createNode('select', '');
  53. $field = new ChoiceFormField($node);
  54. $this->assertEquals(false, $field->isMultiple(), '->isMultiple() returns false for selects without the multiple attribute');
  55. $node = $this->createNode('select', '', array('multiple' => 'multiple'));
  56. $field = new ChoiceFormField($node);
  57. $this->assertEquals(true, $field->isMultiple(), '->isMultiple() returns true for selects with the multiple attribute');
  58. }
  59. public function testSelects()
  60. {
  61. $node = $this->createSelectNode(array('foo' => false, 'bar' => false));
  62. $field = new ChoiceFormField($node);
  63. $this->assertTrue($field->hasValue(), '->hasValue() returns true for selects');
  64. $this->assertEquals('foo', $field->getValue(), '->getValue() returns the first option if none are selected');
  65. $this->assertFalse($field->isMultiple(), '->isMultiple() returns false when no multiple attribute is defined');
  66. $node = $this->createSelectNode(array('foo' => false, 'bar' => true));
  67. $field = new ChoiceFormField($node);
  68. $this->assertEquals('bar', $field->getValue(), '->getValue() returns the selected option');
  69. $field->setValue('foo');
  70. $this->assertEquals('foo', $field->getValue(), '->setValue() changes the selected option');
  71. try {
  72. $field->setValue('foobar');
  73. $this->fail('->setValue() throws an \InvalidArgumentException if the value is not one of the selected options');
  74. } catch (\InvalidArgumentException $e) {
  75. $this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is not one of the selected options');
  76. }
  77. try {
  78. $field->setValue(array('foobar'));
  79. $this->fail('->setValue() throws an \InvalidArgumentException if the value is an array');
  80. } catch (\InvalidArgumentException $e) {
  81. $this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is an array');
  82. }
  83. }
  84. public function testMultipleSelects()
  85. {
  86. $node = $this->createSelectNode(array('foo' => false, 'bar' => false), array('multiple' => 'multiple'));
  87. $field = new ChoiceFormField($node);
  88. $this->assertEquals(array(), $field->getValue(), '->setValue() returns an empty array if multiple is true and no option is selected');
  89. $field->setValue('foo');
  90. $this->assertEquals(array('foo'), $field->getValue(), '->setValue() returns an array of options if multiple is true');
  91. $field->setValue('bar');
  92. $this->assertEquals(array('bar'), $field->getValue(), '->setValue() returns an array of options if multiple is true');
  93. $field->setValue(array('foo', 'bar'));
  94. $this->assertEquals(array('foo', 'bar'), $field->getValue(), '->setValue() returns an array of options if multiple is true');
  95. $node = $this->createSelectNode(array('foo' => true, 'bar' => true), array('multiple' => 'multiple'));
  96. $field = new ChoiceFormField($node);
  97. $this->assertEquals(array('foo', 'bar'), $field->getValue(), '->getValue() returns the selected options');
  98. try {
  99. $field->setValue(array('foobar'));
  100. $this->fail('->setValue() throws an \InvalidArgumentException if the value is not one of the options');
  101. } catch (\InvalidArgumentException $e) {
  102. $this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is not one of the options');
  103. }
  104. }
  105. public function testRadioButtons()
  106. {
  107. $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo'));
  108. $field = new ChoiceFormField($node);
  109. $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'bar'));
  110. $field->addChoice($node);
  111. $this->assertFalse($field->hasValue(), '->hasValue() returns false when no radio button is selected');
  112. $this->assertEquals(null, $field->getValue(), '->getValue() returns null if no radio button is selected');
  113. $this->assertFalse($field->isMultiple(), '->isMultiple() returns false for radio buttons');
  114. $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo'));
  115. $field = new ChoiceFormField($node);
  116. $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'bar', 'checked' => 'checked'));
  117. $field->addChoice($node);
  118. $this->assertTrue($field->hasValue(), '->hasValue() returns true when a radio button is selected');
  119. $this->assertEquals('bar', $field->getValue(), '->getValue() returns the value attribute of the selected radio button');
  120. $field->setValue('foo');
  121. $this->assertEquals('foo', $field->getValue(), '->setValue() changes the selected radio button');
  122. try {
  123. $field->setValue('foobar');
  124. $this->fail('->setValue() throws an \InvalidArgumentException if the value is not one of the radio button values');
  125. } catch (\InvalidArgumentException $e) {
  126. $this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is not one of the radio button values');
  127. }
  128. }
  129. public function testRadioButtonIsDisabled()
  130. {
  131. $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo', 'disabled' => 'disabled'));
  132. $field = new ChoiceFormField($node);
  133. $node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'bar'));
  134. $field->addChoice($node);
  135. $field->select('foo');
  136. $this->assertEquals('foo', $field->getValue(), '->getValue() returns the value attribute of the selected radio button');
  137. $this->assertTrue($field->isDisabled());
  138. $field->select('bar');
  139. $this->assertEquals('bar', $field->getValue(), '->getValue() returns the value attribute of the selected radio button');
  140. $this->assertFalse($field->isDisabled());
  141. }
  142. public function testCheckboxes()
  143. {
  144. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name'));
  145. $field = new ChoiceFormField($node);
  146. $this->assertFalse($field->hasValue(), '->hasValue() returns false when the checkbox is not checked');
  147. $this->assertEquals(null, $field->getValue(), '->getValue() returns null if the checkbox is not checked');
  148. $this->assertFalse($field->isMultiple(), '->hasValue() returns false for checkboxes');
  149. try {
  150. $field->addChoice(new \DOMNode());
  151. $this->fail('->addChoice() throws a \LogicException for checkboxes');
  152. } catch (\LogicException $e) {
  153. $this->assertTrue(true, '->initialize() throws a \LogicException for checkboxes');
  154. }
  155. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked'));
  156. $field = new ChoiceFormField($node);
  157. $this->assertTrue($field->hasValue(), '->hasValue() returns true when the checkbox is checked');
  158. $this->assertEquals('1', $field->getValue(), '->getValue() returns 1 if the checkbox is checked and has no value attribute');
  159. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked', 'value' => 'foo'));
  160. $field = new ChoiceFormField($node);
  161. $this->assertEquals('foo', $field->getValue(), '->getValue() returns the value attribute if the checkbox is checked');
  162. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked', 'value' => 'foo'));
  163. $field = new ChoiceFormField($node);
  164. $field->setValue(false);
  165. $this->assertEquals(null, $field->getValue(), '->setValue() unchecks the checkbox is value is false');
  166. $field->setValue(true);
  167. $this->assertEquals('foo', $field->getValue(), '->setValue() checks the checkbox is value is true');
  168. try {
  169. $field->setValue('bar');
  170. $this->fail('->setValue() throws an \InvalidArgumentException if the value is not one from the value attribute');
  171. } catch (\InvalidArgumentException $e) {
  172. $this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is not one from the value attribute');
  173. }
  174. }
  175. public function testTick()
  176. {
  177. $node = $this->createSelectNode(array('foo' => false, 'bar' => false));
  178. $field = new ChoiceFormField($node);
  179. try {
  180. $field->tick();
  181. $this->fail('->tick() throws a \LogicException for select boxes');
  182. } catch (\LogicException $e) {
  183. $this->assertTrue(true, '->tick() throws a \LogicException for select boxes');
  184. }
  185. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name'));
  186. $field = new ChoiceFormField($node);
  187. $field->tick();
  188. $this->assertEquals(1, $field->getValue(), '->tick() ticks checkboxes');
  189. }
  190. public function testUntick()
  191. {
  192. $node = $this->createSelectNode(array('foo' => false, 'bar' => false));
  193. $field = new ChoiceFormField($node);
  194. try {
  195. $field->untick();
  196. $this->fail('->untick() throws a \LogicException for select boxes');
  197. } catch (\LogicException $e) {
  198. $this->assertTrue(true, '->untick() throws a \LogicException for select boxes');
  199. }
  200. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked'));
  201. $field = new ChoiceFormField($node);
  202. $field->untick();
  203. $this->assertEquals(null, $field->getValue(), '->untick() unticks checkoxes');
  204. }
  205. public function testSelect()
  206. {
  207. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked'));
  208. $field = new ChoiceFormField($node);
  209. $field->select(true);
  210. $this->assertEquals(1, $field->getValue(), '->select() changes the value of the field');
  211. $field->select(false);
  212. $this->assertEquals(null, $field->getValue(), '->select() changes the value of the field');
  213. $node = $this->createSelectNode(array('foo' => false, 'bar' => false));
  214. $field = new ChoiceFormField($node);
  215. $field->select('foo');
  216. $this->assertEquals('foo', $field->getValue(), '->select() changes the selected option');
  217. }
  218. public function testOptionWithNoValue()
  219. {
  220. $node = $this->createSelectNodeWithEmptyOption(array('foo' => false, 'bar' => false));
  221. $field = new ChoiceFormField($node);
  222. $field->select('foo');
  223. $this->assertEquals('foo', $field->getValue(), '->select() changes the selected option');
  224. }
  225. protected function createSelectNode($options, $attributes = array())
  226. {
  227. $document = new \DOMDocument();
  228. $node = $document->createElement('select');
  229. foreach ($attributes as $name => $value) {
  230. $node->setAttribute($name, $value);
  231. }
  232. $node->setAttribute('name', 'name');
  233. foreach ($options as $value => $selected) {
  234. $option = $document->createElement('option', $value);
  235. $option->setAttribute('value', $value);
  236. if ($selected) {
  237. $option->setAttribute('selected', 'selected');
  238. }
  239. $node->appendChild($option);
  240. }
  241. return $node;
  242. }
  243. protected function createSelectNodeWithEmptyOption($options, $attributes = array())
  244. {
  245. $document = new \DOMDocument();
  246. $node = $document->createElement('select');
  247. foreach ($attributes as $name => $value) {
  248. $node->setAttribute($name, $value);
  249. }
  250. $node->setAttribute('name', 'name');
  251. foreach ($options as $value => $selected) {
  252. $option = $document->createElement('option', $value);
  253. if ($selected) {
  254. $option->setAttribute('selected', 'selected');
  255. }
  256. $node->appendChild($option);
  257. }
  258. return $node;
  259. }
  260. }