packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Syntax/TreatTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Invocation\Syntax;
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\Model\Statement\Identifier\QualifiedName;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Missing\UndeclaredRoutine;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Dependent;
19use SqlSemantics\Platform\PostgreSql\Platform;
20use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
21use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Syntax\Treat;
22use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
23use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
25use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\KeywordDesignation;
26use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\TypeKeyword;
27use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
28
29#[CoversClass(Treat::class)]
30#[Small]
31final class TreatTest extends TestCase
32{
33 public function testOutputNameIsTheCatalogNameOfTheType(): void
34 {
35 self::assertSame('int4', (new Treat(new NullLiteral(), new TypeName(new KeywordDesignation(TypeKeyword::Int))))->outputName()->value);
36 }
37
38 public function testDeriveScalarDependsOnTheConversionFunction(): void
39 {
40 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
41 $fact = $derivation->scalar(new Treat(new Constant(new IntegerConstant('1')), new TypeName(new KeywordDesignation(TypeKeyword::Int))), $derivation->environment());
42 self::assertEquals(new Dependent([new UndeclaredRoutine(new QualifiedName(new Name('int4'), new Name('pg_catalog')))]), $fact->type);
43 }
44
45 public function testRenderWritesTheValueAndTheType(): void
46 {
47 $treat = new Treat(new Constant(new IntegerConstant('1')), new TypeName(new KeywordDesignation(TypeKeyword::Integer)));
48 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
49 $treat->render($out);
50 self::assertSame('TREAT(1 AS INTEGER)', (new Lexical())->join($out->pieces()));
51 }
52}
53