packages/sql-semantics-mysql/tests/Unit/Lowering/TableDefinition/Column/ColumnRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\TableDefinition\Column;
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\Column\ColumnRule;
12use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
13use SqlSemantics\Platform\MySql\Model\Diagnostic\AnalysisException;
14use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
15use SqlSemantics\Platform\MySql\Platform;
16use SqlSemantics\Platform\MySql\Semantics;
17use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
18use SqlSemantics\Platform\MySql\Statement\Table\Column\GeneratedColumn;
19use SqlSemantics\Platform\MySql\Statement\Table\Column\Kind\GeneratedStorage;
20use SqlSemantics\Platform\MySql\Statement\Table\Column\OrdinaryColumn;
21
22#[CoversClass(ColumnRule::class)]
23#[Medium]
24final class ColumnRuleTest extends TestCase
25{
26    public function testSpecificationLowersAGeneratedColumn(): void
27    {
28        $platform = new Platform();
29        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
30        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
31        $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT AS (1) STORED)')->find('field_def')[0];
32
33        self::assertInstanceOf(GeneratedColumn::class, (new ColumnRule($lowering))->specification($node));
34    }
35
36    public function testSpecificationLowersALegacyTypeAndAttributes(): void
37    {
38        $platform = new Platform();
39        $profile = $platform->profile('5.6.51', null, ParameterStyle::Native);
40        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
41        $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT NOT NULL)')->find('type')[0];
42        $specification = (new ColumnRule($lowering))->specification($node, $platform->parser($profile)->parse('CREATE TABLE t (a INT NOT NULL)')->find('opt_attribute')[0]);
43
44        self::assertInstanceOf(OrdinaryColumn::class, $specification);
45        self::assertCount(1, $specification->attributes);
46    }
47
48    public function testAttributesLowersTheListInOrder(): 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 TABLE t (a INT NULL DEFAULT 1 COMMENT \'c\')')->find('opt_column_attribute_list')[0];
54
55        self::assertCount(3, (new ColumnRule($lowering))->attributes($node));
56    }
57
58    public function testSpecificationRefusesAnAttributeOfAGeneratedColumn(): 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, g INT AS (a) NOT NULL DEFAULT 1)')->find('field_def')[1];
64
65        $this->expectException(AnalysisException::class);
66        $this->expectExceptionMessage('Incorrect usage of DEFAULT and generated column');
67
68        (new ColumnRule($lowering))->specification($node);
69    }
70
71    public function testRefuseNamesTheFirstRefusedAttribute(): void
72    {
73        $semantics = new Semantics();
74
75        $this->expectException(AnalysisException::class);
76        $this->expectExceptionMessage('Incorrect usage of COLUMN_FORMAT and generated column');
77
78        $semantics->analyze('CREATE TABLE t (a INT, g INT AS (a) UNIQUE COLUMN_FORMAT FIXED ON UPDATE CURRENT_TIMESTAMP)');
79    }
80
81    public function testRefuseNamesTheTypeSerial(): void
82    {
83        $this->expectException(AnalysisException::class);
84        $this->expectExceptionMessage('Incorrect usage of SERIAL and generated column');
85
86        (new Semantics())->analyze('ALTER TABLE t ADD g SERIAL AS (1)');
87    }
88
89    public function testRefuseAcceptsTheOtherAttributes(): void
90    {
91        $create = (new Semantics())->analyze('CREATE TABLE t (a INT, g INT AS (a) VIRTUAL NOT NULL UNIQUE KEY COMMENT \'c\' INVISIBLE)');
92
93        self::assertSame([], $create->facts->diagnostics);
94    }
95
96    public function testAlwaysAcceptsTheOptionalWords(): void
97    {
98        $platform = new Platform();
99        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
100        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
101        $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT GENERATED ALWAYS AS (1))')->find('opt_generated_always')[0];
102        (new ColumnRule($lowering))->always($node);
103
104        $this->expectExceptionMessage('No semantic rule is implemented for: opt_stored_attribute:');
105
106        (new ColumnRule($lowering))->always($platform->parser($profile)->parse('CREATE TABLE t (a INT AS (1))')->find('opt_stored_attribute')[0]);
107    }
108
109    public function testStorageLowersVirtualAndStored(): void
110    {
111        $platform = new Platform();
112        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
113        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
114        $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT AS (1) VIRTUAL)')->find('opt_stored_attribute')[0];
115
116        self::assertSame(GeneratedStorage::Virtual, (new ColumnRule($lowering))->storage($node));
117    }
118
119    public function testExpressionLowersAGeneratedColumnFunction(): void
120    {
121        $platform = new Platform();
122        $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
123        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
124        $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT AS (1))')->find('generated_column_func')[0];
125
126        self::assertInstanceOf(NumberLiteral::class, (new ColumnRule($lowering))->expression($node));
127    }
128
129    public function testParseLowersParseGcolExpr(): void
130    {
131        $platform = new Platform();
132        $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
133        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
134        $node = $platform->parser($profile)->parse('PARSE_GCOL_EXPR(1)')->find('parse_gcol_expr')[0];
135
136        self::assertInstanceOf(NumberLiteral::class, (new ColumnRule($lowering))->parse($node)->expression);
137    }
138}
139