ChoiceFieldTest.php 14 KB

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