packages/sql-semantics-mysql/tests/Unit/Lowering/Query/Modern/ExpressionRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Query\Modern;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Lowering\Query\Modern\ExpressionRule;
11use SqlSemantics\Platform\MySql\Semantics;
12use SqlSemantics\Platform\MySql\Statement\Query\Into\IntoPosition;
13use SqlSemantics\Platform\MySql\Statement\Query\ParenthesizedQuery;
14use SqlSemantics\Platform\MySql\Statement\Query\QueryExpression;
15use SqlSemantics\Platform\MySql\Statement\Query\QueryStatement;
16use SqlSemantics\Platform\MySql\Statement\Query\Select;
17use SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperation;
18use SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperator;
19use SqlSemantics\Platform\MySql\Statement\Query\Set\SetQuantifier;
20use SqlSemantics\Platform\MySql\Statement\Relation\DerivedTable;
21
22#[CoversClass(ExpressionRule::class)]
23#[Medium]
24final class ExpressionRuleTest extends TestCase
25{
26    public function testStatementFoldsTheClausesIntoASingleBlock(): void
27    {
28        $operation = (new Semantics())->analyze('SELECT a FROM t ORDER BY a LIMIT 1 FOR UPDATE');
29
30        self::assertInstanceOf(Select::class, $operation->statement);
31        self::assertCount(1, $operation->statement->locking);
32        self::assertNotNull($operation->statement->limit);
33    }
34
35    public function testIntoLowersTheIntoAfterTheQuery(): void
36    {
37        $operation = (new Semantics())->analyze('(SELECT a FROM t FOR UPDATE INTO @x)');
38
39        self::assertInstanceOf(ParenthesizedQuery::class, $operation->statement);
40        self::assertInstanceOf(Select::class, $operation->statement->query);
41        self::assertSame(IntoPosition::AfterLocking, $operation->statement->query->intoPosition);
42    }
43
44    public function testExpressionWrapsANonBlockBody(): void
45    {
46        $operation = (new Semantics())->analyze('WITH c AS (SELECT 1) SELECT 1 UNION SELECT 2 ORDER BY 1 FOR UPDATE');
47
48        self::assertInstanceOf(QueryStatement::class, $operation->statement);
49        self::assertInstanceOf(QueryExpression::class, $operation->statement->query);
50        self::assertNotNull($operation->statement->query->with);
51    }
52
53    public function testBodyBuildsALeftDeepChain(): void
54    {
55        $operation = (new Semantics())->analyze('SELECT 1 UNION SELECT 2 EXCEPT SELECT 3');
56
57        self::assertInstanceOf(SetOperation::class, $operation->statement);
58        self::assertSame(SetOperator::Except, $operation->statement->operator);
59        self::assertInstanceOf(SetOperation::class, $operation->statement->left);
60    }
61
62    public function testQuantifierLowersDistinctAndAll(): void
63    {
64        $operation = (new Semantics())->analyze('SELECT 1 UNION DISTINCT SELECT 2');
65
66        self::assertInstanceOf(SetOperation::class, $operation->statement);
67        self::assertSame(SetQuantifier::Distinct, $operation->statement->quantifier);
68    }
69
70    public function testParenthesizedKeepsEveryPair(): void
71    {
72        self::assertSame('((SELECT 1)) UNION (SELECT 2)', (new Semantics('mysql-8.4.7'))->analyze('((select 1)) union (select 2)')->toString());
73    }
74
75    public function testInnerAnswersTheQueryInsideTheOuterParentheses(): void
76    {
77        self::assertSame('SELECT (SELECT 1) AS v, EXISTS ((SELECT 2)) AS w FROM t', (new Semantics('mysql-8.4.7'))->analyze('select (select 1) as v, exists ((select 2)) as w from t')->toString());
78    }
79
80    public function testLockedLowersTheLockingClausesInParentheses(): void
81    {
82        self::assertSame('(SELECT a FROM t FOR UPDATE) UNION SELECT 1', (new Semantics('mysql-8.4.7'))->analyze('(select a from t for update) union select 1')->toString());
83    }
84
85    public function testSubqueryRemovesTheParenthesesOfTheSyntax(): void
86    {
87        $operation = (new Semantics())->analyze('SELECT 1 FROM (SELECT 1) AS d');
88
89        self::assertInstanceOf(Select::class, $operation->statement);
90        self::assertInstanceOf(DerivedTable::class, $operation->statement->from);
91        self::assertInstanceOf(Select::class, $operation->statement->from->query);
92    }
93}
94