Skip to content

Commit 48bb432

Browse files
committed
Add DateTimeTzImmutableTypeTest
1 parent 7c716da commit 48bb432

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Tests\Functional\Types;
6+
7+
use DateTimeImmutable;
8+
use DateTimeZone;
9+
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
10+
use Doctrine\DBAL\Schema\Table;
11+
use Doctrine\DBAL\Tests\FunctionalTestCase;
12+
use Doctrine\DBAL\Types\Type;
13+
use Doctrine\DBAL\Types\Types;
14+
15+
use function sprintf;
16+
17+
class DateTimeTzImmutableTypeTest extends FunctionalTestCase
18+
{
19+
private const TEST_TABLE = 'datetimetz_test';
20+
21+
protected function setUp(): void
22+
{
23+
$this->iniSet('date.timezone', 'UTC');
24+
25+
$table = new Table(self::TEST_TABLE);
26+
$table->addColumn('id', Types::INTEGER);
27+
28+
$table->addColumn('val', Types::DATETIMETZ_IMMUTABLE);
29+
$table->setPrimaryKey(['id']);
30+
31+
$this->dropAndCreateTable($table);
32+
}
33+
34+
public function testInsertAndSelect(): void
35+
{
36+
$platform = $this->connection->getDatabasePlatform();
37+
$dateTimeTzImmutableType = Type::getType(Types::DATETIMETZ_IMMUTABLE);
38+
39+
$id1 = 1;
40+
$value1 = new DateTimeImmutable('1986-03-22 19:45:30', new DateTimeZone('America/Argentina/Buenos_Aires'));
41+
42+
$this->insert($id1, $value1);
43+
44+
$res1 = $this->select($id1);
45+
46+
$resultDateTimeTzValue = $dateTimeTzImmutableType
47+
->convertToPHPValue($res1, $platform)
48+
->setTimezone(new DateTimeZone('UTC'));
49+
50+
self::assertInstanceOf(DateTimeImmutable::class, $resultDateTimeTzValue);
51+
self::assertSame($value1->getTimestamp(), $resultDateTimeTzValue->getTimestamp());
52+
self::assertSame($value1->getTimestamp(), $resultDateTimeTzValue->getTimestamp());
53+
self::assertSame('UTC', $resultDateTimeTzValue->format('T'));
54+
self::assertSame('1986-03-22T22:45:30+00:00', $resultDateTimeTzValue->format(DateTimeImmutable::ATOM));
55+
}
56+
57+
private function insert(int $id, DateTimeImmutable $value): void
58+
{
59+
$result = $this->connection->insert(self::TEST_TABLE, [
60+
'id' => $id,
61+
'val' => $value,
62+
], [
63+
Types::INTEGER,
64+
Type::getType(Types::DATETIMETZ_IMMUTABLE),
65+
]);
66+
67+
self::assertSame(1, $result);
68+
}
69+
70+
private function select(int $id): string
71+
{
72+
$value = $this->connection->fetchOne(
73+
sprintf('SELECT val FROM %s WHERE id = ?', self::TEST_TABLE),
74+
[$id],
75+
[Types::INTEGER],
76+
);
77+
78+
self::assertIsString($value);
79+
80+
return $value;
81+
}
82+
}

0 commit comments

Comments
 (0)