packages/sql-semantics-postgres/tests/Unit/Statement/Expression/ColumnReferenceTest.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\Resolution\Environment;
16use SqlSemantics\Platform\PostgreSql\Model\Resolution\VisibleRelation;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName;
19use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Column\MissingColumn;
20use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Column\ResolvedColumn;
21use SqlSemantics\Platform\PostgreSql\Model\Statement\Relation;
22use SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot;
23use SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\RowShape;
24use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Invalid;
25use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
26use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
27use SqlSemantics\Platform\PostgreSql\Platform;
28use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
29use SqlSemantics\Platform\PostgreSql\Statement\Expression\ColumnReference;
30use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\Composite;
31use SqlSemantics\Platform\PostgreSql\Statement\Name\ImproperName;
32use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
33
34#[CoversClass(ColumnReference::class)]
35#[Small]
36final class ColumnReferenceTest extends TestCase
37{
38 public function testOutputNameIsTheLastPart(): void
39 {
40 self::assertSame('a', (new ColumnReference([new Name('s'), new Name('t'), new Name('a')]))->outputName()->value);
41 }
42
43 public function testDeriveScalarResolvesAQualifiedColumn(): void
44 {
45 $context = (new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true);
46 $slot = new OutputSlot(new Name('a'), new Known(Builtin::Int4), Nullability::NotNull);
47 $environment = new Environment($context, null, [new VisibleRelation(self::createStub(Relation::class), new RowShape([$slot]), null, new QualifiedName(new Name('t'), new Name('s')))]);
48 $derivation = new Derivation($context);
49 $fact = $derivation->scalar(new ColumnReference([new Name('s'), new Name('t'), new Name('a')]), $environment);
50 self::assertInstanceOf(ResolvedColumn::class, $fact->resolution);
51 self::assertSame($slot, $fact->resolution->slot);
52 self::assertEquals(new Known(Builtin::Int4), $fact->type);
53 }
54
55 public function testDeriveScalarReadsAOneWordNameAsTheWholeRowOfARelation(): void
56 {
57 $context = (new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true);
58 $slot = new OutputSlot(new Name('a'), new Known(Builtin::Int4), Nullability::NotNull);
59 $environment = new Environment($context, null, [new VisibleRelation(self::createStub(Relation::class), new RowShape([$slot]), new Name('t'))]);
60 $derivation = new Derivation($context);
61 $fact = $derivation->scalar(new ColumnReference([new Name('t')]), $environment);
62 self::assertEquals(new Known(new Composite([$slot], new Name('t'))), $fact->type);
63 }
64
65 public function testDeriveScalarReportsAMissingColumn(): void
66 {
67 $context = (new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true);
68 $environment = new Environment($context, null, [new VisibleRelation(self::createStub(Relation::class), new RowShape([]), new Name('t'))]);
69 $derivation = new Derivation($context);
70 $fact = $derivation->scalar(new ColumnReference([new Name('t'), new Name('c')]), $environment);
71 self::assertInstanceOf(MissingColumn::class, $fact->resolution);
72 self::assertInstanceOf(Invalid::class, $fact->type);
73 }
74
75 public function testDeriveScalarReportsMoreThanFourParts(): void
76 {
77 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
78 $fact = $derivation->scalar(new ColumnReference([new Name('a'), new Name('b'), new Name('c'), new Name('d'), new Name('e')]), $derivation->environment());
79 self::assertInstanceOf(Invalid::class, $fact->type);
80 self::assertInstanceOf(ImproperName::class, $fact->type->cause);
81 }
82
83 public function testRenderWritesTheParts(): void
84 {
85 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
86 (new ColumnReference([new Name('T'), new Name('order')]))->render($out);
87 self::assertSame('"T".order', (new Lexical())->join($out->pieces()));
88 }
89}
90