packages/sql-semantics-mysql/tests/Unit/Lowering/TableDefinition/Key/KeyOptionRuleTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\TableDefinition\Key;
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\Key\KeyOptionRule;
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\Key\IndexAlgorithm;
16use SqlSemantics\Platform\MySql\Statement\Table\Key\Option\IndexParser;
17
18#[CoversClass(KeyOptionRule::class)]
19#[Medium]
20final class KeyOptionRuleTest extends TestCase
21{
22 public function testAlgorithmLowersTheStructureClause(): 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 (a INT, KEY USING RTREE (a))')->find('key_alg')[0];
28
29 self::assertSame(IndexAlgorithm::Rtree, (new KeyOptionRule($lowering))->algorithm($node));
30 }
31
32 public function testMarkedConfirmsTheMarker(): 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, FULLTEXT (a))')->find('init_key_options')[0];
38
39 self::assertNull((new KeyOptionRule($lowering))->marked($node, null));
40 }
41
42 public function testNameAndTypeLowersTheNameAndTheStructure(): void
43 {
44 $platform = new Platform();
45 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
46 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
47 $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT, KEY k TYPE HASH (a))')->find('opt_index_name_and_type')[0];
48 [$name, $algorithm] = (new KeyOptionRule($lowering))->nameAndType($node);
49
50 self::assertSame('k', $name?->column->value);
51 self::assertSame(IndexAlgorithm::Hash, $algorithm);
52 }
53
54 public function testStructureLowersTheKeyword(): void
55 {
56 $platform = new Platform();
57 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
58 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
59 $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT, KEY USING BTREE (a))')->find('index_type')[0];
60
61 self::assertSame(IndexAlgorithm::Btree, (new KeyOptionRule($lowering))->structure($node));
62 }
63
64 public function testOptionsLowersTheOptionList(): void
65 {
66 $platform = new Platform();
67 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
68 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
69 $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT, KEY (a) COMMENT \'x\' INVISIBLE USING HASH)')->find('opt_index_options')[0];
70
71 self::assertCount(3, (new KeyOptionRule($lowering))->options($node));
72 }
73
74 public function testOptionLowersOneOption(): void
75 {
76 $platform = new Platform();
77 $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
78 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
79 $node = $platform->parser($profile)->parse('CREATE TABLE t (a TEXT, FULLTEXT (a) WITH PARSER ngram)')->find('fulltext_key_opt')[0];
80
81 self::assertInstanceOf(IndexParser::class, (new KeyOptionRule($lowering))->option($node));
82 }
83
84 public function testVisibleLowersVisibility(): void
85 {
86 $platform = new Platform();
87 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
88 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
89 $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT, KEY (a) INVISIBLE)')->find('visibility')[0];
90
91 self::assertFalse((new KeyOptionRule($lowering))->visible($node));
92 }
93}
94