packages/sql-semantics-postgres/tests/Unit/Statement/Literal/ConstantTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Literal;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
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\Type\Known;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\PostgreSql\Platform;
18use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
19use SqlSemantics\Platform\PostgreSql\Statement\Literal\BitStringConstant;
20use SqlSemantics\Platform\PostgreSql\Statement\Literal\BitStringRadix;
21use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
22use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
23use SqlSemantics\Platform\PostgreSql\Statement\Literal\NumericConstant;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
25use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
26
27#[CoversClass(Constant::class)]
28#[Medium]
29final class ConstantTest extends TestCase
30{
31 public function testBuiltinFollowsTheSizeOfAnInteger(): void
32 {
33 self::assertSame(Builtin::Int4, (new Constant(new IntegerConstant('2147483647')))->builtin());
34 self::assertSame(Builtin::Int8, (new Constant(new NumericConstant('2147483648')))->builtin());
35 self::assertSame(Builtin::Int8, (new Constant(new NumericConstant('0x7FFF_FFFF_FFFF_FFFF')))->builtin());
36 self::assertSame(Builtin::Numeric, (new Constant(new NumericConstant('9223372036854775808')))->builtin());
37 }
38
39 public function testBuiltinReadsTheTextOfANumericConstantThroughTheFacade(): void
40 {
41 $query = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze('SELECT 0x1FFFFFFFFF, 1_000_000_000_000, 0001.50, 1e2');
42 self::assertEquals([new Known(Builtin::Int8), new Known(Builtin::Int8), new Known(Builtin::Numeric), new Known(Builtin::Numeric)], [$query->field(0)->type, $query->field(1)->type, $query->field(2)->type, $query->field(3)->type]);
43 self::assertSame('SELECT 0x1FFFFFFFFF, 1_000_000_000_000, 0001.50, 1e2', $query->toString());
44 }
45
46 public function testBuiltinOfTheOtherConstants(): void
47 {
48 self::assertSame(Builtin::Numeric, (new Constant(new NumericConstant('1.')))->builtin());
49 self::assertSame(Builtin::Unknown, (new Constant(new StringConstant('1')))->builtin());
50 self::assertSame(Builtin::Bit, (new Constant(new BitStringConstant(BitStringRadix::Binary, '1')))->builtin());
51 }
52
53 public function testIntegerValueReadsIntegersAndStringsThatSpellOne(): void
54 {
55 self::assertSame('10', (new Constant(new IntegerConstant('10')))->integerValue());
56 self::assertSame('-5', (new Constant(new StringConstant(' -005 ')))->integerValue());
57 self::assertNull((new Constant(new StringConstant('1.5')))->integerValue());
58 self::assertNull((new Constant(new NumericConstant('1.')))->integerValue());
59 }
60
61 public function testOutputNameIsNone(): void
62 {
63 self::assertNull((new Constant(new IntegerConstant('1')))->outputName());
64 }
65
66 public function testDeriveScalarIsTheCatalogTypeAndNeverNull(): void
67 {
68 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
69 $fact = $derivation->scalar(new Constant(new StringConstant('a')), $derivation->environment());
70 self::assertEquals(new Known(Builtin::Unknown), $fact->type);
71 self::assertSame(Nullability::NotNull, $fact->nullability);
72 }
73
74 public function testRenderWritesTheValue(): void
75 {
76 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
77 (new Constant(new StringConstant('a')))->render($out);
78 self::assertSame("'a'", (new Lexical())->join($out->pieces()));
79 }
80}
81