packages/sql-semantics-mysql/tests/Unit/Statement/Expression/Subquery/InQueryTest.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\Comparison;
19use SqlSemantics\Platform\MySql\Statement\Expression\ComparisonOperator;
20use SqlSemantics\Platform\MySql\Statement\Expression\Subquery\InQuery;
21use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
22
23#[CoversClass(InQuery::class)]
24#[Medium]
25final class InQueryTest extends TestCase
26{
27 public function testDeriveScalarIsNullWhenAColumnCanBeAndReportsAWidthMismatch(): void
28 {
29 $platform = new Platform();
30 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
31 $test = (new Lowering($platform->productions($profile), new Leaves(), $profile))->expressions->expression($platform->parser($profile)->parse('SELECT 1 IN (SELECT NULL, 2)')->find('expr')[0]);
32 $derivation = new Derivation($platform->context($profile, null, [], true));
33
34 self::assertInstanceOf(InQuery::class, $test);
35 self::assertSame(Nullability::Nullable, $derivation->scalar($test, $derivation->environment())->nullability);
36 self::assertSame('Operand should contain 1 column(s), not 2.', $derivation->facts()->diagnostics[0]->message());
37 }
38
39 public function testRenderWritesTheNegatedTest(): void
40 {
41 $platform = new Platform();
42 $profile = $platform->profile('mysql-5.7.44', null, ParameterStyle::Native);
43 $test = (new Lowering($platform->productions($profile), new Leaves(), $profile))->expressions->expression($platform->parser($profile)->parse('SELECT 1 not in (select 1)')->find('expr')[0]);
44 $out = new Output($platform->codec($profile));
45 $test->render($out);
46
47 self::assertSame('1 NOT IN (SELECT 1)', (new Lexical())->join($out->pieces()));
48 }
49
50 public function testAComparisonOperandIsRejected(): void
51 {
52 $platform = new Platform();
53 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
54 $test = (new Lowering($platform->productions($profile), new Leaves(), $profile))->expressions->expression($platform->parser($profile)->parse('SELECT 1 IN (SELECT 1)')->find('expr')[0]);
55 self::assertInstanceOf(InQuery::class, $test);
56
57 $this->expectExceptionMessage('The operand of IN needs a grouping to keep its place.');
58
59 new InQuery(new Comparison(ComparisonOperator::Equal, new NumberLiteral('1'), new NumberLiteral('2')), $test->query);
60 }
61}
62