packages/sql-semantics-postgres/tests/Unit/Statement/Catalog/Access/Privilege/GrantRoleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Catalog\Access\Privilege;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Privilege\GrantRole::class)]
12#[Medium]
13final class GrantRoleTest extends TestCase
14{
15    public function testRenderWritesEveryClause(): void
16    {
17        self::assertSame('GRANT staff, SELECT, x (c) TO joe, CURRENT_USER WITH admin OPTION GRANTED BY boss', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('GRANT staff, SELECT, x (c) TO joe, CURRENT_USER WITH ADMIN OPTION GRANTED BY boss')->toString());
18    }
19
20    public function testAnalysisKeepsTheRolesAndMembers(): void
21    {
22        $statement = (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('GRANT staff TO joe, ann')->statement;
23        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Privilege\GrantRole::class, $statement);
24        self::assertSame('staff', $statement->roles[0]->privilege());
25        self::assertCount(2, $statement->members);
26        self::assertSame([], $statement->options);
27    }
28
29    public function testDeriveStatementReportsTheProblems(): void
30    {
31        self::assertSame(['column names cannot be included in GRANT/REVOKE ROLE', 'role "public" does not exist', 'role "public" does not exist', 'role "public" does not exist', 'unrecognized role option "foo"'], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('GRANT staff (a), public TO PUBLIC WITH foo TRUE GRANTED BY PUBLIC')->facts->diagnostics));
32    }
33
34    public function testDeriveStatementAcceptsTheKnownOptions(): void
35    {
36        self::assertSame([], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('GRANT staff TO joe WITH ADMIN TRUE, INHERIT FALSE, SET OPTION')->facts->diagnostics));
37    }
38
39    public function testRejectsAllAsARole(): void
40    {
41        $this->expectExceptionMessage('ALL is no role of a role grant.');
42        new \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Privilege\GrantRole([new \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Privilege\Privilege(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Privilege\PrivilegeKeyword::All, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a')])], [new \SqlSemantics\Platform\PostgreSql\Statement\Name\RoleSpec(\SqlSemantics\Platform\PostgreSql\Statement\Name\RoleSpecKind::Named, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('joe'))]);
43    }
44
45    public function testRejectsNoMember(): void
46    {
47        $this->expectExceptionMessage('A role grant names at least one member.');
48        new \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Privilege\GrantRole([new \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Privilege\Privilege(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('staff'))], []);
49    }
50}
51