packages/sql-semantics-sqlite/tests/Unit/Statement/Schema/Column/NullAllowedTest.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\Model\Statement\Type\Nullability;
11use SqlSemantics\Platform\Sqlite\Semantics;
12use SqlSemantics\Platform\Sqlite\Statement\Mutation\ConflictResolution;
13use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\NullAllowed;
14use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateTable;
15
16#[CoversClass(NullAllowed::class)]
17#[Medium]
18final class NullAllowedTest extends TestCase
19{
20    public function testDeriveConstraintLeavesTheColumnNullable(): void
21    {
22        $operation = (new Semantics())->analyze('CREATE TABLE t (a INT NULL)', []);
23
24        self::assertSame(Nullability::Nullable, $operation->declarations()[0]->columns[0]->nullability);
25        self::assertSame([], $operation->facts->diagnostics);
26    }
27
28    public function testDeriveConstraintDoesNotUndoNotNull(): void
29    {
30        $operation = (new Semantics())->analyze('CREATE TABLE t (a INT NOT NULL NULL)');
31
32        self::assertSame(Nullability::NotNull, $operation->declarations()[0]->columns[0]->nullability);
33    }
34
35    public function testRenderKeepsTheConflictResolution(): void
36    {
37        $operation = (new Semantics())->analyze('create table t (a null on conflict fail)');
38        $statement = $operation->statement;
39
40        self::assertInstanceOf(CreateTable::class, $statement);
41        self::assertInstanceOf(NullAllowed::class, $statement->columns[0]->constraints[0]);
42        self::assertSame(ConflictResolution::Fail, $statement->columns[0]->constraints[0]->conflict);
43        self::assertSame('CREATE TABLE t (a NULL ON CONFLICT FAIL)', $operation->toString());
44    }
45}
46