packages/sql-semantics-postgres/tests/Unit/Statement/Object/Attribute/LengthArgumentTest.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\IntegerConstant;
19use SqlSemantics\Platform\PostgreSql\Statement\Literal\SignedNumber;
20use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
21use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
22use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\LengthArgument;
23use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Reading;
24use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
25use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
26
27#[CoversClass(LengthArgument::class)]
28#[Small]
29final class LengthArgumentTest extends TestCase
30{
31    public function testBytesAnswersTheIntegerOrNull(): void
32    {
33        self::assertSame([16, null], [(new LengthArgument(new SignedNumber(false, new IntegerConstant('16'))))->bytes(), (new LengthArgument(new StringConstant('Variable')))->bytes()]);
34    }
35
36    public function testVariableTellsTheWordVariable(): void
37    {
38        self::assertSame([false, true], [(new LengthArgument(new SignedNumber(false, new IntegerConstant('16'))))->variable(), (new LengthArgument(new TypeName(new NamedDesignation(new DottedName([new Name('variable')])))))->variable()]);
39    }
40
41    public function testFitsOnlyALengthReading(): void
42    {
43        $length = new LengthArgument(new StringConstant('variable'));
44        self::assertSame([true, false], [$length->fits(Reading::Length), $length->fits(Reading::Integer)]);
45    }
46
47    public function testDeriveClauseRecordsNothingForANumber(): void
48    {
49        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
50        (new LengthArgument(new SignedNumber(false, new IntegerConstant('4'))))->deriveClause($derivation, $derivation->environment());
51        self::assertSame([], $derivation->facts()->diagnostics);
52    }
53
54    public function testRenderWritesTheLengthAsWritten(): void
55    {
56        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
57        (new LengthArgument(new StringConstant('variable')))->render($out);
58        self::assertSame("'variable'", (new Lexical())->join($out->pieces()));
59    }
60
61    public function testRejectsAnotherWord(): void
62    {
63        $this->expectExceptionMessage('A type length is an integer that fits in 32 bits or the word variable.');
64        new LengthArgument(new StringConstant('fixed'));
65    }
66}
67