packages/sql-semantics-mysql/tests/Unit/Lowering/TableDefinition/IndexRuleTest.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\IndexRule;
12use SqlSemantics\Platform\MySql\Lowering\TableDefinition\Key\KeyOptionRule;
13use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
14use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
15use SqlSemantics\Platform\MySql\Platform;
16use SqlSemantics\Platform\MySql\Statement\Table\Key\IndexAlgorithm;
17use SqlSemantics\Platform\MySql\Statement\Table\Key\IndexKind;
18
19#[CoversClass(IndexRule::class)]
20#[Medium]
21final class IndexRuleTest extends TestCase
22{
23 public function testIndexLowersTheLegacyForm(): void
24 {
25 $platform = new Platform();
26 $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
27 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
28 $node = $platform->parser($profile)->parse('CREATE SPATIAL INDEX i ON t (g)')->find('create')[0];
29 $index = (new IndexRule($lowering))->index($lowering->form($node));
30
31 self::assertSame(IndexKind::Spatial, $index->kind);
32 }
33
34 public function testStructureLowersTheClauseBeforeOn(): void
35 {
36 $platform = new Platform();
37 $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
38 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
39 $node = $platform->parser($profile)->parse('CREATE INDEX i USING HASH ON t (a)')->find('key_alg')[0];
40
41 self::assertSame(IndexAlgorithm::Hash, (new IndexRule($lowering))->structure($node, new KeyOptionRule($lowering)));
42 }
43
44 public function testStatementLowersCreateIndexStmt(): void
45 {
46 $platform = new Platform();
47 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
48 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
49 $node = $platform->parser($profile)->parse('CREATE UNIQUE INDEX i ON t (a)')->find('create_index_stmt')[0];
50
51 self::assertSame(IndexKind::Unique, (new IndexRule($lowering))->statement($node)->kind);
52 }
53}
54