packages/sql-semantics-postgres/tests/Unit/Rules/Access/RoleChecksTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Access;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Access\RoleChecks::class)]
12#[Medium]
13final class RoleChecksTest extends TestCase
14{
15    public function testOptionsReportsEveryProblemOnce(): void
16    {
17        self::assertSame(['unrecognized role option "bar"', 'invalid connection limit: -3', 'conflicting or redundant options'], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('ALTER ROLE joe bar CONNECTION LIMIT -3 CONNECTION LIMIT 2 INHERIT noinherit')->facts->diagnostics));
18    }
19
20    public function testOptionsAcceptsDistinctOptions(): void
21    {
22        self::assertSame([], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE ROLE joe SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN REPLICATION BYPASSRLS CONNECTION LIMIT -1 PASSWORD NULL VALID UNTIL \'infinity\' IN ROLE a ROLE b ADMIN c')->facts->diagnostics));
23    }
24
25    public function testReservedReportsThePrefix(): void
26    {
27        self::assertSame(['role name "pg_x" is reserved'], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE ROLE pg_x')->facts->diagnostics));
28    }
29
30    public function testAlterableReportsPublicAndReservedNames(): void
31    {
32        self::assertSame(['role name "pg_x" is reserved'], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('ALTER ROLE pg_x LOGIN')->facts->diagnostics));
33    }
34
35    public function testExistingReportsPublic(): void
36    {
37        self::assertSame(['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('DROP OWNED BY PUBLIC')->facts->diagnostics));
38    }
39
40    public function testExistingAcceptsSessionDesignations(): void
41    {
42        self::assertSame([], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('DROP OWNED BY CURRENT_USER, SESSION_USER, CURRENT_ROLE')->facts->diagnostics));
43    }
44
45    public function testDroppableReportsADesignation(): void
46    {
47        self::assertSame(['cannot use special role specifier in DROP ROLE'], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('DROP ROLE SESSION_USER')->facts->diagnostics));
48    }
49}
50