PageRenderTime 32ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/Symfony/Tests/Component/DomCrawler/Field/ChoiceFormFieldTest.php

https://github.com/thewiredman/symfony
PHP | 286 lines | 215 code | 63 blank | 8 comment | 1 complexity | 083773c029b8f1bce6962e9048110dcc MD5 | raw file
  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\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 testCheckboxes()
  130. {
  131. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name'));
  132. $field = new ChoiceFormField($node);
  133. $this->assertFalse($field->hasValue(), '->hasValue() returns false when the checkbox is not checked');
  134. $this->assertEquals(null, $field->getValue(), '->getValue() returns null if the checkbox is not checked');
  135. $this->assertFalse($field->isMultiple(), '->hasValue() returns false for checkboxes');
  136. try {
  137. $field->addChoice(new \DOMNode());
  138. $this->fail('->addChoice() throws a \LogicException for checkboxes');
  139. } catch (\LogicException $e) {
  140. $this->assertTrue(true, '->initialize() throws a \LogicException for checkboxes');
  141. }
  142. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked'));
  143. $field = new ChoiceFormField($node);
  144. $this->assertTrue($field->hasValue(), '->hasValue() returns true when the checkbox is checked');
  145. $this->assertEquals('1', $field->getValue(), '->getValue() returns 1 if the checkbox is checked and has no value attribute');
  146. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked', 'value' => 'foo'));
  147. $field = new ChoiceFormField($node);
  148. $this->assertEquals('foo', $field->getValue(), '->getValue() returns the value attribute if the checkbox is checked');
  149. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked', 'value' => 'foo'));
  150. $field = new ChoiceFormField($node);
  151. $field->setValue(false);
  152. $this->assertEquals(null, $field->getValue(), '->setValue() unchecks the checkbox is value is false');
  153. $field->setValue(true);
  154. $this->assertEquals('foo', $field->getValue(), '->setValue() checks the checkbox is value is true');
  155. try {
  156. $field->setValue('bar');
  157. $this->fail('->setValue() throws an \InvalidArgumentException if the value is not one from the value attribute');
  158. } catch (\InvalidArgumentException $e) {
  159. $this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is not one from the value attribute');
  160. }
  161. }
  162. public function testTick()
  163. {
  164. $node = $this->createSelectNode(array('foo' => false, 'bar' => false));
  165. $field = new ChoiceFormField($node);
  166. try {
  167. $field->tick();
  168. $this->fail('->tick() throws a \LogicException for select boxes');
  169. } catch (\LogicException $e) {
  170. $this->assertTrue(true, '->tick() throws a \LogicException for select boxes');
  171. }
  172. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name'));
  173. $field = new ChoiceFormField($node);
  174. $field->tick();
  175. $this->assertEquals(1, $field->getValue(), '->tick() ticks checkoxes');
  176. }
  177. public function testUntick()
  178. {
  179. $node = $this->createSelectNode(array('foo' => false, 'bar' => false));
  180. $field = new ChoiceFormField($node);
  181. try {
  182. $field->untick();
  183. $this->fail('->untick() throws a \LogicException for select boxes');
  184. } catch (\LogicException $e) {
  185. $this->assertTrue(true, '->untick() throws a \LogicException for select boxes');
  186. }
  187. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked'));
  188. $field = new ChoiceFormField($node);
  189. $field->untick();
  190. $this->assertEquals(null, $field->getValue(), '->untick() unticks checkoxes');
  191. }
  192. public function testSelect()
  193. {
  194. $node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked'));
  195. $field = new ChoiceFormField($node);
  196. $field->select(true);
  197. $this->assertEquals(1, $field->getValue(), '->select() changes the value of the field');
  198. $field->select(false);
  199. $this->assertEquals(null, $field->getValue(), '->select() changes the value of the field');
  200. $node = $this->createSelectNode(array('foo' => false, 'bar' => false));
  201. $field = new ChoiceFormField($node);
  202. $field->select('foo');
  203. $this->assertEquals('foo', $field->getValue(), '->select() changes the selected option');
  204. }
  205. protected function createSelectNode($options, $attributes = array())
  206. {
  207. $document = new \DOMDocument();
  208. $node = $document->createElement('select');
  209. foreach ($attributes as $name => $value) {
  210. $node->setAttribute($name, $value);
  211. }
  212. $node->setAttribute('name', 'name');
  213. foreach ($options as $value => $selected) {
  214. $option = $document->createElement('option', $value);
  215. $option->setAttribute('value', $value);
  216. if ($selected) {
  217. $option->setAttribute('selected', 'selected');
  218. }
  219. $node->appendChild($option);
  220. }
  221. return $node;
  222. }
  223. }