Skip to content

Commit

Permalink
fix: mapping to object with non null datetime (#91)
Browse files Browse the repository at this point in the history
* add support of dateTimeImmutable on the owning side

* fix: mapping to object with non null datetime

* remove null requirement on the target side

* fix datepoint

---------

Co-authored-by: Vladyslav Yarysh <vladyslav.yarysh@macpaw.com>
  • Loading branch information
priyadi and gabplch authored Aug 24, 2024
1 parent c4ead94 commit 444dc07
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 4 deletions.
4 changes: 0 additions & 4 deletions src/Transformer/Implementation/DateTimeTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
35 changes: 35 additions & 0 deletions tests/Fixtures/DateTime/ObjectWithDateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/mapper package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* 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);
}
}
35 changes: 35 additions & 0 deletions tests/Fixtures/DateTime/ObjectWithDateTimeDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/mapper package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* 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;
}
31 changes: 31 additions & 0 deletions tests/IntegrationTest/DateTimeMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'));
}
}

0 comments on commit 444dc07

Please sign in to comment.