packages/sql-semantics-mysql/tests/Unit/Rules/TableDefinition/ElementFactsTest.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\Reference\Table\MissingTable;
13use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
14use SqlSemantics\Platform\MySql\Platform;
15use SqlSemantics\Platform\MySql\Rules\TableDefinition\ElementFacts;
16use SqlSemantics\Platform\MySql\Semantics;
17use SqlSemantics\Platform\MySql\Statement\Table\Column\ColumnDefinition;
18use SqlSemantics\Platform\MySql\Statement\Table\CreateTable;
19use SqlSemantics\Platform\MySql\Statement\Table\Key\ForeignKey;
20
21#[CoversClass(ElementFacts::class)]
22#[Medium]
23final class ElementFactsTest extends TestCase
24{
25 public function testElementResolvesTheReferences(): void
26 {
27 $create = (new Semantics())->analyze('CREATE TABLE t (a INT, FOREIGN KEY (a) REFERENCES p (x))');
28 $statement = $create->statement;
29 self::assertInstanceOf(CreateTable::class, $statement);
30 $platform = new Platform();
31 $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
32 $element1 = $statement->elements[1];
33 self::assertInstanceOf(ForeignKey::class, $element1);
34 (new ElementFacts())->element($statement->elements[1], $derivation, $derivation->environment());
35
36 self::assertInstanceOf(MissingTable::class, $derivation->facts()->relation($element1->references)->table);
37 }
38
39 public function testSpecificationDerivesTheExpressions(): void
40 {
41 $create = (new Semantics())->analyze('CREATE TABLE t (a INT DEFAULT (1 + 2))');
42 $statement = $create->statement;
43 self::assertInstanceOf(CreateTable::class, $statement);
44 $platform = new Platform();
45 $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
46 $element0 = $statement->elements[0];
47 self::assertInstanceOf(ColumnDefinition::class, $element0);
48 $specification = $element0->specification;
49 (new ElementFacts())->specification($specification, $derivation, $derivation->environment());
50
51 self::assertSame([], $derivation->facts()->diagnostics);
52 }
53
54 public function testOptionsAcceptsTableOptions(): void
55 {
56 $create = (new Semantics())->analyze('CREATE TABLE t (a INT) ENGINE x');
57 $statement = $create->statement;
58 self::assertInstanceOf(CreateTable::class, $statement);
59 $platform = new Platform();
60 $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
61 (new ElementFacts())->options($statement->options, $derivation);
62
63 self::assertSame([], $derivation->facts()->diagnostics);
64 }
65
66 public function testDeclarationAnswersTheColumnDeclaration(): void
67 {
68 $create = (new Semantics())->analyze('CREATE TABLE t (a INT AUTO_INCREMENT)');
69 $statement = $create->statement;
70 self::assertInstanceOf(CreateTable::class, $statement);
71
72 $column = $statement->elements[0];
73 self::assertInstanceOf(ColumnDefinition::class, $column);
74
75 self::assertSame(Nullability::NotNull, (new ElementFacts())->declaration($column)->nullability);
76 }
77
78 public function testReferencesAnswersTheInlineReference(): void
79 {
80 $create = (new Semantics())->analyze('CREATE TABLE t (a INT REFERENCES p (x))');
81 $statement = $create->statement;
82 self::assertInstanceOf(CreateTable::class, $statement);
83
84 $element0 = $statement->elements[0];
85 self::assertInstanceOf(ColumnDefinition::class, $element0);
86 self::assertSame('p', (new ElementFacts())->references($element0->specification)?->table->name->value);
87 }
88}
89