packages/sql-semantics-postgres/tests/Unit/Rules/Expression/Typing/OperatorTypingTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Expression\Typing;
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\Fact\ScalarFact;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Missing\UndeclaredRoutine;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Dependent;
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\Typing\OperatorTyping;
22use SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName;
23use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\ArrayOf;
24use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
25
26#[CoversClass(OperatorTyping::class)]
27#[Small]
28final class OperatorTypingTest extends TestCase
29{
30    public function testBinaryTypesAndPropagatesNull(): void
31    {
32        $context = (new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true);
33        $fact = (new OperatorTyping())->binary($context, new OperatorName(new Name('-')), new ScalarFact(new Known(Builtin::Date), Nullability::NotNull), new ScalarFact(new NullOnly(), Nullability::Nullable));
34        self::assertEquals(new ScalarFact(new Known(Builtin::Int4), Nullability::Nullable), $fact);
35    }
36
37    public function testNamedDependsOnAnOperatorOutsideThePath(): void
38    {
39        $context = (new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), ['public', 'pg_catalog'], [], true);
40        self::assertEquals(new Dependent([new UndeclaredRoutine(new QualifiedName(new Name('+')))]), (new OperatorTyping())->named($context, '+', new Known(Builtin::Int4), new Known(Builtin::Int4)));
41    }
42
43    public function testArraysTypesContainment(): void
44    {
45        self::assertSame(Builtin::Bool, (new OperatorTyping())->arrays('@>', new ArrayOf(Builtin::Int4), new ArrayOf(Builtin::Int4)));
46    }
47
48    public function testStructuredTypesArrayConcatenation(): void
49    {
50        self::assertEquals(new ArrayOf(Builtin::Int4), (new OperatorTyping())->structured('||', new Known(new ArrayOf(Builtin::Int4)), new Known(Builtin::Int4)));
51    }
52
53    public function testCatalogTypesDateTimeArithmetic(): void
54    {
55        self::assertSame(Builtin::Interval, (new OperatorTyping())->catalog('-', new Known(Builtin::Timestamptz), new Known(Builtin::Timestamptz)));
56    }
57
58    public function testComparableAcceptsMixedNumbers(): void
59    {
60        self::assertSame([true, false], [(new OperatorTyping())->comparable(Builtin::Int2, Builtin::Float8), (new OperatorTyping())->comparable(Builtin::Int4, Builtin::Text)]);
61    }
62
63    public function testBothIsBooleanUnlessAConditionDepends(): void
64    {
65        $dependent = (new OperatorTyping())->undeclared('=');
66        self::assertEquals([new Known(Builtin::Bool), $dependent], [(new OperatorTyping())->both(new Known(Builtin::Bool), new Known(Builtin::Bool)), (new OperatorTyping())->both(new Known(Builtin::Bool), $dependent)]);
67    }
68
69    public function testVisibleNeedsTheCatalogFirst(): void
70    {
71        $context = (new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true);
72        self::assertSame([true, false], [(new OperatorTyping())->visible($context, '+'), (new OperatorTyping())->visible($context, '+', new OperatorName(new Name('+'), [new Name('public')], true))]);
73    }
74
75    public function testUndeclaredNamesTheRoutine(): void
76    {
77        self::assertEquals(new Dependent([new UndeclaredRoutine(new QualifiedName(new Name('<->')))]), (new OperatorTyping())->undeclared('<->'));
78    }
79
80}
81