packages/sql-semantics-mysql/tests/Unit/Statement/Expression/NullTestTest.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\Nullability;
15use SqlSemantics\Platform\MySql\Platform;
16use SqlSemantics\Platform\MySql\Semantics;
17use SqlSemantics\Platform\MySql\Statement\Expression\NullTest;
18use SqlSemantics\Platform\MySql\Statement\Expression\Truth;
19use SqlSemantics\Platform\MySql\Statement\Expression\TruthTest;
20use SqlSemantics\Platform\MySql\Statement\Literal\NullLiteral;
21use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
22
23#[CoversClass(NullTest::class)]
24#[Medium]
25final class NullTestTest extends TestCase
26{
27 public function testDeriveScalarIsNeverNull(): void
28 {
29 $platform = new Platform();
30 $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
31
32 self::assertSame(Nullability::NotNull, $derivation->scalar(new NullTest(new NullLiteral()), $derivation->environment())->nullability);
33 }
34
35 public function testDeriveScalarReportsARowOperand(): void
36 {
37 $operation = (new Semantics())->inspect('SELECT (1, 2) IS NULL');
38
39 self::assertSame(['Operand should contain 1 column(s), not 2.'], array_map(static fn ($diagnostic): string => $diagnostic->message(), $operation->facts->diagnostics));
40 }
41
42 public function testRenderChainsTestsToTheLeft(): void
43 {
44 $platform = new Platform();
45 $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
46 (new NullTest(new NullTest(new NumberLiteral('1')), true))->render($out);
47
48 self::assertSame('1 IS NULL IS NOT NULL', (new Lexical())->join($out->pieces()));
49 }
50
51 public function testATruthTestOperandIsRejected(): void
52 {
53 $this->expectExceptionMessage('The operand of a NULL test needs a grouping to keep its place.');
54
55 new NullTest(new TruthTest(new NumberLiteral('1'), Truth::True));
56 }
57}
58