packages/sql-semantics-mysql/tests/Unit/Lowering/TableDefinition/CreateTableRuleTest.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\CreateTableRule;
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\CreateTable;
16use SqlSemantics\Platform\MySql\Statement\Table\CreateTableLike;
17
18#[CoversClass(CreateTableRule::class)]
19#[Medium]
20final class CreateTableRuleTest extends TestCase
21{
22    public function testLegacyLowersTheLikeForm(): void
23    {
24        $platform = new Platform();
25        $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
26        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
27        $node = $platform->parser($profile)->parse('CREATE TABLE t (LIKE s)')->find('create')[0];
28
29        self::assertInstanceOf(CreateTableLike::class, (new CreateTableRule($lowering))->legacy($lowering->form($node)));
30    }
31
32    public function testEnclosedLowersAColumnList(): void
33    {
34        $platform = new Platform();
35        $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
36        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
37        $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT) ENGINE = x')->find('create2a')[0];
38        $create = (new CreateTableRule($lowering))->enclosed(new \SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name('t')), $lowering->form($node), 0, false);
39
40        self::assertCount(1, $create->elements);
41        self::assertCount(1, $create->options);
42    }
43
44    public function testPartitionedBuildsTheQueryForm(): void
45    {
46        $platform = new Platform();
47        $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
48        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
49        $node = $platform->parser($profile)->parse('CREATE TABLE t (SELECT 1 AS a)')->find('create2a')[0];
50        $create = (new CreateTableRule($lowering))->enclosed(new \SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name('t')), $lowering->form($node), 2, true);
51
52        self::assertNotNull($create->query);
53        self::assertSame(2, $create->temporaryWords);
54    }
55
56    public function testTemporaryCountsTheTemporaryWords(): void
57    {
58        $platform = new Platform();
59        $profile = $platform->profile('5.6.51', null, ParameterStyle::Native);
60        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
61        $node = $platform->parser($profile)->parse('CREATE TEMPORARY TEMPORARY TABLE t (a INT)')->find('opt_table_options')[0];
62
63        self::assertSame(2, (new CreateTableRule($lowering))->temporary($node));
64    }
65
66    public function testLegacyQueryLowersTheQueryPart(): void
67    {
68        $platform = new Platform();
69        $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
70        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
71        $node = $platform->parser($profile)->parse('CREATE TABLE t IGNORE SELECT 1 AS a')->find('create3')[0];
72
73        self::assertNotNull((new CreateTableRule($lowering))->legacyQuery($node));
74    }
75
76    public function testModernLowersCreateTable(): void
77    {
78        $platform = new Platform();
79        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
80        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
81        $node = $platform->parser($profile)->parse('CREATE TEMPORARY TABLE t (a INT)')->find('create_table_stmt')[0];
82        $create = (new CreateTableRule($lowering))->modern($node);
83
84        self::assertInstanceOf(CreateTable::class, $create);
85        self::assertSame(1, $create->temporaryWords);
86    }
87
88    public function testRestLowersOptionsPartitioningAndQuery(): void
89    {
90        $platform = new Platform();
91        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
92        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
93        $node = $platform->parser($profile)->parse('CREATE TABLE t ENGINE x AS SELECT 1 AS a')->find('opt_create_table_options_etc')[0];
94        $create = (new CreateTableRule($lowering))->rest(new \SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name('t')), [], $node, 0, false);
95
96        self::assertCount(1, $create->options);
97        self::assertNotNull($create->query);
98    }
99
100    public function testQueryLowersTheQueryPart(): void
101    {
102        $platform = new Platform();
103        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
104        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
105        $node = $platform->parser($profile)->parse('CREATE TABLE t REPLACE SELECT 1 AS a')->find('opt_duplicate_as_qe')[0];
106
107        self::assertNotNull((new CreateTableRule($lowering))->query($node));
108    }
109}
110