packages/sql-semantics-postgres/tests/Unit/Statement/Option/DefinitionTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Option;
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\Option\Definition;
22use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\DecimalDesignation;
23use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\DecimalKeyword;
24use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
25
26#[CoversClass(Definition::class)]
27#[Small]
28final class DefinitionTest extends TestCase
29{
30 public function testDeriveClauseDerivesTheModifiersOfATypeValue(): void
31 {
32 $modifier = new Constant(new IntegerConstant('1'));
33 $definition = new Definition(new Name('element'), new TypeName(new DecimalDesignation(DecimalKeyword::Numeric, [$modifier])));
34 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
35 $definition->deriveClause($derivation, $derivation->environment());
36 self::assertTrue($derivation->facts()->covers($modifier));
37 }
38
39 public function testRenderWritesNamespaceNameAndValue(): void
40 {
41 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
42 (new Definition(new Name('fillfactor'), new SignedNumber(false, new IntegerConstant('70')), new Name('toast')))->render($out);
43 self::assertSame('toast.fillfactor = 70', (new Lexical())->join($out->pieces()));
44 $second = new Output(new Codec(GrammarRelease::PostgreSql172));
45 (new Definition(new Name('select')))->render($second);
46 self::assertSame('select', (new Lexical())->join($second->pieces()));
47 }
48}
49