16
16
use Rekalogika \Mapper \Tests \Common \FrameworkTestCase ;
17
17
use Rekalogika \Mapper \Tests \Fixtures \DynamicProperty \AnotherObjectExtendingStdClass ;
18
18
use Rekalogika \Mapper \Tests \Fixtures \DynamicProperty \ObjectExtendingStdClass ;
19
+ use Rekalogika \Mapper \Tests \Fixtures \DynamicProperty \ObjectExtendingStdClassWithExplicitScalarProperties ;
19
20
use Rekalogika \Mapper \Tests \Fixtures \DynamicProperty \ObjectExtendingStdClassWithProperties ;
20
21
use Rekalogika \Mapper \Tests \Fixtures \Scalar \ObjectWithScalarProperties ;
21
22
use Rekalogika \Mapper \Tests \Fixtures \ScalarDto \ObjectWithScalarPropertiesDto ;
@@ -35,10 +36,10 @@ public function testStdClassToObject(): void
35
36
$ target = $ this ->mapper ->map ($ source , ObjectWithScalarPropertiesDto::class);
36
37
37
38
$ this ->assertInstanceOf (ObjectWithScalarPropertiesDto::class, $ target );
38
- $ this ->assertSame (1 , $ target ->a );
39
- $ this ->assertSame ('string ' , $ target ->b );
39
+ $ this ->assertEquals (1 , $ target ->a );
40
+ $ this ->assertEquals ('string ' , $ target ->b );
40
41
$ this ->assertTrue ($ target ->c );
41
- $ this ->assertSame (1.1 , $ target ->d );
42
+ $ this ->assertEquals (1.1 , $ target ->d );
42
43
}
43
44
44
45
public function testObjectExtendingStdClassToObject (): void
@@ -56,10 +57,10 @@ public function testObjectExtendingStdClassToObject(): void
56
57
$ target = $ this ->mapper ->map ($ source , ObjectWithScalarPropertiesDto::class);
57
58
58
59
$ this ->assertInstanceOf (ObjectWithScalarPropertiesDto::class, $ target );
59
- $ this ->assertSame (1 , $ target ->a );
60
- $ this ->assertSame ('string ' , $ target ->b );
60
+ $ this ->assertEquals (1 , $ target ->a );
61
+ $ this ->assertEquals ('string ' , $ target ->b );
61
62
$ this ->assertTrue ($ target ->c );
62
- $ this ->assertSame (1.1 , $ target ->d );
63
+ $ this ->assertEquals (1.1 , $ target ->d );
63
64
}
64
65
65
66
public function testArrayCastToObjectToObject (): void
@@ -74,10 +75,10 @@ public function testArrayCastToObjectToObject(): void
74
75
$ target = $ this ->mapper ->map ((object ) $ source , ObjectWithScalarPropertiesDto::class);
75
76
76
77
$ this ->assertInstanceOf (ObjectWithScalarPropertiesDto::class, $ target );
77
- $ this ->assertSame (1 , $ target ->a );
78
- $ this ->assertSame ('string ' , $ target ->b );
78
+ $ this ->assertEquals (1 , $ target ->a );
79
+ $ this ->assertEquals ('string ' , $ target ->b );
79
80
$ this ->assertTrue ($ target ->c );
80
- $ this ->assertSame (1.1 , $ target ->d );
81
+ $ this ->assertEquals (1.1 , $ target ->d );
81
82
}
82
83
83
84
public function testStdClassWithoutPropertiesToObject (): void
@@ -92,6 +93,38 @@ public function testStdClassWithoutPropertiesToObject(): void
92
93
$ this ->assertNull ($ target ->d );
93
94
}
94
95
96
+ public function testStdClassWithExtraPropertyToObject (): void
97
+ {
98
+ $ source = new \stdClass ();
99
+ $ source ->a = 1 ;
100
+ $ source ->b = 'string ' ;
101
+ $ source ->c = true ;
102
+ $ source ->d = 1.1 ;
103
+ $ source ->e = 'extra ' ;
104
+
105
+ $ target = $ this ->mapper ->map ($ source , ObjectWithScalarPropertiesDto::class);
106
+
107
+ $ this ->assertInstanceOf (ObjectWithScalarPropertiesDto::class, $ target );
108
+ $ this ->assertEquals (1 , $ target ->a );
109
+ $ this ->assertEquals ('string ' , $ target ->b );
110
+ $ this ->assertTrue ($ target ->c );
111
+ $ this ->assertEquals (1.1 , $ target ->d );
112
+ }
113
+
114
+ public function testObjectExtendingStdClassWithExplicitScalarPropertiesToObject (): void
115
+ {
116
+ $ source = new ObjectExtendingStdClassWithExplicitScalarProperties ();
117
+ /** @psalm-suppress UndefinedPropertyAssignment */
118
+ $ source ->e = 'extra ' ;
119
+ $ target = $ this ->mapper ->map ($ source , ObjectWithScalarPropertiesDto::class);
120
+
121
+ $ this ->assertInstanceOf (ObjectWithScalarPropertiesDto::class, $ target );
122
+ $ this ->assertEquals (1 , $ target ->a );
123
+ $ this ->assertEquals ('string ' , $ target ->b );
124
+ $ this ->assertTrue ($ target ->c );
125
+ $ this ->assertEquals (1.1 , $ target ->d );
126
+ }
127
+
95
128
// to stdClass
96
129
97
130
public function testObjectToStdClass (): void
@@ -101,10 +134,10 @@ public function testObjectToStdClass(): void
101
134
102
135
$ this ->assertInstanceOf (\stdClass::class, $ target );
103
136
104
- $ this ->assertSame (1 , $ target ->a );
105
- $ this ->assertSame ('string ' , $ target ->b );
137
+ $ this ->assertEquals (1 , $ target ->a );
138
+ $ this ->assertEquals ('string ' , $ target ->b );
106
139
$ this ->assertTrue ($ target ->c );
107
- $ this ->assertSame (1.1 , $ target ->d );
140
+ $ this ->assertEquals (1.1 , $ target ->d );
108
141
}
109
142
110
143
public function testObjectToObjectExtendingStdClass (): void
@@ -115,13 +148,13 @@ public function testObjectToObjectExtendingStdClass(): void
115
148
$ this ->assertInstanceOf (ObjectExtendingStdClass::class, $ target );
116
149
117
150
/** @psalm-suppress UndefinedPropertyFetch */
118
- $ this ->assertSame (1 , $ target ->a );
151
+ $ this ->assertEquals (1 , $ target ->a );
119
152
/** @psalm-suppress UndefinedPropertyFetch */
120
- $ this ->assertSame ('string ' , $ target ->b );
153
+ $ this ->assertEquals ('string ' , $ target ->b );
121
154
/** @psalm-suppress UndefinedPropertyFetch */
122
155
$ this ->assertTrue ($ target ->c );
123
156
/** @psalm-suppress UndefinedPropertyFetch */
124
- $ this ->assertSame (1.1 , $ target ->d );
157
+ $ this ->assertEquals (1.1 , $ target ->d );
125
158
}
126
159
127
160
// stdclass to stdclass
@@ -142,13 +175,13 @@ public function testStdClassToStdClass(): void
142
175
143
176
$ this ->assertInstanceOf (\stdClass::class, $ target );
144
177
/** @psalm-suppress UndefinedPropertyFetch */
145
- $ this ->assertSame (1 , $ target ->a );
178
+ $ this ->assertEquals (1 , $ target ->a );
146
179
/** @psalm-suppress UndefinedPropertyFetch */
147
- $ this ->assertSame ('string ' , $ target ->b );
180
+ $ this ->assertEquals ('string ' , $ target ->b );
148
181
/** @psalm-suppress UndefinedPropertyFetch */
149
182
$ this ->assertTrue ($ target ->c );
150
183
/** @psalm-suppress UndefinedPropertyFetch */
151
- $ this ->assertSame (1.1 , $ target ->d );
184
+ $ this ->assertEquals (1.1 , $ target ->d );
152
185
}
153
186
154
187
public function testStdClassToStdClassWithExplicitProperties (): void
@@ -162,10 +195,41 @@ public function testStdClassToStdClassWithExplicitProperties(): void
162
195
$ target = $ this ->mapper ->map ($ source , ObjectExtendingStdClassWithProperties::class);
163
196
164
197
$ this ->assertInstanceOf (\stdClass::class, $ target );
165
- $ this ->assertSame ('public ' , $ target ->public );
198
+ $ this ->assertEquals ('public ' , $ target ->public );
166
199
$ this ->assertNull ($ target ->getPrivate ());
167
200
$ this ->assertEquals ('constructor ' , $ target ->getConstructor ());
168
201
/** @psalm-suppress UndefinedPropertyFetch */
169
- $ this ->assertSame ('dynamic ' , $ target ->dynamic );
202
+ $ this ->assertEquals ('dynamic ' , $ target ->dynamic );
203
+ }
204
+
205
+ public function testStdClassToStdClassWithExistingValue (): void
206
+ {
207
+ $ source = new \stdClass ();
208
+ $ source ->property = new ObjectWithScalarProperties ();
209
+
210
+ $ target = new \stdClass ();
211
+ $ targetProperty = new ObjectWithScalarPropertiesDto ();
212
+ $ target ->property = $ targetProperty ;
213
+
214
+ $ this ->mapper ->map ($ source , $ target );
215
+
216
+ $ this ->assertSame ($ targetProperty , $ target ->property );
217
+ }
218
+
219
+ public function testStdClassToStdClassWithExistingNullValue (): void
220
+ {
221
+ $ source = new \stdClass ();
222
+ $ source ->property = new ObjectWithScalarProperties ();
223
+
224
+ $ target = new \stdClass ();
225
+ $ target ->property = null ;
226
+
227
+ $ this ->mapper ->map ($ source , $ target );
228
+
229
+ /**
230
+ * @psalm-suppress TypeDoesNotContainType
231
+ * @phpstan-ignore-next-line
232
+ */
233
+ $ this ->assertInstanceOf (ObjectWithScalarProperties::class, $ target ->property );
170
234
}
171
235
}
0 commit comments