packages/sql-semantics-postgres/tests/Unit/Statement/Object/ExtensionDependencyTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Object;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\PostgreSql\Semantics;
12use SqlSemantics\Platform\PostgreSql\Statement\Name\ObjectKind;
13use SqlSemantics\Platform\PostgreSql\Statement\Object\ExtensionDependency;
14use SqlSemantics\Platform\PostgreSql\Statement\Object\Reference\UnqualifiedName;
15
16#[CoversClass(ExtensionDependency::class)]
17#[Medium]
18final class ExtensionDependencyTest extends TestCase
19{
20 public function testDeriveStatementRecordsNothingForATrigger(): void
21 {
22 $operation = (new Semantics())->inspect('ALTER TRIGGER g ON t DEPENDS ON EXTENSION e');
23 self::assertSame([], $operation->facts->diagnostics);
24 }
25
26 public function testRenderWritesNo(): void
27 {
28 $operation = (new Semantics())->inspect('ALTER FUNCTION f NO DEPENDS ON EXTENSION e');
29 self::assertSame('ALTER FUNCTION f NO DEPENDS ON EXTENSION e', $operation->toString());
30 }
31
32 public function testRejectsAKindWithoutDependencies(): void
33 {
34 $this->expectExceptionMessage('DEPENDS ON EXTENSION names the object as the grammar names objects of its kind.');
35 new ExtensionDependency(ObjectKind::Schema, new UnqualifiedName(new Name('s')), new Name('e'));
36 }
37
38 public function testDeriveStatementReportsAnotherKind(): void
39 {
40 $semantics = new Semantics();
41 $context = [$semantics->inspect('CREATE VIEW v AS SELECT 1 AS a')];
42 self::assertSame(['"v" is not an index'], array_map(static fn ($problem): string => $problem->message(), $semantics->inspect('ALTER INDEX v DEPENDS ON EXTENSION e', $context)->facts->diagnostics));
43 }
44}
45