30
30
use Symfony \Component \Form \Forms ;
31
31
use Symfony \Component \Form \Test \TypeTestCase ;
32
32
use Symfony \Component \PropertyAccess \PropertyAccess ;
33
+ use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleAssociationToIntIdEntity ;
34
+ use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIntIdNoToStringEntity ;
33
35
34
36
class EntityTypeTest extends TypeTestCase
35
37
{
36
38
const ITEM_GROUP_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\GroupableEntity ' ;
37
39
const SINGLE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity ' ;
40
+ const SINGLE_IDENT_NO_TO_STRING_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity ' ;
38
41
const SINGLE_STRING_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity ' ;
42
+ const SINGLE_ASSOC_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleAssociationToIntIdEntity ' ;
39
43
const COMPOSITE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity ' ;
40
44
const COMPOSITE_STRING_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeStringIdEntity ' ;
41
45
@@ -60,7 +64,9 @@ protected function setUp()
60
64
$ classes = array (
61
65
$ this ->em ->getClassMetadata (self ::ITEM_GROUP_CLASS ),
62
66
$ this ->em ->getClassMetadata (self ::SINGLE_IDENT_CLASS ),
67
+ $ this ->em ->getClassMetadata (self ::SINGLE_IDENT_NO_TO_STRING_CLASS ),
63
68
$ this ->em ->getClassMetadata (self ::SINGLE_STRING_IDENT_CLASS ),
69
+ $ this ->em ->getClassMetadata (self ::SINGLE_ASSOC_IDENT_CLASS ),
64
70
$ this ->em ->getClassMetadata (self ::COMPOSITE_IDENT_CLASS ),
65
71
$ this ->em ->getClassMetadata (self ::COMPOSITE_STRING_IDENT_CLASS ),
66
72
);
@@ -304,6 +310,31 @@ public function testSubmitSingleNonExpandedSingleIdentifier()
304
310
$ this ->assertSame ('2 ' , $ field ->getViewData ());
305
311
}
306
312
313
+ public function testSubmitSingleNonExpandedSingleAssocIdentifier ()
314
+ {
315
+ $ innerEntity1 = new SingleIntIdNoToStringEntity (1 , 'InFoo ' );
316
+ $ innerEntity2 = new SingleIntIdNoToStringEntity (2 , 'InBar ' );
317
+
318
+ $ entity1 = new SingleAssociationToIntIdEntity ($ innerEntity1 , 'Foo ' );
319
+ $ entity2 = new SingleAssociationToIntIdEntity ($ innerEntity2 , 'Bar ' );
320
+
321
+ $ this ->persist (array ($ innerEntity1 , $ innerEntity2 , $ entity1 , $ entity2 ));
322
+
323
+ $ field = $ this ->factory ->createNamed ('name ' , 'entity ' , null , array (
324
+ 'multiple ' => false ,
325
+ 'expanded ' => false ,
326
+ 'em ' => 'default ' ,
327
+ 'class ' => self ::SINGLE_ASSOC_IDENT_CLASS ,
328
+ 'choice_label ' => 'name ' ,
329
+ ));
330
+
331
+ $ field ->submit ('2 ' );
332
+
333
+ $ this ->assertTrue ($ field ->isSynchronized ());
334
+ $ this ->assertSame ($ entity2 , $ field ->getData ());
335
+ $ this ->assertSame ('2 ' , $ field ->getViewData ());
336
+ }
337
+
307
338
public function testSubmitSingleNonExpandedCompositeIdentifier ()
308
339
{
309
340
$ entity1 = new CompositeIntIdEntity (10 , 20 , 'Foo ' );
@@ -352,6 +383,35 @@ public function testSubmitMultipleNonExpandedSingleIdentifier()
352
383
$ this ->assertSame (array ('1 ' , '3 ' ), $ field ->getViewData ());
353
384
}
354
385
386
+ public function testSubmitMultipleNonExpandedSingleAssocIdentifier ()
387
+ {
388
+ $ innerEntity1 = new SingleIntIdNoToStringEntity (1 , 'InFoo ' );
389
+ $ innerEntity2 = new SingleIntIdNoToStringEntity (2 , 'InBar ' );
390
+ $ innerEntity3 = new SingleIntIdNoToStringEntity (3 , 'InBaz ' );
391
+
392
+ $ entity1 = new SingleAssociationToIntIdEntity ($ innerEntity1 , 'Foo ' );
393
+ $ entity2 = new SingleAssociationToIntIdEntity ($ innerEntity2 , 'Bar ' );
394
+ $ entity3 = new SingleAssociationToIntIdEntity ($ innerEntity3 , 'Baz ' );
395
+
396
+ $ this ->persist (array ($ innerEntity1 , $ innerEntity2 , $ innerEntity3 , $ entity1 , $ entity2 , $ entity3 ));
397
+
398
+ $ field = $ this ->factory ->createNamed ('name ' , 'entity ' , null , array (
399
+ 'multiple ' => true ,
400
+ 'expanded ' => false ,
401
+ 'em ' => 'default ' ,
402
+ 'class ' => self ::SINGLE_ASSOC_IDENT_CLASS ,
403
+ 'choice_label ' => 'name ' ,
404
+ ));
405
+
406
+ $ field ->submit (array ('1 ' , '3 ' ));
407
+
408
+ $ expected = new ArrayCollection (array ($ entity1 , $ entity3 ));
409
+
410
+ $ this ->assertTrue ($ field ->isSynchronized ());
411
+ $ this ->assertEquals ($ expected , $ field ->getData ());
412
+ $ this ->assertSame (array ('1 ' , '3 ' ), $ field ->getViewData ());
413
+ }
414
+
355
415
public function testSubmitMultipleNonExpandedSingleIdentifierForExistingData ()
356
416
{
357
417
$ entity1 = new SingleIntIdEntity (1 , 'Foo ' );
@@ -611,6 +671,29 @@ public function testDisallowChoicesThatAreNotIncludedChoicesSingleIdentifier()
611
671
$ this ->assertNull ($ field ->getData ());
612
672
}
613
673
674
+ public function testDisallowChoicesThatAreNotIncludedChoicesSingleAssocIdentifier ()
675
+ {
676
+ $ innerEntity1 = new SingleIntIdNoToStringEntity (1 , 'InFoo ' );
677
+ $ innerEntity2 = new SingleIntIdNoToStringEntity (2 , 'InBar ' );
678
+
679
+ $ entity1 = new SingleAssociationToIntIdEntity ($ innerEntity1 , 'Foo ' );
680
+ $ entity2 = new SingleAssociationToIntIdEntity ($ innerEntity2 , 'Bar ' );
681
+
682
+ $ this ->persist (array ($ innerEntity1 , $ innerEntity2 , $ entity1 , $ entity2 ));
683
+
684
+ $ field = $ this ->factory ->createNamed ('name ' , 'entity ' , null , array (
685
+ 'em ' => 'default ' ,
686
+ 'class ' => self ::SINGLE_ASSOC_IDENT_CLASS ,
687
+ 'choices ' => array ($ entity1 , $ entity2 ),
688
+ 'choice_label ' => 'name ' ,
689
+ ));
690
+
691
+ $ field ->submit ('3 ' );
692
+
693
+ $ this ->assertFalse ($ field ->isSynchronized ());
694
+ $ this ->assertNull ($ field ->getData ());
695
+ }
696
+
614
697
public function testDisallowChoicesThatAreNotIncludedChoicesCompositeIdentifier ()
615
698
{
616
699
$ entity1 = new CompositeIntIdEntity (10 , 20 , 'Foo ' );
@@ -656,6 +739,34 @@ public function testDisallowChoicesThatAreNotIncludedQueryBuilderSingleIdentifie
656
739
$ this ->assertNull ($ field ->getData ());
657
740
}
658
741
742
+ public function testDisallowChoicesThatAreNotIncludedQueryBuilderSingleAssocIdentifier ()
743
+ {
744
+ $ innerEntity1 = new SingleIntIdNoToStringEntity (1 , 'InFoo ' );
745
+ $ innerEntity2 = new SingleIntIdNoToStringEntity (2 , 'InBar ' );
746
+ $ innerEntity3 = new SingleIntIdNoToStringEntity (3 , 'InBaz ' );
747
+
748
+ $ entity1 = new SingleAssociationToIntIdEntity ($ innerEntity1 , 'Foo ' );
749
+ $ entity2 = new SingleAssociationToIntIdEntity ($ innerEntity2 , 'Bar ' );
750
+ $ entity3 = new SingleAssociationToIntIdEntity ($ innerEntity3 , 'Baz ' );
751
+
752
+ $ this ->persist (array ($ innerEntity1 , $ innerEntity2 , $ innerEntity3 , $ entity1 , $ entity2 , $ entity3 ));
753
+
754
+ $ repository = $ this ->em ->getRepository (self ::SINGLE_ASSOC_IDENT_CLASS );
755
+
756
+ $ field = $ this ->factory ->createNamed ('name ' , 'entity ' , null , array (
757
+ 'em ' => 'default ' ,
758
+ 'class ' => self ::SINGLE_ASSOC_IDENT_CLASS ,
759
+ 'query_builder ' => $ repository ->createQueryBuilder ('e ' )
760
+ ->where ('e.entity IN (1, 2) ' ),
761
+ 'choice_label ' => 'name ' ,
762
+ ));
763
+
764
+ $ field ->submit ('3 ' );
765
+
766
+ $ this ->assertFalse ($ field ->isSynchronized ());
767
+ $ this ->assertNull ($ field ->getData ());
768
+ }
769
+
659
770
public function testDisallowChoicesThatAreNotIncludedQueryBuilderAsClosureSingleIdentifier ()
660
771
{
661
772
$ entity1 = new SingleIntIdEntity (1 , 'Foo ' );
0 commit comments