packages/sql-semantics-sqlite/tests/Unit/Statement/Schema/DropTest.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\Model\Statement\Reference\Table\MissingTable;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table\UndeclaredTable;
13use SqlSemantics\Platform\Sqlite\Semantics;
14use SqlSemantics\Platform\Sqlite\Statement\Schema\AbsentRelation;
15use SqlSemantics\Platform\Sqlite\Statement\Schema\Drop;
16use SqlSemantics\Platform\Sqlite\Statement\Schema\Refusal\KindRefusal;
17use SqlSemantics\Platform\Sqlite\Statement\Schema\Refusal\WrongRelationKind;
18use SqlSemantics\Platform\Sqlite\Statement\Schema\SchemaObjectKind;
19
20#[CoversClass(Drop::class)]
21#[Medium]
22final class DropTest extends TestCase
23{
24    public function testDeriveStatementResolvesADroppedTable(): void
25    {
26        $semantics = new Semantics();
27        $table = $semantics->inspect('CREATE TABLE t (a)');
28        $operation = $semantics->inspect('DROP TABLE t', [$table]);
29        $resolution = $operation->facts->relation($operation->statement)->table;
30
31        self::assertInstanceOf(DeclaredTable::class, $resolution);
32        self::assertSame($table->declarations()[0], $resolution->table);
33        self::assertSame([], $operation->declarations());
34        self::assertSame([], $operation->facts->diagnostics);
35    }
36
37    public function testDeriveStatementReportsAMissingTableUnlessIfExistsIsWritten(): void
38    {
39        $semantics = new Semantics();
40        $strict = $semantics->inspect('DROP TABLE t', []);
41        $tolerant = $semantics->inspect('DROP VIEW IF EXISTS t', []);
42
43        self::assertInstanceOf(MissingTable::class, $strict->facts->diagnostics[0]);
44        self::assertSame([], $tolerant->facts->diagnostics);
45        self::assertInstanceOf(AbsentRelation::class, $tolerant->facts->relation($tolerant->statement)->table);
46    }
47
48    public function testDeriveStatementReportsTheDropOfARelationOfTheOtherKind(): void
49    {
50        $semantics = new Semantics();
51        $declarations = [$semantics->inspect('CREATE TABLE t (a)'), $semantics->inspect('CREATE VIEW v AS SELECT 1')];
52        $table = $semantics->inspect('DROP TABLE IF EXISTS v', $declarations);
53        $view = $semantics->inspect('DROP VIEW t', $declarations);
54
55        self::assertInstanceOf(WrongRelationKind::class, $table->facts->diagnostics[0]);
56        self::assertSame(KindRefusal::DropTable, $table->facts->diagnostics[0]->refusal);
57        self::assertInstanceOf(WrongRelationKind::class, $view->facts->diagnostics[0]);
58        self::assertSame(KindRefusal::DropView, $view->facts->diagnostics[0]->refusal);
59        self::assertSame([], $semantics->inspect('DROP VIEW v', $declarations)->facts->diagnostics);
60    }
61
62    public function testDeriveStatementKeepsAnUndeclaredTableUndeclared(): void
63    {
64        $operation = (new Semantics())->inspect('DROP TABLE IF EXISTS t');
65
66        self::assertInstanceOf(UndeclaredTable::class, $operation->facts->relation($operation->statement)->table);
67    }
68
69    public function testDeriveStatementRecordsNothingForIndexesAndTriggers(): void
70    {
71        $semantics = new Semantics();
72        $index = $semantics->inspect('DROP INDEX i', []);
73        $trigger = $semantics->inspect('DROP TRIGGER tr', []);
74
75        self::assertFalse($index->facts->covers($index->statement));
76        self::assertFalse($trigger->facts->covers($trigger->statement));
77        self::assertSame([], $index->facts->diagnostics);
78    }
79
80    public function testRenderWritesTheObjectKindAndTheClause(): void
81    {
82        $semantics = new Semantics();
83        $operation = $semantics->inspect('drop trigger if exists main.tr');
84
85        self::assertInstanceOf(Drop::class, $operation->statement);
86        self::assertSame(SchemaObjectKind::Trigger, $operation->statement->object);
87        self::assertSame('DROP TRIGGER IF EXISTS main.tr', $operation->toString());
88        self::assertSame('DROP INDEX i', $semantics->inspect('DROP INDEX i')->toString());
89        self::assertSame('DROP VIEW v', $semantics->inspect('DROP VIEW v')->toString());
90        self::assertSame('DROP TABLE t', $semantics->inspect('DROP TABLE t')->toString());
91    }
92}
93