packages/sql-semantics-mysql/tests/Unit/Lowering/TableDefinition/Key/KeyPartRuleTest.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\KeyPartRule;
12use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
13use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
14use SqlSemantics\Platform\MySql\Platform;
15use SqlSemantics\Platform\MySql\Statement\Query\Direction;
16use SqlSemantics\Platform\MySql\Statement\Table\Key\ColumnPart;
17use SqlSemantics\Platform\MySql\Statement\Table\Key\ExpressionPart;
18
19#[CoversClass(KeyPartRule::class)]
20#[Medium]
21final class KeyPartRuleTest extends TestCase
22{
23    public function testPartsPairsLegacyDirections(): void
24    {
25        $platform = new Platform();
26        $profile = $platform->profile('5.6.51', 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 DESC, b))')->find('key_list')[0];
29        $parts = (new KeyPartRule($lowering))->parts($node);
30
31        self::assertInstanceOf(ColumnPart::class, $parts[0]);
32        self::assertSame(Direction::Descending, $parts[0]->direction);
33        self::assertCount(2, $parts);
34    }
35
36    public function testColumnsLowersForeignKeyColumns(): void
37    {
38        $platform = new Platform();
39        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
40        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
41        $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT, FOREIGN KEY (a, b) REFERENCES p (x, y))')->find('key_list')[0];
42
43        self::assertCount(2, (new KeyPartRule($lowering))->columns($node));
44    }
45
46    public function testPartLowersAnExpressionPart(): void
47    {
48        $platform = new Platform();
49        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
50        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
51        $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT, KEY ((a + 1) DESC))')->find('key_part_with_expression')[0];
52
53        self::assertInstanceOf(ExpressionPart::class, (new KeyPartRule($lowering))->part($node));
54    }
55
56    public function testLengthLowersThePrefixLength(): void
57    {
58        $platform = new Platform();
59        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
60        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
61        $node = $platform->parser($profile)->parse('CREATE TABLE t (a TEXT, KEY (a(7)))')->find('key_part')[0];
62
63        self::assertSame('7', (new KeyPartRule($lowering))->length($node)->text);
64    }
65}
66