packages/sql-semantics-mysql/tests/Unit/Statement/Relation/TableListTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Relation;
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\Column\MissingColumn;
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\Select;
19use SqlSemantics\Platform\MySql\Statement\Relation\Dual;
20use SqlSemantics\Platform\MySql\Statement\Relation\TableList;
21use SqlSemantics\Platform\MySql\Statement\Type\Integral;
22use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
23
24#[CoversClass(TableList::class)]
25#[Medium]
26final class TableListTest extends TestCase
27{
28    public function testDeriveRelationJoinsTheMembersLeftToRight(): void
29    {
30        $semantics = new Semantics();
31        $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [
32            new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
33            new Column(new Name('b'), new Integral(IntegralKind::BigInt), Nullability::Nullable),
34        ]);
35        $u = new Table(new QualifiedName(new Name('u'), new Name('(current)')), $semantics->profile(), [
36            new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
37            new Column(new Name('c'), new Integral(IntegralKind::Int), Nullability::NotNull),
38        ]);
39        $operation = $semantics->inspect('SELECT * FROM t, u', [$t, $u]);
40
41        self::assertInstanceOf(Select::class, $operation->statement);
42        self::assertInstanceOf(TableList::class, $operation->statement->from);
43        self::assertCount(4, $operation->facts->relation($operation->statement->from)->shape->slots);
44        self::assertSame(['a', 'b', 'a', 'c'], array_map(static fn (Field $field): ?string => $field->name?->value, $operation->fields()->items ?? []));
45    }
46
47    public function testDeriveRelationKeepsTheJoinConditionToItsOperands(): void
48    {
49        $semantics = new Semantics();
50        $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [
51            new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
52            new Column(new Name('b'), new Integral(IntegralKind::BigInt), Nullability::Nullable),
53        ]);
54        $u = new Table(new QualifiedName(new Name('u'), new Name('(current)')), $semantics->profile(), [
55            new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
56            new Column(new Name('c'), new Integral(IntegralKind::Int), Nullability::NotNull),
57        ]);
58        $operation = $semantics->inspect('SELECT 1 FROM t, u JOIN u AS v ON t.a = v.a', [$t, $u]);
59
60        self::assertCount(1, $operation->facts->diagnostics);
61        self::assertInstanceOf(MissingColumn::class, $operation->facts->diagnostics[0]);
62    }
63
64    public function testRenderWritesTheMembers(): void
65    {
66        self::assertSame('SELECT 1 FROM t, u, v', (new Semantics())->inspect('select 1 from t,u,v')->toString());
67    }
68
69    public function testAListOfOneMemberIsRejected(): void
70    {
71        $this->expectExceptionMessage('A comma list joins at least two table references.');
72
73        new TableList([new Dual()]);
74    }
75
76    public function testANestedListIsRejected(): void
77    {
78        $this->expectExceptionMessage('A comma list used as a member is written in parentheses.');
79
80        new TableList([new Dual(), new TableList([new Dual(), new Dual()])]);
81    }
82}
83