packages/sql-semantics-postgres/tests/Unit/Statement/Literal/NamedParameterTest.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\Statement\Type\Dependent;
16use SqlSemantics\Platform\PostgreSql\Platform;
17use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
18use SqlSemantics\Platform\PostgreSql\Statement\Literal\NamedParameter;
19
20#[CoversClass(NamedParameter::class)]
21#[Small]
22final class NamedParameterTest extends TestCase
23{
24 public function testOutputNameIsNone(): void
25 {
26 self::assertNull((new NamedParameter('id'))->outputName());
27 }
28
29 public function testDeriveScalarDependsOnTheBoundValue(): void
30 {
31 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
32 $fact = $derivation->scalar(new NamedParameter('id'), $derivation->environment());
33 self::assertInstanceOf(Dependent::class, $fact->type);
34 self::assertSame('the value bound to parameter :id', $fact->type->missing[0]->describe());
35 }
36
37 public function testRenderWritesThePlaceholder(): void
38 {
39 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
40 (new NamedParameter('user_id'))->render($out);
41 self::assertSame(':user_id', (new Lexical())->join($out->pieces()));
42 }
43
44 public function testRejectsANameWithOtherCharacters(): void
45 {
46 $this->expectExceptionMessage('A placeholder name is letters, digits and underscores.');
47 new NamedParameter('a-b');
48 }
49}
50