packages/sql-semantics-postgres/tests/Unit/Lowering/Access/GrantRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Access;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Lowering\Access\GrantRule::class)]
12#[Medium]
13final class GrantRuleTest extends TestCase
14{
15    public function testStatementLowersGrant(): void
16    {
17        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('GRANT SELECT ON t TO a');
18        $result = (new \SqlSemantics\Platform\PostgreSql\Lowering\Access\GrantRule(new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)))->statement($tree->find('GrantStmt')[0]);
19        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Privilege\Grant::class, $result);
20    }
21
22    public function testStatementLowersRevokeOfTheGrantOption(): void
23    {
24        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('REVOKE GRANT OPTION FOR SELECT ON t FROM a CASCADE');
25        $result = (new \SqlSemantics\Platform\PostgreSql\Lowering\Access\GrantRule(new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)))->statement($tree->find('RevokeStmt')[0]);
26        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Privilege\Revoke::class, $result);
27        self::assertTrue($result->grantOptionOnly);
28    }
29
30    public function testTargetLowersRelationsWithoutTheNoiseWord(): void
31    {
32        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('GRANT SELECT ON a, s.b TO u');
33        $result = (new \SqlSemantics\Platform\PostgreSql\Lowering\Access\GrantRule(new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)))->target($tree->find('privilege_target')[0]);
34        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Target\RelationsTarget::class, $result);
35        self::assertSame('s', $result->relations[1]->name->schema?->value);
36    }
37
38    public function testTargetLowersSchemaContents(): void
39    {
40        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('GRANT SELECT ON ALL SEQUENCES IN SCHEMA a TO u');
41        $result = (new \SqlSemantics\Platform\PostgreSql\Lowering\Access\GrantRule(new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)))->target($tree->find('privilege_target')[0]);
42        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Target\SchemaContentsTarget::class, $result);
43        self::assertSame(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Target\PrivilegeObjectKind::Sequence, $result->object());
44    }
45
46    public function testTargetLowersLargeObjects(): void
47    {
48        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('GRANT SELECT ON LARGE OBJECT 5 TO u');
49        $result = (new \SqlSemantics\Platform\PostgreSql\Lowering\Access\GrantRule(new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)))->target($tree->find('privilege_target')[0]);
50        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Target\LargeObjectsTarget::class, $result);
51    }
52
53    public function testTargetLowersEveryNamedKind(): void
54    {
55        self::assertSame('GRANT ALL ON TABLESPACE a TO u', (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze('GRANT ALL ON TABLESPACE a TO u; ')->toString());
56    }
57
58    public function testParametersLowersDottedNames(): void
59    {
60        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('GRANT SET ON PARAMETER a.b, c TO u');
61        $result = (new \SqlSemantics\Platform\PostgreSql\Lowering\Access\GrantRule(new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)))->parameters($tree->find('parameter_name_list')[0]);
62        self::assertSame(['a.b', 'c'], [$result[0]->parameter(), $result[1]->parameter()]);
63    }
64}
65