packages/sql-semantics-sqlite/tests/Unit/Statement/Schema/LiteralColumnTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Schema;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\MissingColumn;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\ResolvedColumn;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
13use SqlSemantics\Platform\Sqlite\Semantics;
14use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\TextLiteral;
15use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateIndex;
16use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateTable;
17use SqlSemantics\Platform\Sqlite\Statement\Schema\LiteralColumn;
18use SqlSemantics\Platform\Sqlite\Statement\Schema\Table\TablePrimaryKey;
19use SqlSemantics\Platform\Sqlite\Statement\Schema\Table\TableUnique;
20use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
21
22#[CoversClass(LiteralColumn::class)]
23#[Medium]
24final class LiteralColumnTest extends TestCase
25{
26 public function testNameIsTheValueOfTheLiteral(): void
27 {
28 self::assertSame("a'b", (new LiteralColumn(new TextLiteral("a'b")))->name()->value);
29 }
30
31 public function testDeriveScalarResolvesTheColumnOfTheTableBeingDefined(): void
32 {
33 $create = (new Semantics())->inspect("CREATE TABLE t (a INTEGER, b, UNIQUE ('b'), PRIMARY KEY ('A'))");
34 $statement = $create->statement;
35 $table = $create->declarations()[0];
36
37 self::assertInstanceOf(CreateTable::class, $statement);
38 $unique = $statement->constraints[0]->items[0];
39 $key = $statement->constraints[1]->items[0];
40 self::assertInstanceOf(TableUnique::class, $unique);
41 self::assertInstanceOf(TablePrimaryKey::class, $key);
42 self::assertInstanceOf(LiteralColumn::class, $unique->terms[0]->expression);
43 self::assertInstanceOf(LiteralColumn::class, $key->terms[0]->expression);
44 $resolution = $create->facts->scalar($unique->terms[0]->expression)->resolution;
45 self::assertInstanceOf(ResolvedColumn::class, $resolution);
46 self::assertSame($table->columns[1], $resolution->slot->column);
47 self::assertSame($table->columns[0], $table->implicit[0]->column);
48 self::assertSame([], $create->facts->diagnostics);
49 self::assertEquals(new Known(Storage::Text), $create->facts->scalar($key->terms[0]->expression->literal)->type);
50 }
51
52 public function testDeriveScalarReportsANameThatIsNoColumn(): void
53 {
54 $semantics = new Semantics();
55 $table = $semantics->inspect('CREATE TABLE t (a)');
56 $create = $semantics->inspect("CREATE TABLE u (a, UNIQUE ('zz'))");
57 $index = $semantics->inspect("CREATE INDEX i ON t ('zz')", [$table]);
58
59 self::assertCount(1, $create->facts->diagnostics);
60 self::assertInstanceOf(MissingColumn::class, $create->facts->diagnostics[0]);
61 self::assertSame('zz', $create->facts->diagnostics[0]->name->value);
62 self::assertInstanceOf(MissingColumn::class, $index->facts->diagnostics[0]);
63 }
64
65 public function testRenderWritesTheLiteralAsWritten(): void
66 {
67 $semantics = new Semantics();
68 $index = $semantics->inspect("create index i on t ('a' collate nocase desc, (('b')))");
69
70 self::assertInstanceOf(CreateIndex::class, $index->statement);
71 self::assertSame("CREATE INDEX i ON t ('a' COLLATE nocase DESC, (('b')))", $index->toString());
72 self::assertSame("CREATE TABLE t (a, PRIMARY KEY ('a' COLLATE nocase COLLATE binary))", $semantics->inspect("CREATE TABLE t (a, PRIMARY KEY ('a' COLLATE nocase COLLATE binary))")->toString());
73 }
74}
75