packages/sql-semantics-postgres/tests/Unit/Rules/Expression/Typing/SubscriptingTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Expression\Typing;
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\Statement\Fact\ScalarFact;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Invalid;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
19use SqlSemantics\Platform\PostgreSql\Platform;
20use SqlSemantics\Platform\PostgreSql\Rules\Expression\Typing\Subscripting;
21use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\Composite;
22use SqlSemantics\Platform\PostgreSql\Statement\Expression\FieldSelection;
23use SqlSemantics\Platform\PostgreSql\Statement\Expression\Problem\ImproperStar;
24use SqlSemantics\Platform\PostgreSql\Statement\Expression\Problem\MissingField;
25use SqlSemantics\Platform\PostgreSql\Statement\Expression\Problem\NotSubscriptable;
26use SqlSemantics\Platform\PostgreSql\Statement\Expression\Step\AllFields;
27use SqlSemantics\Platform\PostgreSql\Statement\Expression\Step\Slice;
28use SqlSemantics\Platform\PostgreSql\Statement\Expression\Step\Subscript;
29use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
30use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\ArrayOf;
31use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
32
33#[CoversClass(Subscripting::class)]
34#[Small]
35final class SubscriptingTest extends TestCase
36{
37 public function testApplyReadsTheElementOfAnArray(): void
38 {
39 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
40 $fact = (new Subscripting())->apply($derivation, new ScalarFact(new Known(new ArrayOf(Builtin::Text)), Nullability::NotNull), [new Subscript(new NullLiteral()), new Subscript(new NullLiteral())], false);
41 self::assertEquals(new ScalarFact(new Known(Builtin::Text), Nullability::Nullable), $fact);
42 }
43
44 public function testApplyReportsAStarBeforeAnotherStep(): void
45 {
46 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
47 $fact = (new Subscripting())->apply($derivation, new ScalarFact(new Known(Builtin::Int4), Nullability::NotNull), [new AllFields(), new Subscript(new NullLiteral())], false);
48 self::assertEquals(new Invalid(new ImproperStar()), $fact->type);
49 }
50
51 public function testSubscriptKeepsTheArrayForASlice(): void
52 {
53 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
54 $type = new Known(new ArrayOf(Builtin::Int4));
55 self::assertSame($type, (new Subscripting())->subscript($derivation, $type, [new Subscript(new NullLiteral()), new Slice(null, null)]));
56 self::assertEquals(new Invalid(new NotSubscriptable('integer')), (new Subscripting())->subscript($derivation, new Known(Builtin::Int4), [new Subscript(new NullLiteral())]));
57 }
58
59 public function testFieldReadsTheSlotOfAComposite(): void
60 {
61 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
62 $composite = new Known(new Composite([new OutputSlot(new Name('a'), new Known(Builtin::Int4), Nullability::NotNull)]));
63 self::assertEquals([new Known(Builtin::Int4), Nullability::NotNull], (new Subscripting())->field($derivation, $composite, new FieldSelection(new Name('a'))));
64 self::assertEquals(new Invalid(new MissingField('b', 'record')), (new Subscripting())->field($derivation, $composite, new FieldSelection(new Name('b'))));
65 }
66
67 public function testProblemReportsTheDiagnostic(): void
68 {
69 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
70 (new Subscripting())->problem($derivation, new ImproperStar());
71 self::assertEquals([new ImproperStar()], $derivation->facts()->diagnostics);
72 }
73}
74