packages/sql-semantics-postgres/tests/Unit/Statement/Object/CommentTest.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\Contract\GrammarRelease;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Column\MissingColumn;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Table\DeclaredTable;
18use SqlSemantics\Platform\PostgreSql\Semantics;
19use SqlSemantics\Platform\PostgreSql\Statement\Name\ObjectKind;
20use SqlSemantics\Platform\PostgreSql\Statement\Object\Comment;
21use SqlSemantics\Platform\PostgreSql\Statement\Object\Problem\ObjectProblem;
22use SqlSemantics\Platform\PostgreSql\Statement\Object\Problem\ObjectProblemKind;
23use SqlSemantics\Platform\PostgreSql\Statement\Object\Reference\UnqualifiedName;
24use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
25
26#[CoversClass(Comment::class)]
27#[Medium]
28final class CommentTest extends TestCase
29{
30    public function testDeriveStatementChecksTheColumn(): void
31    {
32        $table = new Table(new QualifiedName(new Name('t')), new LanguageProfile(GrammarRelease::PostgreSql172), [new Column(new Name('a'), Builtin::Int4)]);
33        $semantics = new Semantics();
34        $missing = $semantics->inspect("COMMENT ON COLUMN public.t.b IS 'x'", [$table]);
35        $unqualified = $semantics->inspect("COMMENT ON COLUMN b IS 'x'", [$table]);
36        $long = $semantics->inspect("COMMENT ON COLUMN a.b.c.d.e IS 'x'", [$table]);
37        self::assertEquals([new MissingColumn(new Name('b'), new QualifiedName(new Name('t'), new Name('public')))], $missing->facts->diagnostics);
38        self::assertEquals([new ObjectProblem(ObjectProblemKind::UnqualifiedColumn)], $unqualified->facts->diagnostics);
39        self::assertEquals([new ObjectProblem(ObjectProblemKind::ImproperRelation, 'a.b.c.d.e')], $long->facts->diagnostics);
40    }
41
42    public function testDeriveStatementAcceptsADeclaredColumn(): void
43    {
44        $table = new Table(new QualifiedName(new Name('t')), new LanguageProfile(GrammarRelease::PostgreSql172), [new Column(new Name('a'), Builtin::Int4)]);
45        $operation = (new Semantics())->inspect('COMMENT ON COLUMN t.a IS NULL', [$table]);
46        self::assertSame([], $operation->facts->diagnostics);
47        $statement = $operation->statement;
48        self::assertInstanceOf(Comment::class, $statement);
49        $resolution = $operation->facts->relation($statement->object)->table;
50        self::assertInstanceOf(DeclaredTable::class, $resolution);
51        self::assertSame($table, $resolution->table);
52    }
53
54    public function testRenderWritesEachForm(): void
55    {
56        $semantics = new Semantics();
57        self::assertSame(
58            ["COMMENT ON CONSTRAINT c ON DOMAIN d IS 'x'", "COMMENT ON LARGE OBJECT 5 IS 'x'", "COMMENT ON TRANSFORM FOR int4 LANGUAGE l IS 'x'", 'COMMENT ON OPERATOR - (NONE, int4) IS NULL', "COMMENT ON ROLE r IS ''"],
59            [$semantics->inspect("COMMENT ON CONSTRAINT c ON DOMAIN d IS 'x'")->toString(), $semantics->inspect("COMMENT ON LARGE OBJECT 5 IS 'x'")->toString(), $semantics->inspect("COMMENT ON TRANSFORM FOR int4 LANGUAGE l IS 'x'")->toString(), $semantics->inspect('COMMENT ON OPERATOR - (NONE, int4) IS NULL')->toString(), $semantics->inspect("COMMENT ON ROLE r IS ''")->toString()],
60        );
61    }
62
63    public function testRejectsAWrongForm(): void
64    {
65        $this->expectExceptionMessage('COMMENT names the object as the grammar names objects of its kind.');
66        new Comment(ObjectKind::Table, new UnqualifiedName(new Name('t')), null);
67    }
68
69    public function testDeriveStatementReportsAnotherKind(): void
70    {
71        $semantics = new Semantics();
72        $context = [$semantics->inspect('CREATE VIEW v AS SELECT 1 AS a')];
73        self::assertSame(['"v" is not a table'], array_map(static fn ($problem): string => $problem->message(), $semantics->inspect('COMMENT ON TABLE v IS \'x\'', $context)->facts->diagnostics));
74        self::assertSame([], array_map(static fn ($problem): string => $problem->message(), $semantics->inspect('COMMENT ON VIEW v IS \'x\'', $context)->facts->diagnostics));
75    }
76}
77