|
15 | 15 | use Psr\Container\ContainerInterface;
|
16 | 16 | use Symfony\Component\ObjectMapper\Exception\MappingException;
|
17 | 17 | use Symfony\Component\ObjectMapper\Exception\MappingTransformException;
|
| 18 | +use Symfony\Component\ObjectMapper\Exception\NoSuchPropertyException; |
18 | 19 | use Symfony\Component\ObjectMapper\Metadata\Mapping;
|
19 | 20 | use Symfony\Component\ObjectMapper\Metadata\ObjectMapperMetadataFactoryInterface;
|
20 | 21 | use Symfony\Component\ObjectMapper\Metadata\ReflectionObjectMapperMetadataFactory;
|
|
28 | 29 | use Symfony\Component\ObjectMapper\Tests\Fixtures\DeeperRecursion\RecursiveDto;
|
29 | 30 | use Symfony\Component\ObjectMapper\Tests\Fixtures\DeeperRecursion\Relation;
|
30 | 31 | use Symfony\Component\ObjectMapper\Tests\Fixtures\DeeperRecursion\RelationDto;
|
| 32 | +use Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultValueStdClass\TargetDto; |
31 | 33 | use Symfony\Component\ObjectMapper\Tests\Fixtures\Flatten\TargetUser;
|
32 | 34 | use Symfony\Component\ObjectMapper\Tests\Fixtures\Flatten\User;
|
33 | 35 | use Symfony\Component\ObjectMapper\Tests\Fixtures\Flatten\UserProfile;
|
@@ -236,8 +238,16 @@ public function testSourceOnly()
|
236 | 238 | $mapped = $mapper->map($a, SourceOnly::class);
|
237 | 239 | $this->assertInstanceOf(SourceOnly::class, $mapped);
|
238 | 240 | $this->assertSame('test', $mapped->mappedName);
|
| 241 | + } |
239 | 242 |
|
| 243 | + public function testSourceOnlyWithMagicMethods() |
| 244 | + { |
| 245 | + $mapper = new ObjectMapper(); |
240 | 246 | $a = new class {
|
| 247 | + public function __isset(string $key): bool { |
| 248 | + return $key === 'name'; |
| 249 | + } |
| 250 | + |
241
251 | public function __get(string $key): string
|
242 | 252 | {
|
243 | 253 | return match ($key) {
|
@@ -303,4 +313,27 @@ public function testMultipleTargetMapProperty()
|
303 | 313 | $this->assertEquals('donotmap', $c->foo);
|
304 | 314 | $this->assertEquals('foo', $c->doesNotExistInTargetB);
|
305 | 315 | }
|
| 316 | + |
| 317 | + public function testDefaultValueStdClass() |
| 318 | + { |
| 319 | + $this->expectException(NoSuchPropertyException::class); |
| 320 | + $u = new \stdClass(); |
| 321 | + $u->id = 'abc'; |
| 322 | + $mapper = new ObjectMapper(); |
| 323 | + $b = $mapper->map($u, TargetDto::class); |
| 324 | + $this->assertInstanceOf(TargetDto::class, $b); |
| 325 | + $this->assertEquals('abc', $b->id); |
| 326 | + $this->assertEquals(null, $b->optional); |
| 327 | + } |
| 328 | + |
| 329 | + public function testDefaultValueStdClassWithPropertyInfo() |
| 330 | + { |
| 331 | + $u = new \stdClass(); |
| 332 | + $u->id = 'abc'; |
| 333 | + $mapper = new ObjectMapper(propertyAccessor: PropertyAccess::createPropertyAccessorBuilder()->disableExceptionOnInvalidPropertyPath()->getPropertyAccessor()); |
| 334 | + $b = $mapper->map($u, TargetDto::class); |
| 335 | + $this->assertInstanceOf(TargetDto::class, $b); |
| 336 | + $this->assertEquals('abc', $b->id); |
| 337 | + $this->assertEquals(null, $b->optional); |
| 338 | + } |
306 | 339 | }
|
0 commit comments