33namespace EasyCorp \Bundle \EasyAdminBundle \Tests \Field \Configurator ;
44
55use Doctrine \ORM \EntityManagerInterface ;
6+ use Doctrine \ORM \Mapping \ClassMetadata ;
7+ use EasyCorp \Bundle \EasyAdminBundle \Collection \FieldCollection ;
68use EasyCorp \Bundle \EasyAdminBundle \Contracts \Field \FieldInterface ;
79use EasyCorp \Bundle \EasyAdminBundle \Dto \EntityDto ;
810use EasyCorp \Bundle \EasyAdminBundle \Factory \ControllerFactory ;
1517use EasyCorp \Bundle \EasyAdminBundle \Tests \Field \AbstractFieldTest ;
1618use EasyCorp \Bundle \EasyAdminBundle \Tests \TestApplication \Controller \ProjectDomain \DeveloperCrudController ;
1719use EasyCorp \Bundle \EasyAdminBundle \Tests \TestApplication \Controller \ProjectDomain \ProjectCrudController ;
20+ use EasyCorp \Bundle \EasyAdminBundle \Tests \TestApplication \Controller \ProjectDomain \ProjectReleaseCategoryCrudController ;
1821use EasyCorp \Bundle \EasyAdminBundle \Tests \TestApplication \Entity \ProjectDomain \Developer ;
1922use EasyCorp \Bundle \EasyAdminBundle \Tests \TestApplication \Entity \ProjectDomain \Project ;
23+ use EasyCorp \Bundle \EasyAdminBundle \Tests \TestApplication \Entity \ProjectDomain \ProjectReleaseCategory ;
2024use EasyCorp \Bundle \EasyAdminBundle \Tests \TestApplication \Entity \ProjectDomain \ProjectTag ;
2125use Symfony \Bridge \Doctrine \Form \Type \EntityType ;
2226use Symfony \Component \HttpFoundation \RequestStack ;
@@ -47,42 +51,43 @@ protected function getEntityDto(): EntityDto
4751 return $ this ->projectDto ;
4852 }
4953
50- /**
51- * @dataProvider toOneAssociation
52- */
53- public function testToOneAssociation (FieldInterface $ field ): void
54+ public function testToOneAssociation (): void
5455 {
56+ $ field = AssociationField::new ('leadDeveloper ' );
57+ $ entityDto = new EntityDto (Project::class, $ this ->createStub (ClassMetadata::class));
58+ $ entityDto ->setFields (FieldCollection::new ([$ field ]));
59+
5560 $ field ->getAsDto ()->setDoctrineMetadata ((array ) $ this ->projectDto ->getClassMetadata ()->getAssociationMapping ($ field ->getAsDto ()->getProperty ()));
5661 $ field ->setCustomOption (AssociationField::OPTION_EMBEDDED_CRUD_FORM_CONTROLLER , DeveloperCrudController::class);
5762
58- $ field = $ this ->configure ($ field , controllerFqcn: ProjectCrudController::class);
59- $ this ->assertSame ('toOne ' , $ field ->getCustomOption (AssociationField::OPTION_DOCTRINE_ASSOCIATION_TYPE ));
60- $ this ->assertSame (EntityType::class, $ field ->getFormType ());
61- $ this ->assertSame (Developer::class, $ field ->getFormTypeOption ('class ' ));
62- }
63-
64- public static function toOneAssociation (): \Generator
65- {
66- yield [AssociationField::new ('leadDeveloper ' )];
63+ $ fieldDto = $ this ->configure ($ field , controllerFqcn: ProjectCrudController::class);
64+ $ this ->assertSame ('toOne ' , $ fieldDto ->getCustomOption (AssociationField::OPTION_DOCTRINE_ASSOCIATION_TYPE ));
65+ $ this ->assertSame (EntityType::class, $ fieldDto ->getFormType ());
66+ $ this ->assertSame (Developer::class, $ fieldDto ->getFormTypeOption ('class ' ));
6767 }
6868
69- /**
70- * @dataProvider toManyAssociation
71- */
72- public function testToManyAssociation (FieldInterface $ field ): void
69+ public function testToManyAssociation (): void
7370 {
71+ $ field = AssociationField::new ('projectTags ' );
7472 $ field ->getAsDto ()->setDoctrineMetadata ((array ) $ this ->projectDto ->getClassMetadata ()->getAssociationMapping ($ field ->getAsDto ()->getProperty ()));
7573 $ field ->setCustomOption (AssociationField::OPTION_EMBEDDED_CRUD_FORM_CONTROLLER , DeveloperCrudController::class);
7674
77- $ field = $ this ->configure ($ field , controllerFqcn: ProjectCrudController::class);
78- $ this ->assertSame ('toMany ' , $ field ->getCustomOption (AssociationField::OPTION_DOCTRINE_ASSOCIATION_TYPE ));
79- $ this ->assertSame (EntityType::class, $ field ->getFormType ());
80- $ this ->assertSame (ProjectTag::class, $ field ->getFormTypeOption ('class ' ));
75+ $ fieldDto = $ this ->configure ($ field , controllerFqcn: ProjectCrudController::class);
76+ $ this ->assertSame ('toMany ' , $ fieldDto ->getCustomOption (AssociationField::OPTION_DOCTRINE_ASSOCIATION_TYPE ));
77+ $ this ->assertSame (EntityType::class, $ fieldDto ->getFormType ());
78+ $ this ->assertSame (ProjectTag::class, $ fieldDto ->getFormTypeOption ('class ' ));
8179 }
8280
83- public static function toManyAssociation (): \ Generator
81+ public function testNestedAssociationWithCrudControllerSet (): void
8482 {
85- yield [AssociationField::new ('projectTags ' )];
83+ $ field = AssociationField::new ('latestRelease.category ' )
84+ ->setCrudController (ProjectReleaseCategoryCrudController::class)
85+ ;
86+
87+ $ fieldDto = $ this ->configure ($ field );
88+
89+ $ this ->assertSame (EntityType::class, $ fieldDto ->getFormType ());
90+ $ this ->assertSame (ProjectReleaseCategory::class, $ fieldDto ->getFormTypeOption ('class ' ));
8691 }
8792
8893 /**
@@ -103,7 +108,7 @@ public static function failsIfPropertyIsNotAssociation(): \Generator
103108 {
104109 yield [TextField::new ('name ' )];
105110 yield [TextField::new ('price ' )];
106- yield [TextField::new ('price.currency ' )];
111+ yield [TextField::new ('price.currency ' )]; // Doctrine embeddable
107112 }
108113
109114 /**
0 commit comments