packages/sql-semantics-sqlite/tests/Unit/Statement/Schema/Problem/GeneratedColumnProblemTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Schema\Problem;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\Sqlite\Semantics;
12use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\GeneratedColumnFlaw;
13use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\GeneratedColumnProblem;
14
15#[CoversClass(GeneratedColumnProblem::class)]
16#[Medium]
17final class GeneratedColumnProblemTest extends TestCase
18{
19 public function testMessageIsThatOfTheFlaw(): void
20 {
21 $problem = new GeneratedColumnProblem(GeneratedColumnFlaw::WithDefault, new Name('b'));
22
23 self::assertSame('A generated column cannot have a DEFAULT value.', $problem->message());
24 self::assertSame('b', $problem->column?->value);
25 }
26
27 public function testMessageIsReportedForEachWrongUseOfAGeneratedColumn(): void
28 {
29 $semantics = new Semantics();
30 $flaw = static fn (string $sql): array => array_map(static fn (object $diagnostic): array => $diagnostic instanceof GeneratedColumnProblem ? [$diagnostic->flaw->name, $diagnostic->column?->value] : [$diagnostic::class], $semantics->inspect($sql, [])->facts->diagnostics);
31
32 self::assertSame([['WithDefault', 'b']], $flaw('CREATE TABLE t (a, b DEFAULT 1 AS (a))'));
33 self::assertSame([['WithDefault', 'b']], $flaw('CREATE TABLE t (a, b GENERATED ALWAYS AS (a) STORED DEFAULT (1))'));
34 self::assertSame([['InPrimaryKey', 'b']], $flaw('CREATE TABLE t (a, b AS (a) PRIMARY KEY)'));
35 self::assertSame([['InPrimaryKey', 'b']], $flaw('CREATE TABLE t (a, b AS (a), PRIMARY KEY (b))'));
36 self::assertSame([['NoPlainColumn', null]], $flaw('CREATE TABLE t (a AS (1), b AS (a))'));
37 self::assertSame([], $flaw('CREATE TABLE t (a, b AS (a) VIRTUAL, c AS (b) STORED)'));
38 }
39}
40