packages/sql-semantics-postgres/tests/Unit/Rules/Table/KeyColumnsTest.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\KeyColumns::class)]
12#[Medium]
13final class KeyColumnsTest extends TestCase
14{
15    public function testPositionFindsTheSlotOfAName(): void
16    {
17        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
18        $context = [];
19        array_push($context, ...$semantics->inspect('CREATE TABLE t (a int NOT NULL, b int, c text)')->declarations());
20        $statement = $semantics->inspect('SELECT 1', $context);
21        $derivation = new \SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation($statement->context);
22        self::assertSame(2, (new \SqlSemantics\Platform\PostgreSql\Rules\Table\KeyColumns())->position($derivation, (new \SqlSemantics\Platform\PostgreSql\Rules\Table\Targets())->shape($context[0]), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('c')));
23    }
24
25    public function testReportReportsANameAShapeLacks(): void
26    {
27        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
28        $context = [];
29        array_push($context, ...$semantics->inspect('CREATE TABLE t (a int NOT NULL, b int, c text)')->declarations());
30        $statement = $semantics->inspect('CREATE TABLE n (a int, UNIQUE (a, zz))', $context);
31        self::assertSame([
32          0 => 'column "zz" named in key does not exist',
33        ], array_map(static fn ($problem): string => $problem->message(), $statement->facts->diagnostics));
34    }
35}
36