packages/sql-semantics-mysql/tests/Unit/Statement/Expression/TruthTestTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
11use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
12use SqlSemantics\Platform\MySql\Model\Rendering\Lexical;
13use SqlSemantics\Platform\MySql\Model\Rendering\Output;
14use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
16use SqlSemantics\Platform\MySql\Platform;
17use SqlSemantics\Platform\MySql\Statement\Expression\Comparison;
18use SqlSemantics\Platform\MySql\Statement\Expression\ComparisonOperator;
19use SqlSemantics\Platform\MySql\Statement\Expression\Truth;
20use SqlSemantics\Platform\MySql\Statement\Expression\TruthTest;
21use SqlSemantics\Platform\MySql\Statement\Literal\NullLiteral;
22use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
23use SqlSemantics\Platform\MySql\Statement\Type\Integral;
24use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
25
26#[CoversClass(TruthTest::class)]
27#[Medium]
28final class TruthTestTest extends TestCase
29{
30    public function testDeriveScalarIsNeverNull(): void
31    {
32        $platform = new Platform();
33        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
34        $fact = $derivation->scalar(new TruthTest(new NullLiteral(), Truth::Unknown), $derivation->environment());
35
36        self::assertEquals([new Known(new Integral(IntegralKind::BigInt)), Nullability::NotNull], [$fact->type, $fact->nullability]);
37    }
38
39    public function testRenderWritesTheNegatedTest(): void
40    {
41        $platform = new Platform();
42        $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
43        (new TruthTest(new Comparison(ComparisonOperator::Equal, new NumberLiteral('1'), new NumberLiteral('2')), Truth::False, true))->render($out);
44
45        self::assertSame('1 = 2 IS NOT FALSE', (new Lexical())->join($out->pieces()));
46    }
47
48    public function testATruthTestOperandIsRejected(): void
49    {
50        $this->expectExceptionMessage('The operand of a truth test needs a grouping to keep its place.');
51
52        new TruthTest(new TruthTest(new NumberLiteral('1'), Truth::True), Truth::True);
53    }
54}
55