packages/sql-semantics-sqlite/tests/Unit/Lowering/Definition/SchemaRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Definition;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Lowering\Definition\SchemaRule;
11use SqlSemantics\Platform\Sqlite\Semantics;
12use SqlSemantics\Platform\Sqlite\Statement\Schema\AlterAddColumn;
13use SqlSemantics\Platform\Sqlite\Statement\Schema\AlterDropColumn;
14use SqlSemantics\Platform\Sqlite\Statement\Schema\AlterRenameColumn;
15use SqlSemantics\Platform\Sqlite\Statement\Schema\AlterRenameTable;
16use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateIndex;
17use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateView;
18use SqlSemantics\Platform\Sqlite\Statement\Schema\Drop;
19use SqlSemantics\Platform\Sqlite\Statement\Schema\SchemaObjectKind;
20
21#[CoversClass(SchemaRule::class)]
22#[Medium]
23final class SchemaRuleTest extends TestCase
24{
25    public function testCommandLowersEachSchemaCommandToItsOwnRequest(): void
26    {
27        $semantics = new Semantics();
28
29        self::assertInstanceOf(CreateView::class, $semantics->analyze('CREATE VIEW v AS SELECT 1')->statement);
30        self::assertInstanceOf(CreateIndex::class, $semantics->analyze('CREATE INDEX i ON t (a)')->statement);
31        self::assertInstanceOf(AlterRenameTable::class, $semantics->analyze('ALTER TABLE t RENAME TO u')->statement);
32        self::assertInstanceOf(AlterAddColumn::class, $semantics->analyze('ALTER TABLE t ADD a')->statement);
33        self::assertInstanceOf(AlterDropColumn::class, $semantics->analyze('ALTER TABLE t DROP a')->statement);
34        self::assertInstanceOf(AlterRenameColumn::class, $semantics->analyze('ALTER TABLE t RENAME a TO b')->statement);
35    }
36
37    public function testCommandLowersEachDropToItsObjectKind(): void
38    {
39        $semantics = new Semantics();
40        $kinds = array_map(static fn (string $sql): ?SchemaObjectKind => $semantics->analyze($sql)->statement instanceof Drop ? $semantics->analyze($sql)->statement->object : null, ['DROP TABLE a', 'DROP VIEW a', 'DROP INDEX a', 'DROP TRIGGER a']);
41
42        self::assertSame([SchemaObjectKind::Table, SchemaObjectKind::View, SchemaObjectKind::Index, SchemaObjectKind::Trigger], $kinds);
43    }
44
45    public function testViewKeepsTheHeaderFlagsAndTheOptionalColumnList(): void
46    {
47        $semantics = new Semantics();
48        $plain = $semantics->analyze('CREATE VIEW v AS SELECT 1')->statement;
49        $full = $semantics->analyze('CREATE TEMP VIEW IF NOT EXISTS v (a) AS SELECT 1')->statement;
50
51        self::assertInstanceOf(CreateView::class, $plain);
52        self::assertInstanceOf(CreateView::class, $full);
53        self::assertNull($plain->columns);
54        self::assertFalse($plain->temporary);
55        self::assertCount(1, $full->columns ?? []);
56        self::assertTrue($full->temporary);
57        self::assertTrue($full->ifNotExists);
58    }
59
60    public function testIndexKeepsTheFlagsTheTermsAndTheCondition(): void
61    {
62        $semantics = new Semantics();
63        $plain = $semantics->analyze('CREATE INDEX i ON t (a)')->statement;
64        $full = $semantics->analyze('CREATE UNIQUE INDEX IF NOT EXISTS aux.i ON t (a, b DESC) WHERE a > 0')->statement;
65
66        self::assertInstanceOf(CreateIndex::class, $plain);
67        self::assertInstanceOf(CreateIndex::class, $full);
68        self::assertFalse($plain->unique);
69        self::assertNull($plain->where);
70        self::assertTrue($full->unique);
71        self::assertTrue($full->ifNotExists);
72        self::assertSame('t', $full->table->value);
73        self::assertCount(2, $full->terms);
74        self::assertNotNull($full->where);
75    }
76
77    public function testIfExistsLowersTheOptionalClause(): void
78    {
79        $semantics = new Semantics();
80        $plain = $semantics->analyze('DROP TABLE t')->statement;
81        $tolerant = $semantics->analyze('DROP TABLE IF EXISTS t')->statement;
82
83        self::assertInstanceOf(Drop::class, $plain);
84        self::assertInstanceOf(Drop::class, $tolerant);
85        self::assertFalse($plain->ifExists);
86        self::assertTrue($tolerant->ifExists);
87    }
88
89    public function testAddedLowersTheQualifiedTableName(): void
90    {
91        $statement = (new Semantics())->analyze('ALTER TABLE aux.t ADD a')->statement;
92
93        self::assertInstanceOf(AlterAddColumn::class, $statement);
94        self::assertSame('aux', $statement->table->schema?->value);
95        self::assertSame('t', $statement->table->name->value);
96    }
97
98    public function testKeywordIsOptionalBeforeTheColumn(): void
99    {
100        $semantics = new Semantics();
101        $short = $semantics->analyze('ALTER TABLE t DROP a')->statement;
102        $long = $semantics->analyze('ALTER TABLE t DROP COLUMN a')->statement;
103
104        self::assertInstanceOf(AlterDropColumn::class, $short);
105        self::assertInstanceOf(AlterDropColumn::class, $long);
106        self::assertSame('a', $short->column->value);
107        self::assertSame('a', $long->column->value);
108    }
109}
110