14
14
15
15
class AutoMapperTest extends TestCase
16
16
{
17
- public function testAutoMapping ()
17
+ private $ compiler ;
18
+
19
+ public function setUp ()
18
20
{
19
- $ configuration = new MapperConfiguration ( new Compiler (new PropertyInfoExtractor (
21
+ $ this -> compiler = new Compiler (new PropertyInfoExtractor (
20
22
[new ReflectionExtractor ()],
21
23
[new ReflectionExtractor (), new PhpDocExtractor ()],
22
24
[new ReflectionExtractor ()],
23
25
[new ReflectionExtractor ()]
24
26
),
25
27
new Accessor (),
26
28
new TransformerFactory ()
27
- ), User::class, UserDTO::class);
29
+ );
30
+ }
31
+
32
+ public function testAutoMapping ()
33
+ {
34
+ $ configurationUser = new MapperConfiguration ($ this ->compiler , User::class, UserDTO::class);
35
+ $ configurationAddress = new MapperConfiguration ($ this ->compiler , Address::class, AddressDTO::class);
28
36
29
37
$ automapper = new AutoMapper ();
30
- $ automapper ->register ($ configuration );
38
+ $ automapper ->register ($ configurationUser );
39
+ $ automapper ->register ($ configurationAddress );
31
40
41
+ $ address = new Address ();
42
+ $ address ->setCity ('Toulon ' );
32
43
$ user = new User (1 , 'yolo ' , '13 ' );
44
+ $ user ->address = $ address ;
45
+ $ user ->addresses [] = $ address ;
46
+
33
47
/** @var UserDTO $userDto */
34
48
$ userDto = $ automapper ->map ($ user , UserDTO::class);
35
49
@@ -38,6 +52,11 @@ public function testAutoMapping()
38
52
self ::assertEquals ('yolo ' , $ userDto ->name );
39
53
self ::assertEquals ('13 ' , $ userDto ->age );
40
54
self ::assertNull ($ userDto ->email );
55
+ self ::assertCount (1 , $ userDto ->addresses );
56
+ self ::assertInstanceOf (AddressDTO::class, $ userDto ->address );
57
+ self ::assertInstanceOf (AddressDTO::class, $ userDto ->addresses [0 ]);
58
+ self ::assertEquals ('Toulon ' , $ userDto ->address ->city );
59
+ self ::assertEquals ('Toulon ' , $ userDto ->addresses [0 ]->city );
41
60
}
42
61
}
43
62
@@ -52,20 +71,30 @@ class User
52
71
*/
53
72
public $ name ;
54
73
/**
55
- * @var string
74
+ * @var string|int
56
75
*/
57
76
public $ age ;
58
77
/**
59
78
* @var string
60
79
*/
61
80
private $ email ;
62
81
63
- public function __construct ($ id , $ name , $ age , $ email = 'test ' )
82
+ /**
83
+ * @var Address
84
+ */
85
+ public $ address ;
86
+
87
+ /**
88
+ * @var Address[]
89
+ */
90
+ public $ addresses = [];
91
+
92
+ public function __construct ($ id , $ name , $ age )
64
93
{
65
94
$ this ->id = $ id ;
66
95
$ this ->name = $ name ;
67
96
$ this ->age = $ age ;
68
- $ this ->email = $ email ;
97
+ $ this ->email = ' test ' ;
69
98
}
70
99
71
100
/**
@@ -77,6 +106,30 @@ public function getId()
77
106
}
78
107
}
79
108
109
+ class Address
110
+ {
111
+ /**
112
+ * @var string|null
113
+ */
114
+ private $ city ;
115
+
116
+ /**
117
+ * @return string
118
+ */
119
+ public function getCity (): ?string
120
+ {
121
+ return $ this ->city ;
122
+ }
123
+
124
+ /**
125
+ * @param string $city
126
+ */
127
+ public function setCity (?string $ city ): void
128
+ {
129
+ $ this ->city = $ city ;
130
+ }
131
+ }
132
+
80
133
class UserDTO
81
134
{
82
135
/**
@@ -88,11 +141,29 @@ class UserDTO
88
141
*/
89
142
public $ name ;
90
143
/**
91
- * @var string
144
+ * @var string|int
92
145
*/
93
146
public $ age ;
94
147
/**
95
148
* @var string
96
149
*/
97
150
public $ email ;
151
+
152
+ /**
153
+ * @var AddressDTO
154
+ */
155
+ public $ address ;
156
+
157
+ /**
158
+ * @var AddressDTO[]
159
+ */
160
+ public $ addresses = [];
161
+ }
162
+
163
+ class AddressDTO
164
+ {
165
+ /**
166
+ * @var string|null
167
+ */
168
+ public $ city ;
98
169
}
0 commit comments