packages/sql-semantics-postgres/tests/Unit/Rules/Catalog/SchemaMemberTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Catalog;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
13use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
15use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
16use SqlSemantics\Platform\PostgreSql\Rules\Catalog\SchemaMember;
17use SqlSemantics\Platform\PostgreSql\Semantics;
18
19#[CoversClass(SchemaMember::class)]
20#[Medium]
21final class SchemaMemberTest extends TestCase
22{
23 public function testContextSearchesTheSchemaBeforeTheWrittenPath(): void
24 {
25 $semantics = new Semantics();
26 $create = $semantics->analyze('CREATE TABLE t (a int4)', []);
27 $context = (new SchemaMember($create->statement, new Name('s')))->context($semantics->context([]));
28 self::assertSame(['pg_temp', 'pg_catalog', 's', 'public'], array_map(static fn (Name $name): string => $name->value, $context->searchPath));
29 self::assertSame('public', $context->declarationSchema->value);
30 }
31
32 public function testContextAddsTheRelationsTheStatementProvided(): void
33 {
34 $semantics = new Semantics();
35 $create = $semantics->analyze('CREATE TABLE t (a int4)', []);
36 $context = (new SchemaMember($create->statement, new Name('s')))->context($semantics->context([]), $create->declarations());
37 self::assertSame($create->declarations(), $context->tables);
38 }
39
40 public function testStepRunsSequencesBeforeTablesAndGrantsLast(): void
41 {
42 $semantics = new Semantics();
43 $sequence = new SchemaMember($semantics->analyze('CREATE SEQUENCE q', [])->statement, new Name('s'));
44 $table = new SchemaMember($semantics->analyze('CREATE TABLE t (a int4)', [])->statement, new Name('s'));
45 $grant = new SchemaMember($semantics->analyze('GRANT SELECT ON t TO joe', [])->statement, new Name('s'));
46 self::assertSame([0, 1, 5], [$sequence->step(), $table->step(), $grant->step()]);
47 }
48
49 public function testDeriveStatementDeclaresACreatedObjectInTheSchema(): void
50 {
51 $semantics = new Semantics();
52 $derivation = new Derivation($semantics->context([]));
53 (new SchemaMember($semantics->analyze('CREATE TABLE t (a int4)', [])->statement, new Name('s')))->deriveStatement($derivation);
54 self::assertSame('s', $derivation->facts()->declarations[0]->name->schema?->value);
55 }
56
57 public function testDeriveStatementDerivesAnotherStatementAsItIs(): void
58 {
59 $semantics = new Semantics();
60 $derivation = new Derivation($semantics->context([]));
61 (new SchemaMember($semantics->analyze('SELECT 1', [])->statement, new Name('s')))->deriveStatement($derivation);
62 self::assertSame([], $derivation->facts()->declarations);
63 }
64
65 public function testRenderWritesTheElement(): void
66 {
67 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
68 (new SchemaMember((new Semantics())->analyze('CREATE TABLE t (a int4)', [])->statement, new Name('s')))->render($out);
69 self::assertSame('CREATE TABLE t (a int4)', (new Lexical())->join($out->pieces()));
70 }
71}
72