packages/sql-semantics-mysql/tests/Unit/Lowering/Expression/ConditionRuleTest.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\ConditionRule;
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\Comparison;
16use SqlSemantics\Platform\MySql\Statement\Expression\ComparisonOperator;
17use SqlSemantics\Platform\MySql\Statement\Expression\Logical;
18use SqlSemantics\Platform\MySql\Statement\Expression\LogicalOperator;
19use SqlSemantics\Platform\MySql\Statement\Expression\Not;
20use SqlSemantics\Platform\MySql\Statement\Expression\NullTest;
21use SqlSemantics\Platform\MySql\Statement\Expression\Subquery\QuantifiedComparison;
22use SqlSemantics\Platform\MySql\Statement\Expression\Subquery\Quantifier;
23use SqlSemantics\Platform\MySql\Statement\Expression\Truth;
24use SqlSemantics\Platform\MySql\Statement\Expression\TruthTest;
25use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
26
27#[CoversClass(ConditionRule::class)]
28#[Medium]
29final class ConditionRuleTest extends TestCase
30{
31 public function testExpressionBuildsTheLeftAssociationOfAChain(): void
32 {
33 $platform = new Platform();
34 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
35 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
36 $expression = (new ConditionRule($lowering))->expression($platform->parser($profile)->parse('SELECT 1 OR 2 && 3 OR 4')->find('expr')[0]);
37
38 self::assertInstanceOf(Logical::class, $expression);
39 self::assertInstanceOf(Logical::class, $expression->left);
40 self::assertSame([LogicalOperator::Or, LogicalOperator::Or], [$expression->operator, $expression->left->operator]);
41 self::assertInstanceOf(Logical::class, $expression->left->right);
42 self::assertSame(LogicalOperator::And, $expression->left->right->operator);
43 }
44
45 public function testUnitLowersNotAndTheTruthTests(): void
46 {
47 $platform = new Platform();
48 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
49 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
50 $rule = new ConditionRule($lowering);
51 $not = $rule->unit($platform->productions($profile)->form($platform->parser($profile)->parse('SELECT NOT 1')->find('expr')[0]));
52 $truth = $rule->unit($platform->productions($profile)->form($platform->parser($profile)->parse('SELECT 1 IS NOT FALSE')->find('expr')[0]));
53
54 self::assertInstanceOf(Not::class, $not);
55 self::assertInstanceOf(TruthTest::class, $truth);
56 self::assertSame([Truth::False, true], [$truth->truth, $truth->negated]);
57 }
58
59 public function testBooleanLowersAChainOfNullTestsAndComparisons(): void
60 {
61 $platform = new Platform();
62 $profile = $platform->profile('mysql-5.7.44', null, ParameterStyle::Native);
63 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
64 $expression = (new ConditionRule($lowering))->boolean($platform->parser($profile)->parse('SELECT 1 = 2 IS NULL <=> 3')->find('bool_pri')[0]);
65
66 self::assertInstanceOf(Comparison::class, $expression);
67 self::assertSame(ComparisonOperator::NullSafeEqual, $expression->operator);
68 self::assertInstanceOf(NullTest::class, $expression->left);
69 }
70
71 public function testApplyLowersAQuantifiedComparison(): void
72 {
73 $platform = new Platform();
74 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
75 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
76 $tree = $platform->parser($profile)->parse('SELECT 1 < ALL (SELECT 2)');
77 $expression = (new ConditionRule($lowering))->apply($platform->productions($profile)->form($tree->find('bool_pri')[0]), new NumberLiteral('1'));
78
79 self::assertInstanceOf(QuantifiedComparison::class, $expression);
80 self::assertSame([ComparisonOperator::Less, Quantifier::All], [$expression->operator, $expression->quantifier]);
81 }
82
83 public function testNegatedNullLowersIsNotNull(): void
84 {
85 $platform = new Platform();
86 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
87 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
88 $form = $platform->productions($profile)->form($platform->parser($profile)->parse('SELECT 1 IS NOT NULL')->find('bool_pri')[0]);
89
90 self::assertTrue((new ConditionRule($lowering))->negatedNull($form, new NumberLiteral('1'))->negated);
91 }
92
93 public function testOperatorLowersTheComparisonOperators(): void
94 {
95 $platform = new Platform();
96 $profile = $platform->profile('mysql-5.6.51', null, ParameterStyle::Native);
97 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
98
99 self::assertSame(ComparisonOperator::GreaterOrEqual, (new ConditionRule($lowering))->operator($platform->parser($profile)->parse('SELECT 1 >= 2')->find('comp_op')[0]));
100 }
101
102 public function testQuantifierLowersSomeAsAny(): void
103 {
104 $platform = new Platform();
105 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
106 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
107
108 self::assertSame(Quantifier::Any, (new ConditionRule($lowering))->quantifier($platform->parser($profile)->parse('SELECT 1 = SOME (SELECT 2)')->find('all_or_any')[0]));
109 }
110
111 public function testSynonymRejectsANodeThatIsNoLogicalKeyword(): void
112 {
113 $platform = new Platform();
114 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
115 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
116
117 $this->expectExceptionMessage('No semantic rule is implemented for: comp_op: EQ');
118
119 (new ConditionRule($lowering))->synonym($platform->parser($profile)->parse('SELECT 1 = 2')->find('comp_op')[0]);
120 }
121}
122