packages/sql-semantics-postgres/tests/Unit/Statement/Table/Policy/AlterPolicyTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Table\Policy;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Statement\Table\Policy\AlterPolicy::class)]
12#[Medium]
13final class AlterPolicyTest extends TestCase
14{
15 public function testDeriveStatementDerivesTheConditionsAgainstTheTable(): void
16 {
17 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
18 $context = [];
19 array_push($context, ...$semantics->inspect('CREATE TABLE t (a int NOT NULL, b int, c text)')->declarations());
20 $statement = $semantics->inspect('ALTER POLICY p ON t USING (zz > 0) WITH CHECK (a)', $context);
21 self::assertSame([
22 0 => 'Column zz does not exist.',
23 1 => 'argument of POLICY must be type boolean',
24 ], array_map(static fn ($problem): string => $problem->message(), $statement->facts->diagnostics));
25 }
26
27 public function testDeriveRelationResolvesTheTable(): void
28 {
29 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
30 $context = [];
31 array_push($context, ...$semantics->inspect('CREATE TABLE t (a int NOT NULL, b int, c text)')->declarations());
32 $statement = $semantics->inspect('ALTER POLICY p ON t TO bob', $context);
33 $n1 = $statement->statement;
34 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\Policy\AlterPolicy::class, $n1);
35 self::assertSame(3, count($n1->deriveRelation(new \SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation($statement->context), new \SqlSemantics\Platform\PostgreSql\Model\Resolution\Environment($statement->context))->shape->slots));
36 }
37
38 public function testRenderWritesTheClauseAsWritten(): void
39 {
40 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
41 $statement = $semantics->inspect('ALTER POLICY p ON s.t TO CURRENT_USER, bob USING (a > 0) WITH CHECK (b > 0)', []);
42 self::assertSame('ALTER POLICY p ON s.t TO CURRENT_USER, bob USING (a > 0) WITH CHECK (b > 0)', $statement->toString());
43 }
44}
45