packages/sql-semantics-sqlite/tests/Unit/Statement/Schema/AlterDropColumnTest.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\Table\DeclaredTable;
11use SqlSemantics\Platform\Sqlite\Semantics;
12use SqlSemantics\Platform\Sqlite\Statement\Schema\AlterDropColumn;
13use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\AlterationRefused;
14use SqlSemantics\Platform\Sqlite\Statement\Schema\Problem\UnknownColumn;
15
16#[CoversClass(AlterDropColumn::class)]
17#[Medium]
18final class AlterDropColumnTest extends TestCase
19{
20 public function testDeriveStatementResolvesTheTableAndProvidesNoDeclaration(): void
21 {
22 $semantics = new Semantics();
23 $table = $semantics->inspect('CREATE TABLE t (a, b)');
24 $operation = $semantics->inspect('ALTER TABLE t DROP COLUMN B', [$table]);
25
26 self::assertInstanceOf(DeclaredTable::class, $operation->facts->relation($operation->statement)->table);
27 self::assertSame([], $operation->facts->diagnostics);
28 self::assertSame([], $operation->declarations());
29 self::assertCount(2, $table->declarations()[0]->columns);
30 }
31
32 public function testDeriveStatementReportsAColumnTheTableDoesNotHave(): void
33 {
34 $semantics = new Semantics();
35 $table = $semantics->inspect('CREATE TABLE t (a, b)');
36
37 self::assertInstanceOf(UnknownColumn::class, $semantics->inspect('ALTER TABLE t DROP c', [$table])->facts->diagnostics[0]);
38 }
39
40 public function testDeriveStatementReportsTheRemovalOfTheOnlyColumn(): void
41 {
42 $semantics = new Semantics();
43 $table = $semantics->inspect('CREATE TABLE t (a)');
44
45 self::assertInstanceOf(AlterationRefused::class, $semantics->inspect('ALTER TABLE t DROP a', [$table])->facts->diagnostics[0]);
46 }
47
48 public function testDeriveStatementReportsNothingAboutColumnsOfAnUndeclaredTable(): void
49 {
50 self::assertSame([], (new Semantics())->inspect('ALTER TABLE t DROP COLUMN c')->facts->diagnostics);
51 }
52
53 public function testRenderDropsTheOptionalKeyword(): void
54 {
55 $operation = (new Semantics())->inspect('alter table main.t drop column b');
56
57 self::assertInstanceOf(AlterDropColumn::class, $operation->statement);
58 self::assertSame('ALTER TABLE main.t DROP b', $operation->toString());
59 }
60}
61