packages/sql-semantics-mysql/tests/Unit/Lowering/Call/WeightRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Call;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Lowering\Call\WeightRule;
11use SqlSemantics\Platform\MySql\Lowering\Lowering;
12use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
13use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
14use SqlSemantics\Platform\MySql\Platform;
15use SqlSemantics\Platform\MySql\Statement\Call\Weight\WeightCast;
16use SqlSemantics\Platform\MySql\Statement\Call\Weight\WeightString;
17use SqlSemantics\Platform\MySql\Statement\Call\Weight\WeightStringParameters;
18use SqlSemantics\Platform\MySql\Statement\Query\Direction;
19
20#[CoversClass(WeightRule::class)]
21#[Small]
22final class WeightRuleTest extends TestCase
23{
24    public function testClaimsTellsTheProductionsOfTheRule(): void
25    {
26        $platform = new Platform();
27        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
28        $rule = new WeightRule(new Lowering($platform->productions($profile), new Leaves(), $profile));
29
30        self::assertTrue($rule->claims('function_call_conflict: WEIGHT_STRING_SYM ( expr )'));
31        self::assertFalse($rule->claims('function_call_conflict: IF ( expr , expr , expr )'));
32    }
33
34    public function testCallLowersEveryForm(): void
35    {
36        $platform = new Platform();
37        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
38        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
39        $list = $platform->parser($profile)->parse('SELECT WEIGHT_STRING(a AS BINARY(4)), WEIGHT_STRING(a, 1, 2, 3)')->find('select_item_list')[0];
40        $rule = new WeightRule($lowering);
41        $cast = $rule->call($lowering->form($list->find('function_call_conflict')[0]));
42        $parameters = $rule->call($lowering->form($list->find('function_call_conflict')[1]));
43
44        self::assertInstanceOf(WeightString::class, $cast);
45        self::assertSame(WeightCast::Binary, $cast->cast);
46        self::assertInstanceOf(WeightStringParameters::class, $parameters);
47    }
48
49    public function testCallReportsAProductionOutsideTheRule(): void
50    {
51        $platform = new Platform();
52        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
53        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
54        $node = $platform->parser($profile)->parse('SELECT IF(1, 2, 3)')->find('function_call_conflict')[0];
55        $this->expectExceptionMessage('No semantic rule is implemented for: ');
56
57        (new WeightRule($lowering))->call($lowering->form($node));
58    }
59
60    public function testLengthLowersTheLengthOfTheCast(): void
61    {
62        $platform = new Platform();
63        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
64        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
65        $node = $platform->parser($profile)->parse('SELECT WEIGHT_STRING(a AS CHAR(7))')->find('ws_num_codepoints')[0];
66        self::assertSame('7', (new WeightRule($lowering))->length($node)->text);
67    }
68
69    public function testLevelsLowersARange(): void
70    {
71        $platform = new Platform();
72        $profile = $platform->profile('mysql-5.7.44', null, ParameterStyle::Native);
73        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
74        $node = $platform->parser($profile)->parse('SELECT WEIGHT_STRING(a LEVEL 1 - 3)')->find('opt_ws_levels')[0];
75        self::assertSame('3', (new WeightRule($lowering))->levels($node)[1]?->to->text);
76    }
77
78    public function testLevelLowersTheFlags(): void
79    {
80        $platform = new Platform();
81        $profile = $platform->profile('mysql-5.6.51', null, ParameterStyle::Native);
82        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
83        $node = $platform->parser($profile)->parse('SELECT WEIGHT_STRING(a LEVEL 2 DESC REVERSE)')->find('ws_level_list_item')[0];
84        $level = (new WeightRule($lowering))->level($node);
85
86        self::assertSame(Direction::Descending, $level->direction);
87        self::assertTrue($level->reverse);
88    }
89
90    public function testDirectionLowersAscending(): void
91    {
92        $platform = new Platform();
93        $profile = $platform->profile('mysql-5.7.44', null, ParameterStyle::Native);
94        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
95        $node = $platform->parser($profile)->parse('SELECT WEIGHT_STRING(a LEVEL 2 ASC)')->find('ws_level_flag_desc')[0];
96        self::assertSame(Direction::Ascending, (new WeightRule($lowering))->direction($node));
97    }
98
99    public function testNumberLowersALevelNumber(): void
100    {
101        $platform = new Platform();
102        $profile = $platform->profile('mysql-5.7.44', null, ParameterStyle::Native);
103        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
104        $node = $platform->parser($profile)->parse('SELECT WEIGHT_STRING(a LEVEL 5)')->find('ws_level_number')[0];
105        self::assertSame('5', (new WeightRule($lowering))->number($node)->text);
106    }
107}
108