packages/sql-semantics-mysql/tests/Unit/Rules/TableDefinition/TableTargetsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\TableDefinition;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
11use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
12use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
14use SqlSemantics\Platform\MySql\Model\Statement\Reference\Table\DeclaredTable;
15use SqlSemantics\Platform\MySql\Model\Statement\Reference\Table\MissingTable;
16use SqlSemantics\Platform\MySql\Platform;
17use SqlSemantics\Platform\MySql\Rules\TableDefinition\TableTargets;
18use SqlSemantics\Platform\MySql\Semantics;
19use SqlSemantics\Platform\MySql\Statement\Table\CreateTable;
20
21#[CoversClass(TableTargets::class)]
22#[Medium]
23final class TableTargetsTest extends TestCase
24{
25    public function testShapeHasOneSlotPerDeclaredColumn(): void
26    {
27        $create = (new Semantics())->analyze('CREATE TABLE t (a INT, b INT INVISIBLE)');
28
29        self::assertCount(1, (new TableTargets())->shape($create->declarations()[0])->slots);
30    }
31
32    public function testImplicitFindsTheInvisibleColumns(): void
33    {
34        $create = (new Semantics())->analyze('CREATE TABLE t (a INT, b INT INVISIBLE)');
35
36        self::assertSame('b', (new TableTargets())->implicit($create->declarations()[0])[0]->names[0]->value);
37    }
38
39    public function testExistingResolvesATableName(): void
40    {
41        $platform = new Platform();
42        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
43
44        self::assertInstanceOf(MissingTable::class, (new TableTargets())->existing($derivation, new QualifiedName(new Name('t')))->table);
45    }
46
47    public function testParentResolvesTheTableBeingDefined(): void
48    {
49        $create = (new Semantics())->analyze('CREATE TABLE t (a INT)');
50        $platform = new Platform();
51        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
52        $fact = (new TableTargets())->parent($derivation, new QualifiedName(new Name('t')), $create->declarations()[0], new QualifiedName(new Name('t')));
53
54        self::assertInstanceOf(DeclaredTable::class, $fact->table);
55    }
56
57    public function testScopeSeesTheTableOnly(): void
58    {
59        $create = (new Semantics())->analyze('CREATE TABLE t (a INT)');
60        $statement = $create->statement;
61        self::assertInstanceOf(CreateTable::class, $statement);
62        $platform = new Platform();
63        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
64        $scope = (new TableTargets())->scope($derivation, $statement, new QualifiedName(new Name('t')), (new TableTargets())->shape($create->declarations()[0]));
65
66        self::assertCount(1, $scope->relations);
67    }
68}
69