packages/sql-semantics-postgres/tests/Unit/Statement/Object/Attribute/IntegerArgumentTest.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\Platform;
16use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
17use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
18use SqlSemantics\Platform\PostgreSql\Statement\Literal\NumericConstant;
19use SqlSemantics\Platform\PostgreSql\Statement\Literal\SignedNumber;
20use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\IntegerArgument;
21use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Reading;
22
23#[CoversClass(IntegerArgument::class)]
24#[Small]
25final class IntegerArgumentTest extends TestCase
26{
27    public function testValueKeepsTheSign(): void
28    {
29        self::assertSame(-16, (new IntegerArgument(new SignedNumber(true, new IntegerConstant('16'))))->value());
30    }
31
32    public function testFitsOnlyAnIntegerReading(): void
33    {
34        $integer = new IntegerArgument(new SignedNumber(false, new IntegerConstant('1')));
35        self::assertSame([true, false], [$integer->fits(Reading::Integer), $integer->fits(Reading::Length)]);
36    }
37
38    public function testDeriveClauseRecordsNothing(): void
39    {
40        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
41        (new IntegerArgument(new SignedNumber(false, new IntegerConstant('1'))))->deriveClause($derivation, $derivation->environment());
42        self::assertSame([], $derivation->facts()->diagnostics);
43    }
44
45    public function testRenderWritesTheNumber(): void
46    {
47        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
48        (new IntegerArgument(new SignedNumber(true, new IntegerConstant('2'))))->render($out);
49        self::assertSame('- 2', (new Lexical())->join($out->pieces()));
50    }
51
52    public function testRejectsANumberWithAPoint(): void
53    {
54        $this->expectExceptionMessage('An integer attribute is an integer that fits in 32 bits.');
55        new IntegerArgument(new SignedNumber(false, new NumericConstant('1.0')));
56    }
57}
58