packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/NamedArgumentTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Invocation;
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\Platform;
17use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
18use SqlSemantics\Platform\PostgreSql\Statement\Invocation\ArgumentSpelling;
19use SqlSemantics\Platform\PostgreSql\Statement\Invocation\NamedArgument;
20use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
21use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
22use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
23
24#[CoversClass(NamedArgument::class)]
25#[Small]
26final class NamedArgumentTest extends TestCase
27{
28 public function testValueAnswersTheValuePassed(): void
29 {
30 $value = new Constant(new IntegerConstant('1'));
31 self::assertSame($value, (new NamedArgument(new Name('a'), $value))->value());
32 }
33
34 public function testNameAnswersTheParameterName(): void
35 {
36 self::assertSame('a', (new NamedArgument(new Name('a'), new NullLiteral()))->name()->value);
37 }
38
39 public function testDeriveClauseDerivesTheValue(): void
40 {
41 $value = new Constant(new IntegerConstant('1'));
42 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
43 (new NamedArgument(new Name('a'), $value))->deriveClause($derivation, $derivation->environment());
44 self::assertTrue($derivation->facts()->covers($value));
45 }
46
47 public function testRenderWritesTheArrowSpelling(): void
48 {
49 $argument = new NamedArgument(new Name('days'), new Constant(new IntegerConstant('1')));
50 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
51 $argument->render($out);
52 self::assertSame('days => 1', (new Lexical())->join($out->pieces()));
53 }
54
55 public function testRenderWritesTheAssignmentSpellingAndQuotesAColumnNameKeyword(): void
56 {
57 $argument = new NamedArgument(new Name('coalesce'), new Constant(new IntegerConstant('1')), ArgumentSpelling::Assignment);
58 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
59 $argument->render($out);
60 self::assertSame('"coalesce" := 1', (new Lexical())->join($out->pieces()));
61 }
62}
63