diff --git a/src/Transformer/Implementation/DateTimeTransformer.php b/src/Transformer/Implementation/DateTimeTransformer.php index 7b3b0b2c..b9387739 100644 --- a/src/Transformer/Implementation/DateTimeTransformer.php +++ b/src/Transformer/Implementation/DateTimeTransformer.php @@ -50,10 +50,6 @@ public function transform( return $target; } - if ($target !== null) { - throw new InvalidArgumentException(sprintf('Target must be null unless it is a DateTime, "%s" given', get_debug_type($target)), context: $context); - } - if (TypeCheck::isObjectOfType($targetType, \DateTime::class)) { return \DateTime::createFromInterface($source); } diff --git a/tests/Fixtures/DateTime/ObjectWithDateTime.php b/tests/Fixtures/DateTime/ObjectWithDateTime.php new file mode 100644 index 00000000..7a54d91a --- /dev/null +++ b/tests/Fixtures/DateTime/ObjectWithDateTime.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +namespace Rekalogika\Mapper\Tests\Fixtures\DateTime; + +use Symfony\Component\Clock\DatePoint; + +class ObjectWithDateTime +{ + public const DATETIME = '2024-01-01 00:00:00'; + + public function getDateTimeImmutable(): \DateTimeImmutable + { + return new \DateTimeImmutable(self::DATETIME); + } + public function getDateTime(): \DateTime + { + return new \DateTime(self::DATETIME); + } + + public function getDatePoint(): DatePoint + { + return new DatePoint(self::DATETIME); + } +} diff --git a/tests/Fixtures/DateTime/ObjectWithDateTimeDto.php b/tests/Fixtures/DateTime/ObjectWithDateTimeDto.php new file mode 100644 index 00000000..8d72a483 --- /dev/null +++ b/tests/Fixtures/DateTime/ObjectWithDateTimeDto.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +namespace Rekalogika\Mapper\Tests\Fixtures\DateTime; + +use Symfony\Component\Clock\DatePoint; + +class ObjectWithDateTimeDto +{ + public const DATETIME = '2023-01-01 00:00:00'; + + public static function getInitialized(): self + { + $dto = new self(); + $dto->dateTimeImmutable = new \DateTimeImmutable(self::DATETIME); + $dto->dateTime = new \DateTime(self::DATETIME); + $dto->datePoint = new DatePoint(self::DATETIME); + + return $dto; + } + + public ?\DateTimeImmutable $dateTimeImmutable = null; + public ?\DateTime $dateTime = null; + public ?DatePoint $datePoint = null; +} diff --git a/tests/IntegrationTest/DateTimeMappingTest.php b/tests/IntegrationTest/DateTimeMappingTest.php index 6ef5f0c0..ead58f1d 100644 --- a/tests/IntegrationTest/DateTimeMappingTest.php +++ b/tests/IntegrationTest/DateTimeMappingTest.php @@ -14,6 +14,8 @@ namespace Rekalogika\Mapper\Tests\IntegrationTest; use Rekalogika\Mapper\Tests\Common\FrameworkTestCase; +use Rekalogika\Mapper\Tests\Fixtures\DateTime\ObjectWithDateTime; +use Rekalogika\Mapper\Tests\Fixtures\DateTime\ObjectWithDateTimeDto; use Symfony\Component\Clock\DatePoint; class DateTimeMappingTest extends FrameworkTestCase @@ -54,4 +56,33 @@ public static function dateTimeProvider(): iterable } } } + + public function testObjectWithDateTime(): void + { + $source = new ObjectWithDateTime(); + $target = $this->mapper->map($source, ObjectWithDateTimeDto::class); + + $this->assertInstanceOf(ObjectWithDateTimeDto::class, $target); + $this->assertInstanceOf(\DateTimeImmutable::class, $target->dateTimeImmutable); + $this->assertInstanceOf(\DateTime::class, $target->dateTime); + $this->assertInstanceOf(DatePoint::class, $target->datePoint); + $this->assertEquals('2024-01-01 00:00:00', $target->dateTimeImmutable->format('Y-m-d H:i:s')); + $this->assertEquals('2024-01-01 00:00:00', $target->dateTime->format('Y-m-d H:i:s')); + $this->assertEquals('2024-01-01 00:00:00', $target->datePoint->format('Y-m-d H:i:s')); + } + + public function testObjectWithDateTimeWithTargetHavingExistingValues(): void + { + $source = new ObjectWithDateTime(); + $target = ObjectWithDateTimeDto::getInitialized(); + $target = $this->mapper->map($source, $target); + + $this->assertInstanceOf(ObjectWithDateTimeDto::class, $target); + $this->assertInstanceOf(\DateTimeImmutable::class, $target->dateTimeImmutable); + $this->assertInstanceOf(\DateTime::class, $target->dateTime); + $this->assertInstanceOf(DatePoint::class, $target->datePoint); + $this->assertEquals('2024-01-01 00:00:00', $target->dateTimeImmutable->format('Y-m-d H:i:s')); + $this->assertEquals('2024-01-01 00:00:00', $target->dateTime->format('Y-m-d H:i:s')); + $this->assertEquals('2024-01-01 00:00:00', $target->datePoint->format('Y-m-d H:i:s')); + } }