packages/sql-semantics-postgres/tests/Unit/Rules/Routine/ArgumentTextTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Routine;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\PostgreSql\Rules\Routine\ArgumentText;
12use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
13use SqlSemantics\Platform\PostgreSql\Statement\Literal\NumericConstant;
14use SqlSemantics\Platform\PostgreSql\Statement\Literal\SignedNumber;
15use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
16use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
17use SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName;
18use SqlSemantics\Platform\PostgreSql\Statement\Option\KeywordWord;
19use SqlSemantics\Platform\PostgreSql\Statement\Type\ArrayBound;
20use SqlSemantics\Platform\PostgreSql\Statement\Type\ArraySpecifier;
21use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\ColumnDesignation;
22use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\KeywordDesignation;
23use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\TypeKeyword;
24use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
25
26#[CoversClass(ArgumentText::class)]
27#[Small]
28final class ArgumentTextTest extends TestCase
29{
30    public function testTextReadsEachValueKind(): void
31    {
32        $text = new ArgumentText();
33        self::assertSame(
34            ['pg_catalog.int4[]', 't.a%TYPE', 'select', 's.+', '-7', 'X'],
35            [
36                $text->text(new TypeName(new KeywordDesignation(TypeKeyword::Integer), false, new ArraySpecifier([new ArrayBound()]))),
37                $text->text(new TypeName(new ColumnDesignation(new DottedName([new Name('t'), new Name('a')])))),
38                $text->text(new KeywordWord(new Name('select'))),
39                $text->text(new OperatorName(new Name('+'), [new Name('s')])),
40                $text->text(new SignedNumber(true, new IntegerConstant('7'))),
41                $text->text(new StringConstant('X')),
42            ],
43        );
44    }
45
46    public function testNamesAddsTheCatalogSchemaToATypeKeyword(): void
47    {
48        self::assertSame(['pg_catalog', 'int4'], array_map(static fn (Name $name): string => $name->value, (new ArgumentText())->names(new TypeName(new KeywordDesignation(TypeKeyword::Integer)))));
49    }
50
51    public function testIntegerAnswersOnlyThirtyTwoBitIntegers(): void
52    {
53        $text = new ArgumentText();
54        self::assertSame([2147483647, null, null], [$text->integer(new SignedNumber(false, new IntegerConstant('2147483647'))), $text->integer(new SignedNumber(true, new NumericConstant('2147483648'))), $text->integer(new SignedNumber(false, new NumericConstant('1.5')))]);
55    }
56
57    public function testNumberWritesTheTextOfAFloatAsWritten(): void
58    {
59        $text = new ArgumentText();
60        self::assertSame(['-1.50E3', '9_999_999_999', '-7'], [$text->number(new SignedNumber(true, new NumericConstant('1.50E3'))), $text->number(new SignedNumber(false, new NumericConstant('9_999_999_999'))), $text->number(new SignedNumber(true, new IntegerConstant('7')))]);
61    }
62
63    public function testJoinedSeparatesNamesWithDots(): void
64    {
65        self::assertSame('a.b', (new ArgumentText())->joined([new Name('a'), new Name('b')]));
66    }
67}
68