packages/sql-semantics-postgres/tests/Unit/Statement/Expression/ColumnStarTest.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\Table\MissingTable;
20use SqlSemantics\Platform\PostgreSql\Model\Statement\Relation;
21use SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot;
22use SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\RowShape;
23use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Invalid;
24use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
25use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
26use SqlSemantics\Platform\PostgreSql\Platform;
27use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
28use SqlSemantics\Platform\PostgreSql\Statement\Expression\ColumnStar;
29use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\Composite;
30use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
31
32#[CoversClass(ColumnStar::class)]
33#[Small]
34final class ColumnStarTest extends TestCase
35{
36    public function testOutputNameIsTheRelation(): void
37    {
38        self::assertSame('t', (new ColumnStar([new Name('s'), new Name('t')]))->outputName()->value);
39    }
40
41    public function testDeriveScalarIsTheWholeRowOfTheRelation(): void
42    {
43        $context = (new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true);
44        $slot = new OutputSlot(new Name('a'), new Known(Builtin::Int4), Nullability::NotNull);
45        $environment = new Environment($context, null, [new VisibleRelation(self::createStub(Relation::class), new RowShape([$slot]), null, new QualifiedName(new Name('t'), new Name('s')))]);
46        $derivation = new Derivation($context);
47        $fact = $derivation->scalar(new ColumnStar([new Name('s'), new Name('t')]), $environment);
48        self::assertEquals(new Known(new Composite([$slot], new Name('t'))), $fact->type);
49    }
50
51    public function testDeriveScalarReportsARelationThatIsNotVisible(): void
52    {
53        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
54        $fact = $derivation->scalar(new ColumnStar([new Name('t')]), $derivation->environment());
55        self::assertInstanceOf(Invalid::class, $fact->type);
56        self::assertInstanceOf(MissingTable::class, $fact->type->cause);
57    }
58
59    public function testRenderWritesTheStar(): void
60    {
61        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
62        (new ColumnStar([new Name('s'), new Name('t')]))->render($out);
63        self::assertSame('s.t.*', (new Lexical())->join($out->pieces()));
64    }
65
66    public function testRejectsNoQualifier(): void
67    {
68        $this->expectExceptionMessage('A star reference names its relation.');
69        new ColumnStar([]);
70    }
71}
72