packages/sql-semantics-postgres/tests/Unit/Statement/Catalog/Extension/AlterExtensionContentsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Catalog\Extension;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Extension\AlterExtensionContents::class)]
12#[Medium]
13final class AlterExtensionContentsTest extends TestCase
14{
15    public function testRenderWritesTheKindAndObject(): void
16    {
17        self::assertSame('ALTER EXTENSION e ADD OPERATOR CLASS c USING btree', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('ALTER EXTENSION e ADD OPERATOR CLASS c USING btree')->toString());
18    }
19
20    public function testRenderWritesACast(): void
21    {
22        self::assertSame('ALTER EXTENSION e DROP CAST (int4 AS text)', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('ALTER EXTENSION e DROP CAST (int4 AS text)')->toString());
23    }
24
25    public function testDeriveStatementDerivesTheObject(): void
26    {
27        self::assertSame([], (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('ALTER EXTENSION e ADD TYPE numeric(4, 2)')->facts->diagnostics);
28    }
29
30    public function testRejectsAKindAlterExtensionCannotAddress(): void
31    {
32        $this->expectExceptionMessage('ALTER EXTENSION cannot address an object of this kind.');
33        new \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Extension\AlterExtensionContents(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('e'), \SqlSemantics\Platform\PostgreSql\Statement\Option\AddOrDrop::Add, \SqlSemantics\Platform\PostgreSql\Statement\Name\ObjectKind::Trigger, new \SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')]));
34    }
35
36    public function testDeriveStatementReportsAnotherKind(): void
37    {
38        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
39        $context = [$semantics->inspect('CREATE MATERIALIZED VIEW m AS SELECT 1 AS a')];
40        self::assertSame(['"m" is not a view'], array_map(static fn ($problem): string => $problem->message(), $semantics->inspect('ALTER EXTENSION e ADD VIEW m', $context)->facts->diagnostics));
41    }
42}
43