packages/sql-semantics-mysql/tests/Unit/Lowering/Expression/PrimaryRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Expression;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Lowering\Expression\PrimaryRule;
11use SqlSemantics\Platform\MySql\Lowering\Lowering;
12use SqlSemantics\Platform\MySql\Mode;
13use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
14use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
15use SqlSemantics\Platform\MySql\Platform;
16use SqlSemantics\Platform\MySql\Statement\Expression\Operator\Collated;
17use SqlSemantics\Platform\MySql\Statement\Expression\Operator\Concatenation;
18use SqlSemantics\Platform\MySql\Statement\Expression\Operator\Unary;
19use SqlSemantics\Platform\MySql\Statement\Expression\Operator\UnaryOperator;
20use SqlSemantics\Platform\MySql\Statement\Literal\Parameter;
21use SqlSemantics\Platform\MySql\Statement\Name\ColumnUse;
22
23#[CoversClass(PrimaryRule::class)]
24#[Medium]
25final class PrimaryRuleTest extends TestCase
26{
27    public function testSimpleExpressionAppliesCollateAndConcatenationFromTheLeft(): void
28    {
29        $platform = new Platform();
30        $profile = $platform->profile('mysql-8.4.7', Mode::fromString('HIGH_NOT_PRECEDENCE,PIPES_AS_CONCAT'), ParameterStyle::Native);
31        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
32        $expression = (new PrimaryRule($lowering))->simpleExpression($platform->parser($profile)->parse("SELECT 'a' || 'b' COLLATE utf8mb4_bin || 'c'")->find('simple_expr')[0]);
33
34        self::assertInstanceOf(Concatenation::class, $expression);
35        self::assertInstanceOf(Concatenation::class, $expression->left);
36        self::assertInstanceOf(Collated::class, $expression->left->right);
37    }
38
39    public function testUnitLowersTheTightNegationOfHighNotPrecedence(): void
40    {
41        $platform = new Platform();
42        $profile = $platform->profile('mysql-8.4.7', Mode::fromString('HIGH_NOT_PRECEDENCE,PIPES_AS_CONCAT'), ParameterStyle::Native);
43        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
44        $expression = (new PrimaryRule($lowering))->unit($platform->productions($profile)->form($platform->parser($profile)->parse('SELECT NOT 1')->find('simple_expr')[0]));
45
46        self::assertInstanceOf(Unary::class, $expression);
47        self::assertSame(UnaryOperator::Not, $expression->operator);
48    }
49
50    public function testLeafLowersAColumnAndAParameter(): void
51    {
52        $platform = new Platform();
53        $profile = $platform->profile('mysql-5.6.51', null, ParameterStyle::Native);
54        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
55        $rule = new PrimaryRule($lowering);
56        $tree = $platform->parser($profile)->parse('SELECT a = ?');
57
58        self::assertInstanceOf(ColumnUse::class, $rule->leaf('column', $tree->find('simple_ident')[0]));
59        self::assertInstanceOf(Parameter::class, $rule->leaf('parameter', $tree->find('param_marker')[0]));
60    }
61
62    public function testNegationRejectsAnotherNode(): void
63    {
64        $platform = new Platform();
65        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
66        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
67
68        $this->expectExceptionMessage('No semantic rule is implemented for: comp_op: EQ');
69
70        (new PrimaryRule($lowering))->negation($platform->parser($profile)->parse('SELECT 1 = 2')->find('comp_op')[0]);
71    }
72}
73