packages/sql-semantics-postgres/tests/Unit/Statement/Expression/Operator/AtLocalTest.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\AtLocal;
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(AtLocal::class)]
28#[Small]
29final class AtLocalTest extends TestCase
30{
31 public function testDeriveScalarTurnsATimestampWithZoneIntoATimestamp(): void
32 {
33 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
34 $fact = $derivation->scalar(new AtLocal(new Constant(new StringConstant('2024-01-01'))), $derivation->environment());
35 self::assertEquals(new Known(Builtin::Timestamp), $fact->type);
36 }
37
38 public function testRenderWritesAtLocal(): void
39 {
40 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
41 (new AtLocal(new NullLiteral()))->render($out);
42 self::assertSame('NULL AT LOCAL', (new Lexical())->join($out->pieces()));
43 }
44
45 public function testRejectsAProductAsTheValue(): void
46 {
47 $product = new BinaryOperation(new OperatorName(new Name('*')), new NullLiteral(), new NullLiteral());
48 $this->expectExceptionMessage('The time value needs parentheses to keep its place.');
49 new AtLocal($product);
50 }
51}
52