packages/sql-semantics-postgres/tests/Unit/Statement/Object/Attribute/TextArgumentTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Object\Attribute;
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\Literal\Constant;
19use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
20use SqlSemantics\Platform\PostgreSql\Statement\Literal\SignedNumber;
21use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
22use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
23use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Reading;
24use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\TextArgument;
25use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
26use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
27
28#[CoversClass(TextArgument::class)]
29#[Small]
30final class TextArgumentTest extends TestCase
31{
32 public function testFitsOnlyATextReading(): void
33 {
34 $text = new TextArgument(new StringConstant('x'));
35 self::assertSame([true, false], [$text->fits(Reading::Text), $text->fits(Reading::Type)]);
36 }
37
38 public function testDeriveClauseDerivesTheModifiersOfATypeName(): void
39 {
40 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
41 $modifier = new Constant(new IntegerConstant('1'));
42 (new TextArgument(new TypeName(new NamedDesignation(new DottedName([new Name('f')]), [$modifier]))))->deriveClause($derivation, $derivation->environment());
43 self::assertTrue($derivation->facts()->covers($modifier));
44 }
45
46 public function testRenderWritesTheValueAsWritten(): void
47 {
48 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
49 (new TextArgument(new SignedNumber(false, new IntegerConstant('0'))))->render($out);
50 self::assertSame('0', (new Lexical())->join($out->pieces()));
51 }
52}
53