|
| 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 | + |
| <
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
+ $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 | +} |
0 commit comments