@@ -740,6 +740,55 @@ public function testOverrideChoices()
740
740
$ this ->assertSame ('2 ' , $ field ->getViewData ());
741
741
}
742
742
743
+ public function testOverrideChoicesValues ()
744
+ {
745
+ $ entity1 = new SingleIntIdEntity (1 , 'Foo ' );
746
+ $ entity2 = new SingleIntIdEntity (2 , 'Bar ' );
747
+
748
+ $ this ->persist (array ($ entity1 , $ entity2 ));
749
+
750
+ $ field = $ this ->factory ->createNamed ('name ' , 'entity ' , null , array (
751
+ 'em ' => 'default ' ,
752
+ 'class ' => self ::SINGLE_IDENT_CLASS ,
753
+ 'choice_label ' => 'name ' ,
754
+ 'choice_value ' => 'name ' ,
755
+ ));
756
+
757
+ $ field ->submit ('Bar ' );
758
+
759
+ $ this ->assertEquals (array ('Foo ' => new ChoiceView ($ entity1 , 'Foo ' , 'Foo ' ), 'Bar ' => new ChoiceView ($ entity2 , 'Bar ' , 'Bar ' )), $ field ->createView ()->vars ['choices ' ]);
760
+ $ this ->assertTrue ($ field ->isSynchronized (), 'Field should be synchronized. ' );
761
+ $ this ->assertSame ($ entity2 , $ field ->getData (), 'Entity should be loaded by custom value. ' );
762
+ $ this ->assertSame ('Bar ' , $ field ->getViewData ());
763
+ }
764
+
765
+ public function testOverrideChoicesValuesWithCallable ()
766
+ {
767
+ $ entity1 = new GroupableEntity (1 , 'Foo ' , 'BazGroup ' );
768
+ $ entity2 = new GroupableEntity (2 , 'Bar ' , 'BooGroup ' );
769
+
770
+ $ this ->persist (array ($ entity1 , $ entity2 ));
771
+
772
+ $ field = $ this ->factory ->createNamed ('name ' , 'entity ' , null , array (
773
+ 'em ' => 'default ' ,
774
+ 'class ' => self ::ITEM_GROUP_CLASS ,
775
+ 'choice_label ' => 'name ' ,
776
+ 'choice_value ' => function (GroupableEntity $ entity ) {
777
+ return $ entity ->groupName .'/ ' .$ entity ->name ;
778
+ },
779
+ ));
780
+
781
+ $ field ->submit ('BooGroup/Bar ' );
782
+
783
+ $ this ->assertEquals (array (
784
+ 'BazGroup/Foo ' => new ChoiceView ($ entity1 , 'BazGroup/Foo ' , 'Foo ' ),
785
+ 'BooGroup/Bar ' => new ChoiceView ($ entity2 , 'BooGroup/Bar ' , 'Bar ' ),
786
+ ), $ field ->createView ()->vars ['choices ' ]);
787
+ $ this ->assertTrue ($ field ->isSynchronized (), 'Field should be synchronized. ' );
788
+ $ this ->assertSame ($ entity2 , $ field ->getData (), 'Entity should be loaded by custom value. ' );
789
+ $ this ->assertSame ('BooGroup/Bar ' , $ field ->getViewData ());
790
+ }
791
+
743
792
public function testGroupByChoices ()
744
793
{
745
794
$ item1 = new GroupableEntity (1 , 'Foo ' , 'Group1 ' );
0 commit comments