packages/sql-semantics-postgres/tests/Unit/Statement/Expression/Operator/AtTimeZoneTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Operator;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
13use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
14use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
17use SqlSemantics\Platform\PostgreSql\Platform;
18use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
19use SqlSemantics\Platform\PostgreSql\Statement\Expression\BinaryOperation;
20use SqlSemantics\Platform\PostgreSql\Statement\Expression\Operator\AtTimeZone;
21use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
22use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
23use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
24use SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName;
25use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
26
27#[CoversClass(AtTimeZone::class)]
28#[Small]
29final class AtTimeZoneTest extends TestCase
30{
31    public function testDeriveScalarTurnsAnUnknownConstantIntoATimestamp(): void
32    {
33        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
34        $fact = $derivation->scalar(new AtTimeZone(new Constant(new StringConstant('2024-01-01')), new Constant(new StringConstant('UTC'))), $derivation->environment());
35        self::assertEquals(new Known(Builtin::Timestamp), $fact->type);
36    }
37
38    public function testRenderWritesAtTimeZone(): void
39    {
40        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
41        (new AtTimeZone(new NullLiteral(), new NullLiteral()))->render($out);
42        self::assertSame('NULL AT TIME ZONE NULL', (new Lexical())->join($out->pieces()));
43    }
44
45    public function testRejectsASumAsTheZone(): void
46    {
47        $sum = new BinaryOperation(new OperatorName(new Name('+')), new NullLiteral(), new NullLiteral());
48        $this->expectExceptionMessage('The time value needs parentheses to keep its place.');
49        new AtTimeZone($sum, new NullLiteral());
50    }
51}
52