2
2
3
3
namespace EasyCorp \Bundle \EasyAdminBundle \Tests \Field ;
4
4
5
+ use EasyCorp \Bundle \EasyAdminBundle \Dto \EntityDto ;
5
6
use EasyCorp \Bundle \EasyAdminBundle \Field \Configurator \MoneyConfigurator ;
6
7
use EasyCorp \Bundle \EasyAdminBundle \Field \MoneyField ;
7
8
use EasyCorp \Bundle \EasyAdminBundle \Intl \IntlFormatter ;
8
- use Symfony \Component \PropertyAccess \PropertyAccessor ;
9
9
10
10
class MoneyFieldTest extends AbstractFieldTest
11
11
{
12
12
protected function setUp (): void
13
13
{
14
14
parent ::setUp ();
15
15
16
- $ intlFormatterMock = $ this ->getMockBuilder (IntlFormatter::class)
17
- ->disableOriginalConstructor ()
18
- ->onlyMethods (['formatCurrency ' ])
19
- ->getMock ();
20
- $ intlFormatterMock ->method ('formatCurrency ' )->willReturnCallback (
21
- function ($ value ) { return $ value .'€ ' ; }
22
- );
23
-
24
- $ propertyAccessorMock = $ this ->getMockBuilder (PropertyAccessor::class)
25
- ->disableOriginalConstructor ()
26
- ->onlyMethods (['isReadable ' , 'getValue ' ])
27
- ->getMock ();
28
- $ propertyAccessorMock ->method ('isReadable ' )->willReturn (true );
29
- $ propertyAccessorMock ->method ('getValue ' )->willReturn ('USD ' );
30
-
31
- $ this ->configurator = new MoneyConfigurator ($ intlFormatterMock , $ propertyAccessorMock );
16
+ $ this ->configurator = new MoneyConfigurator (new IntlFormatter (), static ::getContainer ()->get ('property_accessor ' ));
32
17
}
33
18
34
19
public function testFieldWithoutCurrency ()
@@ -72,6 +57,25 @@ public function testFieldWithHardcodedCurrency()
72
57
self ::assertSame ('EUR ' , $ fieldDto ->getFormTypeOption ('currency ' ));
73
58
}
74
59
60
+ protected function getEntityDto (): EntityDto
61
+ {
62
+ $ reflectedClass = new \ReflectionClass (EntityDto::class);
63
+ $ entityDto = $ reflectedClass ->newInstanceWithoutConstructor ();
64
+ $ primaryKeyNameProperty = $ reflectedClass ->getProperty ('primaryKeyName ' );
65
+ $ primaryKeyNameProperty ->setValue ($ entityDto , 'id ' );
66
+ $ primaryKeyValueProperty = $ reflectedClass ->getProperty ('primaryKeyValue ' );
67
+ $ primaryKeyValueProperty ->setValue ($ entityDto , 1 );
68
+ $ fqcnProperty = $ reflectedClass ->getProperty ('fqcn ' );
69
+ $ fqcnProperty ->setValue ($ entityDto , 'App\Entity\MyEntity ' );
70
+ $ instanceProperty = $ reflectedClass ->getProperty ('instance ' );
71
+ $ instanceProperty ->setValue ($ entityDto , new class {
72
+ public int $ id = 1 ;
73
+ public string $ bar = 'USD ' ;
74
+ });
75
+
76
+ return $ this ->entityDto = $ entityDto ;
77
+ }
78
+
75
79
public function testFieldWithPropertyPathCurrency ()
76
80
{
77
81
$ field = MoneyField::new ('foo ' )->setValue (100 )->setCurrencyPropertyPath ('bar ' );
@@ -97,7 +101,7 @@ public function testFieldsDefaultsToCents()
97
101
{
98
102
$ field = MoneyField::new ('foo ' )->setValue (100 )->setCurrency ('EUR ' );
99
103
$ fieldDto = $ this ->configure ($ field );
100
- self ::assertSame ('1€ ' , $ fieldDto ->getFormattedValue ());
104
+ self ::assertSame ('€1.00 ' , $ fieldDto ->getFormattedValue ());
101
105
self ::assertSame (100 , $ fieldDto ->getFormTypeOption ('divisor ' ));
102
106
}
103
107
@@ -106,7 +110,7 @@ public function testFieldCents()
106
110
$ field = MoneyField::new ('foo ' )->setValue (100 )->setCurrency ('EUR ' );
107
111
$ field ->setStoredAsCents (false );
108
112
$ fieldDto = $ this ->configure ($ field );
109
- self ::assertSame ('100€ ' , $ fieldDto ->getFormattedValue ());
113
+ self ::assertSame ('€100.00 ' , $ fieldDto ->getFormattedValue ());
110
114
self ::assertSame (1 , $ fieldDto ->getFormTypeOption ('divisor ' ));
111
115
}
112
116
@@ -115,7 +119,7 @@ public function testFieldWithCustomDivisor()
115
119
$ field = MoneyField::new ('foo ' )->setValue (725 )->setCurrency ('EUR ' );
116
120
$ field ->setFormTypeOption ('divisor ' , 10000 );
117
121
$ fieldDto = $ this ->configure ($ field );
118
- self ::assertSame ('0.0725€ ' , $ fieldDto ->getFormattedValue ());
122
+ self ::assertSame ('€0.07 ' , $ fieldDto ->getFormattedValue ());
119
123
self ::assertSame (10000 , $ fieldDto ->getFormTypeOption ('divisor ' ));
120
124
}
121
125
}
0 commit comments