packages/sql-semantics-mysql/tests/Unit/Statement/Expression/Subquery/ExistsTest.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\Subquery\Exists;
19
20#[CoversClass(Exists::class)]
21#[Medium]
22final class ExistsTest extends TestCase
23{
24    public function testDeriveScalarIsNeverNull(): void
25    {
26        $platform = new Platform();
27        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
28        $exists = (new Lowering($platform->productions($profile), new Leaves(), $profile))->expressions->expression($platform->parser($profile)->parse('SELECT EXISTS (SELECT NULL)')->find('expr')[0]);
29        $derivation = new Derivation($platform->context($profile, null, [], true));
30
31        self::assertInstanceOf(Exists::class, $exists);
32        self::assertSame(Nullability::NotNull, $derivation->scalar($exists, $derivation->environment())->nullability);
33    }
34
35    public function testRenderWritesExistsAndTheQuery(): void
36    {
37        $platform = new Platform();
38        $profile = $platform->profile('mysql-5.6.51', null, ParameterStyle::Native);
39        $exists = (new Lowering($platform->productions($profile), new Leaves(), $profile))->expressions->expression($platform->parser($profile)->parse('SELECT exists(select 1)')->find('expr')[0]);
40        $out = new Output($platform->codec($profile));
41        $exists->render($out);
42
43        self::assertSame('EXISTS (SELECT 1)', (new Lexical())->join($out->pieces()));
44    }
45}
46