packages/sql-semantics-postgres/tests/Unit/Statement/Expression/Predicate/NullTestTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Predicate;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
13use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
14use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
18use SqlSemantics\Platform\PostgreSql\Platform;
19use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
20use SqlSemantics\Platform\PostgreSql\Statement\Expression\BinaryOperation;
21use SqlSemantics\Platform\PostgreSql\Statement\Expression\Operator\Negation;
22use SqlSemantics\Platform\PostgreSql\Statement\Expression\Predicate\NullTest;
23use SqlSemantics\Platform\PostgreSql\Statement\Expression\Predicate\NullTestSpelling;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
25use SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName;
26use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
27
28#[CoversClass(NullTest::class)]
29#[Small]
30final class NullTestTest extends TestCase
31{
32 public function testDeriveScalarIsANonNullBoolean(): void
33 {
34 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
35 $fact = $derivation->scalar(new NullTest(new NullLiteral(), true), $derivation->environment());
36 self::assertEquals(new Known(Builtin::Bool), $fact->type);
37 self::assertSame(Nullability::NotNull, $fact->nullability);
38 }
39
40 public function testRenderWritesBothSpellings(): void
41 {
42 $keywords = new Output(new Codec(GrammarRelease::PostgreSql172));
43 (new NullTest(new NullLiteral(), true))->render($keywords);
44 $postfix = new Output(new Codec(GrammarRelease::PostgreSql172));
45 (new NullTest(new NullTest(new NullLiteral(), false, NullTestSpelling::Postfix), true, NullTestSpelling::Postfix))->render($postfix);
46 self::assertSame(['NULL IS NOT NULL', 'NULL ISNULL NOTNULL'], [(new Lexical())->join($keywords->pieces()), (new Lexical())->join($postfix->pieces())]);
47 }
48
49 public function testAcceptsAComparison(): void
50 {
51 $comparison = new BinaryOperation(new OperatorName(new Name('=')), new NullLiteral(), new NullLiteral());
52 self::assertSame($comparison, (new NullTest($comparison, false))->operand);
53 }
54
55 public function testRejectsANegation(): void
56 {
57 $this->expectExceptionMessage('The tested value needs parentheses to keep its place.');
58 new NullTest(new Negation(new NullLiteral()), false);
59 }
60}
61