packages/sql-semantics-postgres/tests/Unit/Statement/Catalog/Access/Role/CreateRoleTest.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\CreateRole::class)]
12#[Medium]
13final class CreateRoleTest extends TestCase
14{
15    public function testRenderDropsTheNoiseWordWith(): void
16    {
17        self::assertSame('CREATE USER joe login PASSWORD \'s\'', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE USER joe WITH LOGIN ENCRYPTED PASSWORD \'s\'')->toString());
18    }
19
20    public function testRenderKeepsTheGroupWord(): void
21    {
22        self::assertSame('CREATE GROUP "Staff"', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE GROUP "Staff"')->toString());
23    }
24
25    public function testAnalysisKeepsTheOptionsInOrder(): void
26    {
27        $statement = (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE ROLE joe NOLOGIN CONNECTION LIMIT 3')->statement;
28        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Access\Role\CreateRole::class, $statement);
29        self::assertSame(\SqlSemantics\Platform\PostgreSql\Statement\Object\RoleWord::Role, $statement->word);
30        self::assertSame(['canlogin', 'connectionlimit'], [$statement->options[0]->option(), $statement->options[1]->option()]);
31    }
32
33    public function testDeriveStatementReportsAReservedName(): void
34    {
35        self::assertSame(['role name "pg_mine" 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_mine')->facts->diagnostics));
36    }
37
38    public function testDeriveStatementReportsTheOptionProblems(): void
39    {
40        self::assertSame(['unrecognized role option "foo"', 'UNENCRYPTED PASSWORD is no longer supported', 'invalid connection limit: -2', 'role "public" does not exist', '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('CREATE ROLE joe foo UNENCRYPTED PASSWORD \'x\' CONNECTION LIMIT -2 LOGIN NOLOGIN ROLE public')->facts->diagnostics));
41    }
42
43    public function testDeriveStatementAcceptsRepeatedSysid(): void
44    {
45        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 SYSID 1 SYSID 2 SUPERUSER')->facts->diagnostics));
46    }
47}
48