packages/sql-semantics-postgres/tests/Unit/Statement/Expression/Operator/DistinctTestTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Operator;
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\Type\Known;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\PostgreSql\Platform;
18use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
19use SqlSemantics\Platform\PostgreSql\Statement\Expression\Operator\DistinctTest;
20use SqlSemantics\Platform\PostgreSql\Statement\Expression\Predicate\NullTest;
21use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
22use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
23use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
24use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
25
26#[CoversClass(DistinctTest::class)]
27#[Small]
28final class DistinctTestTest extends TestCase
29{
30    public function testDeriveScalarIsNeverNull(): void
31    {
32        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
33        $fact = $derivation->scalar(new DistinctTest(new Constant(new IntegerConstant('1')), new NullLiteral(), false), $derivation->environment());
34        self::assertEquals(new Known(Builtin::Bool), $fact->type);
35        self::assertSame(Nullability::NotNull, $fact->nullability);
36    }
37
38    public function testRenderWritesTheNegatedForm(): void
39    {
40        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
41        (new DistinctTest(new NullLiteral(), new NullLiteral(), true))->render($out);
42        self::assertSame('NULL IS NOT DISTINCT FROM NULL', (new Lexical())->join($out->pieces()));
43    }
44
45    public function testRejectsANullTestOnTheRight(): void
46    {
47        $this->expectExceptionMessage('The right operand needs parentheses to keep its place.');
48        new DistinctTest(new NullLiteral(), new NullTest(new NullLiteral(), false), false);
49    }
50}
51