packages/sql-semantics-mysql/tests/Unit/Lowering/TableDefinition/TableOptionRuleTest.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\TableOptionRule;
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\Option\EngineOption;
16use SqlSemantics\Platform\MySql\Statement\Table\Option\Kind\InsertMethod;
17use SqlSemantics\Platform\MySql\Statement\Table\Option\Kind\RowFormat;
18use SqlSemantics\Platform\MySql\Statement\Table\Option\NumberOption;
19
20#[CoversClass(TableOptionRule::class)]
21#[Medium]
22final class TableOptionRuleTest extends TestCase
23{
24    public function testOptionsLowersTheOptionsInOrder(): void
25    {
26        $platform = new Platform();
27        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
28        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
29        $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT) ENGINE = x, MAX_ROWS 3')->find('create_table_options')[0];
30        $options = (new TableOptionRule($lowering))->options($node);
31
32        self::assertInstanceOf(EngineOption::class, $options[0]);
33        self::assertInstanceOf(NumberOption::class, $options[1]);
34    }
35
36    public function testOptionLowersOneOption(): 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) PACK_KEYS = DEFAULT')->find('create_table_option')[0];
42        $option = (new TableOptionRule($lowering))->option($node);
43
44        self::assertInstanceOf(NumberOption::class, $option);
45        self::assertNull($option->value);
46    }
47
48    public function testNamedLowersANamedOption(): 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) ENGINE InnoDB')->find('create_table_option')[0];
54        $option = (new TableOptionRule($lowering))->named($lowering->form($node));
55
56        self::assertInstanceOf(EngineOption::class, $option);
57    }
58
59    public function testKeywordLowersARowFormat(): void
60    {
61        $platform = new Platform();
62        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
63        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
64        $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT) ROW_FORMAT REDUNDANT')->find('row_types')[0];
65
66        self::assertSame(RowFormat::Redundant, (new TableOptionRule($lowering))->keyword($node, RowFormat::class));
67
68        $this->expectExceptionMessage('No semantic rule is implemented for: row_types: REDUNDANT_SYM');
69
70        (new TableOptionRule($lowering))->keyword($node, InsertMethod::class);
71    }
72}
73