packages/sql-semantics-mysql/tests/Unit/Statement/Query/ExplicitTableTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Query;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Statement\Declaration\Column;
11use SqlSemantics\Platform\MySql\Model\Statement\Declaration\Table;
12use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
14use SqlSemantics\Platform\MySql\Model\Statement\Reference\Table\UndeclaredTable;
15use SqlSemantics\Platform\MySql\Model\Statement\Shape\Field;
16use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\MySql\Semantics;
18use SqlSemantics\Platform\MySql\Statement\Query\ExplicitTable;
19use SqlSemantics\Platform\MySql\Statement\Type\Integral;
20use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
21
22#[CoversClass(ExplicitTable::class)]
23#[Medium]
24final class ExplicitTableTest extends TestCase
25{
26 public function testNameAnswersTheTable(): void
27 {
28 $operation = (new Semantics())->analyze('TABLE shop.t');
29
30 self::assertInstanceOf(ExplicitTable::class, $operation->statement);
31 self::assertSame('t', $operation->statement->name()->name->value);
32 self::assertSame('shop', $operation->statement->name()->schema?->value);
33 }
34
35 public function testAliasAnswersNoCorrelationName(): void
36 {
37 $operation = (new Semantics())->analyze('TABLE t');
38
39 self::assertInstanceOf(ExplicitTable::class, $operation->statement);
40 self::assertNull($operation->statement->alias());
41 }
42
43 public function testDeriveStatementRecordsTheColumnsAsTheOutput(): void
44 {
45 $semantics = new Semantics();
46 $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [
47 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
48 new Column(new Name('b'), new Integral(IntegralKind::BigInt), Nullability::Nullable),
49 ]);
50 $u = new Table(new QualifiedName(new Name('u'), new Name('(current)')), $semantics->profile(), [
51 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
52 new Column(new Name('c'), new Integral(IntegralKind::Int), Nullability::NotNull),
53 ]);
54 $operation = $semantics->analyze('TABLE t', [$t]);
55
56 self::assertInstanceOf(ExplicitTable::class, $operation->statement);
57 self::assertSame($operation->facts->query($operation->statement), $operation->facts->output);
58 self::assertSame($t->columns[1], $operation->field('b')->column());
59 }
60
61 public function testDeriveQueryAnswersEveryColumnOfTheTable(): void
62 {
63 $semantics = new Semantics();
64 $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [
65 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
66 new Column(new Name('b'), new Integral(IntegralKind::BigInt), Nullability::Nullable),
67 ]);
68 $u = new Table(new QualifiedName(new Name('u'), new Name('(current)')), $semantics->profile(), [
69 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
70 new Column(new Name('c'), new Integral(IntegralKind::Int), Nullability::NotNull),
71 ]);
72 $operation = $semantics->analyze('TABLE t', [$t]);
73
74 self::assertSame(['a', 'b'], array_map(static fn (Field $field): ?string => $field->name?->value, $operation->fields()->items ?? []));
75 self::assertSame(Nullability::Nullable, $operation->field('b')->nullability);
76 }
77
78 public function testDeriveRelationDependsOnAnUndeclaredTable(): void
79 {
80 $operation = (new Semantics())->analyze('TABLE t');
81
82 self::assertInstanceOf(ExplicitTable::class, $operation->statement);
83 self::assertInstanceOf(UndeclaredTable::class, $operation->facts->relation($operation->statement)->table);
84 self::assertNull($operation->fields());
85 }
86
87 public function testRenderWritesTheStatement(): void
88 {
89 self::assertSame('TABLE shop.t', (new Semantics())->analyze('table shop.t')->toString());
90 }
91}
92