packages/sql-semantics-postgres/tests/Unit/Rules/Table/Definition/QueryTablesTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Table\Definition;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\QueryTables::class)]
12#[Medium]
13final class QueryTablesTest extends TestCase
14{
15 public function testNameIsTheSchemaOfAnEnclosingCreateSchema(): void
16 {
17 self::assertSame('s', (new \SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\QueryTables())->name(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('v')), \SqlSemantics\Platform\PostgreSql\Statement\Table\Persistence::Permanent, new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('s'))->schema?->value);
18 }
19
20 public function testTableTurnsUnknownIntoText(): void
21 {
22 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
23 $context = [];
24 array_push($context, ...$semantics->inspect('CREATE TABLE t (a int NOT NULL, b int, c text)')->declarations());
25 $statement = $semantics->inspect('CREATE TABLE n (x, y, z) AS SELECT \'a\', a FROM t', $context);
26 self::assertSame([
27 0 =>
28 [
29 0 => 'x text Nullable',
30 1 => 'y integer Nullable',
31 ],
32 1 =>
33 [
34 0 => 'too many column names were specified',
35 ],
36 ], [array_map(static fn ($column): string => $column->name->value . ' ' . $column->type->name() . ' ' . $column->nullability->name, $statement->declarations()[0]->columns), array_map(static fn ($problem): string => $problem->message(), $statement->facts->diagnostics)]);
37 }
38
39 public function testTableDeclaresEveryNamedColumnOfADependentType(): void
40 {
41 $operation = (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE TABLE x AS SELECT f(1) AS a, NULL AS b, 2 AS c', []);
42 $table = $operation->declarations()[0];
43 self::assertTrue($table->complete);
44 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Undetermined::class, $table->columns[0]->type);
45 self::assertSame([\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Text, \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4], [$table->columns[1]->type, $table->columns[2]->type]);
46 }
47
48 public function testTableIsIncompleteAtAnOpenRow(): void
49 {
50 $operation = (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('CREATE VIEW w AS SELECT 1 AS k, * FROM zz');
51 self::assertFalse($operation->declarations()[0]->complete);
52 self::assertCount(1, $operation->declarations()[0]->columns);
53 }
54
55 public function testTableDeclaresTheKind(): void
56 {
57 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
58 $view = $semantics->inspect('CREATE VIEW v AS SELECT 1 AS a')->declarations()[0];
59 $table = $semantics->inspect('CREATE TABLE c AS SELECT 1 AS a')->declarations()[0];
60 self::assertSame(
61 [\SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\RelationKind::View, [], \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\RelationKind::BaseTable, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::Nullable],
62 [$view->kind, $view->implicit, $table->kind, $table->columns[0]->nullability],
63 );
64 }
65}
66