packages/sql-semantics-postgres/tests/Unit/Rules/Access/GrantedRelationsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Access;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Access\GrantedRelations::class)]
12#[Medium]
13final class GrantedRelationsTest extends TestCase
14{
15 public function testDeriveReportsNothingForADeclaredTable(): void
16 {
17 self::assertSame([], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('GRANT SELECT (id, price, ctid) ON items TO joe', [(new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE TABLE items (id int4, price int4)')])->facts->diagnostics));
18 }
19
20 public function testDeriveKeepsAnUndeclaredNameOpen(): void
21 {
22 self::assertSame([], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('REVOKE SELECT (x) ON other FROM joe', [(new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE TABLE items (id int4, price int4)')])->facts->diagnostics));
23 }
24
25 public function testColumnsReportsEveryMissingColumn(): void
26 {
27 self::assertSame(['Column items.x does not exist.', 'Column items.y does not exist.'], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('REVOKE SELECT (x), update (id, y) ON items FROM joe', [(new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE TABLE items (id int4, price int4)')])->facts->diagnostics));
28 }
29}
30