ChoiceFieldTest.php 15 KB

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