packages/sql-semantics-postgres/tests/Unit/Rules/Invocation/CoercionsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Invocation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Missing\UndeclaredRoutine;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Dependent;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\NullOnly;
16use SqlSemantics\Platform\PostgreSql\Rules\Invocation\Coercions;
17use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\ArrayOf;
18use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
19use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Parameterized;
20
21#[CoversClass(Coercions::class)]
22#[Small]
23final class CoercionsTest extends TestCase
24{
25 public function testKeyAnswersTheLookupType(): void
26 {
27 $coercions = new Coercions();
28 self::assertSame(['unknown', 'int4', null], [$coercions->key(new NullOnly()), $coercions->key(new Known(Builtin::Int4)), $coercions->key(new Dependent([new UndeclaredRoutine(new QualifiedName(new Name('f')))]))]);
29 }
30
31 public function testDescriptorDropsModifiersAndNamesArrays(): void
32 {
33 $coercions = new Coercions();
34 self::assertSame(['varchar', 'int4[]'], [$coercions->descriptor(new Parameterized(Builtin::Varchar, 4)), $coercions->descriptor(new ArrayOf(Builtin::Int4))]);
35 }
36
37 public function testImplicitFollowsTheCastTable(): void
38 {
39 $coercions = new Coercions();
40 self::assertSame([true, false, true, true], [$coercions->implicit('int4', 'numeric'), $coercions->implicit('numeric', 'int4'), $coercions->implicit('unknown', 'date'), $coercions->implicit('xml', 'any')]);
41 }
42
43 public function testTypeAnswersTheDescriptorOfAName(): void
44 {
45 self::assertEquals(new ArrayOf(Builtin::Text), (new Coercions())->type('text[]'));
46 }
47}
48