ChoiceFieldTest.php 12 KB

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