Skip to content

Commit 822cc50

Browse files
committed
Add DateTimeTzImmutableTypeTest
1 parent 7c716da commit 822cc50

File tree

1 file changed

+81
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)