1414
1515class AutoMapperTest extends TestCase
1616{
17- public function testAutoMapping ()
17+ private $ compiler ;
18+
19+ public function setUp ()
1820 {
19- $ configuration = new MapperConfiguration ( new Compiler (new PropertyInfoExtractor (
21+ $ this -> compiler = new Compiler (new PropertyInfoExtractor (
2022 [new ReflectionExtractor ()],
2123 [new ReflectionExtractor (), new PhpDocExtractor ()],
2224 [new ReflectionExtractor ()],
2325 [new ReflectionExtractor ()]
2426 ),
2527 new Accessor (),
2628 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);
2836
2937 $ automapper = new AutoMapper ();
30- $ automapper ->register ($ configuration );
38+ $ automapper ->register ($ configurationUser );
39+ $ automapper ->register ($ configurationAddress );
3140
41+ $ address = new Address ();
42+ $ address ->setCity ('Toulon ' );
3243 $ user = new User (1 , 'yolo ' , '13 ' );
44+ $ user ->address = $ address ;
45+ $ user ->addresses [] = $ address ;
46+
3347 /** @var UserDTO $userDto */
3448 $ userDto = $ automapper ->map ($ user , UserDTO::class);
3549
@@ -38,6 +52,11 @@ public function testAutoMapping()
3852 self ::assertEquals ('yolo ' , $ userDto ->name );
3953 self ::assertEquals ('13 ' , $ userDto ->age );
4054 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 );
4160 }
4261}
4362
@@ -52,20 +71,30 @@ class User
5271 */
5372 public $ name ;
5473 /**
55- * @var string
74+ * @var string|int
5675 */
5776 public $ age ;
5877 /**
5978 * @var string
6079 */
6180 private $ email ;
6281
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 )
6493 {
6594 $ this ->id = $ id ;
6695 $ this ->name = $ name ;
6796 $ this ->age = $ age ;
68- $ this ->email = $ email ;
97+ $ this ->email = ' test ' ;
6998 }
7099
71100 /**
@@ -77,6 +106,30 @@ public function getId()
77106 }
78107}
79108
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+
80133class UserDTO
81134{
82135 /**
@@ -88,11 +141,29 @@ class UserDTO
88141 */
89142 public $ name ;
90143 /**
91- * @var string
144+ * @var string|int
92145 */
93146 public $ age ;
94147 /**
95148 * @var string
96149 */
97150 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 ;
98169}
0 commit comments