22
33namespace EasyCorp \Bundle \EasyAdminBundle \Tests \Field ;
44
5+ use Doctrine \ORM \Mapping \ClassMetadata ;
56use EasyCorp \Bundle \EasyAdminBundle \Config \Action ;
67use EasyCorp \Bundle \EasyAdminBundle \Config \Crud ;
8+ use EasyCorp \Bundle \EasyAdminBundle \Config \Option \TextDirection ;
79use EasyCorp \Bundle \EasyAdminBundle \Context \AdminContext ;
10+ use EasyCorp \Bundle \EasyAdminBundle \Contracts \Context \AdminContextInterface ;
811use EasyCorp \Bundle \EasyAdminBundle \Contracts \Field \FieldInterface ;
912use EasyCorp \Bundle \EasyAdminBundle \Dto \CrudDto ;
1013use EasyCorp \Bundle \EasyAdminBundle \Dto \EntityDto ;
1114use EasyCorp \Bundle \EasyAdminBundle \Dto \FieldDto ;
1215use EasyCorp \Bundle \EasyAdminBundle \Dto \I18nDto ;
1316use EasyCorp \Bundle \EasyAdminBundle \Field \DateTimeField ;
17+ use EasyCorp \Bundle \EasyAdminBundle \Registry \TemplateRegistry ;
1418use Symfony \Bundle \FrameworkBundle \Test \KernelTestCase ;
1519use Symfony \Component \HttpFoundation \Request ;
1620
@@ -22,64 +26,52 @@ abstract class AbstractFieldTest extends KernelTestCase
2226
2327 protected function getEntityDto (): EntityDto
2428 {
25- $ entityDtoMock = $ this ->createMock (EntityDto::class);
26- $ entityDtoMock
27- ->expects ($ this ->any ())
28- ->method ('getInstance ' )
29- ->willReturn (new class {});
29+ $ reflectedClass = new \ReflectionClass (ClassMetadata::class);
30+ $ classMetadata = $ reflectedClass ->newInstanceWithoutConstructor ();
3031
31- return $ this ->entityDto = $ entityDtoMock ;
32+ $ reflectedClass = new \ReflectionClass (EntityDto::class);
33+ $ entityDto = $ reflectedClass ->newInstanceWithoutConstructor ();
34+ $ instanceProperty = $ reflectedClass ->getProperty ('instance ' );
35+ $ instanceProperty ->setValue ($ entityDto , new class {});
36+ $ metadataProperty = $ reflectedClass ->getProperty ('metadata ' );
37+ $ metadataProperty ->setValue ($ entityDto , $ classMetadata );
38+
39+ return $ this ->entityDto = $ entityDto ;
3240 }
3341
34- private function getAdminContext (string $ pageName , string $ requestLocale , string $ actionName ): AdminContext
42+ private function getAdminContext (string $ pageName , string $ requestLocale , string $ actionName ): AdminContextInterface
3543 {
3644 self ::bootKernel ();
3745
38- $ crudMock = $ this ->getMockBuilder (CrudDto::class)
39- ->disableOriginalConstructor ()
40- ->onlyMethods (['getCurrentPage ' , 'getCurrentAction ' , 'getDatePattern ' , 'getDateTimePattern ' , 'getTimePattern ' ])
41- ->getMock ();
42- $ crudMock ->method ('getCurrentPage ' )->willReturn ($ pageName );
43- $ crudMock ->method ('getCurrentAction ' )->willReturn ($ actionName );
44- $ crudMock ->method ('getDatePattern ' )->willReturn (DateTimeField::FORMAT_MEDIUM );
45- $ crudMock ->method ('getTimePattern ' )->willReturn (DateTimeField::FORMAT_MEDIUM );
46- $ crudMock ->method ('getDateTimePattern ' )->willReturn ([DateTimeField::FORMAT_MEDIUM , DateTimeField::FORMAT_MEDIUM ]);
46+ $ crudDto = new CrudDto ();
47+ $ crudDto ->setPageName ($ pageName );
48+ $ crudDto ->setCurrentAction ($ actionName );
49+ $ crudDto ->setDatePattern (DateTimeField::FORMAT_MEDIUM );
50+ $ crudDto ->setTimePattern (DateTimeField::FORMAT_MEDIUM );
51+ $ crudDto ->setDateTimePattern (DateTimeField::FORMAT_MEDIUM , DateTimeField::FORMAT_MEDIUM );
52+
53+ $ i18Dto = new I18nDto ($ requestLocale , TextDirection::LTR , 'messages ' , []);
4754
48- $ i18nMock = $ this ->getMockBuilder (I18nDto::class)
49- ->disableOriginalConstructor ()
50- ->onlyMethods (['getTranslationParameters ' , 'getTranslationDomain ' ])
51- ->getMock ();
52- $ i18nMock ->method ('getTranslationParameters ' )->willReturn ([]);
53- $ i18nMock ->method ('getTranslationDomain ' )->willReturn ('messages ' );
55+ $ reflectedClass = new \ReflectionClass (Request::class);
56+ $ request = $ reflectedClass ->newInstanceWithoutConstructor ();
57+ $ instanceProperty = $ reflectedClass ->getProperty ('locale ' );
58+ $ instanceProperty ->setValue ($ request , $ requestLocale );
5459
55- $ requestMock = $ this ->getMockBuilder (Request::class)
56- ->disableOriginalConstructor ()
57- ->onlyMethods (['getLocale ' ])
58- ->getMock ();
59- $ requestMock ->method ('getLocale ' )->willReturn ($ requestLocale );
60+ $ reflectedClass = new \ReflectionClass (TemplateRegistry::class);
61+ $ templateRegistry = $ reflectedClass ->newInstanceWithoutConstructor ();
6062
61- $ adminContextMock = $ this ->getMockBuilder (AdminContext::class)
62- ->disableOriginalConstructor ()
63- ->onlyMethods (['getRequest ' , 'getCrud ' , 'getI18n ' , 'getTemplatePath ' ])
64- ->getMock ();
65- $ adminContextMock
66- ->expects ($ this ->any ())
67- ->method ('getRequest ' )
68- ->willReturn ($ requestMock );
69- $ adminContextMock
70- ->expects ($ this ->any ())
71- ->method ('getCrud ' )
72- ->willReturn ($ crudMock );
73- $ adminContextMock
74- ->expects ($ this ->any ())
75- ->method ('getI18n ' )
76- ->willReturn ($ i18nMock );
77- $ adminContextMock
78- ->expects ($ this ->any ())
79- ->method ('getTemplatePath ' )
80- ->willReturn ('@EasyAdmin/layout.html.twig ' ); // return any path to avoid injecting a TemplateRegistry
63+ $ reflectedClass = new \ReflectionClass (AdminContext::class);
64+ $ adminContext = $ reflectedClass ->newInstanceWithoutConstructor ();
65+ $ requestProperty = $ reflectedClass ->getProperty ('request ' );
66+ $ requestProperty ->setValue ($ adminContext , $ request );
67+ $ crudDtoProperty = $ reflectedClass ->getProperty ('crudDto ' );
68+ $ crudDtoProperty ->setValue ($ adminContext , $ crudDto );
69+ $ i18nDtoProperty = $ reflectedClass ->getProperty ('i18nDto ' );
70+ $ i18nDtoProperty ->setValue ($ adminContext , $ i18Dto );
71+ $ templateRegistryProperty = $ reflectedClass ->getProperty ('templateRegistry ' );
72+ $ templateRegistryProperty ->setValue ($ adminContext , $ templateRegistry );
8173
82- return $ this ->adminContext = $ adminContextMock ;
74+ return $ this ->adminContext = $ adminContext ;
8375 }
8476
8577 protected function configure (FieldInterface $ field , string $ pageName = Crud::PAGE_INDEX , string $ requestLocale = 'en ' , string $ actionName = Action::INDEX ): FieldDto
0 commit comments