packages/sql-semantics-postgres/tests/Unit/Statement/Object/Attribute/ChoiceArgumentTest.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\StringConstant;
21use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
22use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Choice\Alignment;
23use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Choice\Parallelism;
24use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\ChoiceArgument;
25use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Reading;
26use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\KeywordDesignation;
27use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
28use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\TypeKeyword;
29use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
30
31#[CoversClass(ChoiceArgument::class)]
32#[Small]
33final class ChoiceArgumentTest extends TestCase
34{
35    public function testFitsTheReadingOfItsWordSet(): void
36    {
37        $choice = new ChoiceArgument(new StringConstant('safe'), Parallelism::Safe);
38        self::assertSame([true, false], [$choice->fits(Reading::Parallelism), $choice->fits(Reading::Alignment)]);
39    }
40
41    public function testDeriveClauseDerivesTheModifiersOfATypeName(): void
42    {
43        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
44        $modifier = new Constant(new IntegerConstant('1'));
45        (new ChoiceArgument(new TypeName(new NamedDesignation(new DottedName([new Name('int4')]), [$modifier])), Alignment::Int))->deriveClause($derivation, $derivation->environment());
46        self::assertTrue($derivation->facts()->covers($modifier));
47    }
48
49    public function testRenderWritesTheValueAsWritten(): void
50    {
51        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
52        (new ChoiceArgument(new TypeName(new KeywordDesignation(TypeKeyword::Integer)), Alignment::Int))->render($out);
53        self::assertSame('INTEGER', (new Lexical())->join($out->pieces()));
54    }
55
56    public function testRejectsAMemberTheTextDoesNotName(): void
57    {
58        $this->expectExceptionMessage('The member of a choice is the one the written text names.');
59        new ChoiceArgument(new StringConstant('SAFE'), Parallelism::Safe);
60    }
61}
62