packages/sql-semantics-mysql/tests/Unit/Rules/TableDefinition/RelationKindsTest.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\Statement\Declaration\RelationKind;
12use SqlSemantics\Platform\MySql\Model\Statement\Declaration\Table;
13use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
15use SqlSemantics\Platform\MySql\Model\Statement\Reference\Table\DeclaredTable;
16use SqlSemantics\Platform\MySql\Model\Statement\Reference\Table\MissingTable;
17use SqlSemantics\Platform\MySql\Rules\TableDefinition\RelationKinds;
18use SqlSemantics\Platform\MySql\Semantics;
19use SqlSemantics\Platform\MySql\Statement\Table\Problem\KindRefusal;
20use SqlSemantics\Platform\MySql\Statement\Table\Problem\WrongRelationKind;
21
22#[CoversClass(RelationKinds::class)]
23#[Medium]
24final class RelationKindsTest extends TestCase
25{
26    public function testKindAnswersTheKindOfADeclarationMySqlHas(): void
27    {
28        $semantics = new Semantics();
29        $profile = $semantics->context()->profile;
30        $name = new QualifiedName(new Name('t'));
31
32        self::assertSame(RelationKind::View, (new RelationKinds())->kind(new DeclaredTable(new Table($name, $profile, [], [], true, RelationKind::View))));
33        self::assertSame(RelationKind::BaseTable, (new RelationKinds())->kind(new DeclaredTable(new Table($name, $profile, []))));
34        self::assertNull((new RelationKinds())->kind(new DeclaredTable(new Table($name, $profile, [], [], true, RelationKind::Sequence))));
35        self::assertNull((new RelationKinds())->kind(new MissingTable($name)));
36        self::assertNull((new RelationKinds())->kind(null));
37    }
38
39    public function testRequireReportsADeclarationOfTheOtherKind(): void
40    {
41        $semantics = new Semantics();
42        $derivation = new Derivation($semantics->context());
43        $name = new QualifiedName(new Name('t'));
44        $table = new DeclaredTable(new Table($name, $semantics->context()->profile, []));
45        (new RelationKinds())->require($derivation, $name, $table, RelationKind::BaseTable);
46        (new RelationKinds())->require($derivation, $name, $table, RelationKind::View);
47
48        self::assertEquals([new WrongRelationKind($name, KindRefusal::NotView)], $derivation->facts()->diagnostics);
49    }
50
51    public function testRefuseViewReportsADeclaredView(): void
52    {
53        $semantics = new Semantics();
54        $derivation = new Derivation($semantics->context());
55        $name = new QualifiedName(new Name('v'));
56        (new RelationKinds())->refuseView($derivation, $name, new DeclaredTable(new Table($name, $semantics->context()->profile, [])), KindRefusal::NoSuchTable);
57        (new RelationKinds())->refuseView($derivation, $name, new DeclaredTable(new Table($name, $semantics->context()->profile, [], [], true, RelationKind::View)), KindRefusal::NoSuchTable);
58
59        self::assertEquals([new WrongRelationKind($name, KindRefusal::NoSuchTable)], $derivation->facts()->diagnostics);
60    }
61
62    public function testRefusalsFollowLiveServers(): void
63    {
64        $semantics = new Semantics();
65        $table = $semantics->inspect('CREATE TABLE t (a INT)');
66        $view = $semantics->inspect('CREATE VIEW v AS SELECT a FROM t', [$table]);
67        $context = [$table, $view];
68
69        self::assertSame(['v is not BASE TABLE.'], array_map(static fn ($d): string => $d->message(), $semantics->inspect('CREATE TRIGGER tr BEFORE INSERT ON v FOR EACH ROW SET @x = 1', $context)->facts->diagnostics));
70        self::assertSame(['t is not VIEW.'], array_map(static fn ($d): string => $d->message(), $semantics->inspect('CREATE OR REPLACE VIEW t AS SELECT 1', $context)->facts->diagnostics));
71        self::assertSame([], $semantics->inspect('CREATE OR REPLACE VIEW v AS SELECT 1', $context)->facts->diagnostics);
72    }
73}
74