packages/sql-semantics-sqlite/tests/Unit/Model/Statement/Declaration/ColumnTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Model\Statement\Declaration;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Declaration\Column;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
13use SqlSemantics\Platform\Sqlite\Semantics;
14use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
15
16#[CoversClass(Column::class)]
17#[Medium]
18final class ColumnTest extends TestCase
19{
20    public function testADeclaredColumnIsNullableUnlessSaidOtherwise(): void
21    {
22        $column = new Column(new Name('a'), Storage::Integer);
23
24        self::assertSame('a', $column->name->value);
25        self::assertSame('INTEGER', $column->type->name());
26        self::assertSame(Nullability::Nullable, $column->nullability);
27        self::assertFalse($column->generated);
28    }
29
30    public function testAGeneratedColumnSaysSo(): void
31    {
32        self::assertTrue((new Column(new Name('a'), Storage::Integer, Nullability::Nullable, true))->generated);
33    }
34
35    public function testAResolvedReferenceReachesTheDeclarationObjectItself(): void
36    {
37        $semantics = new Semantics();
38        $table = $semantics->analyze('CREATE TABLE t (a INTEGER NOT NULL, b TEXT)')->declarations()[0];
39
40        $query = $semantics->analyze('SELECT b, a FROM t', [$table]);
41
42        self::assertSame($table->columns[1], $query->field('b')->column());
43        self::assertSame($table->columns[0], $query->field('a')->column());
44        self::assertSame(Nullability::NotNull, $table->columns[0]->nullability);
45    }
46}
47