Skip to content

Commit cf56c88

Browse files
committed
Add DateTimeTzImmutableTypeTest
1 parent 7c716da commit cf56c88

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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->convertToPHPValue($res1, $platform);
46+
47+
self::assertInstanceOf(DateTimeImmutable::class, $resultDateTimeTzValue);
48+
self::assertSame($value1->getTimestamp(), $resultDateTimeTzValue->getTimestamp());
49+
self::assertSame('1986-03-22T19:45:30-03:00', $resultDateTimeTzValue->format(DateTimeImmutable::ATOM));
50+
self::assertSame('GMT-0300', $resultDateTimeTzValue->format('T'));
51+
self::assertSame(-10800, $resultDateTimeTzValue->getOffset());
52+
}
53+
54+
private function insert(int $id, DateTimeImmutable $value): void
55+
{
56+
$result = $this->connection->insert(self::TEST_TABLE, [
57+
'id' => $id,
58+
'val' => $value,
59+
], [
60+
Types::INTEGER,
61+
Type::getType(Types::DATETIMETZ_IMMUTABLE),
62+
]);
63+
64+
self::assertSame(1, $result);
65+
}
66+
67+
private function select(int $id): string
68+
{
69+
$value = $this->connection->fetchOne(
70+
sprintf('SELECT val FROM %s WHERE id = ?', self::TEST_TABLE),
71+
[$id],
72+
[Types::INTEGER],
73+
);
74+
75+
self::assertIsString($value);
76+
77+
return $value;
78+
}
79+
}

0 commit comments

Comments
 (0)