packages/sql-semantics-postgres/tests/Unit/Statement/Utility/Session/SetTimeZoneTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Utility\Session;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Statement\Utility\Session\SetTimeZone::class)]
12#[Medium]
13final class SetTimeZoneTest extends TestCase
14{
15    public function testDeriveStatementTypesTheIntervalConstant(): void
16    {
17        $operation = (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect("SET TIME ZONE INTERVAL '1' HOUR");
18        $statement = $operation->statement;
19        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Utility\Session\SetTimeZone::class, $statement);
20        $zone = $statement->zone;
21        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Literal\TypedLiteral::class, $zone);
22        self::assertSame(\SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull, $operation->facts->scalar($zone)->nullability);
23        self::assertSame([], $operation->facts->diagnostics);
24    }
25
26    public function testDeriveStatementReportsOtherIntervalFields(): void
27    {
28        self::assertSame('time zone interval must be HOUR or HOUR TO MINUTE', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('SET TIME ZONE INTERVAL \'1\' DAY TO HOUR')->facts->diagnostics[0]->message());
29    }
30
31    public function testDeriveStatementAcceptsAPrecision(): void
32    {
33        self::assertSame([], (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('SET TIME ZONE INTERVAL (2) \'1\'')->facts->diagnostics);
34    }
35
36    public function testRenderQuotesAnIdentifierThatIsAKeyword(): void
37    {
38        self::assertSame('SET LOCAL TIME ZONE "local"', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('SET LOCAL TIME ZONE "local"')->toString());
39    }
40
41    public function testRenderWritesEachValueForm(): void
42    {
43        self::assertSame(["SET TIME ZONE 'UTC'", 'SET TIME ZONE utc', 'SET TIME ZONE - 8', "SET TIME ZONE INTERVAL '-08:00' HOUR TO MINUTE"], [(new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect("SET TIME ZONE 'UTC'")->toString(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('SET TIME ZONE UTC')->toString(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('SET TIME ZONE -8')->toString(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect("SET TIME ZONE INTERVAL '-08:00' HOUR TO MINUTE")->toString()]);
44    }
45}
46