packages/sql-semantics-postgres/tests/Unit/Statement/Catalog/Access/Privilege/GrantTest.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\Grant::class)]
12#[Medium]
13final class GrantTest extends TestCase
14{
15    public function testRenderDropsTheNoiseWords(): void
16    {
17        self::assertSame('GRANT ALL ON TABLE accounts TO staff WITH GRANT OPTION GRANTED BY boss', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('GRANT ALL PRIVILEGES ON accounts TO GROUP staff WITH GRANT OPTION GRANTED BY boss')->toString());
18    }
19
20    public function testRenderWritesAllWithColumns(): void
21    {
22        self::assertSame('GRANT ALL (a, b) ON TABLE t TO public', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('GRANT ALL PRIVILEGES (a, b) ON TABLE t TO PUBLIC')->toString());
23    }
24
25    public function testAnalysisKeepsAllPrivilegesAsAnEmptyList(): void
26    {
27        $statement = (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('GRANT ALL ON SCHEMA s TO joe')->statement;
28        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Privilege\Grant::class, $statement);
29        self::assertSame([], $statement->privileges);
30        self::assertFalse($statement->grantOption);
31        self::assertNull($statement->grantor);
32    }
33
34    public function testDeriveStatementReportsTheProblems(): void
35    {
36        self::assertSame(['unrecognized privilege type "foo"', 'invalid privilege type EXECUTE for schema', 'grant options can only be granted to roles', 'role "public" does not exist'], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('GRANT foo, EXECUTE ON SCHEMA s TO PUBLIC WITH GRANT OPTION GRANTED BY PUBLIC')->facts->diagnostics));
37    }
38
39    public function testDeriveStatementKeepsAGrantOptionOnSchemaContentsOpen(): void
40    {
41        self::assertSame([], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('GRANT SELECT ON ALL TABLES IN SCHEMA s TO PUBLIC WITH GRANT OPTION')->facts->diagnostics));
42    }
43
44    public function testRejectsAllAmongOtherPrivileges(): void
45    {
46        $this->expectExceptionMessage('ALL with a column list is the only privilege of its list.');
47        new \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Privilege\Grant([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\Catalog\Access\Privilege\Privilege(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Privilege\PrivilegeKeyword::Select)], new \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Target\NamedObjectsTarget(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Target\PrivilegeObjectKind::Schema, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('s')]), [new \SqlSemantics\Platform\PostgreSql\Statement\Name\RoleSpec(\SqlSemantics\Platform\PostgreSql\Statement\Name\RoleSpecKind::Named, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('joe'))]);
48    }
49
50    public function testRejectsNoGrantee(): void
51    {
52        $this->expectExceptionMessage('A grant names at least one grantee.');
53        new \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Privilege\Grant([], new \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Target\NamedObjectsTarget(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Target\PrivilegeObjectKind::Schema, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('s')]), []);
54    }
55}
56