packages/sql-semantics-postgres/tests/Unit/Rules/Resolution/VisibilityTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Resolution;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Resolution\Visibility::class)]
12#[Small]
13final class VisibilityTest extends TestCase
14{
15 public function testQualifiedOnlyHidesEverySlot(): void
16 {
17 $relation = new \SqlSemantics\Platform\PostgreSql\Model\Resolution\VisibleRelation(new \SqlSemantics\Platform\PostgreSql\Statement\Relation\TableInput(new \SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')))), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\RowShape([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('b'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull)]), null, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('s')));
18 self::assertSame([-1, 0, 1], (new \SqlSemantics\Platform\PostgreSql\Rules\Resolution\Visibility())->qualifiedOnly($relation)->hidden);
19 }
20
21 public function testRestrictedTellsAQualifiedOnlyRelation(): void
22 {
23 $relation = new \SqlSemantics\Platform\PostgreSql\Model\Resolution\VisibleRelation(new \SqlSemantics\Platform\PostgreSql\Statement\Relation\TableInput(new \SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')))), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\RowShape([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('b'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull)]), null, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('s')));
24 self::assertSame([false, true], [(new \SqlSemantics\Platform\PostgreSql\Rules\Resolution\Visibility())->restricted($relation), (new \SqlSemantics\Platform\PostgreSql\Rules\Resolution\Visibility())->restricted((new \SqlSemantics\Platform\PostgreSql\Rules\Resolution\Visibility())->qualifiedOnly($relation))]);
25 }
26
27 public function testAdmitsMatchesTheNameAndSchema(): void
28 {
29 $context = (new \SqlSemantics\Platform\PostgreSql\Platform())->context(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172), null, [], true);
30 $derivation = new \SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation($context);
31 $relation = new \SqlSemantics\Platform\PostgreSql\Model\Resolution\VisibleRelation(new \SqlSemantics\Platform\PostgreSql\Statement\Relation\TableInput(new \SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')))), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\RowShape([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('b'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull)]), null, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('s')));
32 $visibility = new \SqlSemantics\Platform\PostgreSql\Rules\Resolution\Visibility();
33 self::assertSame([true, true, false], [$visibility->admits($derivation->environment(), $relation, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t'))), $visibility->admits($derivation->environment(), $relation, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('s'))), $visibility->admits($derivation->environment(), $relation, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('x')))]);
34 }
35
36 public function testStarSkipsQualifiedOnlyRelations(): void
37 {
38 $relation = new \SqlSemantics\Platform\PostgreSql\Model\Resolution\VisibleRelation(new \SqlSemantics\Platform\PostgreSql\Statement\Relation\TableInput(new \SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')))), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\RowShape([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('b'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull)]), null, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('s')));
39 self::assertSame([2, 0], [count((new \SqlSemantics\Platform\PostgreSql\Rules\Resolution\Visibility())->star([$relation])->slots), count((new \SqlSemantics\Platform\PostgreSql\Rules\Resolution\Visibility())->star([(new \SqlSemantics\Platform\PostgreSql\Rules\Resolution\Visibility())->qualifiedOnly($relation)])->slots)]);
40 }
41
42 public function testUnhiddenSkipsHiddenSlots(): void
43 {
44 $relation = new \SqlSemantics\Platform\PostgreSql\Model\Resolution\VisibleRelation(new \SqlSemantics\Platform\PostgreSql\Statement\Relation\TableInput(new \SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')))), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\RowShape([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('b'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull)]), null, null, [0]);
45 self::assertCount(1, (new \SqlSemantics\Platform\PostgreSql\Rules\Resolution\Visibility())->unhidden($relation));
46 }
47
48 public function testUnqualifiedDropsTheSystemColumnsOfQualifiedOnlyRelations(): void
49 {
50 $visibility = new \SqlSemantics\Platform\PostgreSql\Rules\Resolution\Visibility();
51 $relation = new \SqlSemantics\Platform\PostgreSql\Model\Resolution\VisibleRelation(new \SqlSemantics\Platform\PostgreSql\Statement\Relation\TableInput(new \SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')))), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\RowShape([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull)]), null, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')), [], [new \SqlSemantics\Platform\PostgreSql\Model\Resolution\ImplicitSlot([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('ctid')], new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('ctid'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Tid), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull))]);
52 $context = (new \SqlSemantics\Platform\PostgreSql\Platform())->context(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172), null, [], true);
53 $outer = new \SqlSemantics\Platform\PostgreSql\Model\Resolution\Environment($context, null, [$relation]);
54 $environment = new \SqlSemantics\Platform\PostgreSql\Model\Resolution\Environment($context, $outer, [$visibility->qualifiedOnly($relation)]);
55 $unqualified = $visibility->unqualified($environment);
56 self::assertSame([], $unqualified->relations[0]->implicit);
57 self::assertSame($outer, $unqualified->outer);
58 }
59
60 public function testUnqualifiedKeepsAnEnvironmentWithoutQualifiedOnlyRelations(): void
61 {
62 $environment = new \SqlSemantics\Platform\PostgreSql\Model\Resolution\Environment((new \SqlSemantics\Platform\PostgreSql\Platform())->context(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172), null, [], true), null, [new \SqlSemantics\Platform\PostgreSql\Model\Resolution\VisibleRelation(new \SqlSemantics\Platform\PostgreSql\Statement\Relation\TableInput(new \SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')))), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\RowShape([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull)]), null, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')), [], [new \SqlSemantics\Platform\PostgreSql\Model\Resolution\ImplicitSlot([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('ctid')], new \SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('ctid'), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Tid), \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull))])]);
63 self::assertSame($environment, (new \SqlSemantics\Platform\PostgreSql\Rules\Resolution\Visibility())->unqualified($environment));
64 }
65}
66