packages/sql-semantics-sqlite/tests/Unit/Statement/Schema/Column/ConstraintNameTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Schema\Column;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Semantics;
11use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\ConstraintName;
12use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateTable;
13
14#[CoversClass(ConstraintName::class)]
15#[Medium]
16final class ConstraintNameTest extends TestCase
17{
18    public function testDeriveConstraintRecordsNothing(): void
19    {
20        $operation = (new Semantics())->analyze('CREATE TABLE t (a CONSTRAINT lonely)', []);
21
22        self::assertSame([], $operation->facts->diagnostics);
23    }
24
25    public function testRenderKeepsTheNameAtItsPlaceInAColumn(): void
26    {
27        $operation = (new Semantics())->analyze('create table t (a constraint one constraint "two" not null constraint three)');
28        $statement = $operation->statement;
29
30        self::assertInstanceOf(CreateTable::class, $statement);
31        self::assertInstanceOf(ConstraintName::class, $statement->columns[0]->constraints[0]);
32        self::assertInstanceOf(ConstraintName::class, $statement->columns[0]->constraints[1]);
33        self::assertInstanceOf(ConstraintName::class, $statement->columns[0]->constraints[3]);
34        self::assertSame('two', $statement->columns[0]->constraints[1]->name->value);
35        self::assertSame('CREATE TABLE t (a CONSTRAINT one CONSTRAINT two NOT NULL CONSTRAINT three)', $operation->toString());
36    }
37
38    public function testRenderKeepsTheNameAtItsPlaceAmongTableConstraints(): void
39    {
40        $operation = (new Semantics())->analyze('CREATE TABLE t (a, CONSTRAINT k UNIQUE (a))');
41        $statement = $operation->statement;
42
43        self::assertInstanceOf(CreateTable::class, $statement);
44        self::assertCount(2, $statement->constraints[0]->items);
45        self::assertInstanceOf(ConstraintName::class, $statement->constraints[0]->items[0]);
46        self::assertSame('CREATE TABLE t (a, CONSTRAINT k UNIQUE (a))', $operation->toString());
47    }
48}
49