packages/sql-semantics-postgres/tests/Unit/Rules/Table/Definition/IdentityColumnsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Table\Definition;
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\Identifier\Name;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Missing\UndeclaredDomain;
16use SqlSemantics\Platform\PostgreSql\Platform;
17use SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\IdentityColumns;
18use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\ArrayOf;
19use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
20use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\NamedOnPath;
21
22#[CoversClass(IdentityColumns::class)]
23#[Small]
24final class IdentityColumnsTest extends TestCase
25{
26    public function testCheckReportsATypeOtherThanTheThreeIntegerTypes(): void
27    {
28        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
29        (new IdentityColumns())->check($derivation, Builtin::Text);
30        (new IdentityColumns())->check($derivation, new ArrayOf(Builtin::Int4));
31        self::assertSame(['identity column type must be smallint, integer, or bigint', 'identity column type must be smallint, integer, or bigint'], array_map(static fn ($problem): string => $problem->message(), $derivation->facts()->diagnostics));
32    }
33
34    public function testCheckAdmitsTheIntegerTypesAndTypesKnownByName(): void
35    {
36        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
37        (new IdentityColumns())->check($derivation, Builtin::Int2);
38        (new IdentityColumns())->check($derivation, Builtin::Int8);
39        (new IdentityColumns())->check($derivation, new NamedOnPath(new QualifiedName(new Name('d')), [new UndeclaredDomain(new QualifiedName(new Name('d')))]));
40        (new IdentityColumns())->check($derivation, null);
41        self::assertSame([], $derivation->facts()->diagnostics);
42    }
43}
44