packages/sql-semantics-postgres/tests/Unit/Rules/Routine/AttributeReaderTest.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\AttributeReader;
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\ObjectKind;
18use SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName;
19use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\BooleanArgument;
20use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Choice\Parallelism;
21use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\ChoiceArgument;
22use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\IntegerArgument;
23use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Known\DictionaryAttribute;
24use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Known\OperatorAttribute;
25use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\LengthArgument;
26use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\NameArgument;
27use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Reading;
28use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\TextArgument;
29use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\TypeArgument;
30use SqlSemantics\Platform\PostgreSql\Statement\Option\KeywordWord;
31use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
32use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
33
34#[CoversClass(AttributeReader::class)]
35#[Small]
36final class AttributeReaderTest extends TestCase
37{
38    public function testCommandAnswersTheAttributeSetOfAKind(): void
39    {
40        self::assertSame([OperatorAttribute::class, DictionaryAttribute::class], [(new AttributeReader())->command(ObjectKind::Operator), (new AttributeReader())->command(ObjectKind::TextSearchDictionary)]);
41    }
42
43    public function testCommandRejectsAKindWithoutAttributes(): void
44    {
45        $this->expectExceptionMessage('Only an aggregate, an operator, a type, a text search object and a collation are defined with attributes.');
46        (new AttributeReader())->command(ObjectKind::Table);
47    }
48
49    public function testAttributeReadsARecognizedNameAndKeepsAnotherAsWritten(): void
50    {
51        $reader = new AttributeReader();
52        $function = $reader->attribute(OperatorAttribute::class, new Name('function'), new TypeName(new NamedDesignation(new DottedName([new Name('int4eq')]))));
53        $other = $reader->attribute(OperatorAttribute::class, new Name('Function'), new StringConstant('x'));
54        self::assertSame([OperatorAttribute::Function, true, null, true], [$function->known, $function->value instanceof NameArgument, $other->known, $other->value instanceof StringConstant]);
55    }
56
57    public function testReadYieldsTheArgumentOfEachReading(): void
58    {
59        $reader = new AttributeReader();
60        self::assertSame(
61            [TypeArgument::class, BooleanArgument::class, IntegerArgument::class, TextArgument::class, ChoiceArgument::class, StringConstant::class],
62            [
63                get_debug_type($reader->read(Reading::Type, new StringConstant('int4'))),
64                get_debug_type($reader->read(Reading::Boolean, null)),
65                get_debug_type($reader->read(Reading::Integer, new SignedNumber(false, new IntegerConstant('8')))),
66                get_debug_type($reader->read(Reading::Text, new KeywordWord(new Name('none')))),
67                get_debug_type($reader->read(Reading::Parallelism, new StringConstant('safe'))),
68                get_debug_type($reader->read(Reading::Ignored, new StringConstant('x'))),
69            ],
70        );
71    }
72
73    public function testReadKeepsARejectedValueAsWritten(): void
74    {
75        $number = new SignedNumber(false, new NumericConstant('1.5'));
76        self::assertSame([$number, null], [(new AttributeReader())->read(Reading::Integer, $number), (new AttributeReader())->read(Reading::Text, null)]);
77    }
78
79    public function testNameUnwrapsAPlainTypeNameAndRejectsNumbers(): void
80    {
81        $reader = new AttributeReader();
82        $name = $reader->name(ObjectKind::Function, new TypeName(new NamedDesignation(new DottedName([new Name('f')]))));
83        self::assertSame([true, true], [$name instanceof NameArgument && $name->name instanceof DottedName, $reader->name(ObjectKind::Function, new SignedNumber(false, new IntegerConstant('1'))) instanceof SignedNumber]);
84    }
85
86    public function testChoiceReadsOnlyAMemberText(): void
87    {
88        $reader = new AttributeReader();
89        $choice = $reader->choice(Parallelism::class, new KeywordWord(new Name('none')));
90        self::assertSame([true, true], [$choice instanceof KeywordWord, $reader->choice(Parallelism::class, new StringConstant('unsafe')) instanceof ChoiceArgument]);
91    }
92
93    public function testLengthReadsANumberOrVariable(): void
94    {
95        $reader = new AttributeReader();
96        self::assertSame([LengthArgument::class, LengthArgument::class, OperatorName::class], [get_debug_type($reader->length(new SignedNumber(false, new IntegerConstant('4')))), get_debug_type($reader->length(new StringConstant('VARIABLE'))), get_debug_type($reader->length(new OperatorName(new Name('+'))))]);
97    }
98
99    public function testBooleanReadsTheServerSpellings(): void
100    {
101        $reader = new AttributeReader();
102        self::assertSame([true, true, false, null, null], [$reader->boolean(null), $reader->boolean(new SignedNumber(false, new IntegerConstant('1'))), $reader->boolean(new StringConstant('OFF')), $reader->boolean(new SignedNumber(false, new IntegerConstant('2'))), $reader->boolean(new StringConstant('yes'))]);
103    }
104
105    public function testPlainRejectsModifiersArraysAndSetof(): void
106    {
107        $reader = new AttributeReader();
108        self::assertSame([true, false], [$reader->plain(new TypeName(new NamedDesignation(new DottedName([new Name('f')])))), $reader->plain(new TypeName(new NamedDesignation(new DottedName([new Name('f')])), true))]);
109    }
110}
111