packages/sql-semantics-sqlite/tests/Unit/Rules/Definition/Expression/LiteralDefaultsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Definition\Expression;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Scalar;
11use SqlSemantics\Platform\Sqlite\Rules\Definition\Expression\LiteralDefaults;
12use SqlSemantics\Platform\Sqlite\Semantics;
13use SqlSemantics\Platform\Sqlite\Statement\Expression\Grouped;
14use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\IntegerLiteral;
15use SqlSemantics\Platform\Sqlite\Statement\Expression\RowExpression;
16use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\DefaultExpression;
17use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\DefaultLiteral;
18use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\DefaultWord;
19use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateTable;
20
21#[CoversClass(LiteralDefaults::class)]
22#[Medium]
23final class LiteralDefaultsTest extends TestCase
24{
25    public function testDefaultsAnswersTheDefaultClausesInWrittenOrder(): void
26    {
27        $statement = (new Semantics())->inspect('CREATE TABLE t (a NOT NULL DEFAULT 1 UNIQUE DEFAULT (2) DEFAULT word)')->statement;
28
29        self::assertInstanceOf(CreateTable::class, $statement);
30        $defaults = (new LiteralDefaults())->defaults($statement->columns[0]->constraints);
31        self::assertCount(3, $defaults);
32        self::assertInstanceOf(DefaultLiteral::class, $defaults[0]);
33        self::assertInstanceOf(DefaultExpression::class, $defaults[1]);
34        self::assertInstanceOf(DefaultWord::class, $defaults[2]);
35    }
36
37    public function testNullIsTheLiteralNullWithOrWithoutParentheses(): void
38    {
39        $statement = (new Semantics())->inspect('CREATE TABLE t (a DEFAULT NULL DEFAULT (NULL) DEFAULT ((NULL)) DEFAULT -NULL DEFAULT (+NULL) DEFAULT 0 DEFAULT null)')->statement;
40
41        self::assertInstanceOf(CreateTable::class, $statement);
42        $rule = new LiteralDefaults();
43        self::assertSame([true, true, true, false, false, false, true], array_map(static fn (DefaultLiteral|DefaultExpression|DefaultWord $default): bool => $rule->null($default), $rule->defaults($statement->columns[0]->constraints)));
44    }
45
46    public function testLiteralTellsTheDefaultsSqliteComputesWhenAColumnIsAdded(): void
47    {
48        $statement = (new Semantics())->inspect("CREATE TABLE t (a DEFAULT 1 DEFAULT -1.5 DEFAULT 'x' DEFAULT x'00' DEFAULT CURRENT_TIME DEFAULT word DEFAULT TRUE DEFAULT (1) DEFAULT (-(-5)) DEFAULT (+'x') DEFAULT (CAST(1 AS TEXT)) DEFAULT (true) DEFAULT (1 + 1) DEFAULT (abs(1)) DEFAULT (CURRENT_DATE) DEFAULT (CAST(abs(1) AS TEXT)) DEFAULT (~1))")->statement;
49
50        self::assertInstanceOf(CreateTable::class, $statement);
51        $rule = new LiteralDefaults();
52        self::assertSame([true, true, true, true, false, true, true, true, true, true, true, true, false, false, false, false, false], array_map(static fn (DefaultLiteral|DefaultExpression|DefaultWord $default): bool => $rule->literal($default), $rule->defaults($statement->columns[0]->constraints)));
53    }
54
55    public function testConstantLooksThroughParenthesesSignsAndCasts(): void
56    {
57        $row = (new Semantics())->inspect('SELECT (((-(+CAST((1) AS INT)))), -(1 + 1), NOT 1, (?))')->field(0)->expression;
58        $rule = new LiteralDefaults();
59
60        self::assertInstanceOf(RowExpression::class, $row);
61        self::assertSame([true, false, false, false], array_map(static fn (Scalar $item): bool => $rule->constant($item), $row->items));
62    }
63
64    public function testCoreRemovesTheParentheses(): void
65    {
66        $literal = new IntegerLiteral('1');
67
68        self::assertSame($literal, (new LiteralDefaults())->core(new Grouped(new Grouped($literal))));
69        self::assertSame($literal, (new LiteralDefaults())->core($literal));
70    }
71}
72