packages/sql-semantics-mysql/tests/Unit/Lowering/TableDefinition/TableDefinitionRulesTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\TableDefinition;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Lowering\Lowering;
11use SqlSemantics\Platform\MySql\Lowering\TableDefinition\TableDefinitionRules;
12use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
13use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
14use SqlSemantics\Platform\MySql\Platform;
15use SqlSemantics\Platform\MySql\Statement\Table\Column\ColumnDefinition;
16use SqlSemantics\Platform\MySql\Statement\Table\Column\OrdinaryColumn;
17use SqlSemantics\Platform\MySql\Statement\Table\CreateIndex;
18use SqlSemantics\Platform\MySql\Statement\Table\CreateTable;
19use SqlSemantics\Platform\MySql\Statement\Table\Option\EngineOption;
20use SqlSemantics\Platform\MySql\Statement\View\CreateView;
21use SqlSemantics\Platform\MySql\Statement\View\DropView;
22
23#[CoversClass(TableDefinitionRules::class)]
24#[Medium]
25final class TableDefinitionRulesTest extends TestCase
26{
27 public function testStatementLowersCreateTable(): void
28 {
29 $platform = new Platform();
30 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
31 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
32 $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT)')->find('create_table_stmt')[0];
33
34 self::assertInstanceOf(CreateTable::class, (new TableDefinitionRules($lowering))->statement($node));
35 }
36
37 public function testDefinitionLowersALegacyCreateIndex(): void
38 {
39 $platform = new Platform();
40 $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
41 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
42 $node = $platform->parser($profile)->parse('CREATE INDEX i ON t (a)')->find('create')[0];
43
44 self::assertInstanceOf(CreateIndex::class, (new TableDefinitionRules($lowering))->definition($lowering->form($node)));
45 self::assertInstanceOf(DropView::class, (new TableDefinitionRules($lowering))->definition($lowering->form($platform->parser($profile)->parse('DROP VIEW v')->find('drop')[0])));
46 }
47
48 public function testCreateViewLowersAView(): void
49 {
50 $platform = new Platform();
51 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
52 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
53 $node = $platform->parser($profile)->parse('CREATE VIEW v AS SELECT 1')->find('view_tail')[0];
54
55 self::assertInstanceOf(CreateView::class, (new TableDefinitionRules($lowering))->createView($node, null, null));
56 }
57
58 public function testTableElementLowersAColumn(): void
59 {
60 $platform = new Platform();
61 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
62 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
63 $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT)')->find('column_def')[0];
64
65 self::assertInstanceOf(ColumnDefinition::class, (new TableDefinitionRules($lowering))->tableElement($node));
66 }
67
68 public function testTableElementsLowersTheList(): void
69 {
70 $platform = new Platform();
71 $profile = $platform->profile('5.6.51', null, ParameterStyle::Native);
72 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
73 $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT, b INT)')->find('create_field_list')[0];
74
75 self::assertCount(2, (new TableDefinitionRules($lowering))->tableElements($node));
76 }
77
78 public function testColumnSpecificationLowersAFieldDefWithItsReference(): void
79 {
80 $platform = new Platform();
81 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
82 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
83 $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT REFERENCES p (x))')->find('field_def')[0];
84 $references = $platform->parser($profile)->parse('CREATE TABLE t (a INT REFERENCES p (x))')->find('opt_references')[0];
85 $specification = (new TableDefinitionRules($lowering))->columnSpecification($node, $references);
86
87 self::assertInstanceOf(OrdinaryColumn::class, $specification);
88 self::assertNotNull($specification->references);
89 }
90
91 public function testTableOptionsLowersTheOptions(): void
92 {
93 $platform = new Platform();
94 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
95 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
96 $node = $platform->parser($profile)->parse('ALTER TABLE t ENGINE = x')->find('create_table_options_space_separated')[0];
97 $options = (new TableDefinitionRules($lowering))->tableOptions($node);
98
99 self::assertInstanceOf(EngineOption::class, $options[0]);
100 }
101
102 public function testVisibleTellsWhetherVisibleIsWritten(): void
103 {
104 $platform = new Platform();
105 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
106 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
107 $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT VISIBLE)')->find('visibility')[0];
108
109 self::assertTrue((new TableDefinitionRules($lowering))->visible($node));
110 }
111
112 public function testEnforcedTellsWhetherEnforcedIsWritten(): void
113 {
114 $platform = new Platform();
115 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
116 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
117 $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT CHECK (a > 0) ENFORCED)')->find('constraint_enforcement')[0];
118
119 self::assertTrue((new TableDefinitionRules($lowering))->enforced($node));
120 }
121}
122