2424use Symfony \Bridge \Doctrine \Tests \Fixtures \CompositeStringIdEntity ;
2525use Symfony \Bridge \Doctrine \Tests \Fixtures \GroupableEntity ;
2626use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdEntity ;
27+ use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleStringCastableIdEntity ;
2728use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleStringIdEntity ;
2829use Symfony \Component \Form \ChoiceList \View \ChoiceGroupView ;
2930use Symfony \Component \Form \ChoiceList \View \ChoiceView ;
@@ -40,6 +41,7 @@ class EntityTypeTest extends TypeTestCase
4041 const SINGLE_IDENT_NO_TO_STRING_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity ' ;
4142 const SINGLE_STRING_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity ' ;
4243 const SINGLE_ASSOC_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleAssociationToIntIdEntity ' ;
44+ const SINGLE_STRING_CASTABLE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringCastableIdEntity ' ;
4345 const COMPOSITE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity ' ;
4446 const COMPOSITE_STRING_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeStringIdEntity ' ;
4547
@@ -67,6 +69,7 @@ protected function setUp()
6769 $ this ->em ->getClassMetadata (self ::SINGLE_IDENT_NO_TO_STRING_CLASS ),
6870 $ this ->em ->getClassMetadata (self ::SINGLE_STRING_IDENT_CLASS ),
6971 $ this ->em ->getClassMetadata (self ::SINGLE_ASSOC_IDENT_CLASS ),
72+ $ this ->em ->getClassMetadata (self ::SINGLE_STRING_CASTABLE_IDENT_CLASS ),
7073 $ this ->em ->getClassMetadata (self ::COMPOSITE_IDENT_CLASS ),
7174 $ this ->em ->getClassMetadata (self ::COMPOSITE_STRING_IDENT_CLASS ),
7275 );
@@ -606,6 +609,139 @@ public function testSubmitMultipleExpandedWithNegativeIntegerId()
606609 $ this ->assertFalse ($ field ['2 ' ]->getData ());
607610 }
608611
612+ public function testSubmitSingleNonExpandedStringCastableIdentifier ()
613+ {
614+ $ entity1 = new SingleStringCastableIdEntity (1 , 'Foo ' );
615+ $ entity2 = new SingleStringCastableIdEntity (2 , 'Bar ' );
616+
617+ $ this ->persist (array ($ entity1 , $ entity2 ));
618+
619+ $ field = $ this ->factory ->createNamed ('name ' , 'entity ' , null , array (
620+ 'multiple ' => false ,
621+ 'expanded ' => false ,
622+ 'em ' => 'default ' ,
623+ 'class ' => self ::SINGLE_STRING_CASTABLE_IDENT_CLASS ,
624+ 'choice_label ' => 'name ' ,
625+ ));
626+
627+ $ field ->submit ('2 ' );
628+
629+ $ this ->assertTrue ($ field ->isSynchronized ());
630+ $ this ->assertSame ($ entity2 , $ field ->getData ());
631+ $ this ->assertSame ('2 ' , $ field ->getViewData ());
632+ }
633+
634+ public function testSubmitSingleStringCastableIdentifierExpanded ()
635+ {
636+ $ entity1 = new SingleStringCastableIdEntity (1 , 'Foo ' );
637+ $ entity2 = new SingleStringCastableIdEntity (2 , 'Bar ' );
638+
639+ $ this ->persist (array ($ entity1 , $ entity2 ));
640+
641+ $ field = $ this ->factory ->createNamed ('name ' , 'entity ' , null , array (
642+ 'multiple ' => false ,
643+ 'expanded ' => true ,
644+ 'em ' => 'default ' ,
645+ 'class ' => self ::SINGLE_STRING_CASTABLE_IDENT_CLASS ,
646+ 'choice_label ' => 'name ' ,
647+ ));
648+
649+ $ field ->submit ('2 ' );
650+
651+ $ this ->assertTrue ($ field ->isSynchronized ());
652+ $ this ->assertSame ($ entity2 , $ field ->getData ());
653+ $ this ->assertFalse ($ field ['0 ' ]->getData ());
654+ $ this ->assertTrue ($ field ['1 ' ]->getData ());
655+ $ this ->assertNull ($ field ['0 ' ]->getViewData ());
656+ $ this ->assertSame ('2 ' , $ field ['1 ' ]->getViewData ());
657+ }
658+
659+ public function testSubmitMultipleNonExpandedStringCastableIdentifierForExistingData ()
660+ {
661+ $ entity1 = new SingleStringCastableIdEntity (1 , 'Foo ' );
662+ $ entity2 = new SingleStringCastableIdEntity (2 , 'Bar ' );
663+ $ entity3 = new SingleStringCastableIdEntity (3 , 'Baz ' );
664+
665+ $ this ->persist (array ($ entity1 , $ entity2 , $ entity3 ));
666+
667+ $ field = $ this ->factory ->createNamed ('name ' , 'entity ' , null , array (
668+ 'multiple ' => true ,
669+ 'expanded ' => false ,
670+ 'em ' => 'default ' ,
671+ 'class ' => self ::SINGLE_STRING_CASTABLE_IDENT_CLASS ,
672+ 'choice_label ' => 'name ' ,
673+ ));
674+
675+ $ existing = new ArrayCollection (array (0 => $ entity2 ));
676+
677+ $ field ->setData ($ existing );
678+ $ field ->submit (array ('1 ' , '3 ' ));
679+
680+ // entry with index 0 ($entity2) was replaced
681+ $ expected = new ArrayCollection (array (0 => $ entity1 , 1 => $ entity3 ));
682+
683+ $ this ->assertTrue ($ field ->isSynchronized ());
684+ $ this ->assertEquals ($ expected , $ field ->getData ());
685+ // same object still, useful if it is a PersistentCollection
686+ $ this ->assertSame ($ existing , $ field ->getData ());
687+ $ this ->assertSame (array ('1 ' , '3 ' ), $ field ->getViewData ());
688+ }
689+
690+ public function testSubmitMultipleNonExpandedStringCastableIdentifier ()
691+ {
692+ $ entity1 = new SingleStringCastableIdEntity (1 , 'Foo ' );
693+ $ entity2 = new SingleStringCastableIdEntity (2 , 'Bar ' );
694+ $ entity3 = new SingleStringCastableIdEntity (3 , 'Baz ' );
695+
696+ $ this ->persist (array ($ entity1 , $ entity2 , $ entity3 ));
697+
698+ $ field = $ this ->factory ->createNamed ('name ' , 'entity ' , null , array (
699+ 'multiple ' => true ,
700+ 'expanded ' => false ,
701+ 'em ' => 'default ' ,
702+ 'class ' => self ::SINGLE_STRING_CASTABLE_IDENT_CLASS ,
703+ 'choice_label ' => 'name ' ,
704+ ));
705+
706+ $ field ->submit (array ('1 ' , '3 ' ));
707+
708+ $ expected = new ArrayCollection (array ($ entity1 , $ entity3 ));
709+
710+ $ this ->assertTrue ($ field ->isSynchronized ());
711+ $ this ->assertEquals ($ expected , $ field ->getData ());
712+ $ this ->assertSame (array ('1 ' , '3 ' ), $ field ->getViewData ());
713+ }
714+
715+ public function testSubmitMultipleStringCastableIdentifierExpanded ()
716+ {
717+ $ entity1 = new SingleStringCastableIdEntity (1 , 'Foo ' );
718+ $ entity2 = new SingleStringCastableIdEntity (2 , 'Bar ' );
719+ $ entity3 = new SingleStringCastableIdEntity (3 , 'Bar ' );
720+
721+ $ this ->persist (array ($ entity1 , $ entity2 , $ entity3 ));
722+
723+ $ field = $ this ->factory ->createNamed ('name ' , 'entity ' , null , array (
724+ 'multiple ' => true ,
725+ 'expanded ' => true ,
726+ 'em ' => 'default ' ,
727+ 'class ' => self ::SINGLE_STRING_CASTABLE_IDENT_CLASS ,
728+ 'choice_label ' => 'name ' ,
729+ ));
730+
731+ $ field ->submit (array ('1 ' , '3 ' ));
732+
733+ $ expected = new ArrayCollection (array ($ entity1 , $ entity3 ));
734+
735+ $ this ->assertTrue ($ field ->isSynchronized ());
736+ $ this ->assertEquals ($ expected , $ field ->getData ());
737+ $ this ->assertTrue ($ field ['0 ' ]->getData ());
738+ $ this ->assertFalse ($ field ['1 ' ]->getData ());
739+ $ this ->assertTrue ($ field ['2 ' ]->getData ());
740+ $ this ->assertSame ('1 ' , $ field ['0 ' ]->getViewData ());
741+ $ this ->assertNull ($ field ['1 ' ]->getViewData ());
742+ $ this ->assertSame ('3 ' , $ field ['2 ' ]->getViewData ());
743+ }
744+
609745 public function testOverrideChoices ()
610746 {
611747 $ entity1 = new SingleIntIdEntity (1 , 'Foo ' );
0 commit comments