packages/sql-semantics-mysql/tests/Unit/Lowering/Expression/ExpressionRulesTest.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\ExpressionRules;
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\Expression\IntervalUnit;
16use SqlSemantics\Platform\MySql\Statement\Expression\Logical;
17use SqlSemantics\Platform\MySql\Statement\Expression\Operator\Arithmetic;
18use SqlSemantics\Platform\MySql\Statement\Expression\Operator\Collated;
19use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
20
21#[CoversClass(ExpressionRules::class)]
22#[Medium]
23final class ExpressionRulesTest extends TestCase
24{
25    public function testExpressionLowersALogicalOperation(): void
26    {
27        $platform = new Platform();
28        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
29        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
30
31        self::assertInstanceOf(Logical::class, $lowering->expressions->expression($platform->parser($profile)->parse('SELECT 1 AND 2')->find('expr')[0]));
32    }
33
34    public function testBitExpressionLowersAnArithmeticOperation(): 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
40        self::assertInstanceOf(Arithmetic::class, $lowering->expressions->bitExpression($platform->parser($profile)->parse('SELECT 1 + 2')->find('bit_expr')[0]));
41    }
42
43    public function testSimpleExpressionLowersACollation(): void
44    {
45        $platform = new Platform();
46        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
47        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
48
49        self::assertInstanceOf(Collated::class, $lowering->expressions->simpleExpression($platform->parser($profile)->parse("SELECT 'a' COLLATE utf8mb4_bin")->find('simple_expr')[0]));
50    }
51
52    public function testExpressionsLowersAListInOrder(): void
53    {
54        $platform = new Platform();
55        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
56        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
57        $expressions = $lowering->expressions->expressions($platform->parser($profile)->parse('SELECT 1 IN (2, 3, 4)')->find('expr_list')[0]);
58
59        self::assertCount(2, $expressions);
60        self::assertInstanceOf(NumberLiteral::class, $expressions[1]);
61        self::assertSame('4', $expressions[1]->text);
62    }
63
64    public function testIntervalUnitLowersAUnit(): void
65    {
66        $platform = new Platform();
67        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
68        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
69
70        self::assertSame(IntervalUnit::Quarter, $lowering->expressions->intervalUnit($platform->parser($profile)->parse('SELECT 1 + INTERVAL 1 QUARTER')->find('interval')[0]));
71    }
72
73    public function testIntervalLowersTheQuantityAndTheUnit(): void
74    {
75        $platform = new Platform();
76        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
77        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
78        $tree = $platform->parser($profile)->parse('SELECT 1 + INTERVAL 7 WEEK');
79        $interval = $lowering->expressions->interval($tree->find('expr')[1], $tree->find('interval')[0]);
80
81        self::assertInstanceOf(NumberLiteral::class, $interval->quantity);
82        self::assertSame(['7', IntervalUnit::Week], [$interval->quantity->text, $interval->unit]);
83    }
84}
85