packages/sql-semantics-postgres/tests/Unit/Statement/Catalog/Access/Role/AlterRoleTest.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\AlterRole::class)]
12#[Medium]
13final class AlterRoleTest extends TestCase
14{
15 public function testRenderDropsTheNoiseWordWith(): void
16 {
17 self::assertSame('ALTER USER CURRENT_USER nosuperuser USER a', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('ALTER USER CURRENT_USER WITH NOSUPERUSER USER a')->toString());
18 }
19
20 public function testDeriveStatementReportsPublicAndAReservedName(): void
21 {
22 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 LOGIN; ')->facts->diagnostics));
23 }
24
25 public function testDeriveStatementReportsAReservedName(): void
26 {
27 self::assertSame(['role name "pg_monitor" is reserved', '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 pg_monitor LOGIN LOGIN')->facts->diagnostics));
28 }
29
30 public function testRejectsTheGroupWord(): void
31 {
32 $this->expectExceptionMessage('ALTER GROUP changes members only.');
33 new \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Role\AlterRole(\SqlSemantics\Platform\PostgreSql\Statement\Object\RoleWord::Group, new \SqlSemantics\Platform\PostgreSql\Statement\Name\RoleSpec(\SqlSemantics\Platform\PostgreSql\Statement\Name\RoleSpecKind::Named, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('r')));
34 }
35
36 public function testRejectsAnOptionOnlyCreateRoleHas(): void
37 {
38 $this->expectExceptionMessage('ALTER ROLE accepts no option that only CREATE ROLE has.');
39 new \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Role\AlterRole(\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')), [new \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Role\RoleMembers(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Role\RoleMembersKind::Admin, [new \SqlSemantics\Platform\PostgreSql\Statement\Name\RoleSpec(\SqlSemantics\Platform\PostgreSql\Statement\Name\RoleSpecKind::Named, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'))])]);
40 }
41}
42