packages/sql-semantics-postgres/tests/Unit/Statement/Name/RoleSpecTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Name;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
11use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
12use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
15use SqlSemantics\Platform\PostgreSql\Statement\Name\RoleSpec;
16use SqlSemantics\Platform\PostgreSql\Statement\Name\RoleSpecKind;
17
18#[CoversClass(RoleSpec::class)]
19#[Small]
20final class RoleSpecTest extends TestCase
21{
22 public function testRenderWritesARoleName(): void
23 {
24 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
25 (new RoleSpec(RoleSpecKind::Named, new Name('Admin')))->render($out);
26 self::assertSame('"Admin"', (new Lexical())->join($out->pieces()));
27 }
28
29 public function testRenderWritesTheDesignations(): void
30 {
31 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
32 (new RoleSpec(RoleSpecKind::Everyone))->render($out);
33 self::assertSame('public', (new Lexical())->join($out->pieces()));
34 $second = new Output(new Codec(GrammarRelease::PostgreSql172));
35 (new RoleSpec(RoleSpecKind::SessionUser))->render($second);
36 self::assertSame('SESSION_USER', (new Lexical())->join($second->pieces()));
37 }
38
39 public function testRejectsANameOnADesignation(): void
40 {
41 $this->expectExceptionMessage('A role specification has a name exactly when it names a role.');
42 new RoleSpec(RoleSpecKind::CurrentUser, new Name('x'));
43 }
44
45 public function testRejectsTheReservedWordNone(): void
46 {
47 $this->expectExceptionMessage('The words public and none cannot name a role.');
48 new RoleSpec(RoleSpecKind::Named, new Name('none'));
49 }
50}
51