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

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Step;
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\Type\Known;
16use SqlSemantics\Platform\PostgreSql\Platform;
17use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
18use SqlSemantics\Platform\PostgreSql\Statement\Expression\Step\Subscript;
19use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
20use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
21use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
22use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
23
24#[CoversClass(Subscript::class)]
25#[Small]
26final class SubscriptTest extends TestCase
27{
28    public function testFieldIsNone(): void
29    {
30        self::assertNull((new Subscript(new NullLiteral()))->field());
31    }
32
33    public function testDeriveClauseDerivesTheIndex(): void
34    {
35        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
36        $index = new Constant(new IntegerConstant('1'));
37        (new Subscript($index))->deriveClause($derivation, $derivation->environment());
38        self::assertEquals(new Known(Builtin::Int4), $derivation->facts()->scalar($index)->type);
39    }
40
41    public function testRenderWritesBrackets(): void
42    {
43        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
44        (new Subscript(new Constant(new IntegerConstant('1'))))->render($out);
45        self::assertSame('[1]', (new Lexical())->join($out->pieces()));
46    }
47}
48