packages/sql-semantics-postgres/tests/Unit/Statement/Object/DropTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Object;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Table\DeclaredTable;
17use SqlSemantics\Platform\PostgreSql\Semantics;
18use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
19use SqlSemantics\Platform\PostgreSql\Statement\Name\ObjectKind;
20use SqlSemantics\Platform\PostgreSql\Statement\Object\Drop;
21use SqlSemantics\Platform\PostgreSql\Statement\Object\Problem\ObjectProblem;
22use SqlSemantics\Platform\PostgreSql\Statement\Object\Problem\ObjectProblemKind;
23use SqlSemantics\Platform\PostgreSql\Statement\Object\Reference\UnqualifiedName;
24use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
25
26#[CoversClass(Drop::class)]
27#[Medium]
28final class DropTest extends TestCase
29{
30    public function testDeriveStatementResolvesTheDroppedTables(): void
31    {
32        $table = new Table(new QualifiedName(new Name('t')), new LanguageProfile(GrammarRelease::PostgreSql172), [new Column(new Name('a'), Builtin::Int4)]);
33        $operation = (new Semantics())->inspect('DROP TABLE t, u', [$table]);
34        $statement = $operation->statement;
35        self::assertInstanceOf(Drop::class, $statement);
36        $resolution = $operation->facts->relation($statement->objects[0])->table;
37        self::assertInstanceOf(DeclaredTable::class, $resolution);
38        self::assertSame($table, $resolution->table);
39        self::assertSame(['Relation u does not exist.'], [$operation->facts->diagnostics[0]->message()]);
40    }
41
42    public function testDeriveStatementSkipsAMissingTableUnderIfExists(): void
43    {
44        $operation = (new Semantics())->inspect('DROP VIEW IF EXISTS v', []);
45        $statement = $operation->statement;
46        self::assertInstanceOf(Drop::class, $statement);
47        self::assertNull($operation->facts->relation($statement->objects[0])->table);
48        self::assertSame([], $operation->facts->diagnostics);
49    }
50
51    public function testDeriveStatementReportsConcurrentDrops(): void
52    {
53        $operation = (new Semantics())->inspect('DROP INDEX CONCURRENTLY i, j CASCADE');
54        self::assertEquals([new ObjectProblem(ObjectProblemKind::ConcurrentMultiple), new ObjectProblem(ObjectProblemKind::ConcurrentCascade)], $operation->facts->diagnostics);
55    }
56
57    public function testRenderWritesEachKind(): void
58    {
59        $semantics = new Semantics();
60        self::assertSame(
61            ['DROP LANGUAGE l', 'DROP TRIGGER IF EXISTS g ON t RESTRICT', 'DROP DOMAIN d, e', 'DROP OPERATOR FAMILY f USING btree', 'DROP TEXT SEARCH PARSER p'],
62            [$semantics->inspect('DROP PROCEDURAL LANGUAGE l')->toString(), $semantics->inspect('DROP TRIGGER IF EXISTS g ON t RESTRICT')->toString(), $semantics->inspect('DROP DOMAIN d, e')->toString(), $semantics->inspect('DROP OPERATOR FAMILY f USING btree')->toString(), $semantics->inspect('drop text search parser p')->toString()],
63        );
64    }
65
66    public function testRejectsAWrongForm(): void
67    {
68        $this->expectExceptionMessage('DROP names each object as the grammar names objects of its kind.');
69        new Drop(ObjectKind::Schema, [new DottedName([new Name('a'), new Name('b')])]);
70    }
71
72    public function testRejectsSeveralObjectsOfASingleKind(): void
73    {
74        $this->expectExceptionMessage('DROP names one object of this kind.');
75        new Drop(ObjectKind::Cast, [new \SqlSemantics\Platform\PostgreSql\Statement\Object\Reference\CastPair(new \SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName(new \SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation(new DottedName([new Name('a')]))), new \SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName(new \SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation(new DottedName([new Name('b')])))), new \SqlSemantics\Platform\PostgreSql\Statement\Object\Reference\CastPair(new \SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName(new \SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation(new DottedName([new Name('c')]))), new \SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName(new \SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation(new DottedName([new Name('d')]))))]);
76    }
77
78    public function testRejectsConcurrentlyForAnotherKind(): void
79    {
80        $this->expectExceptionMessage('Only DROP INDEX is written CONCURRENTLY.');
81        new Drop(ObjectKind::Schema, [new UnqualifiedName(new Name('s'))], false, null, true);
82    }
83
84    public function testDeriveStatementReportsAnotherKind(): void
85    {
86        $semantics = new Semantics();
87        $context = [$semantics->inspect('CREATE TABLE t (a int)'), $semantics->inspect('CREATE VIEW v AS SELECT 1 AS a')];
88        self::assertSame(['"v" is not a table'], array_map(static fn ($problem): string => $problem->message(), $semantics->inspect('DROP TABLE IF EXISTS v, t', $context)->facts->diagnostics));
89        self::assertSame(['"t" is not a sequence'], array_map(static fn ($problem): string => $problem->message(), $semantics->inspect('DROP SEQUENCE t', $context)->facts->diagnostics));
90    }
91}
92