packages/sql-semantics-postgres/tests/Unit/Rules/Expression/WholeRowsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\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\Resolution\Environment;
14use SqlSemantics\Platform\PostgreSql\Model\Resolution\VisibleRelation;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Missing\UndeclaredRelation;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Relation;
19use SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\RowShape;
20use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Dependent;
21use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Invalid;
22use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
23use SqlSemantics\Platform\PostgreSql\Platform;
24use SqlSemantics\Platform\PostgreSql\Rules\Expression\WholeRows;
25use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\Composite;
26use SqlSemantics\Platform\PostgreSql\Statement\Expression\Problem\AmbiguousRelation;
27
28#[CoversClass(WholeRows::class)]
29#[Small]
30final class WholeRowsTest extends TestCase
31{
32 public function testStarReportsAnImproperName(): void
33 {
34 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
35 $fact = (new WholeRows())->star($derivation, $derivation->environment(), [new Name('a'), new Name('b'), new Name('c'), new Name('d')]);
36 self::assertInstanceOf(Invalid::class, $fact->type);
37 }
38
39 public function testRowDependsOnAnOpenShape(): void
40 {
41 $context = (new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true);
42 $missing = new UndeclaredRelation(new QualifiedName(new Name('t')));
43 $environment = new Environment($context, null, [new VisibleRelation(self::createStub(Relation::class), new RowShape([], [$missing]), new Name('t'))]);
44 self::assertEquals(new Dependent([$missing]), (new WholeRows())->row(new Derivation($context), $environment, new QualifiedName(new Name('t')))?->type);
45 }
46
47 public function testRowReportsTwoRelationsOfOneName(): void
48 {
49 $context = (new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true);
50 $relation = new VisibleRelation(self::createStub(Relation::class), new RowShape([]), new Name('t'));
51 $environment = new Environment($context, null, [$relation, new VisibleRelation(self::createStub(Relation::class), new RowShape([]), new Name('t'))]);
52 $fact = (new WholeRows())->row(new Derivation($context), $environment, new QualifiedName(new Name('t')));
53 self::assertInstanceOf(AmbiguousRelation::class, $fact?->type instanceof Invalid ? $fact->type->cause : null);
54 }
55
56 public function testFindSearchesTheEnclosingLevels(): void
57 {
58 $context = (new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true);
59 $relation = new VisibleRelation(self::createStub(Relation::class), new RowShape([]), null, new QualifiedName(new Name('t'), new Name('s')));
60 $environment = new Environment($context, new Environment($context, null, [$relation]));
61 self::assertSame([$relation], (new WholeRows())->find($environment, new QualifiedName(new Name('t'))));
62 self::assertEquals(new Known(new Composite([], new Name('t'))), (new WholeRows())->row(new Derivation($context), $environment, new QualifiedName(new Name('t'), new Name('s')))?->type);
63 }
64}
65