packages/sql-semantics-postgres/tests/Unit/Rules/Table/TargetsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Table;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Table\Targets::class)]
12#[Medium]
13final class TargetsTest extends TestCase
14{
15 public function testResolveAnswersTheDeclaredTable(): void
16 {
17 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
18 $context = [];
19 array_push($context, ...$semantics->analyze('CREATE TABLE t (a int NOT NULL, b int, c text)')->declarations());
20 $statement = $semantics->analyze('SELECT 1', $context);
21 $derivation = new \SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation($statement->context);
22 $value = (new \SqlSemantics\Platform\PostgreSql\Rules\Table\Targets())->resolve($derivation, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')));
23 $n1 = $value->table;
24 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Table\DeclaredTable::class, $n1);
25 self::assertSame(true, $n1->table === $context[0]);
26 }
27
28 public function testShapeHasASlotPerColumn(): void
29 {
30 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
31 $context = [];
32 array_push($context, ...$semantics->analyze('CREATE TABLE t (a int NOT NULL, b int, c text)')->declarations());
33 $statement = $semantics->analyze('SELECT 1', $context);
34 $derivation = new \SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation($statement->context);
35 self::assertSame(3, count((new \SqlSemantics\Platform\PostgreSql\Rules\Table\Targets())->shape($context[0])->slots));
36 }
37
38 public function testImplicitAnswersTheSystemColumns(): void
39 {
40 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
41 $context = [];
42 array_push($context, ...$semantics->analyze('CREATE TABLE t (a int NOT NULL, b int, c text)')->declarations());
43 $statement = $semantics->analyze('SELECT 1', $context);
44 $derivation = new \SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation($statement->context);
45 self::assertSame(6, count((new \SqlSemantics\Platform\PostgreSql\Rules\Table\Targets())->implicit((new \SqlSemantics\Platform\PostgreSql\Rules\Table\Targets())->resolve($derivation, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t'))))));
46 }
47
48 public function testScopeMakesTheTableTheOnlyVisibleRelation(): void
49 {
50 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
51 $context = [];
52 array_push($context, ...$semantics->analyze('CREATE TABLE t (a int NOT NULL, b int, c text)')->declarations());
53 $statement = $semantics->analyze('CREATE INDEX ON t (a, ctid)', $context);
54 self::assertSame([
55 ], array_map(static fn ($problem): string => $problem->message(), $statement->facts->diagnostics));
56 }
57
58 public function testExistingOpensTheShapeOfAMissingRelationUnderIfExists(): void
59 {
60 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
61 $statement = $semantics->analyze('SELECT 1', []);
62 $derivation = new \SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation($statement->context);
63 $name = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('nope'));
64 $skipped = (new \SqlSemantics\Platform\PostgreSql\Rules\Table\Targets())->existing($derivation, $name, true);
65 $missing = (new \SqlSemantics\Platform\PostgreSql\Rules\Table\Targets())->existing($derivation, $name, false);
66 self::assertSame([false, null], [$skipped->shape->complete(), $skipped->table]);
67 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Table\MissingTable::class, $missing->table);
68 }
69}
70