packages/sql-semantics-postgres/tests/Unit/Rules/Table/Definition/TableConstraintsTest.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\TableConstraints::class)]
12#[Medium]
13final class TableConstraintsTest extends TestCase
14{
15    public function testAdmitsIsTrueForTableConstraints(): void
16    {
17        self::assertSame([
18          0 => true,
19          1 => false,
20        ], [(new \SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\TableConstraints())->admits(new \SqlSemantics\Platform\PostgreSql\Statement\Constraint\Table\IndexConstraint(true, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('i'))), (new \SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\TableConstraints())->admits(new \SqlSemantics\Platform\PostgreSql\Statement\Constraint\Column\NotNull())]);
21    }
22
23    public function testTypedElementsRefusesAColumnDefinition(): void
24    {
25        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
26        $statement = $semantics->analyze('CREATE TABLE n (a int)', []);
27        $n1 = $statement->statement;
28        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\CreateTable::class, $n1);
29        $n2 = $n1->definition;
30        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\Element\ListedColumns::class, $n2);
31        $n3 = $n2->elements[0];
32        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\Element\ColumnDefinition::class, $n3);
33        $this->expectExceptionMessage('An element of a typed table or a partition is column options or a table constraint.');
34        (new \SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\TableConstraints())->typedElements([$n3]);
35    }
36}
37