8000 [DoctrineBridge] Add new `DatePointDateType` Doctrine type by wkania · Pull Request #60237 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DoctrineBridge] Add new DatePointDateType Doctrine type #60237

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

Open
wants to merge 1 commit into
base: 7.4
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
5 changes: 5 additions & 0 deletions src/Symfony/Bridge/Doctrine/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.4
---

* Add support for `Symfony\Component\Clock\DatePoint` as `DatePointDateType` Doctrine type

7.3
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass;

use Symfony\Bridge\Doctrine\Types\DatePointType;
use Symfony\Bridge\Doctrine\Types\DatePointDateType;
use Symfony\Component\Clock\DatePoint;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -30,7 +31,10 @@ public function process(ContainerBuilder $container): void

$types = $container->getParameter('doctrine.dbal.connection_factory.types');

$types['date_point'] ??= ['class' => DatePointType::class];
if (is_array($types)) {
$types['date_point'] ??= ['class' => DatePointType::class];
$types['date_point_date'] ??= ['class' => DatePointDateType::class];
}

$container->setParameter('doctrine.dbal.connection_factory.types', $types);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterDatePointTypePass;
use Symfony\Bridge\Doctrine\Types\DatePointType;
use Symfony\Bridge\Doctrine\Types\DatePointDateType;
use Symfony\Component\Clock\DatePoint;
use Symfony\Component\DependencyInjection\ContainerBuilder;

Expand All @@ -35,6 +36,7 @@ public function testRegistered()
$expected = [
'foo' => 'bar',
'date_point' => ['class' => DatePointType::class],
'date_point_date' => ['class' => DatePointDateType::class],
];
$this->assertSame($expected, $container->getParameter('doctrine.dbal.connection_factory.types'));
}
Expand Down
106 changes: 106 additions & 0 deletions src/Symfony/Bridge/Doctrine/Tests/Types/DatePointDateTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Tests\Types;

use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Types\Type;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Types\DatePointDateType;
use Symfony\Component\Clock\DatePoint;

final class DatePointDateTypeTest extends TestCase
{
private DatePointDateType $type;

public static function setUpBeforeClass(): void
{
$name = DatePointDateType::NAME;
if (Type::hasType($name)) {
Type::overrideType($name, DatePointDateType::class);
} else {
Type::addType($name, DatePointDateType::class);
}
}

protected function setUp(): void
{
if (!class_exists(DatePoint::class)) {
self::markTestSkipped('The DatePoint class is not available.');
}
$this->type = Type::getType(DatePointDateType::NAME);
}

public function testDatePointConvertsToDatabaseValue()
{
$datePoint = DatePoint::createFromFormat('!Y-m-d', '2025-03-03');

$expected = $datePoint->format('Y-m-d');
$actual = $this->type->convertToDatabaseValue($datePoint, new PostgreSQLPlatform());

$this->assertSame($expected, $actual);
}

public function testDatePointConvertsToPHPValue()
{
$datePoint = new DatePoint();
$actual = $this->type->convertToPHPValue($datePoint, self::getSqlitePlatform());

$this->assertSame($datePoint, $actual);
}

public function testNullConvertsToPHPValue()
{
$actual = $this->type->convertToPHPValue(null, self::getSqlitePlatform());

$this->assertNull($actual);
}

public function testDateTimeImmutableConvertsToPHPValue()
{
$format = 'Y-m-d H:i:s.u';
$date = '2025-03-03';
$dateTime = \DateTimeImmutable::createFromFormat('!Y-m-d', $date);
$actual = $this->type->convertToPHPValue($dateTime, self::getSqlitePlatform());
$expected = DatePoint::createFromFormat('!Y-m-d', $date);

$this->assertInstanceOf(DatePoint::class, $actual);
$this->assertSame($expected->format($format), $actual->format($format));
}

public function testDatabaseValueConvertsToPHPValue()
{
$format = 'Y-m-d H:i:s.u';
$date = '2025-03-03';
$actual = $this->type->convertToPHPValue($date, new PostgreSQLPlatform());
$expected = DatePoint::createFromFormat('!Y-m-d', $date);

$this->assertInstanceOf(DatePoint::class, $actual);
$this->assertSame($expected->format($format), $actual->format($format));
}

public function testGetName()
{
$this->assertSame('date_point_date', $this->type->getName());
}

private static function getSqlitePlatform(): AbstractPlatform
{
if (interface_exists(Exception::class)) {
// DBAL 4+
return new \Doctrine\DBAL\Platforms\SQLitePlatform();
}

return new \Doctrine\DBAL\Platforms\SqlitePlatform();
}
}
40 changes: 40 additions & 0 deletions src/Symfony/Bridge/Doctrine/Types/DatePointDateType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Types;

< 6D40 /td> use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\DateImmutableType;
use Symfony\Component\Clock\DatePoint;

final class DatePointDateType extends DateImmutableType
{
public const NAME = 'date_point_date';

/**
* @return ($value is null ? null : DatePoint)
*/
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?DatePoint
{
if (null === $value || $value instanceof DatePoint) {
return $value;
}

$value = parent::convertToPHPValue($value, $platform);

return DatePoint::createFromInterface($value);
}

public function getName(): string
{
return self::NAME;
}
}
0