packages/sql-semantics-postgres/tests/Unit/Statement/Catalog/Publication/CreatePublicationTest.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\CreatePublication::class)]
12#[Medium]
13final class CreatePublicationTest extends TestCase
14{
15    public function testRenderWritesAllTables(): void
16    {
17        self::assertSame('CREATE PUBLICATION p FOR ALL TABLES WITH (publish = \'insert\')', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE PUBLICATION p FOR ALL TABLES WITH (publish = \'insert\')')->toString());
18    }
19
20    public function testRenderWithoutObjects(): void
21    {
22        self::assertSame('CREATE PUBLICATION p', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE PUBLICATION p')->toString());
23    }
24
25    public function testDeriveStatementReportsAListWithoutKeywords(): void
26    {
27        self::assertSame('invalid publication object list', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE PUBLICATION p FOR t')->facts->diagnostics[0]->message());
28    }
29
30    public function testDeriveStatementReportsCurrentSchemaAfterATable(): void
31    {
32        self::assertSame('invalid table name', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE PUBLICATION p FOR TABLE t, CURRENT_SCHEMA')->facts->diagnostics[0]->message());
33    }
34
35    public function testRejectsObjectsWithAllTables(): void
36    {
37        $this->expectExceptionMessage('FOR ALL TABLES has no object list.');
38        new \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Publication\CreatePublication(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('p'), true, [new \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Publication\PublicationSchema(null)]);
39    }
40
41    public function testRejectsASchemaNameThatWouldReadAsATable(): void
42    {
43        $this->expectExceptionMessage('A schema name without TABLES IN SCHEMA continues a schema item.');
44        new \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Publication\CreatePublication(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('p'), false, [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')))), new \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Publication\PublicationSchema(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('s'), false)]);
45    }
46}
47