packages/sql-semantics-postgres/tests/Unit/Rules/Table/Definition/QualifiersTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Table\Definition;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\Qualifiers::class)]
12#[Medium]
13final class QualifiersTest extends TestCase
14{
15    public function testCheckedRefusesATableConstraint(): void
16    {
17        $this->expectExceptionMessage('A column qualifier is a column constraint, a deferral attribute or a collation.');
18        (new \SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\Qualifiers())->checked([new \SqlSemantics\Platform\PostgreSql\Statement\Constraint\Table\IndexConstraint(true, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('i'))]);
19    }
20
21    public function testDeriveReportsSeveralCollations(): void
22    {
23        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
24        $statement = $semantics->inspect('CREATE TABLE n (a text COLLATE "C" COLLATE "POSIX")', []);
25        self::assertSame([
26          0 => 'multiple COLLATE clauses not allowed',
27        ], array_map(static fn ($problem): string => $problem->message(), $statement->facts->diagnostics));
28    }
29
30    public function testAttributesReportsMisplacedAndRepeatedAttributes(): void
31    {
32        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
33        $statement = $semantics->inspect('CREATE TABLE n (a int DEFERRABLE, b int UNIQUE DEFERRABLE NOT DEFERRABLE INITIALLY DEFERRED INITIALLY IMMEDIATE)', []);
34        self::assertSame([
35          0 => 'misplaced DEFERRABLE clause',
36          1 => 'multiple DEFERRABLE/NOT DEFERRABLE clauses not allowed',
37          2 => 'constraint declared INITIALLY DEFERRED must be DEFERRABLE',
38          3 => 'multiple INITIALLY IMMEDIATE/DEFERRED clauses not allowed',
39        ], array_map(static fn ($problem): string => $problem->message(), $statement->facts->diagnostics));
40    }
41
42    public function testConflictsReportsCombinedValueSources(): void
43    {
44        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
45        $statement = $semantics->inspect('CREATE TABLE n (a int DEFAULT 1 GENERATED ALWAYS AS (2) STORED GENERATED ALWAYS AS IDENTITY GENERATED ALWAYS AS (3) STORED)', []);
46        self::assertSame([
47          0 => 'multiple generation clauses specified for column "a"',
48          1 => 'both default and identity specified for column "a"',
49          2 => 'both default and generation expression specified for column "a"',
50          3 => 'both identity and generation expression specified for column "a"',
51        ], array_map(static fn ($problem): string => $problem->message(), $statement->facts->diagnostics));
52    }
53}
54