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

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression;
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\ColumnReference;
20use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\RowConstructor;
21use SqlSemantics\Platform\PostgreSql\Statement\Expression\FieldSelection;
22use SqlSemantics\Platform\PostgreSql\Statement\Expression\Grouped;
23use SqlSemantics\Platform\PostgreSql\Statement\Expression\Indirection;
24use SqlSemantics\Platform\PostgreSql\Statement\Expression\Step\Subscript;
25use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
26use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
27use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
28use SqlSemantics\Platform\PostgreSql\Statement\Literal\PositionalParameter;
29use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
30
31#[CoversClass(Indirection::class)]
32#[Small]
33final class IndirectionTest extends TestCase
34{
35    public function testOutputNameIsTheLastSelectedField(): void
36    {
37        self::assertSame('b', (new Indirection(new PositionalParameter('1'), [new FieldSelection(new Name('b')), new Subscript(new NullLiteral())]))->outputName()?->value);
38    }
39
40    public function testDeriveScalarSelectsAFieldOfARow(): void
41    {
42        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
43        $row = new Grouped(new RowConstructor([new Constant(new IntegerConstant('1'))]));
44        $fact = $derivation->scalar(new Indirection($row, [new FieldSelection(new Name('f1'))]), $derivation->environment());
45        self::assertEquals(new Known(Builtin::Int4), $fact->type);
46    }
47
48    public function testRenderWritesTheBaseAndTheSteps(): void
49    {
50        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
51        (new Indirection(new ColumnReference([new Name('a')]), [new Subscript(new Constant(new IntegerConstant('1'))), new FieldSelection(new Name('b'))]))->render($out);
52        self::assertSame('a [1].b', (new Lexical())->join($out->pieces()));
53    }
54
55    public function testRejectsAFieldStepRightAfterADottedName(): void
56    {
57        $this->expectExceptionMessage('A column reference takes steps once a subscript follows its dotted name.');
58        new Indirection(new ColumnReference([new Name('a')]), [new FieldSelection(new Name('b'))]);
59    }
60
61    public function testRejectsABaseTheGrammarDoesNotIndirect(): void
62    {
63        $this->expectExceptionMessage('Steps apply to a parenthesized expression, a parameter, a scalar subquery or a column reference.');
64        new Indirection(new NullLiteral(), [new FieldSelection(new Name('b'))]);
65    }
66}
67