8000 [DoctrineBridge] rename DatePointType to DateTimePointType, add new D… · symfony/symfony@acb035e · GitHub
[go: up one dir, main page]

Skip to content

Commit acb035e

Browse files
committed
[DoctrineBridge] rename DatePointType to DateTimePointType, add new DatePointType
1 parent ed7dba6 commit acb035e

File tree

7 files changed

+150
-9
lines changed

7 files changed

+150
-9
lines changed

src/Symfony/Bridge/Doctrine/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CHANGELOG
66

77
* Reset the manager registry using native lazy objects when applicable
88
* Deprecate the `DoctrineExtractor::getTypes()` method, use `DoctrineExtractor::getType()` instead
9-
* Add support for `Symfony\Component\Clock\DatePoint` as `DatePointType` Doctrine type
9+
* Add support for `Symfony\Component\Clock\DatePoint` as `DatePointType` and `DateTimePointType` Doctrine types
1010
* Improve exception message when `EntityValueResolver` gets no mapping information
1111

1212
7.2

src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/RegisterDatePointTypePass.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass;
1313

1414
use Symfony\Bridge\Doctrine\Types\DatePointType;
15+
use Symfony\Bridge\Doctrine\Types\DateTimePointType;
1516
use Symfony\Component\Clock\DatePoint;
1617
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1718
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -31,6 +32,7 @@ public function process(ContainerBuilder $container): void
3132
$types = $container->getParameter('doctrine.dbal.connection_factory.types');
3233

3334
$types['date_point'] ??= ['class' => DatePointType::class];
35+
$types['datetime_point'] ??= ['class' => DateTimePointType::class];
3436

3537
$container->setParameter('doctrine.dbal.connection_factory.types', $types);
3638
}

src/Symfony/Bridge/Doctrine/Tests/DependencyInjection/CompilerPass/RegisterDatePointTypePassTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterDatePointTypePass;
1616
use Symfony\Bridge\Doctrine\Types\DatePointType;
17+
use Symfony\Bridge\Doctrine\Types\DateTimePointType;
1718
use Symfony\Component\Clock\DatePoint;
1819
use Symfony\Component\DependencyInjection\ContainerBuilder;
1920

@@ -35,6 +36,7 @@ public function testRegistered()
3536
$expected = [
3637
'foo' => 'bar',
3738
'date_point' => ['class' => DatePointType::class],
39+
'datetime_point' => ['class' => DateTimePointType::class],
3840
];
3941
$this->assertSame($expected, $container->getParameter('doctrine.dbal.connection_factory.types'));
4042
}

src/Symfony/Bridge/Doctrine/Tests/Types/DatePointTypeTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ protected function setUp(): void
4343

4444
public function testDatePointConvertsToDatabaseValue()
4545
{
46-
$datePoint = new DatePoint('2025-03-03 12:13:14');
46+
$datePoint = DatePoint::createFromFormat('!Y-m-d', '2025-03-03');
4747

48-
$expected = $datePoint->format('Y-m-d H:i:s');
48+
$expected = $datePoint->format('Y-m-d');
4949
$actual = $this->type->convertToDatabaseValue($datePoint, new PostgreSQLPlatform());
5050

5151
$this->assertSame($expected, $actual);
@@ -68,10 +68,10 @@ public function testNullConvertsToPHPValue()
6868

6969
public function testDateTimeImmutableConvertsToPHPValue()
7070
{
71-
$format = 'Y-m-d H:i:s';
72-
$dateTime = new \DateTimeImmutable('2025-03-03 12:13:14');
73-
$actual = $this->type->convertToPHPValue($dateTime, self::getSqlitePlatform());
74-
$expected = DatePoint::createFromInterface($dateTime);
71+
$format = 'Y-m-d H:i:s.u';
72+
$date = '2025-03-03';
73+
$actual = $this->type->convertToPHPValue($date, self::getSqlitePlatform());
74+
$expected = DatePoint::createFromFormat('!Y-m-d', $date);
7575

7676
$this->assertSame($expected->format($format), $actual->format($format));
7777
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Types;
13+
14+
use Doctrine\DBAL\Exception;
15+
use Doctrine\DBAL\Platforms\AbstractPlatform;
16+
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
17+
use Doctrine\DBAL\Types\Type;
18+
use PHPUnit\Framework\TestCase;
19+
use Symfony\Bridge\Doctrine\Types\DateTimePointType;
20+
use Symfony\Component\Clock\DatePoint;
21+
22+
final class DateTimePointTypeTest extends TestCase
23+
{
24+
private DateTimePointType $type;
25+
26+
public static function setUpBeforeClass(): void
27+
{
28+
$name = DateTimePointType::NAME;
29+
if (Type::hasType($name)) {
30+
Type::overrideType($name, DateTimePointType::class);
31+
} else {
32+
Type::addType($name, DateTimePointType::class);
33+
}
34+
}
35+
36+
protected function setUp(): void
37+
{
38+
if (!class_exists(DatePoint::class)) {
39+
self::markTestSkipped('The DatePoint class is not available.');
40+
}
41+
$this->type = Type::getType(DateTimePointType::NAME);
42+
}
43+
44+
public function testDatePointConvertsToDatabaseValue()
45+
{
46+
$datePoint = new DatePoint('2025-03-03 12:13:14');
47+
48+
$expected = $datePoint->format('Y-m-d H:i:s');
49+
$actual = $this->type->convertToDatabaseValue($datePoint, new PostgreSQLPlatform());
50+
51+
$this->assertSame($expected, $actual);
52+
}
53+
54+
public function testDatePointConvertsToPHPValue()
55+
{
56+
$datePoint = new DatePoint();
57+
$actual = $this->type->convertToPHPValue($datePoint, self::getSqlitePlatform());
58+
59+
$this->assertSame($datePoint, $actual);
60+
}
61+
62+
public function testNullConvertsToPHPValue()
63+
{
64+
$actual = $this->type->convertToPHPValue(null, self::getSqlitePlatform());
65+
66+
$this->assertNull($actual);
67+
}
68+
69+
public function testDateTimeImmutableConvertsToPHPValue()
70+
{
71+
$format = 'Y-m-d H:i:s';
72+
$dateTime = new \DateTimeImmutable('2025-03-03 12:13:14');
73+
$actual = $this->type->convertToPHPValue($dateTime, self::getSqlitePlatform());
74+
$expected = DatePoint::createFromInterface($dateTime);
75+
76+
$this->assertSame($expected->format($format), $actual->format($format));
77+
}
78+
79+
public function testGetName()
80+
{
81+
$this->assertSame('datetime_point', $this->type->getName());
82+
}
83+
84+
private static function getSqlitePlatform(): AbstractPlatform
85+
{
86+
if (interface_exists(Exception::class)) {
87+
// DBAL 4+
88+
return new \Doctrine\DBAL\Platforms\SQLitePlatform();
89+
}
90+
91+
return new \Doctrine\DBAL\Platforms\SqlitePlatform();
92+
}
93+
}

src/Symfony/Bridge/Doctrine/Types/DatePointType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
namespace Symfony\Bridge\Doctrine\Types;
1313

1414
use Doctrine\DBAL\Platforms\AbstractPlatform;
15-
use Doctrine\DBAL\Types\DateTimeImmutableType;
15+
use Doctrine\DBAL\Types\DateImmutableType;
1616
use Symfony\Component\Clock\DatePoint;
1717

18-
final class DatePointType extends DateTimeImmutableType
18+
final class DatePointType extends DateImmutableType
1919
{
2020
public const NAME = 'date_point';
2121

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Types;
13+
14+
use Doctrine\DBAL\Platforms\AbstractPlatform;
15+
use Doctrine\DBAL\Types\DateTimeImmutableType;
16+
use Symfony\Component\Clock\DatePoint;
17+
18+
final class DateTimePointType extends DateTimeImmutableType
19+
{
20+
public const NAME = 'datetime_point';
21+
22+
/**
23+
* @param T $value
24+
*
25+
* @return (T is null ? null : DatePoint)
26+
*
27+
* @template T
28+
*/
29+
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?DatePoint
30+
{
31+
if (null === $value || $value instanceof DatePoint) {
32+
return $value;
33+
}
34+
35+
$value = parent::convertToPHPValue($value, $platform);
36+
37+
return DatePoint::createFromInterface($value);
38+
}
39+
40+
public function getName(): string
41+
{
42+
return self::NAME;
43+ 3720
}
44+
}

0 commit comments

Comments
 (0)
0