2
2
3
3
namespace EasyCorp \Bundle \EasyAdminBundle \Tests \Field ;
4
4
5
+ use Doctrine \ORM \Mapping \ClassMetadata ;
5
6
use EasyCorp \Bundle \EasyAdminBundle \Config \Action ;
6
7
use EasyCorp \Bundle \EasyAdminBundle \Config \Crud ;
8
+ use EasyCorp \Bundle \EasyAdminBundle \Config \Option \TextDirection ;
7
9
use EasyCorp \Bundle \EasyAdminBundle \Context \AdminContext ;
10
+ use EasyCorp \Bundle \EasyAdminBundle \Contracts \Context \AdminContextInterface ;
8
11
use EasyCorp \Bundle \EasyAdminBundle \Contracts \Field \FieldInterface ;
9
12
use EasyCorp \Bundle \EasyAdminBundle \Dto \CrudDto ;
10
13
use EasyCorp \Bundle \EasyAdminBundle \Dto \EntityDto ;
11
14
use EasyCorp \Bundle \EasyAdminBundle \Dto \FieldDto ;
12
15
use EasyCorp \Bundle \EasyAdminBundle \Dto \I18nDto ;
13
16
use EasyCorp \Bundle \EasyAdminBundle \Field \DateTimeField ;
17
+ use EasyCorp \Bundle \EasyAdminBundle \Registry \TemplateRegistry ;
14
18
use Symfony \Bundle \FrameworkBundle \Test \KernelTestCase ;
15
19
use Symfony \Component \HttpFoundation \Request ;
16
20
@@ -22,64 +26,52 @@ abstract class AbstractFieldTest extends KernelTestCase
22
26
23
27
protected function getEntityDto (): EntityDto
24
28
{
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 ();
30
31
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 ;
32
40
}
33
41
34
- private function getAdminContext (string $ pageName , string $ requestLocale , string $ actionName ): AdminContext
42
+ private function getAdminContext (string $ pageName , string $ requestLocale , string $ actionName ): AdminContextInterface
35
43
{
36
44
self ::bootKernel ();
37
45
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 ' , []);
47
54
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 );
54
59
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 ();
60
62
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 );
81
73
82
- return $ this ->adminContext = $ adminContextMock ;
74
+ return $ this ->adminContext = $ adminContext ;
83
75
}
84
76
85
77
protected function configure (FieldInterface $ field , string $ pageName = Crud::PAGE_INDEX , string $ requestLocale = 'en ' , string $ actionName = Action::INDEX ): FieldDto
0 commit comments