packages/sql-semantics-postgres/tests/Unit/Lowering/Routine/DropRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Routine;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlParser\PostgreSql\PostgreSqlParser;
11use SqlSemantics\Platform\PostgreSql\Lowering\Lowering;
12use SqlSemantics\Platform\PostgreSql\Lowering\Routine\DropRule;
13use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
14use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
15use SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
17use SqlSemantics\Platform\PostgreSql\Platform;
18use SqlSemantics\Platform\PostgreSql\Statement\Name\ObjectKind;
19use SqlSemantics\Platform\PostgreSql\Statement\Object\Reference\TypeReference;
20use SqlSemantics\Platform\PostgreSql\Statement\Object\Reference\UnqualifiedName;
21
22#[CoversClass(DropRule::class)]
23#[Small]
24final class DropRuleTest extends TestCase
25{
26    public function testDropLowersEachForm(): void
27    {
28        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
29        $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP INDEX CONCURRENTLY IF EXISTS i; DROP ROUTINE IF EXISTS r CASCADE; DROP OPERATOR CLASS IF EXISTS c USING btree');
30        $rule = new DropRule($lowering);
31        $index = $rule->drop($tree->find('DropStmt')[0]);
32        $routine = $rule->drop($tree->find('RemoveFuncStmt')[0]);
33        $class = $rule->drop($tree->find('DropOpClassStmt')[0]);
34        self::assertSame([true, true, ObjectKind::Routine, ObjectKind::OperatorClass], [$index->concurrently, $index->ifExists, $routine->kind, $class->kind]);
35    }
36
37    public function testSignedLowersAggregates(): void
38    {
39        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
40        $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP AGGREGATE a(*), b(*)');
41        self::assertCount(2, (new DropRule($lowering))->signed($lowering->productions->form($tree->find('RemoveAggrStmt')[0]), ObjectKind::Aggregate, 2));
42    }
43
44    public function testGenericLowersMembersOfATable(): void
45    {
46        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
47        $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP TRIGGER IF EXISTS g ON s.t');
48        $drop = (new DropRule($lowering))->generic($lowering->productions->form($tree->find('DropStmt')[0]), true, null);
49        self::assertSame([ObjectKind::Trigger, 1], [$drop->kind, count($drop->objects)]);
50    }
51
52    public function testUnqualifiedWrapsEachName(): void
53    {
54        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
55        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT 1');
56        self::assertEquals([new UnqualifiedName(new Name('a'))], (new DropRule($lowering))->unqualified([new Name('a')]));
57    }
58
59    public function testTypesWrapsEachType(): void
60    {
61        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
62        $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP TYPE a, b');
63        self::assertContainsOnlyInstancesOf(TypeReference::class, (new DropRule($lowering))->types($tree->find('type_name_list')[0]));
64    }
65}
66