packages/sql-semantics-mysql/tests/Unit/Rules/Query/From/FromScopeTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Query\From;
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\Type\Nullability;
16use SqlSemantics\Platform\MySql\Rules\Query\From\FromScope;
17use SqlSemantics\Platform\MySql\Semantics;
18use SqlSemantics\Platform\MySql\Statement\Expression\Comparison;
19use SqlSemantics\Platform\MySql\Statement\Query\Select;
20use SqlSemantics\Platform\MySql\Statement\Relation\JoinedTable;
21use SqlSemantics\Platform\MySql\Statement\Type\Integral;
22use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
23
24#[CoversClass(FromScope::class)]
25#[Medium]
26final class FromScopeTest extends TestCase
27{
28    public function testOpenMakesATableVisibleUnderItsCorrelationName(): 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 x.a FROM t AS x', [$t]);
40        $wrong = $semantics->inspect('SELECT t.a FROM t AS x', [$t]);
41
42        self::assertSame($t->columns[0], $operation->field('a')->column());
43        self::assertInstanceOf(MissingColumn::class, $wrong->facts->diagnostics[0]);
44    }
45
46    public function testEnterReportsARowAsTheJoinCondition(): void
47    {
48        $operation = (new Semantics())->inspect('SELECT 1 FROM t JOIN u ON (1, 2)');
49
50        self::assertSame(['Operand should contain 1 column(s), not 2.'], array_map(static fn ($diagnostic): string => $diagnostic->message(), $operation->facts->diagnostics));
51    }
52
53    public function testEnterDerivesTheConditionWithTheOperandsBeforeExtension(): void
54    {
55        $semantics = new Semantics();
56        $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [
57            new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
58            new Column(new Name('b'), new Integral(IntegralKind::BigInt), Nullability::Nullable),
59        ]);
60        $u = new Table(new QualifiedName(new Name('u'), new Name('(current)')), $semantics->profile(), [
61            new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull),
62            new Column(new Name('c'), new Integral(IntegralKind::Int), Nullability::NotNull),
63        ]);
64        $operation = $semantics->inspect('SELECT 1 FROM t LEFT JOIN u ON u.c = 1', [$t, $u]);
65        $select = $operation->statement;
66
67        self::assertInstanceOf(Select::class, $select);
68        self::assertInstanceOf(JoinedTable::class, $select->from);
69        self::assertNotNull($select->from->on);
70        self::assertInstanceOf(Comparison::class, $select->from->on);
71        self::assertSame(Nullability::NotNull, $operation->facts->scalar($select->from->on->left)->nullability);
72    }
73}
74