packages/sql-semantics-postgres/tests/Unit/Statement/Literal/PositionalParameterTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Literal;
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\Resolution\Environment;
16use SqlSemantics\Platform\PostgreSql\Model\Resolution\VisibleRelation;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Dependent;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Invalid;
19use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
20use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
21use SqlSemantics\Platform\PostgreSql\Platform;
22use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
23use SqlSemantics\Platform\PostgreSql\Statement\Literal\PositionalParameter;
24use SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Prepared\PreparedParameters;
25use SqlSemantics\Platform\PostgreSql\Statement\Routine\FunctionParameter;
26use SqlSemantics\Platform\PostgreSql\Statement\Routine\RoutineParameters;
27use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
28use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\KeywordDesignation;
29use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\TypeKeyword;
30use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
31
32#[CoversClass(PositionalParameter::class)]
33#[Small]
34final class PositionalParameterTest extends TestCase
35{
36 public function testBoundIsTheNumberWithinTheLimitOfTheServer(): void
37 {
38 self::assertSame(1, (new PositionalParameter('1'))->bound(GrammarRelease::PostgreSql172));
39 self::assertNull((new PositionalParameter('0'))->bound(GrammarRelease::PostgreSql172));
40 self::assertNull((new PositionalParameter('268435456'))->bound(GrammarRelease::PostgreSql172));
41 }
42
43 public function testBoundReducesTheNumberToThirtyTwoBitsInPostgreSql16(): void
44 {
45 self::assertSame(1, (new PositionalParameter('4294967297'))->bound(GrammarRelease::PostgreSql166));
46 self::assertNull((new PositionalParameter('4294967295'))->bound(GrammarRelease::PostgreSql166));
47 self::assertNull((new PositionalParameter('99999999999999999999'))->bound(GrammarRelease::PostgreSql166));
48 }
49
50 public function testOutputNameIsNone(): void
51 {
52 self::assertNull((new PositionalParameter('1'))->outputName());
53 }
54
55 public function testDeriveScalarDependsOnTheBoundValue(): void
56 {
57 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
58 $fact = $derivation->scalar(new PositionalParameter('2'), $derivation->environment());
59 self::assertInstanceOf(Dependent::class, $fact->type);
60 self::assertSame('the value bound to parameter $2', $fact->type->missing[0]->describe());
61 self::assertSame(Nullability::Dependent, $fact->nullability);
62 }
63
64 public function testDeriveScalarReportsParameterZero(): void
65 {
66 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
67 $fact = $derivation->scalar(new PositionalParameter('0'), $derivation->environment());
68 self::assertInstanceOf(Invalid::class, $fact->type);
69 self::assertSame('There is no parameter $0.', $derivation->facts()->diagnostics[0]->message());
70 }
71
72 public function testDeriveScalarHasTheDeclaredTypeOfItsPosition(): void
73 {
74 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
75 $parameters = new PreparedParameters([new TypeName(new KeywordDesignation(TypeKeyword::Integer))]);
76 $environment = new Environment($derivation->context, new Environment($derivation->context, null, [new VisibleRelation($parameters, $derivation->relation($parameters, $derivation->environment())->shape)]));
77 $fact = $derivation->scalar(new PositionalParameter('1'), $environment);
78 self::assertInstanceOf(Known::class, $fact->type);
79 self::assertSame([Builtin::Int4, Nullability::Nullable], [$fact->type->descriptor, $fact->nullability]);
80 self::assertInstanceOf(Dependent::class, $derivation->scalar(new PositionalParameter('2'), $environment)->type);
81 }
82
83 public function testDeriveScalarReportsAParameterARoutineDoesNotDeclare(): void
84 {
85 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
86 $parameters = new RoutineParameters([new FunctionParameter(new TypeName(new KeywordDesignation(TypeKeyword::Integer)))]);
87 $environment = new Environment($derivation->context, null, [new VisibleRelation($parameters, $derivation->relation($parameters, $derivation->environment())->shape)]);
88 self::assertInstanceOf(Known::class, $derivation->scalar(new PositionalParameter('1'), $environment)->type);
89 self::assertInstanceOf(Invalid::class, $derivation->scalar(new PositionalParameter('2'), $environment)->type);
90 self::assertSame('There is no parameter $2.', $derivation->facts()->diagnostics[0]->message());
91 }
92
93 public function testRenderWritesTheMarker(): void
94 {
95 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
96 (new PositionalParameter('12'))->render($out);
97 self::assertSame('$12', (new Lexical())->join($out->pieces()));
98 }
99
100 public function testRejectsLeadingZeros(): void
101 {
102 $this->expectExceptionMessage('A parameter number is canonical decimal digits.');
103 new PositionalParameter('01');
104 }
105}
106