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 );
@@ -593,6 +596,139 @@ public function testSubmitMultipleExpandedWithNegativeIntegerId()
593596 $ this ->assertFalse ($ field ['2 ' ]->getData ());
594597 }
595598
599+ public function testSubmitSingleNonExpandedStringCastableIdentifier ()
600+ {
601+ $ entity1 = new SingleStringCastableIdEntity (1 , 'Foo ' );
602+ $ entity2 = new SingleStringCastableIdEntity (2 , 'Bar ' );
603+
604+ $ this ->persist (array ($ entity1 , $ entity2 ));
605+
606+ $ field = $ this ->factory ->createNamed ('name ' , 'entity ' , null , array (
607+ 'multiple ' => false ,
608+ 'expanded ' => false ,
609+ 'em ' => 'default ' ,
610+ 'class ' => self ::SINGLE_STRING_CASTABLE_IDENT_CLASS ,
611+ 'choice_label ' => 'name ' ,
612+ ));
613+
614+ $ field ->submit ('2 ' );
615+
616+ $ this ->assertTrue ($ field ->isSynchronized ());
617+ $ this ->assertSame ($ entity2 , $ field ->getData ());
618+ $ this ->assertSame ('2 ' , $ field ->getViewData ());
619+ }
620+
621+ public function testSubmitSingleStringCastableIdentifierExpanded ()
622+ {
623+ $ entity1 = new SingleStringCastableIdEntity (1 , 'Foo ' );
624+ $ entity2 = new SingleStringCastableIdEntity (2 , 'Bar ' );
625+
626+ $ this ->persist (array ($ entity1 , $ entity2 ));
627+
628+ $ field = $ this ->factory ->createNamed ('name ' , 'entity ' , null , array (
629+ 'multiple ' => false ,
630+ 'expanded ' => true ,
631+ 'em ' => 'default ' ,
632+ 'class ' => self ::SINGLE_STRING_CASTABLE_IDENT_CLASS ,
633+ 'choice_label ' => 'name ' ,
634+ ));
635+
636+ $ field ->submit ('2 ' );
637+
638+ $ this ->assertTrue ($ field ->isSynchronized ());
639+ $ this ->assertSame ($ entity2 , $ field ->getData ());
640+ $ this ->assertFalse ($ field ['0 ' ]->getData ());
641+ $ this ->assertTrue ($ field ['1 ' ]->getData ());
642+ $ this ->assertNull ($ field ['0 ' ]->getViewData ());
643+ $ this ->assertSame ('2 ' , $ field ['1 ' ]->getViewData ());
644+ }
645+
646+ public function testSubmitMultipleNonExpandedStringCastableIdentifierForExistingData ()
647+ {
648+ $ entity1 = new SingleStringCastableIdEntity (1 , 'Foo ' );
649+ $ entity2 = new SingleStringCastableIdEntity (2 , 'Bar ' );
650+ $ entity3 = new SingleStringCastableIdEntity (3 , 'Baz ' );
651+
652+ $ this ->persist (array ($ entity1 , $ entity2 , $ entity3 ));
653+
654+ $ field = $ this ->factory ->createNamed ('name ' , 'entity ' , null , array (
655+ 'multiple ' => true ,
656+ 'expanded ' => false ,
657+ 'em ' => 'default ' ,
658+ 'class ' => self ::SINGLE_STRING_CASTABLE_IDENT_CLASS ,
659+ 'choice_label ' => 'name ' ,
660+ ));
661+
662+ $ existing = new ArrayCollection (array (0 => $ entity2 ));
663+
664+ $ field ->setData ($ existing );
665+ $ field ->submit (array ('1 ' , '3 ' ));
666+
667+ // entry with index 0 ($entity2) was replaced
668+ $ expected = new ArrayCollection (array (0 => $ entity1 , 1 => $ entity3 ));
669+
670+ $ this ->assertTrue ($ field ->isSynchronized ());
671+ $ this ->assertEquals ($ expected , $ field ->getData ());
672+ // same object still, useful if it is a PersistentCollection
673+ $ this ->assertSame ($ existing , $ field ->getData ());
674+ $ this ->assertSame (array ('1 ' , '3 ' ), $ field ->getViewData ());
675+ }
676+
677+ public function testSubmitMultipleNonExpandedStringCastableIdentifier ()
678+ {
679+ $ entity1 = new SingleStringCastableIdEntity (1 , 'Foo ' );
680+ $ entity2 = new SingleStringCastableIdEntity (2 , 'Bar ' );
681+ $ entity3 = new SingleStringCastableIdEntity (3 , 'Baz ' );
682+
683+ $ this ->persist (array ($ entity1 , $ entity2 , $ entity3 ));
684+
685+ $ field = $ this ->factory ->createNamed ('name ' , 'entity ' , null , array (
686+ 'multiple ' => true ,
687+ 'expanded ' => false ,
688+ 'em ' => 'default ' ,
689+ 'class ' => self ::SINGLE_STRING_CASTABLE_IDENT_CLASS ,
690+ 'choice_label ' => 'name ' ,
691+ ));
692+
693+ $ field ->submit (array ('1 ' , '3 ' ));
694+
695+ $ expected = new ArrayCollection (array ($ entity1 , $ entity3 ));
696+
697+ $ this ->assertTrue ($ field ->isSynchronized ());
698+ $ this ->assertEquals ($ expected , $ field ->getData ());
699+ $ this ->assertSame (array ('1 ' , '3 ' ), $ field ->getViewData ());
700+ }
701+
702+ public function testSubmitMultipleStringCastableIdentifierExpanded ()
703+ {
704+ $ entity1 = new SingleStringCastableIdEntity (1 , 'Foo ' );
705+ $ entity2 = new SingleStringCastableIdEntity (2 , 'Bar ' );
706+ $ entity3 = new SingleStringCastableIdEntity (3 , 'Bar ' );
707+
708+ $ this ->persist (array ($ entity1 , $ entity2 , $ entity3 ));
709+
710+ $ field = $ this ->factory ->createNamed ('name ' , 'entity ' , null , array (
711+ 'multiple ' => true ,
712+ 'expanded ' => true ,
713+ 'em ' => 'default ' ,
714+ 'class ' => self ::SINGLE_STRING_CASTABLE_IDENT_CLASS ,
715+ 'choice_label ' => 'name ' ,
716+ ));
717+
718+ $ field ->submit (array ('1 ' , '3 ' ));
719+
720+ $ expected = new ArrayCollection (array ($ entity1 , $ entity3 ));
721+
722+ $ this ->assertTrue ($ field ->isSynchronized ());
723+ $ this ->assertEquals ($ expected , $ field ->getData ());
724+ $ this ->assertTrue ($ field ['0 ' ]->getData ());
725+ $ this ->assertFalse ($ field ['1 ' ]->getData ());
726+ $ this ->assertTrue ($ field ['2 ' ]->getData ());
727+ $ this ->assertSame ('1 ' , $ field ['0 ' ]->getViewData ());
728+ $ this ->assertNull ($ field ['1 ' ]->getViewData ());
729+ $ this ->assertSame ('3 ' , $ field ['2 ' ]->getViewData ());
730+ }
731+
596732 public function testOverrideChoices ()
597733 {
598734 $ entity1 = new SingleIntIdEntity (1 , 'Foo ' );
0 commit comments