Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[loggable] Allows the use of DateTimeImmutable in entities #721

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Model/Loggable/LoggableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Knp\DoctrineBehaviors\Model\Loggable;

use DateTime;
use DateTimeInterface;

trait LoggableTrait
{
Expand All @@ -17,7 +17,7 @@ public function getUpdateLogMessage(array $changeSets = []): string
for ($i = 0, $s = $itemCount; $i < $s; ++$i) {
$item = $changeSet[$i];

if ($item instanceof DateTime) {
if ($item instanceof DateTimeInterface) {
$changeSet[$i] = $item->format('Y-m-d H:i:s.u');
}
}
Expand Down
53 changes: 53 additions & 0 deletions tests/ORM/LoggableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Knp\DoctrineBehaviors\Tests\ORM;

use DateTime;
use DateTimeImmutable;
use DateTimeZone;
use Knp\DoctrineBehaviors\Tests\AbstractBehaviorTestCase;
use Knp\DoctrineBehaviors\Tests\Fixtures\Entity\LoggableEntity;
use Psr\Log\Test\TestLogger;
Expand Down Expand Up @@ -119,6 +122,56 @@ public function testShouldLogRemovalMessageWhenDeleted(): void
$this->assertSame($expectedMessage, $lastRecord['message']);
}

public function testDateTimeMustBeFormated(): void
{
$loggableEntity = new LoggableEntity();

$this->entityManager->persist($loggableEntity);
$this->entityManager->flush();

$loggableEntity->setDate(new DateTime('2023-02-11 10:00:00', new DateTimeZone('UTC')));

$this->entityManager->flush();

$expectedRecordCount = $this->isPostgreSql() ? 3 : 2;
$this->assertCount($expectedRecordCount, $this->testLogger->records);

$lastRecord = array_pop($this->testLogger->records);

$expectedMessage = sprintf(
'%s #1 : property "%s" changed from "" to "%s"',
LoggableEntity::class,
'dateTime',
'2023-02-11 10:00:00.000000'
);
$this->assertStringContainsString($expectedMessage, $lastRecord['message']);
}

public function testDateTimeImmutableMustBeFormated(): void
{
$loggableEntity = new LoggableEntity();

$this->entityManager->persist($loggableEntity);
$this->entityManager->flush();

$loggableEntity->setDate(new DateTimeImmutable('2023-02-11 10:00:00', new DateTimeZone('UTC')));

$this->entityManager->flush();

$expectedRecordCount = $this->isPostgreSql() ? 3 : 2;
$this->assertCount($expectedRecordCount, $this->testLogger->records);

$lastRecord = array_pop($this->testLogger->records);

$expectedMessage = sprintf(
'%s #1 : property "%s" changed from "" to "%s"',
LoggableEntity::class,
'dateTime',
'2023-02-11 10:00:00.000000'
);
$this->assertStringContainsString($expectedMessage, $lastRecord['message']);
}

private function doTestChangesetMessage(LoggableEntity $loggableEntity, string $field, string $expected): void
{
$this->entityManager->persist($loggableEntity);
Expand Down