8000 [DoctrineBridge] add new DatePointDateType Doctrine type · symfony/symfony@e5ec79a · GitHub
[go: up one dir, main page]

Skip to content
Dismiss alert

Commit e5ec79a

Browse files
committed
[DoctrineBridge] add new DatePointDateType Doctrine type
1 parent ed7dba6 commit e5ec79a

File tree

5 files changed

+154
-1
lines changed

5 files changed

+154
-1
lines changed

src/Symfony/Bridge/Doctrine/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Deprecate the `DoctrineExtractor::getTypes()` method, use `DoctrineExtractor::getType()` instead
99
* Add support for `Symfony\Component\Clock\DatePoint` as `DatePointType` Doctrine type
1010
* Improve exception message when `EntityValueResolver` gets no mapping information
11+
* Add support for `Symfony\Component\Clock\DatePoint` as `DatePointDateType` Doctrine type
1112

1213
7.2
1314
---

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

+5-1
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\DatePointDateType;
1516
use Symfony\Component\Clock\DatePoint;
1617
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1718
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -30,7 +31,10 @@ public function process(ContainerBuilder $container): void
3031

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

33-
$types['date_point'] ??= ['class' => DatePointType::class];
34+
if (is_array($types)) {
35+
$types['date_point'] ??= ['class' => DatePointType::class];
36+
$types['date_point_date'] ??= ['class' => DatePointDateType::class];
37+
}
3438

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

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

+2
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\DatePointDateType;
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+
'date_point_date' => ['class' => DatePointDateType::class],
3840
];
3941
$this->assertSame($expected, $container->getParameter('doctrine.dbal.connection_factory.types'));
4042
}
< 10000 td data-grid-cell-id="diff-96d1c7f588364e737c1a610e59bcf364b6e620456b4f311b202e8050f9c6f6fd-empty-66-1" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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\DatePointDateType;
20+
use Symfony\Component\Clock\DatePoint;
21+
22+
final class DatePointDateTypeTest extends TestCase
23+
{
24+
private DatePointDateType $type;
25+
26+
public static function setUpBeforeClass(): void
27+
{
28+
$name = DatePointDateType::NAME;
29+
if (Type::hasType($name)) {
30+
Type::overrideType($name, DatePointDateType::class);
31+
} else {
32+
Type::addType($name, DatePointDateType::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(DatePointDateType::NAME);
42+
}
43+
44+
public function testDatePointConvertsToDatabaseValue()
45+
{
46+
$datePoint = DatePoint::createFromFormat('!Y-m-d', '2025-03-03');
47+
48+
$expected = $datePoint->format('Y-m-d');
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+
+
$this->assertNull($actual);
67+
}
68+
69+
public function testDateTimeImmutableConvertsToPHPValue()
70+
{
71+
$format = 'Y-m-d H:i:s.u';
72+
$date = '2025-03-03';
73+
$dateTime = \DateTimeImmutable::createFromFormat('!Y-m-d', $date);
74+
$actual = $this->type->convertToPHPValue($dateTime, self::getSqlitePlatform());
75+
$expected = DatePoint::createFromFormat('!Y-m-d', $date);
76+
77+
$this->assertInstanceOf(DatePoint::class, $actual);
78+
$this->assertSame($expected->format($format), $actual->format($format));
79+
}
80+
81+
public function testDatabaseValueConvertsToPHPValue()
82+
{
83+
$format = 'Y-m-d H:i:s.u';
84+
$date = '2025-03-03';
85+
$actual = $this->type->convertToPHPValue($date, new PostgreSQLPlatform());
86+
$expected = DatePoint::createFromFormat('!Y-m-d', $date);
87+
88+
$this->assertInstanceOf(DatePoint::class, $actual);
89+
$this->assertSame($expected->format($format), $actual->format($format));
90+
}
91+
92+
public function testGetName()
93+
{
94+
$this->assertSame('date_point_date', $this->type->getName());
95+
}
96+
97+
private static function getSqlitePlatform(): AbstractPlatform
98+
{
99+
if (interface_exists(Exception::class)) {
100+
// DBAL 4+
101+
return new \Doctrine\DBAL\Platforms\SQLitePlatform();
102+
}
103+
104+
return new \Doctrine\DBAL\Platforms\SqlitePlatform();
105+
}
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\DateImmutableType;
16+
use Symfony\Component\Clock\DatePoint;
17+
18+
final class DatePointDateType extends DateImmutableType
19+
{
20+
public const NAME = 'date_point_date';
21+
22+
/**
23+
* @return ($value is null ? null : DatePoint)
24+
*/
25+
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?DatePoint
26+
{
27+
if (null === $value || $value instanceof DatePoint) {
28+
return $value;
29+
}
30+
31+
$value = parent::convertToPHPValue($value, $platform);
32+
33+
return DatePoint::createFromInterface($value);
34+
}
35+
36+
public function getName(): string
37+
{
38+
return self::NAME;
39+
}
40+
}

0 commit comments

Comments
 (0)