packages/sql-semantics-postgres/tests/Unit/Lowering/Routine/CommentRuleTest.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\CommentRule;
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\Option\Word;
19
20#[CoversClass(CommentRule::class)]
21#[Small]
22final class CommentRuleTest extends TestCase
23{
24    public function testCommentLowersTheObject(): void
25    {
26        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
27        $tree = (new PostgreSqlParser('pg-17.2'))->parse("COMMENT ON CONSTRAINT c ON DOMAIN d IS 'x'");
28        self::assertSame(ObjectKind::DomainConstraint, (new CommentRule($lowering))->comment($tree->find('CommentStmt')[0])->kind);
29    }
30
31    public function testLabelLowersTheProvider(): void
32    {
33        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
34        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SECURITY LABEL FOR p ON LARGE OBJECT 5 IS NULL');
35        $label = (new CommentRule($lowering))->label($tree->find('SecLabelStmt')[0]);
36        self::assertSame([ObjectKind::LargeObject, null], [$label->kind, $label->label]);
37        self::assertInstanceOf(Word::class, $label->provider);
38    }
39
40    public function testTextLowersNull(): void
41    {
42        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
43        $tree = (new PostgreSqlParser('pg-17.2'))->parse('COMMENT ON SCHEMA s IS NULL');
44        self::assertNull((new CommentRule($lowering))->text($tree->find('comment_text')[0]));
45    }
46
47    public function testProviderLowersNoProvider(): void
48    {
49        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
50        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SECURITY LABEL ON SCHEMA s IS NULL');
51        self::assertNull((new CommentRule($lowering))->provider($tree->find('opt_provider')[0]));
52    }
53}
54