packages/sql-semantics-postgres/tests/Unit/Lowering/Routine/ObjectRuleTest.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\ObjectRule;
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\Platform;
17use SqlSemantics\Platform\PostgreSql\Statement\Name\ObjectKind;
18use SqlSemantics\Platform\PostgreSql\Statement\Object\Reference\CastPair;
19use SqlSemantics\Platform\PostgreSql\Statement\Object\Reference\MemberName;
20use SqlSemantics\Platform\PostgreSql\Statement\Object\Reference\OperatorGroupName;
21use SqlSemantics\Platform\PostgreSql\Statement\Object\Reference\RelationTarget;
22
23#[CoversClass(ObjectRule::class)]
24#[Small]
25final class ObjectRuleTest extends TestCase
26{
27    public function testKindLowersEachKindNonterminal(): void
28    {
29        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
30        $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP PROCEDURAL LANGUAGE l; DROP MATERIALIZED VIEW v; DROP RULE r ON t; COMMENT ON TABLESPACE s IS NULL');
31        $rule = new ObjectRule($lowering);
32        self::assertSame([ObjectKind::Language, ObjectKind::MaterializedView, ObjectKind::Rule, ObjectKind::Tablespace], [$rule->kind($tree->find('drop_type_name')[0]), $rule->kind($tree->find('object_type_any_name')[0]), $rule->kind($tree->find('object_type_name_on_any_name')[0]), $rule->kind($tree->find('object_type_name')[0])]);
33    }
34
35    public function testKindRejectsAnotherNonterminal(): void
36    {
37        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
38        $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP TABLE t');
39        $this->expectExceptionMessage('No semantic rule is implemented for: any_name: ColId');
40        (new ObjectRule($lowering))->kind($tree->find('any_name')[0]);
41    }
42
43    public function testTokenTellsTheTerminalAtAPosition(): void
44    {
45        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
46        $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP TABLE IF EXISTS t');
47        $form = $lowering->productions->form($tree->find('DropStmt')[0]);
48        self::assertSame([true, false], [(new ObjectRule($lowering))->token($form, 2, 'IF_P'), (new ObjectRule($lowering))->token($form, 1, 'IF_P')]);
49    }
50
51    public function testHasTellsWhetherATerminalIsWritten(): void
52    {
53        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
54        $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP TABLE IF EXISTS t');
55        $form = $lowering->productions->form($tree->find('DropStmt')[0]);
56        self::assertSame([true, false], [(new ObjectRule($lowering))->has($form, 'EXISTS'), (new ObjectRule($lowering))->has($form, 'CASCADE')]);
57    }
58
59    public function testStartSkipsProcedural(): 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('ALTER PROCEDURAL LANGUAGE l OWNER TO r');
63        self::assertSame(3, (new ObjectRule($lowering))->start($lowering->productions->form($tree->find('AlterOwnerStmt')[0])));
64    }
65
66    public function testStartRejectsAProductionWithoutObject(): void
67    {
68        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
69        $tree = (new PostgreSqlParser('pg-17.2'))->parse('CHECKPOINT');
70        $this->expectExceptionMessage('No semantic rule is implemented for: CheckPointStmt: CHECKPOINT');
71        (new ObjectRule($lowering))->start($lowering->productions->form($tree->find('CheckPointStmt')[0]));
72    }
73
74    public function testNamedLowersAMemberOfATable(): void
75    {
76        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
77        $tree = (new PostgreSqlParser('pg-17.2'))->parse('COMMENT ON POLICY p ON s.t IS NULL');
78        [$kind, $object] = (new ObjectRule($lowering))->named($lowering->productions->form($tree->find('CommentStmt')[0]), 2);
79        self::assertSame(ObjectKind::Policy, $kind);
80        self::assertInstanceOf(MemberName::class, $object);
81    }
82
83    public function testKeyedLowersACast(): void
84    {
85        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
86        $tree = (new PostgreSqlParser('pg-17.2'))->parse('COMMENT ON CAST (int AS text) IS NULL');
87        [$kind, $object] = (new ObjectRule($lowering))->keyed($lowering->productions->form($tree->find('CommentStmt')[0]), 2);
88        self::assertSame(ObjectKind::Cast, $kind);
89        self::assertInstanceOf(CastPair::class, $object);
90    }
91
92    public function testOperatorLowersAnOperatorFamily(): void
93    {
94        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
95        $tree = (new PostgreSqlParser('pg-17.2'))->parse('COMMENT ON OPERATOR FAMILY f USING btree IS NULL');
96        [$kind, $object] = (new ObjectRule($lowering))->operator($lowering->productions->form($tree->find('CommentStmt')[0]), 2);
97        self::assertSame(ObjectKind::OperatorFamily, $kind);
98        self::assertInstanceOf(OperatorGroupName::class, $object);
99    }
100
101    public function testAlteredLowersARelationAndTheNextPosition(): void
102    {
103        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
104        $tree = (new PostgreSqlParser('pg-17.2'))->parse('ALTER TABLE ONLY t RENAME TO u');
105        [$object, $next] = (new ObjectRule($lowering))->altered($lowering->productions->form($tree->find('RenameStmt')[0]), 2);
106        self::assertInstanceOf(RelationTarget::class, $object);
107        self::assertSame([true, 3], [$object->relation->only, $next]);
108    }
109}
110