ChoiceFieldTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. namespace Symfony\Tests\Component\Form;
  3. use Symfony\Component\Form\ChoiceField;
  4. class ChoiceFieldTest extends \PHPUnit_Framework_TestCase
  5. {
  6. protected $choices = array(
  7. 'a' => 'Bernhard',
  8. 'b' => 'Fabien',
  9. 'c' => 'Kris',
  10. 'd' => 'Jon',
  11. 'e' => 'Roman',
  12. );
  13. protected $preferredChoices = array('d', 'e');
  14. protected $groupedChoices = array(
  15. 'Symfony' => array(
  16. 'a' => 'Bernhard',
  17. 'b' => 'Fabien',
  18. 'c' => 'Kris',
  19. ),
  20. 'Doctrine' => array(
  21. 'd' => 'Jon',
  22. 'e' => 'Roman',
  23. )
  24. );
  25. public function testConfigureChoicesWithArrayObject()
  26. {
  27. $choices = new \ArrayObject($this->choices);
  28. $field = new ChoiceField('name', array(
  29. 'multiple' => false,
  30. 'expanded' => true,
  31. 'choices' => $choices,
  32. 'preferred_choices' => $this->preferredChoices,
  33. ));
  34. $this->assertEquals($this->choices, $choices->getArrayCopy());
  35. }
  36. public function testBindSingleNonExpanded()
  37. {
  38. $field = new ChoiceField('name', array(
  39. 'multiple' => false,
  40. 'expanded' => false,
  41. 'choices' => $this->choices,
  42. ));
  43. $field->bind('b');
  44. $this->assertEquals('b', $field->getData());
  45. $this->assertEquals('b', $field->getDisplayedData());
  46. }
  47. public function testRenderSingleNonExpanded()
  48. {
  49. $field = new ChoiceField('name', array(
  50. 'multiple' => false,
  51. 'expanded' => false,
  52. 'choices' => $this->choices,
  53. ));
  54. $field->setData('b');
  55. $html = <<<EOF
  56. <select id="name" name="name" class="foobar">
  57. <option value="a">Bernhard</option>
  58. <option value="b" selected="selected">Fabien</option>
  59. <option value="c">Kris</option>
  60. <option value="d">Jon</option>
  61. <option value="e">Roman</option>
  62. </select>
  63. EOF;
  64. $this->assertEquals($html, $field->render(array(
  65. 'class' => 'foobar',
  66. )));
  67. }
  68. public function testRenderSingleNonExpanded_translateChoices()
  69. {
  70. $translator = $this->getMock('Symfony\Component\I18N\TranslatorInterface');
  71. $translator->expects($this->any())
  72. ->method('translate')
  73. ->will($this->returnCallback(function($text) {
  74. return 'translated['.$text.']';
  75. }));
  76. $field = new ChoiceField('name', array(
  77. 'multiple' => false,
  78. 'expanded' => false,
  79. 'choices' => $this->choices,
  80. 'translate_choices' => true,
  81. ));
  82. $field->setTranslator($translator);
  83. $field->setData('b');
  84. $html = <<<EOF
  85. <select id="name" name="name" class="foobar">
  86. <option value="a">translated[Bernhard]</option>
  87. <option value="b" selected="selected">translated[Fabien]</option>
  88. <option value="c">translated[Kris]</option>
  89. <option value="d">translated[Jon]</option>
  90. <option value="e">translated[Roman]</option>
  91. </select>
  92. EOF;
  93. $this->assertEquals($html, $field->render(array(
  94. 'class' => 'foobar',
  95. )));
  96. }
  97. public function testRenderSingleNonExpanded_disabled()
  98. {
  99. $field = new ChoiceField('name', array(
  100. 'multiple' => false,
  101. 'expanded' => false,
  102. 'choices' => $this->choices,
  103. 'disabled' => true,
  104. ));
  105. $html = <<<EOF
  106. <select id="name" name="name" disabled="disabled">
  107. <option value="a">Bernhard</option>
  108. <option value="b">Fabien</option>
  109. <option value="c">Kris</option>
  110. <option value="d">Jon</option>
  111. <option value="e">Roman</option>
  112. </select>
  113. EOF;
  114. $this->assertEquals($html, $field->render());
  115. }
  116. public function testRenderSingleNonExpandedWithPreferred()
  117. {
  118. $field = new ChoiceField('name', array(
  119. 'multiple' => false,
  120. 'expanded' => false,
  121. 'choices' => $this->choices,
  122. 'preferred_choices' => $this->preferredChoices,
  123. 'separator' => '---',
  124. ));
  125. $field->setData('d');
  126. $html = <<<EOF
  127. <select id="name" name="name">
  128. <option value="d" selected="selected">Jon</option>
  129. <option value="e">Roman</option>
  130. <option disabled="disabled">---</option>
  131. <option value="a">Bernhard</option>
  132. <option value="b">Fabien</option>
  133. <option value="c">Kris</option>
  134. </select>
  135. EOF;
  136. $this->assertEquals($html, $field->render());
  137. }
  138. public function testRenderSingleNonExpandedWithGroups()
  139. {
  140. $field = new ChoiceField('name', array(
  141. 'multiple' => false,
  142. 'expanded' => false,
  143. 'choices' => $this->groupedChoices,
  144. ));
  145. $html = <<<EOF
  146. <select id="name" name="name">
  147. <optgroup label="Symfony">
  148. <option value="a">Bernhard</option>
  149. <option value="b">Fabien</option>
  150. <option value="c">Kris</option>
  151. </optgroup>
  152. <optgroup label="Doctrine">
  153. <option value="d">Jon</option>
  154. <option value="e">Roman</option>
  155. </optgroup>
  156. </select>
  157. EOF;
  158. $this->assertEquals($html, $field->render());
  159. }
  160. public function testRenderSingleNonExpandedNonRequired()
  161. {
  162. $field = new ChoiceField('name', array(
  163. 'multiple' => false,
  164. 'expanded' => false,
  165. 'choices' => $this->choices,
  166. 'empty_value' => 'empty',
  167. ));
  168. $field->setData(null);
  169. $field->setRequired(false);
  170. $html = <<<EOF
  171. <select id="name" name="name">
  172. <option value="" selected="selected">empty</option>
  173. <option value="a">Bernhard</option>
  174. <option value="b">Fabien</option>
  175. <option value="c">Kris</option>
  176. <option value="d">Jon</option>
  177. <option value="e">Roman</option>
  178. </select>
  179. EOF;
  180. $this->assertEquals($html, $field->render());
  181. }
  182. public function testBindMultipleNonExpanded()
  183. {
  184. $field = new ChoiceField('name', array(
  185. 'multiple' => true,
  186. 'expanded' => false,
  187. 'choices' => $this->choices,
  188. ));
  189. $field->bind(array('a', 'b'));
  190. $this->assertEquals(array('a', 'b'), $field->getData());
  191. $this->assertEquals(array('a', 'b'), $field->getDisplayedData());
  192. }
  193. public function testRenderMultipleNonExpanded()
  194. {
  195. $field = new ChoiceField('name', array(
  196. 'multiple' => true,
  197. 'expanded' => false,
  198. 'choices' => $this->choices,
  199. ));
  200. $field->setData(array('a', 'b'));
  201. $html = <<<EOF
  202. <select id="name" name="name[]" multiple="multiple">
  203. <option value="a" selected="selected">Bernhard</option>
  204. <option value="b" selected="selected">Fabien</option>
  205. <option value="c">Kris</option>
  206. <option value="d">Jon</option>
  207. <option value="e">Roman</option>
  208. </select>
  209. EOF;
  210. $this->assertEquals($html, $field->render());
  211. }
  212. public function testBindSingleExpanded()
  213. {
  214. $field = new ChoiceField('name', array(
  215. 'multiple' => true,
  216. 'expanded' => false,
  217. 'choices' => $this->choices,
  218. ));
  219. $field->bind('b');
  220. $this->assertEquals('b', $field->getData());
  221. $this->assertEquals('b', $field->getDisplayedData());
  222. }
  223. public function testRenderSingleExpanded()
  224. {
  225. $field = new ChoiceField('name', array(
  226. 'multiple' => false,
  227. 'expanded' => true,
  228. 'choices' => $this->choices,
  229. ));
  230. $field->setData('b');
  231. $html = <<<EOF
  232. <input id="name_a" name="name" value="a" type="radio" /> <label for="name_a">Bernhard</label>
  233. <input id="name_b" name="name" value="b" checked="checked" type="radio" /> <label for="name_b">Fabien</label>
  234. <input id="name_c" name="name" value="c" type="radio" /> <label for="name_c">Kris</label>
  235. <input id="name_d" name="name" value="d" type="radio" /> <label for="name_d">Jon</label>
  236. <input id="name_e" name="name" value="e" type="radio" /> <label for="name_e">Roman</label>
  237. EOF;
  238. $this->assertEquals($html, $field->render());
  239. }
  240. public function testRenderSingleExpanded_translateChoices()
  241. {
  242. $translator = $this->getMock('Symfony\Component\I18N\TranslatorInterface');
  243. $translator->expects($this->any())
  244. ->method('translate')
  245. ->will($this->returnCallback(function($text) {
  246. return 'translated['.$text.']';
  247. }));
  248. $field = new ChoiceField('name', array(
  249. 'multiple' => false,
  250. 'expanded' => true,
  251. 'choices' => $this->choices,
  252. 'translate_choices' => true,
  253. ));
  254. $field->setTranslator($translator);
  255. $field->setData('b');
  256. $html = <<<EOF
  257. <input id="name_a" name="name" value="a" type="radio" /> <label for="name_a">translated[Bernhard]</label>
  258. <input id="name_b" name="name" value="b" checked="checked" type="radio" /> <label for="name_b">translated[Fabien]</label>
  259. <input id="name_c" name="name" value="c" type="radio" /> <label for="name_c">translated[Kris]</label>
  260. <input id="name_d" name="name" value="d" type="radio" /> <label for="name_d">translated[Jon]</label>
  261. <input id="name_e" name="name" value="e" type="radio" /> <label for="name_e">translated[Roman]</label>
  262. EOF;
  263. $this->assertEquals($html, $field->render());
  264. }
  265. public function testRenderSingleExpandedWithPreferred()
  266. {
  267. $field = new ChoiceField('name', array(
  268. 'multiple' => false,
  269. 'expanded' => true,
  270. 'choices' => $this->choices,
  271. 'preferred_choices' => $this->preferredChoices,
  272. ));
  273. $html = <<<EOF
  274. <input id="name_d" name="name" value="d" type="radio" /> <label for="name_d">Jon</label>
  275. <input id="name_e" name="name" value="e" type="radio" /> <label for="name_e">Roman</label>
  276. <input id="name_a" name="name" value="a" type="radio" /> <label for="name_a">Bernhard</label>
  277. <input id="name_b" name="name" value="b" type="radio" /> <label for="name_b">Fabien</label>
  278. <input id="name_c" name="name" value="c" type="radio" /> <label for="name_c">Kris</label>
  279. EOF;
  280. $this->assertEquals($html, $field->render());
  281. }
  282. public function testBindMultipleExpanded()
  283. {
  284. $field = new ChoiceField('name', array(
  285. 'multiple' => true,
  286. 'expanded' => true,
  287. 'choices' => $this->choices,
  288. ));
  289. $field->bind(array('a' => 'a', 'b' => 'b'));
  290. $this->assertSame(array('a', 'b'), $field->getData());
  291. $this->assertSame(true, $field['a']->getData());
  292. $this->assertSame(true, $field['b']->getData());
  293. $this->assertSame(null, $field['c']->getData());
  294. $this->assertSame(null, $field['d']->getData());
  295. $this->assertSame(null, $field['e']->getData());
  296. $this->assertSame('1', $field['a']->getDisplayedData());
  297. $this->assertSame('1', $field['b']->getDisplayedData());
  298. $this->assertSame('', $field['c']->getDisplayedData());
  299. $this->assertSame('', $field['d']->getDisplayedData());
  300. $this->assertSame('', $field['e']->getDisplayedData());
  301. $this->assertSame(array('a' => '1', 'b' => '1', 'c' => '', 'd' => '', 'e' => ''), $field->getDisplayedData());
  302. }
  303. public function testRenderMultipleExpanded()
  304. {
  305. $field = new ChoiceField('name', array(
  306. 'multiple' => true,
  307. 'expanded' => true,
  308. 'choices' => $this->choices,
  309. ));
  310. $field->setData(array('a', 'b'));
  311. $html = <<<EOF
  312. <input id="name_a" name="name[a]" value="a" checked="checked" type="checkbox" /> <label for="name_a">Bernhard</label>
  313. <input id="name_b" name="name[b]" value="b" checked="checked" type="checkbox" /> <label for="name_b">Fabien</label>
  314. <input id="name_c" name="name[c]" value="c" type="checkbox" /> <label for="name_c">Kris</label>
  315. <input id="name_d" name="name[d]" value="d" type="checkbox" /> <label for="name_d">Jon</label>
  316. <input id="name_e" name="name[e]" value="e" type="checkbox" /> <label for="name_e">Roman</label>
  317. EOF;
  318. $this->assertEquals($html, $field->render());
  319. }
  320. }