24
24
use Symfony \Bridge \Doctrine \Tests \Fixtures \CompositeStringIdEntity ;
25
25
use Symfony \Bridge \Doctrine \Tests \Fixtures \GroupableEntity ;
26
26
use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdEntity ;
27
+ use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleStringCastableIdEntity ;
27
28
use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleStringIdEntity ;
28
29
use Symfony \Component \Form \ChoiceList \View \ChoiceGroupView ;
29
30
use Symfony \Component \Form \ChoiceList \View \ChoiceView ;
@@ -40,6 +41,7 @@ class EntityTypeTest extends TypeTestCase
40
41
const SINGLE_IDENT_NO_TO_STRING_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity ' ;
41
42
const SINGLE_STRING_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity ' ;
42
43
const SINGLE_ASSOC_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleAssociationToIntIdEntity ' ;
44
+ const SINGLE_STRING_CASTABLE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringCastableIdEntity ' ;
43
45
const COMPOSITE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity ' ;
44
46
const COMPOSITE_STRING_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeStringIdEntity ' ;
45
47
@@ -67,6 +69,7 @@ protected function setUp()
67
69
$ this ->em ->getClassMetadata (self ::SINGLE_IDENT_NO_TO_STRING_CLASS ),
68
70
$ this ->em ->getClassMetadata (self ::SINGLE_STRING_IDENT_CLASS ),
69
71
$ this ->em ->getClassMetadata (self ::SINGLE_ASSOC_IDENT_CLASS ),
72
+ $ this ->em ->getClassMetadata (self ::SINGLE_STRING_CASTABLE_IDENT_CLASS ),
70
73
$ this ->em ->getClassMetadata (self ::COMPOSITE_IDENT_CLASS ),
71
74
$ this ->em ->getClassMetadata (self ::COMPOSITE_STRING_IDENT_CLASS ),
72
75
);
@@ -580,6 +583,139 @@ public function testSubmitMultipleExpandedWithNegativeIntegerId()
580
583
$ this ->assertFalse ($ field ['2 ' ]->getData ());
581
584
}
582
585
586
+ public function testSubmitSingleNonExpandedStringCastableIdentifier ()
587
+ {
588
+ $ entity1 = new SingleStringCastableIdEntity (1 , 'Foo ' );
589
+ $ entity2 = new SingleStringCastableIdEntity (2 , 'Bar ' );
590
+
591
+ $ this ->persist (array ($ entity1 , $ entity2 ));
592
+
593
+ $ field = $ this ->factory ->createNamed ('name ' , 'entity ' , null , array (
594
+ 'multiple ' => false ,
595
+ 'expanded ' => false ,
596
+ 'em ' => 'default ' ,
597
+ 'class ' => self ::SINGLE_STRING_CASTABLE_IDENT_CLASS ,
598
+ 'choice_label ' => 'name ' ,
599
+ ));
600
+
601
+ $ field ->submit ('2 ' );
602
+
603
+ $ this ->assertTrue ($ field ->isSynchronized ());
604
+ $ this ->assertSame ($ entity2 , $ field ->getData ());
605
+ $ this ->assertSame ('2 ' , $ field ->getViewData ());
606
+ }
607
+
608
+ public function testSubmitSingleStringCastableIdentifierExpanded ()
609
+ {
610
+ $ entity1 = new SingleStringCastableIdEntity (1 , 'Foo ' );
611
+ $ entity2 = new SingleStringCastableIdEntity (2 , 'Bar ' );
612
+
613
+ $ this ->persist (array ($ entity1 , $ entity2 ));
614
+
615
+ $ field = $ this ->factory ->createNamed ('name ' , 'entity ' , null , array (
616
+ 'multiple ' => false ,
617
+ 'expanded ' => true ,
618
+ 'em ' => 'default ' ,
619
+ 'class ' => self ::SINGLE_STRING_CASTABLE_IDENT_CLASS ,
620
+ 'choice_label ' => 'name ' ,
621
+ ));
622
+
623
+ $ field ->submit ('2 ' );
624
+
625
+ $ this ->assertTrue ($ field ->isSynchronized ());
626
+ $ this ->assertSame ($ entity2 , $ field ->getData ());
627
+ $ this ->assertFalse ($ field ['0 ' ]->getData ());
628
+ $ this ->assertTrue ($ field ['1 ' ]->getData ());
629
+ $ this ->assertNull ($ field ['0 ' ]->getViewData ());
630
+ $ this ->assertSame ('2 ' , $ field ['1 ' ]->getViewData ());
631
+ }
632
+
633
+ public function testSubmitMultipleNonExpandedStringCastableIdentifierForExistingData ()
634
+ {
635
+ $ entity1 = new SingleStringCastableIdEntity (1 , 'Foo ' );
636
+ $ entity2 = new SingleStringCastableIdEntity (2 , 'Bar ' );
637
+ $ entity3 = new SingleStringCastableIdEntity (3 , 'Baz ' );
638
+
639
+ $ this ->persist (array ($ entity1 , $ entity2 , $ entity3 ));
640
+
641
+ $ field = $ this ->factory ->createNamed ('name ' , 'entity ' , null , array (
642
+ 'multiple ' => true ,
643
+ 'expanded ' => false ,
644
+ 'em ' => 'default ' ,
645
+ 'class ' => self ::SINGLE_STRING_CASTABLE_IDENT_CLASS ,
646
+ 'choice_label ' => 'name ' ,
647
+ ));
648
+
649
+ $ existing = new ArrayCollection (array (0 => $ entity2 ));
650
+
651
+ $ field ->setData ($ existing );
652
+ $ field ->submit (array ('1 ' , '3 ' ));
653
+
654
+ // entry with index 0 ($entity2) was replaced
655
+ $ expected = new ArrayCollection (array (0 => $ entity1 , 1 => $ entity3 ));
656
+
657
+ $ this ->assertTrue ($ field ->isSynchronized ());
658
+ $ this ->assertEquals ($ expected , $ field ->getData ());
659
+ // same object still, useful if it is a PersistentCollection
660
+ $ this ->assertSame ($ existing , $ field ->getData ());
661
+ $ this ->assertSame (array ('1 ' , '3 ' ), $ field ->getViewData ());
662
+ }
663
+
664
+ public function testSubmitMultipleNonExpandedStringCastableIdentifier ()
665
+ {
666
+ $ entity1 = new SingleStringCastableIdEntity (1 , 'Foo ' );
667
+ $ entity2 = new SingleStringCastableIdEntity (2 , 'Bar ' );
668
+ $ entity3 = new SingleStringCastableIdEntity (3 , 'Baz ' );
669
+
670
+ $ this ->persist (array ($ entity1 , $ entity2 , $ entity3 ));
671
+
672
+ $ field = $ this ->factory ->createNamed ('name ' , 'entity ' , null , array (
673
+ 'multiple ' => true ,
674
+ 'expanded ' => false ,
675
+ 'em ' => 'default ' ,
676
+ 'class ' => self ::SINGLE_STRING_CASTABLE_IDENT_CLASS ,
677
+ 'choice_label ' => 'name ' ,
678
+ ));
679
+
680
+ $ field ->submit (array ('1 ' , '3 ' ));
681
+
682
+ $ expected = new ArrayCollection (array ($ entity1 , $ entity3 ));
683
+
684
+ $ this ->assertTrue ($ field ->isSynchronized ());
685
+ $ this ->assertEquals ($ expected , $ field ->getData ());
686
+ $ this ->assertSame (array ('1 ' , '3 ' ), $ field ->getViewData ());
687
+ }
688
+
689
+ public function testSubmitMultipleStringCastableIdentifierExpanded ()
690
+ {
691
+ $ entity1 = new SingleStringCastableIdEntity (1 , 'Foo ' );
692
+ $ entity2 = new SingleStringCastableIdEntity (2 , 'Bar ' );
693
+ $ entity3 = new SingleStringCastableIdEntity (3 , 'Bar ' );
694
+
695
+ $ this ->persist (array ($ entity1 , $ entity2 , $ entity3 ));
696
+
697
+ $ field = $ this ->factory ->createNamed ('name ' , 'entity ' , null , array (
698
+ 'multiple ' => true ,
699
+ 'expanded ' => true ,
700
+ 'em ' => 'default ' ,
701
+ 'class ' => self ::SINGLE_STRING_CASTABLE_IDENT_CLASS ,
702
+ 'choice_label ' => 'name ' ,
703
+ ));
704
+
705
+ $ field ->submit (array ('1 ' , '3 ' ));
706
+
707
+ $ expected = new ArrayCollection (array ($ entity1 , $ entity3 ));
708
+
709
+ $ this ->assertTrue ($ field ->isSynchronized ());
710
+ $ this ->assertEquals ($ expected , $ field ->getData ());
711
+ $ this ->assertTrue ($ field ['0 ' ]->getData ());
712
+ $ this ->assertFalse ($ field ['1 ' ]->getData ());
713
+ $ this ->assertTrue ($ field ['2 ' ]->getData ());
714
+ $ this ->assertSame ('1 ' , $ field ['0 ' ]->getViewData ());
715
+ $ this ->assertNull ($ field ['1 ' ]->getViewData ());
716
+ $ this ->assertSame ('3 ' , $ field ['2 ' ]->getViewData ());
717
+ }
718
+
583
719
public function testOverrideChoices ()
584
720
{
585
721
$ entity1 = new SingleIntIdEntity (1 , 'Foo ' );
0 commit comments