packages/sql-semantics-postgres/tests/Unit/Statement/Catalog/Access/Role/AlterRoleSettingTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Catalog\Access\Role;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Role\AlterRoleSetting::class)]
12#[Medium]
13final class AlterRoleSettingTest extends TestCase
14{
15    public function testRenderWritesTheRoleTheDatabaseAndTheSetting(): void
16    {
17        self::assertSame('ALTER ROLE joe IN DATABASE app SET work_mem TO \'4MB\'', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('ALTER ROLE joe IN DATABASE app SET work_mem TO \'4MB\'')->toString());
18    }
19
20    public function testRenderWritesAllForEveryRole(): void
21    {
22        self::assertSame('ALTER USER ALL RESET ALL', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('ALTER USER ALL RESET ALL')->toString());
23    }
24
25    public function testAnalysisKeepsNoRoleForAll(): void
26    {
27        $statement = (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('ALTER ROLE ALL IN DATABASE app RESET work_mem')->statement;
28        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Role\AlterRoleSetting::class, $statement);
29        self::assertNull($statement->role);
30        self::assertSame('app', $statement->database?->value);
31    }
32
33    public function testDeriveStatementReportsPublic(): void
34    {
35        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('ALTER ROLE public RESET ALL')->facts->diagnostics));
36    }
37
38    public function testDeriveStatementAcceptsAll(): void
39    {
40        self::assertSame([], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('ALTER ROLE ALL RESET ALL')->facts->diagnostics));
41    }
42
43    public function testRejectsTheGroupWord(): void
44    {
45        $this->expectExceptionMessage('ALTER GROUP changes members only.');
46        new \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Role\AlterRoleSetting(\SqlSemantics\Platform\PostgreSql\Statement\Object\RoleWord::Group, null, null, new \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Role\DropRole(\SqlSemantics\Platform\PostgreSql\Statement\Object\RoleWord::Role, [new \SqlSemantics\Platform\PostgreSql\Statement\Name\RoleSpec(\SqlSemantics\Platform\PostgreSql\Statement\Name\RoleSpecKind::Named, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('r'))]));
47    }
48}
49