packages/sql-semantics-postgres/tests/Unit/Statement/Constraint/Column/ReferencesTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Constraint\Column;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Statement\Constraint\Column\References::class)]
12#[Medium]
13final class ReferencesTest extends TestCase
14{
15    public function testKindIsForeignKey(): void
16    {
17        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
18        $statement = $semantics->inspect('CREATE TABLE t (a int REFERENCES u)', []);
19        $n1 = $statement->statement;
20        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\CreateTable::class, $n1);
21        $n2 = $n1->definition;
22        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\Element\ListedColumns::class, $n2);
23        $n3 = $n2->elements[0];
24        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\Element\ColumnDefinition::class, $n3);
25        $n4 = $n3->qualifiers[0];
26        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Constraint\Column\References::class, $n4);
27        self::assertSame(\SqlSemantics\Platform\PostgreSql\Statement\Constraint\ConstraintKind::ForeignKey, $n4->kind());
28    }
29
30    public function testDeriveClauseResolvesTheReferencedTable(): void
31    {
32        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
33        $context = [];
34        array_push($context, ...$semantics->inspect('CREATE TABLE p (id int PRIMARY KEY)')->declarations());
35        $statement = $semantics->inspect('CREATE TABLE t (a int REFERENCES p (id))', $context);
36        $n1 = $statement->statement;
37        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\CreateTable::class, $n1);
38        $n2 = $n1->definition;
39        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\Element\ListedColumns::class, $n2);
40        $n3 = $n2->elements[0];
41        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\Element\ColumnDefinition::class, $n3);
42        $n4 = $n3->qualifiers[0];
43        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Constraint\Column\References::class, $n4);
44        $n5 = $statement->facts->relation($n4)->table;
45        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Table\DeclaredTable::class, $n5);
46        self::assertSame(true, $n5->table === $context[0]);
47    }
48
49    public function testDeriveClauseReportsAMissingReferencedColumn(): void
50    {
51        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
52        $context = [];
53        array_push($context, ...$semantics->inspect('CREATE TABLE p (id int PRIMARY KEY)')->declarations());
54        $statement = $semantics->inspect('CREATE TABLE t (a int REFERENCES p (nope) MATCH PARTIAL ON UPDATE SET NULL (a))', $context);
55        self::assertSame([
56          0 => 'column "nope" referenced in foreign key constraint does not exist',
57          1 => 'MATCH PARTIAL not yet implemented',
58          2 => 'a column list with SET NULL is only supported for ON DELETE actions',
59        ], array_map(static fn ($problem): string => $problem->message(), $statement->facts->diagnostics));
60    }
61
62    public function testRenderWritesTheClauseAsWritten(): void
63    {
64        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
65        $statement = $semantics->inspect('CREATE TABLE t (a int CONSTRAINT f REFERENCES s.p (id) MATCH FULL ON DELETE CASCADE ON UPDATE RESTRICT)', []);
66        self::assertSame('CREATE TABLE t (a INT CONSTRAINT f REFERENCES s.p (id) MATCH FULL ON DELETE CASCADE ON UPDATE RESTRICT)', $statement->toString());
67    }
68}
69