packages/sql-semantics-mysql/tests/Unit/Lowering/TableDefinition/ElementRuleTest.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\ElementRule;
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\Key\CheckConstraint;
17use SqlSemantics\Platform\MySql\Statement\Table\Key\IndexDefinition;
18
19#[CoversClass(ElementRule::class)]
20#[Medium]
21final class ElementRuleTest extends TestCase
22{
23    public function testElementsLowersTheElementList(): void
24    {
25        $platform = new Platform();
26        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
27        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
28        $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT, KEY (a))')->find('table_element_list')[0];
29        $elements = (new ElementRule($lowering))->elements($node);
30
31        self::assertInstanceOf(ColumnDefinition::class, $elements[0]);
32        self::assertInstanceOf(IndexDefinition::class, $elements[1]);
33    }
34
35    public function testElementLowersAColumnWithItsCheck(): void
36    {
37        $platform = new Platform();
38        $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
39        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
40        $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT NOT NULL CHECK (a > 0))')->find('column_def')[0];
41        $element = (new ElementRule($lowering))->element($node);
42
43        self::assertInstanceOf(ColumnDefinition::class, $element);
44        self::assertInstanceOf(CheckConstraint::class, $element->specification->columnAttributes()[1]);
45    }
46
47    public function testColumnLowersAQualifiedFieldSpec(): void
48    {
49        $platform = new Platform();
50        $profile = $platform->profile('5.6.51', null, ParameterStyle::Native);
51        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
52        $node = $platform->parser($profile)->parse('CREATE TABLE t (t.a INT)')->find('field_spec')[0];
53        $column = (new ElementRule($lowering))->column($node);
54
55        self::assertSame('t', $column->name->table?->name->value);
56    }
57}
58