packages/sql-semantics-postgres/tests/Unit/Rules/Expression/UnificationTest.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\Contract\GrammarRelease;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Missing\UnboundParameter;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Dependent;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Invalid;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\NullOnly;
18use SqlSemantics\Platform\PostgreSql\Platform;
19use SqlSemantics\Platform\PostgreSql\Rules\Expression\Unification;
20use SqlSemantics\Platform\PostgreSql\Statement\Expression\Problem\TypeConflict;
21use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
22
23#[CoversClass(Unification::class)]
24#[Small]
25final class UnificationTest extends TestCase
26{
27    public function testResolvePromotesWithinACategory(): void
28    {
29        $context = (new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true);
30        self::assertEquals(new Known(Builtin::Float8), (new Unification())->resolve($context, [new Known(Builtin::Int4), new NullOnly(), new Known(Builtin::Float8), new Known(Builtin::Numeric)]));
31    }
32
33    public function testResolveGivesTextForUnknownInputs(): void
34    {
35        $context = (new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true);
36        self::assertEquals(new Known(Builtin::Text), (new Unification())->resolve($context, [new Known(Builtin::Unknown), new NullOnly()]));
37    }
38
39    public function testResolveDependsOnMissingInputs(): void
40    {
41        $context = (new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true);
42        $missing = new Dependent([new UnboundParameter('$1')]);
43        self::assertEquals($missing, (new Unification())->resolve($context, [new Known(Builtin::Int4), $missing]));
44    }
45
46    public function testCandidateReportsDifferentCategories(): void
47    {
48        $result = (new Unification())->candidate([new Known(Builtin::Int4), new Known(Builtin::Bool)], 'CASE');
49        self::assertInstanceOf(Invalid::class, $result);
50        self::assertEquals(new TypeConflict('CASE', 'integer', 'boolean'), $result->cause);
51    }
52
53    public function testNullabilityIsNullableWhenAnyInputIs(): void
54    {
55        self::assertSame(Nullability::Nullable, (new Unification())->nullability([Nullability::NotNull, Nullability::Nullable, Nullability::Dependent]));
56    }
57
58    public function testDistinctRemovesRepeatedInputs(): void
59    {
60        self::assertCount(1, (new Unification())->distinct([new UnboundParameter('$1'), new UnboundParameter('$1')]));
61    }
62}
63