packages/sql-semantics-postgres/tests/Unit/Lowering/Access/DefaultPrivilegeRuleTest.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\DefaultPrivilegeRule::class)]
12#[Medium]
13final class DefaultPrivilegeRuleTest extends TestCase
14{
15 public function testStatementLowersDropOwned(): void
16 {
17 $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('DROP OWNED BY a');
18 $result = (new \SqlSemantics\Platform\PostgreSql\Lowering\Access\DefaultPrivilegeRule(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('DropOwnedStmt')[0]);
19 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Owned\DropOwned::class, $result);
20 }
21
22 public function testStatementLowersReassignOwned(): void
23 {
24 $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('REASSIGN OWNED BY a TO b');
25 $result = (new \SqlSemantics\Platform\PostgreSql\Lowering\Access\DefaultPrivilegeRule(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('ReassignOwnedStmt')[0]);
26 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Owned\ReassignOwned::class, $result);
27 }
28
29 public function testStatementLowersDefaultPrivileges(): void
30 {
31 $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('ALTER DEFAULT PRIVILEGES GRANT SELECT ON TABLES TO a');
32 $result = (new \SqlSemantics\Platform\PostgreSql\Lowering\Access\DefaultPrivilegeRule(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('AlterDefaultPrivilegesStmt')[0]);
33 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Defaults\AlterDefaultPrivileges::class, $result);
34 }
35
36 public function testScopesLowersTheClausesInOrder(): void
37 {
38 $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('ALTER DEFAULT PRIVILEGES IN SCHEMA s FOR USER u GRANT SELECT ON TABLES TO a');
39 $result = (new \SqlSemantics\Platform\PostgreSql\Lowering\Access\DefaultPrivilegeRule(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)))->scopes($tree->find('DefACLOptionList')[0]);
40 self::assertSame(['schemas', 'roles'], [$result[0]->option(), $result[1]->option()]);
41 }
42
43 public function testActionLowersARevoke(): void
44 {
45 $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('ALTER DEFAULT PRIVILEGES REVOKE GRANT OPTION FOR SELECT ON TABLES FROM a CASCADE');
46 $result = (new \SqlSemantics\Platform\PostgreSql\Lowering\Access\DefaultPrivilegeRule(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)))->action($tree->find('DefACLAction')[0]);
47 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Defaults\DefaultRevoke::class, $result);
48 self::assertTrue($result->grantOptionOnly);
49 }
50
51 public function testObjectsLowersTheKind(): void
52 {
53 $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('ALTER DEFAULT PRIVILEGES GRANT usage ON TYPES TO a');
54 $result = (new \SqlSemantics\Platform\PostgreSql\Lowering\Access\DefaultPrivilegeRule(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)))->objects($tree->find('defacl_privilege_target')[0]);
55 self::assertSame(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Defaults\DefaultObjectKind::Types, $result);
56 }
57}
58