packages/sql-semantics-postgres/tests/Unit/Statement/Expression/CastTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression;
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\Type\Known;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
18use SqlSemantics\Platform\PostgreSql\Platform;
19use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
20use SqlSemantics\Platform\PostgreSql\Statement\Expression\BinaryOperation;
21use SqlSemantics\Platform\PostgreSql\Statement\Expression\Cast;
22use SqlSemantics\Platform\PostgreSql\Statement\Expression\CastSpelling;
23use SqlSemantics\Platform\PostgreSql\Statement\Expression\ColumnReference;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
25use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
26use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
27use SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName;
28use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
29use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\KeywordDesignation;
30use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\TypeKeyword;
31use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
32
33#[CoversClass(Cast::class)]
34#[Small]
35final class CastTest extends TestCase
36{
37 public function testOutputNameIsTheOperandNameWhenFirm(): void
38 {
39 self::assertSame('a', (new Cast(new ColumnReference([new Name('a')]), new TypeName(new KeywordDesignation(TypeKeyword::Bigint)), CastSpelling::Operator))->outputName()?->value);
40 }
41
42 public function testOutputNameIsTheTypeNameOtherwise(): void
43 {
44 self::assertSame('int8', (new Cast(new NullLiteral(), new TypeName(new KeywordDesignation(TypeKeyword::Bigint)), CastSpelling::Function))->outputName()?->value);
45 }
46
47 public function testDeriveScalarHasTheTargetTypeAndTheOperandNullability(): void
48 {
49 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
50 $fact = $derivation->scalar(new Cast(new NullLiteral(), new TypeName(new KeywordDesignation(TypeKeyword::Bigint)), CastSpelling::Operator), $derivation->environment());
51 self::assertEquals(new Known(Builtin::Int8), $fact->type);
52 self::assertSame(Nullability::Nullable, $fact->nullability);
53 }
54
55 public function testRenderWritesBothSpellings(): void
56 {
57 $operator = new Output(new Codec(GrammarRelease::PostgreSql172));
58 (new Cast(new Constant(new IntegerConstant('1')), new TypeName(new KeywordDesignation(TypeKeyword::Bigint)), CastSpelling::Operator))->render($operator);
59 $function = new Output(new Codec(GrammarRelease::PostgreSql172));
60 (new Cast(new Constant(new IntegerConstant('1')), new TypeName(new KeywordDesignation(TypeKeyword::Bigint)), CastSpelling::Function))->render($function);
61 self::assertSame(['1 :: BIGINT', 'CAST (1 AS BIGINT)'], [(new Lexical())->join($operator->pieces()), (new Lexical())->join($function->pieces())]);
62 }
63
64 public function testRejectsAnOperandThatBindsWeaker(): void
65 {
66 $sum = new BinaryOperation(new OperatorName(new Name('+')), new Constant(new IntegerConstant('1')), new Constant(new IntegerConstant('2')));
67 $this->expectExceptionMessage('The operand of :: needs parentheses to keep its place.');
68 new Cast($sum, new TypeName(new KeywordDesignation(TypeKeyword::Bigint)), CastSpelling::Operator);
69 }
70}
71