packages/sql-semantics-mysql/tests/Unit/Statement/Account/Privilege/GrantPrivilegesTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Account\Privilege;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Statement\Reference\Table\DeclaredTable;
11use SqlSemantics\Platform\MySql\Semantics;
12use SqlSemantics\Platform\MySql\Statement\Account\Privilege\GrantPrivileges;
13use SqlSemantics\Platform\MySql\Statement\Account\Problem\UnknownGrantColumn;
14
15#[CoversClass(GrantPrivileges::class)]
16#[Medium]
17final class GrantPrivilegesTest extends TestCase
18{
19 public function testDeriveStatementResolvesTheTableAndItsColumns(): void
20 {
21 $semantics = new Semantics();
22 $table = $semantics->inspect('CREATE TABLE t (a INT)');
23 $grant = $semantics->inspect('GRANT SELECT (a, b) ON t TO u', [$table]);
24
25 self::assertInstanceOf(GrantPrivileges::class, $grant->statement);
26 self::assertInstanceOf(DeclaredTable::class, $grant->facts->relation($grant->statement->level)->table);
27 self::assertInstanceOf(UnknownGrantColumn::class, $grant->facts->diagnostics[0]);
28 }
29
30 public function testRenderWritesEveryClause(): void
31 {
32 self::assertSame("GRANT SELECT ON FUNCTION db.f TO u IDENTIFIED BY 'x' REQUIRE NONE WITH MAX_USER_CONNECTIONS 2", (new Semantics('mysql-5.7.44'))->inspect("grant select on function db.f to u identified by 'x' require none with max_user_connections 2")->toString());
33 }
34}
35