packages/sql-semantics-postgres/tests/Unit/Statement/Expression/Predicate/OverlapsTest.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\Type\Invalid;
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\Constructor\RowConstructor;
21use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\RowSpelling;
22use SqlSemantics\Platform\PostgreSql\Statement\Expression\Predicate\Overlaps;
23use SqlSemantics\Platform\PostgreSql\Statement\Expression\Problem\RowArity;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
25use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
26
27#[CoversClass(Overlaps::class)]
28#[Small]
29final class OverlapsTest extends TestCase
30{
31    public function testDeriveScalarIsBooleanAndNullableWithANullField(): void
32    {
33        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
34        $fact = $derivation->scalar(new Overlaps(new RowConstructor([new NullLiteral(), new NullLiteral()], RowSpelling::Implicit), new RowConstructor([new NullLiteral(), new NullLiteral()])), $derivation->environment());
35        self::assertEquals(new Known(Builtin::Bool), $fact->type);
36        self::assertSame(Nullability::Nullable, $fact->nullability);
37    }
38
39    public function testDeriveScalarReportsAPeriodOfOneField(): void
40    {
41        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
42        $fact = $derivation->scalar(new Overlaps(new RowConstructor([new NullLiteral()]), new RowConstructor([new NullLiteral(), new NullLiteral()])), $derivation->environment());
43        self::assertInstanceOf(Invalid::class, $fact->type);
44        self::assertInstanceOf(RowArity::class, $fact->type->cause);
45    }
46
47    public function testRenderWritesOverlaps(): void
48    {
49        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
50        (new Overlaps(new RowConstructor([new NullLiteral(), new NullLiteral()], RowSpelling::Implicit), new RowConstructor([])))->render($out);
51        self::assertSame('(NULL, NULL) OVERLAPS ROW ()', (new Lexical())->join($out->pieces()));
52    }
53}
54