packages/sql-semantics-postgres/tests/Unit/Rules/Expression/OperandChecksTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Expression;
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\Statement\Fact\ScalarFact;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Invalid;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
19use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\NullOnly;
20use SqlSemantics\Platform\PostgreSql\Platform;
21use SqlSemantics\Platform\PostgreSql\Rules\Expression\OperandChecks;
22use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\Composite;
23use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\ArrayOf;
24use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
25
26#[CoversClass(OperandChecks::class)]
27#[Small]
28final class OperandChecksTest extends TestCase
29{
30 public function testBooleanAcceptsUnknownAndReportsIntegers(): void
31 {
32 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
33 self::assertNull((new OperandChecks())->boolean($derivation, new Known(Builtin::Unknown), 'AND'));
34 self::assertInstanceOf(Invalid::class, (new OperandChecks())->boolean($derivation, new Known(Builtin::Int4), 'AND'));
35 }
36
37 public function testAcceptedIsBooleanForAnAcceptedType(): void
38 {
39 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
40 self::assertEquals(new Known(Builtin::Bool), (new OperandChecks())->accepted($derivation, new Known(Builtin::Xml), [Builtin::Xml], 'IS DOCUMENT'));
41 }
42
43 public function testCollatableAcceptsArraysOfText(): void
44 {
45 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
46 $type = new Known(new ArrayOf(Builtin::Text));
47 self::assertSame($type, (new OperandChecks())->collatable($derivation, $type));
48 self::assertInstanceOf(Invalid::class, (new OperandChecks())->collatable($derivation, new Known(Builtin::Bool)));
49 }
50
51 public function testNullabilityCountsNullAsNullable(): void
52 {
53 self::assertSame(Nullability::Nullable, (new OperandChecks())->nullability([new ScalarFact(new Known(Builtin::Int4), Nullability::NotNull), new ScalarFact(new NullOnly(), Nullability::Nullable)]));
54 }
55
56 public function testFieldsReadsTheFieldsOfRows(): void
57 {
58 $row = new ScalarFact(new Known(new Composite([new OutputSlot(new Name('f1'), new NullOnly(), Nullability::Nullable)])), Nullability::NotNull);
59 self::assertSame(Nullability::Nullable, (new OperandChecks())->fields([$row]));
60 }
61}
62