packages/sql-semantics-postgres/tests/Unit/Statement/Catalog/Schema/CreateSchemaTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Catalog\Schema;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Statement\Catalog\Schema\CreateSchema::class)]
12#[Medium]
13final class CreateSchemaTest extends TestCase
14{
15    public function testRenderWritesTheAuthorization(): void
16    {
17        self::assertSame('CREATE SCHEMA IF NOT EXISTS AUTHORIZATION joe', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE SCHEMA IF NOT EXISTS AUTHORIZATION joe')->toString());
18    }
19
20    public function testRenderWritesTheElements(): void
21    {
22        self::assertSame('CREATE SCHEMA s CREATE TABLE t (a int4) CREATE VIEW v AS SELECT 1', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE SCHEMA s CREATE TABLE t (a int4) CREATE VIEW v AS SELECT 1')->toString());
23    }
24
25    public function testDeriveStatementReportsElementsWithIfNotExists(): void
26    {
27        self::assertSame('CREATE SCHEMA IF NOT EXISTS cannot include schema elements', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE SCHEMA IF NOT EXISTS s CREATE TABLE t (a int4)')->facts->diagnostics[0]->message());
28    }
29
30    public function testDeriveStatementReportsAReservedName(): void
31    {
32        self::assertSame('unacceptable schema name "pg_app"', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE SCHEMA pg_app')->facts->diagnostics[0]->message());
33    }
34
35    public function testDeriveStatementReportsAnElementOfAnotherSchema(): void
36    {
37        self::assertSame('CREATE specifies a schema (x) different from the one being created (s)', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE SCHEMA s CREATE TABLE x.t (a int4)')->facts->diagnostics[0]->message());
38    }
39
40    public function testDeriveStatementDeclaresInTheNewSchema(): void
41    {
42        self::assertSame('s', (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE SCHEMA s CREATE TABLE t (a int4)')->declarations()[0]->name->schema?->value);
43    }
44
45    public function testDeriveStatementReadsTheElementsWithTheNewSchemaSearchedFirst(): void
46    {
47        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
48        $context = [$semantics->inspect('CREATE TABLE s.t (x int4)', [])->declarations()[0], $semantics->inspect('CREATE TABLE public.t (y int4)', [])->declarations()[0]];
49        self::assertSame(['x', 'y'], [
50            $semantics->inspect('CREATE SCHEMA s CREATE VIEW v AS SELECT * FROM t', $context)->declarations()[0]->columns[0]->name->value,
51            $semantics->inspect('CREATE VIEW v AS SELECT * FROM t', $context)->declarations()[0]->columns[0]->name->value,
52        ]);
53    }
54
55    public function testDeriveStatementLetsAnElementSeeTheRelationsOfEarlierSteps(): void
56    {
57        $operation = (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE SCHEMA s CREATE VIEW v AS SELECT a FROM x CREATE TABLE x (a int4)', []);
58        self::assertSame([], $operation->facts->diagnostics);
59        self::assertSame(['x', 'v'], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table $table): string => $table->name->name->value, $operation->declarations()));
60        self::assertTrue($operation->declarations()[1]->complete);
61    }
62
63    public function testDeriveStatementRunsViewsInTheOrderWritten(): void
64    {
65        $operation = (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE SCHEMA s CREATE VIEW v AS SELECT a FROM w CREATE VIEW w AS SELECT 1 AS a', []);
66        self::assertSame('Relation w does not exist.', $operation->facts->diagnostics[0]->message());
67    }
68
69    public function testDeriveStatementDeclaresNothingForASessionOwner(): void
70    {
71        self::assertSame([], (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE SCHEMA AUTHORIZATION CURRENT_USER CREATE TABLE t (a int4)')->declarations());
72    }
73
74    public function testRejectsASchemaWithoutNameOrOwner(): void
75    {
76        $this->expectExceptionMessage('A schema is named or owned.');
77        new \SqlSemantics\Platform\PostgreSql\Statement\Catalog\Schema\CreateSchema(null);
78    }
79}
80