@@ -26,6 +26,12 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
2626 'Roman ' => 'e ' ,
2727 );
2828
29+ private $ scalarChoices = array (
30+ 'Yes ' => true ,
31+ 'No ' => false ,
32+ 'n/a ' => '' ,
33+ );
34+
2935 private $ numericChoicesFlipped = array (
3036 0 => 'Bernhard ' ,
3137 1 => 'Fabien ' ,
@@ -139,6 +145,58 @@ public function testExpandedFlippedChoicesOptionsTurnIntoChildren()
139145 $ this ->assertCount (count ($ this ->choices ), $ form , 'Each choice should become a new field ' );
140146 }
141147
148+ public function testChoiceListWithScalarValues ()
149+ {
150+ $ view = $ this ->factory ->create ('choice ' , null , array (
151+ 'choices ' => $ this ->scalarChoices ,
152+ 'choices_as_values ' => true ,
153+ ))->createView ();
154+
155+ $ this ->assertSame ('1 ' , $ view ->vars ['choices ' ][0 ]->value );
156+ $ this ->assertSame ('0 ' , $ view ->vars ['choices ' ][1 ]->value );
157+ $ this ->assertSame ('' , $ view ->vars ['choices ' ][2 ]->value );
158+ $ this ->assertFalse ($ view ->vars ['is_selected ' ]($ view ->vars ['choices ' ][0 ], $ view ->vars ['value ' ]), 'True value should not be pre selected ' );
159+ $ this ->assertFalse ($ view ->vars ['is_selected ' ]($ view ->vars ['choices ' ][1 ], $ view ->vars ['value ' ]), 'False value should not be pre selected ' );
160+ $ this ->assertFalse ($ view ->vars ['is_selected ' ]($ view ->vars ['choices ' ][2 ], $ view ->vars ['value ' ]), 'Empty value should not be pre selected ' );
161+ }
162+
163+ public function testChoiceListWithScalarValuesAndFalseAsPreSetData ()
164+ {
165+ $ view = $ this ->factory ->create ('choice ' , false , array (
166+ 'choices ' => $ this ->scalarChoices ,
167+ 'choices_as_values ' => true ,
168+ ))->createView ();
169+
170+ $ this ->assertTrue ($ view ->vars ['is_selected ' ]($ view ->vars ['choices ' ][1 ]->value , $ view ->vars ['value ' ]), 'False value should be pre selected ' );
171+ }
172+
173+ public function testExpandedChoiceListWithScalarValues ()
174+ {
175+ $ view = $ this ->factory ->create ('choice ' , null , array (
176+ 'choices ' => $ this ->scalarChoices ,
177+ 'choices_as_values ' => true ,
178+ 'expanded ' => true ,
179+ ))->createView ();
180+
181+ $ this ->assertFalse ($ view ->children [0 ]->vars ['checked ' ], 'True value should not be pre selected ' );
182+ $ this ->assertFalse ($ view ->children [1 ]->vars ['checked ' ], 'False value should not be pre selected ' );
183+ $ this ->assertTrue ($ view ->children [2 ]->vars ['checked ' ], 'Empty value should be pre selected ' );
184+ }
185+
186+ public function testExpandedChoiceListWithScalarValuesAndFalseAsPreSetData ()
187+ {
188+ $ view = $ this ->factory ->create ('choice ' , false , array (
189+ 'choices ' => $ this ->scalarChoices ,
190+ 'choices_as_values ' => true ,
191+ 'expanded ' => true ,
192+ ))->createView ();
193+
194+ $ this ->assertSame ('1 ' , $ view ->vars ['choices ' ][0 ]->value );
195+ $ this ->assertSame ('0 ' , $ view ->vars ['choices ' ][1 ]->value );
196+ $ this ->assertTrue ($ view ->children [1 ]->vars ['checked ' ], 'False value should be pre selected ' );
197+ $ this ->assertFalse ($ view ->children [2 ]->vars ['checked ' ], 'Empty value should not be pre selected ' );
198+ }
199+
142200 public function testPlaceholderPresentOnNonRequiredExpandedSingleChoice ()
143201 {
144202 $ form = $ this ->factory ->create ('choice ' , null , array (
@@ -198,6 +256,100 @@ public function testPlaceholderNotPresentIfEmptyChoice()
198256 $ this ->assertCount (2 , $ form , 'Each choice should become a new field ' );
199257 }
200258
259+ public function testPlaceholderWithBooleanChoices ()
260+ {
261+ $ form = $ this ->factory ->create ('choice ' , null , array (
262+ 'multiple ' => false ,
263+ 'expanded ' => false ,
264+ 'required ' => false ,
265+ 'choices ' => array (
266+ 'Yes ' => true ,
267+ 'No ' => false ,
268+ ),
269+ 'placeholder ' => 'Select an option ' ,
270+ 'choices_as_values ' => true ,
271+ ));
272+
273+ $ view = $ form ->createView ();
274+
275+ $ this ->assertSame ('' , $ view ->vars ['value ' ], 'Value should be empty ' );
276+ $ this ->assertSame ('1 ' , $ view ->vars ['choices ' ][0 ]->value );
277+ $ this ->assertSame ('0 ' , $ view ->vars ['choices ' ][1 ]->value , 'Choice "false" should have "0" as value ' );
278+ $ this ->assertFalse ($ view ->vars ['is_selected ' ]($ view ->vars ['choices ' ][1 ]->value , $ view ->vars ['value ' ]), 'Choice "false" should not be selected ' );
279+ }
280+
281+ public function testPlaceholderWithBooleanChoicesWithFalseAsPreSetData ()
282+ {
283+ $ form = $ this ->factory ->create ('choice ' , false , array (
284+ 'multiple ' => false ,
285+ 'expanded ' => false ,
286+ 'required ' => false ,
287+ 'choices ' => array (
288+ 'Yes ' => true ,
289+ 'No ' => false ,
290+ ),
291+ 'placeholder ' => 'Select an option ' ,
292+ 'choices_as_values ' => true ,
293+ ));
294+
295+ $ view = $ form ->createView ();
296+
297+ $ this ->assertSame ('0 ' , $ view ->vars ['value ' ], 'Value should be "0" ' );
298+ $ this ->assertSame ('1 ' , $ view ->vars ['choices ' ][0 ]->value );
299+ $ this ->assertSame ('0 ' , $ view ->vars ['choices ' ][1 ]->value , 'Choice "false" should have "0" as value ' );
300+ $ this ->assertTrue ($ view ->vars ['is_selected ' ]($ view ->vars ['choices ' ][1 ]->value , $ view ->vars ['value ' ]), 'Choice "false" should be selected ' );
301+ }
302+
303+ public function testPlaceholderWithExpandedBooleanChoices ()
304+ {
305+ $ form = $ this ->factory ->create ('choice ' , null , array (
306+ 'multiple ' => false ,
307+ 'expanded ' => true ,
308+ 'required ' => false ,
309+ 'choices ' => array (
310+ 'Yes ' => true ,
311+ 'No ' => false ,
312+ ),
313+ 'placeholder ' => 'Select an option ' ,
314+ 'choices_as_values ' => true ,
315+ ));
316+
317+ $ this ->assertTrue (isset ($ form ['placeholder ' ]), 'Placeholder should be set ' );
318+ $ this ->assertCount (3 , $ form , 'Each choice should become a new field, placeholder included ' );
319+
320+ $ view = $ form ->createView ();
321+
322+ $ this ->assertSame ('' , $ view ->vars ['value ' ], 'Value should be empty ' );
323+ $ this ->assertSame ('1 ' , $ view ->vars ['choices ' ][0 ]->value );
324+ $ this ->assertSame ('0 ' , $ view ->vars ['choices ' ][1 ]->value , 'Choice "false" should have "0" as value ' );
325+ $ this ->assertFalse ($ view ->children [1 ]->vars ['checked ' ], 'Choice "false" should not be selected ' );
326+ }
327+
328+ public function testPlaceholderWithExpandedBooleanChoicesAndWithFalseAsPreSetData ()
329+ {
330+ $ form = $ this ->factory ->create ('choice ' , false , array (
331+ 'multiple ' => false ,
332+ 'expanded ' => true ,
333+ 'required ' => false ,
334+ 'choices ' => array (
335+ 'Yes ' => true ,
336+ 'No ' => false ,
337+ ),
338+ 'placeholder ' => 'Select an option ' ,
339+ 'choices_as_values ' => true ,
340+ ));
341+
342+ $ this ->assertTrue (isset ($ form ['placeholder ' ]), 'Placeholder should be set ' );
343+ $ this ->assertCount (3 , $ form , 'Each choice should become a new field, placeholder included ' );
344+
345+ $ view = $ form ->createView ();
346+
347+ $ this ->assertSame ('0 ' , $ view ->vars ['value ' ], 'Value should be "0" ' );
348+ $ this ->assertSame ('1 ' , $ view ->vars ['choices ' ][0 ]->value );
349+ $ this ->assertSame ('0 ' , $ view ->vars ['choices ' ][1 ]->value , 'Choice "false" should have "0" as value ' );
350+ $ this ->assertTrue ($ view ->children [1 ]->vars ['checked ' ], 'Choice "false" should be selected ' );
351+ }
352+
201353 public function testExpandedChoicesOptionsAreFlattened ()
202354 {
203355 $ form = $ this ->factory ->create ('choice ' , null , array (
0 commit comments