packages/sql-semantics-postgres/tests/Unit/Rules/Access/ObjectIdentifiersTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Access;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Access\ObjectIdentifiers::class)]
12#[Medium]
13final class ObjectIdentifiersTest extends TestCase
14{
15    public function testCheckReportsEveryNumberThatIsNoObjectIdentifier(): void
16    {
17        self::assertSame(['invalid input syntax for type oid: "1.5"', 'invalid input syntax for type oid: "-1e2"', 'value "4294967296" is out of range for type oid', 'value "-2147483649" is out of range for type oid'], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('REVOKE SELECT ON LARGE OBJECT 1.5, -1e2, 16385, 4294967296, -2147483649 FROM joe')->facts->diagnostics));
18    }
19
20    public function testCheckAcceptsTheIdentifiersTheServerReads(): void
21    {
22        self::assertSame([], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('GRANT SELECT ON LARGE OBJECT 0, -7, 2147483647, 4294967295, -2147483648 TO joe')->facts->diagnostics));
23    }
24
25    public function testCheckReadsTheWrittenTextOfANumericConstantAsStrtoulDoes(): void
26    {
27        self::assertSame(['invalid input syntax for type oid: "1_0000000000"', 'invalid input syntax for type oid: "0o40000000000"', 'value "0x100000000" is out of range for type oid'], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('GRANT SELECT ON LARGE OBJECT 0x80000000, 1_0000000000, 0o40000000000, 03000000000, 0xFFFFFFFF, -0x80000000, 0x100000000 TO joe')->facts->diagnostics));
28    }
29
30    public function testProblemAcceptsEveryIntegerConstant(): void
31    {
32        self::assertNull((new \SqlSemantics\Platform\PostgreSql\Rules\Access\ObjectIdentifiers())->problem(new \SqlSemantics\Platform\PostgreSql\Statement\Literal\SignedNumber(true, new \SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant('2147483647'))));
33    }
34
35    public function testProblemRejectsANegativeValueBeyondTheSignExtension(): void
36    {
37        self::assertSame('value "-4294967295" is out of range for type oid', (new \SqlSemantics\Platform\PostgreSql\Rules\Access\ObjectIdentifiers())->problem(new \SqlSemantics\Platform\PostgreSql\Statement\Literal\SignedNumber(true, new \SqlSemantics\Platform\PostgreSql\Statement\Literal\NumericConstant('4294967295')))?->message());
38    }
39
40    public function testProblemRejectsAValueOfMoreThanTenDigits(): void
41    {
42        self::assertSame('value "10000000000" is out of range for type oid', (new \SqlSemantics\Platform\PostgreSql\Rules\Access\ObjectIdentifiers())->problem(new \SqlSemantics\Platform\PostgreSql\Statement\Literal\SignedNumber(false, new \SqlSemantics\Platform\PostgreSql\Statement\Literal\NumericConstant('10000000000')))?->message());
43    }
44
45    public function testUnsignedReadsDecimalHexadecimalAndOctalDigits(): void
46    {
47        self::assertSame(['3000000000', '8589934591', '402653184'], [(new \SqlSemantics\Platform\PostgreSql\Rules\Access\ObjectIdentifiers())->unsigned('3000000000'), (new \SqlSemantics\Platform\PostgreSql\Rules\Access\ObjectIdentifiers())->unsigned('0X1ffffffff'), (new \SqlSemantics\Platform\PostgreSql\Rules\Access\ObjectIdentifiers())->unsigned('03000000000')]);
48    }
49
50    public function testUnsignedReadsNothingFromAnyOtherText(): void
51    {
52        self::assertSame([null, null, null, null, null], [(new \SqlSemantics\Platform\PostgreSql\Rules\Access\ObjectIdentifiers())->unsigned('1.5'), (new \SqlSemantics\Platform\PostgreSql\Rules\Access\ObjectIdentifiers())->unsigned('1e10'), (new \SqlSemantics\Platform\PostgreSql\Rules\Access\ObjectIdentifiers())->unsigned('3_000_000_000'), (new \SqlSemantics\Platform\PostgreSql\Rules\Access\ObjectIdentifiers())->unsigned('0b100000000000000000000000000000000'), (new \SqlSemantics\Platform\PostgreSql\Rules\Access\ObjectIdentifiers())->unsigned('08000000000')]);
53    }
54}
55