packages/sql-semantics-postgres/tests/Unit/Statement/Catalog/Publication/PublicationTableTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Catalog\Publication;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Publication\PublicationTable::class)]
12#[Medium]
13final class PublicationTableTest extends TestCase
14{
15    public function testIntroducedWhenTableIsWritten(): void
16    {
17        $statement = (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE PUBLICATION p FOR TABLE a')->statement;
18        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Publication\CreatePublication::class, $statement);
19        self::assertSame(true, $statement->objects[0]->introduced());
20    }
21
22    public function testBareOfAContinuedName(): void
23    {
24        $statement = (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE PUBLICATION p FOR TABLE a, b')->statement;
25        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Publication\CreatePublication::class, $statement);
26        $table = $statement->objects[1];
27        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Publication\PublicationTable::class, $table);
28        self::assertTrue($table->bare());
29    }
30
31    public function testBareIsFalseForAStar(): void
32    {
33        self::assertFalse((new \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Publication\PublicationTable(new \SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t'))), [], null, false, true))->bare());
34    }
35
36    public function testRenderKeepsTheStarAfterASchema(): void
37    {
38        self::assertSame('CREATE PUBLICATION p FOR TABLES IN SCHEMA s, t *', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE PUBLICATION p FOR TABLES IN SCHEMA s, t *')->toString());
39    }
40
41    public function testRenderWritesColumnsAndFilter(): void
42    {
43        self::assertSame('CREATE PUBLICATION p FOR TABLE ONLY t (a, b) WHERE (a > 1)', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE PUBLICATION p FOR TABLE ONLY t (a, b) WHERE (a > 1)')->toString());
44    }
45
46    public function testDeriveRelationResolvesTheTable(): void
47    {
48        $t = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')), new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172), [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4)], [], true);
49        $operation = (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE PUBLICATION p FOR TABLE t (a) WHERE (a > 0)', [$t]);
50        $statement = $operation->statement;
51        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Publication\CreatePublication::class, $statement);
52        $resolution = $operation->facts->relation($statement->objects[0])->table;
53        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Table\DeclaredTable::class, $resolution);
54        self::assertSame($t, $resolution->table);
55        self::assertSame([], $operation->facts->diagnostics);
56    }
57
58    public function testRejectsOnlyWithAStar(): void
59    {
60        $this->expectExceptionMessage('A table name is written with ONLY or with a trailing star, not both.');
61        new \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Publication\PublicationTable(new \SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')), true), [], null, true, true);
62    }
63}
64