packages/sql-semantics-mysql/tests/Unit/Lowering/Leaf/OptionRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Leaf;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlParser\Lexer\Token;
11use SqlParser\Parser\Node;
12use SqlSemantics\Platform\MySql\Lowering\Leaf\OptionRule;
13use SqlSemantics\Platform\MySql\Lowering\Lowering;
14use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
15use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
16use SqlSemantics\Platform\MySql\Platform;
17use SqlSemantics\Platform\MySql\Statement\Alter\DropBehavior;
18
19#[CoversClass(OptionRule::class)]
20#[Medium]
21final class OptionRuleTest extends TestCase
22{
23    public function testPresentTellsWhetherAnOptionalKeywordIsWritten(): void
24    {
25        $platform = new Platform();
26        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
27        $rule = new OptionRule(new Lowering($platform->productions($profile), new Leaves(), $profile));
28
29        self::assertTrue($rule->present(new Node('opt_as', 1, [new Token(0, 'AS', 'AS', 0)])));
30        self::assertFalse($rule->present(new Node('opt_as', 0, [])));
31        self::assertTrue($rule->present(new Node('opt_if_not_exists', 1, [new Token(0, 'IF', 'IF', 0), new Node('not', 0, [new Token(0, 'NOT_SYM', 'NOT', 0)]), new Token(0, 'EXISTS', 'EXISTS', 0)])));
32        self::assertTrue($rule->present(new Node('opt_wild', 1, [new Token(0, '.', '.', 0), new Token(0, '*', '*', 0)])));
33        self::assertFalse($rule->present(new Node('opt_ignore', 0, [])));
34    }
35
36    public function testPresentReportsAProductionWithoutARule(): void
37    {
38        $platform = new Platform();
39        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
40        $rule = new OptionRule(new Lowering($platform->productions($profile), new Leaves(), $profile));
41
42        $this->expectExceptionMessage('No semantic rule is implemented for: opt_restrict: RESTRICT');
43
44        $rule->present(new Node('opt_restrict', 1, [new Token(0, 'RESTRICT', 'RESTRICT', 0)]));
45    }
46
47    public function testSkipAcceptsAnInertProduction(): void
48    {
49        $platform = new Platform();
50        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
51        $rule = new OptionRule(new Lowering($platform->productions($profile), new Leaves(), $profile));
52        $rule->skip(new Node('key_or_index', 0, [new Token(0, 'KEY_SYM', 'KEY', 0)]));
53        $rule->skip(new Node('not', 0, [new Token(0, 'NOT_SYM', 'NOT', 0)]));
54
55        $this->expectExceptionMessage('No semantic rule is implemented for: opt_temporary: TEMPORARY');
56
57        $rule->skip(new Node('opt_temporary', 1, [new Token(0, 'TEMPORARY', 'TEMPORARY', 0)]));
58    }
59
60    public function testDropBehaviorLowersRestrictAndCascade(): void
61    {
62        $platform = new Platform();
63        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
64        $rule = new OptionRule(new Lowering($platform->productions($profile), new Leaves(), $profile));
65
66        self::assertNull($rule->dropBehavior(new Node('opt_restrict', 0, [])));
67        self::assertSame(DropBehavior::Restrict, $rule->dropBehavior(new Node('opt_restrict', 1, [new Token(0, 'RESTRICT', 'RESTRICT', 0)])));
68        self::assertSame(DropBehavior::Cascade, $rule->dropBehavior(new Node('opt_restrict', 2, [new Token(0, 'CASCADE', 'CASCADE', 0)])));
69    }
70}
71