packages/sql-semantics-postgres/tests/Unit/Statement/Object/Reference/CastPairTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Object\Reference;
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\Rendering\Lexical;
14use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
16use SqlSemantics\Platform\PostgreSql\Platform;
17use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
18use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
19use SqlSemantics\Platform\PostgreSql\Statement\Object\Reference\CastPair;
20use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
21use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
22
23#[CoversClass(CastPair::class)]
24#[Small]
25final class CastPairTest extends TestCase
26{
27    public function testDeriveClauseDerivesBothTypes(): void
28    {
29        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
30        (new CastPair(new TypeName(new NamedDesignation(new DottedName([new Name('int4')]))), new TypeName(new NamedDesignation(new DottedName([new Name('text')])))))->deriveClause($derivation, $derivation->environment());
31        self::assertSame([], $derivation->facts()->diagnostics);
32    }
33
34    public function testRenderWritesTheParenthesizedPair(): void
35    {
36        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
37        (new CastPair(new TypeName(new NamedDesignation(new DottedName([new Name('int4')]))), new TypeName(new NamedDesignation(new DottedName([new Name('text')])))))->render($out);
38        self::assertSame('(int4 AS text)', (new Lexical())->join($out->pieces()));
39    }
40}
41