packages/sql-semantics-mysql/tests/Unit/Statement/Expression/Subquery/QuantifiedComparisonTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Subquery;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Lowering\Lowering;
11use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
12use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
13use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
14use SqlSemantics\Platform\MySql\Model\Rendering\Lexical;
15use SqlSemantics\Platform\MySql\Model\Rendering\Output;
16use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\MySql\Platform;
18use SqlSemantics\Platform\MySql\Statement\Expression\ComparisonOperator;
19use SqlSemantics\Platform\MySql\Statement\Expression\Subquery\QuantifiedComparison;
20use SqlSemantics\Platform\MySql\Statement\Expression\Subquery\Quantifier;
21use SqlSemantics\Platform\MySql\Statement\Expression\Truth;
22use SqlSemantics\Platform\MySql\Statement\Expression\TruthTest;
23use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
24
25#[CoversClass(QuantifiedComparison::class)]
26#[Medium]
27final class QuantifiedComparisonTest extends TestCase
28{
29 public function testDeriveScalarIsNotNullForNotNullOperands(): void
30 {
31 $platform = new Platform();
32 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
33 $comparison = (new Lowering($platform->productions($profile), new Leaves(), $profile))->expressions->expression($platform->parser($profile)->parse('SELECT 1 > SOME (SELECT 2)')->find('expr')[0]);
34 $derivation = new Derivation($platform->context($profile, null, [], true));
35
36 self::assertInstanceOf(QuantifiedComparison::class, $comparison);
37 self::assertSame(Quantifier::Any, $comparison->quantifier);
38 self::assertSame(Nullability::NotNull, $derivation->scalar($comparison, $derivation->environment())->nullability);
39 }
40
41 public function testRenderWritesTheOperatorAndTheQuantifier(): void
42 {
43 $platform = new Platform();
44 $profile = $platform->profile('mysql-5.6.51', null, ParameterStyle::Native);
45 $comparison = (new Lowering($platform->productions($profile), new Leaves(), $profile))->expressions->expression($platform->parser($profile)->parse('SELECT 1 <> all (select 2)')->find('expr')[0]);
46 $out = new Output($platform->codec($profile));
47 $comparison->render($out);
48
49 self::assertSame('1 <> ALL (SELECT 2)', (new Lexical())->join($out->pieces()));
50 }
51
52 public function testATruthTestOperandIsRejected(): void
53 {
54 $platform = new Platform();
55 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
56 $comparison = (new Lowering($platform->productions($profile), new Leaves(), $profile))->expressions->expression($platform->parser($profile)->parse('SELECT 1 = ALL (SELECT 2)')->find('expr')[0]);
57 self::assertInstanceOf(QuantifiedComparison::class, $comparison);
58
59 $this->expectExceptionMessage('The operand of a quantified comparison needs a grouping to keep its place.');
60
61 new QuantifiedComparison(new TruthTest(new NumberLiteral('1'), Truth::True), ComparisonOperator::Equal, Quantifier::All, $comparison->query);
62 }
63}
64