packages/sql-semantics-postgres/tests/Unit/Lowering/Leaf/RolesTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Leaf;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlParser\PostgreSql\PostgreSqlParser;
11use SqlSemantics\Platform\PostgreSql\Lowering\Leaf\Roles;
12use SqlSemantics\Platform\PostgreSql\Lowering\Lowering;
13use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
14use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
15use SqlSemantics\Platform\PostgreSql\Model\Diagnostic\AnalysisException;
16use SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves;
17use SqlSemantics\Platform\PostgreSql\Platform;
18use SqlSemantics\Platform\PostgreSql\Statement\Name\RoleSpecKind;
19
20#[CoversClass(Roles::class)]
21#[Small]
22final class RolesTest extends TestCase
23{
24 public function testRoleLowersANameAndTheDesignations(): void
25 {
26 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
27 $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP ROLE "Admin", PUBLIC, CURRENT_USER');
28 $roles = $tree->find('RoleSpec');
29 self::assertSame('Admin', $lowering->roles->role($roles[0])->name?->value);
30 self::assertSame(RoleSpecKind::Everyone, $lowering->roles->role($roles[1])->kind);
31 self::assertSame(RoleSpecKind::CurrentUser, $lowering->roles->role($roles[2])->kind);
32 }
33
34 public function testRoleRejectsTheReservedWordNone(): void
35 {
36 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
37 $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP ROLE none');
38 $this->expectException(AnalysisException::class);
39 $lowering->roles->role($tree->find('RoleSpec')[0]);
40 }
41
42 public function testNameLowersARoleName(): void
43 {
44 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
45 $tree = (new PostgreSqlParser('pg-17.2'))->parse('CREATE ROLE admin');
46 self::assertSame('admin', $lowering->roles->name($tree->find('RoleId')[0])->value);
47 }
48
49 public function testNameRejectsADesignation(): void
50 {
51 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
52 $tree = (new PostgreSqlParser('pg-17.2'))->parse('CREATE ROLE CURRENT_USER');
53 $this->expectException(AnalysisException::class);
54 $lowering->roles->name($tree->find('RoleId')[0]);
55 }
56
57 public function testRolesLowersARoleList(): void
58 {
59 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
60 $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP ROLE a, SESSION_USER');
61 self::assertCount(2, $lowering->roles->roles($tree->find('role_list')[0]));
62 }
63}
64