packages/sql-semantics-postgres/tests/Unit/Statement/Object/Attribute/NameArgumentTest.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\Constant;
19use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
20use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
21use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
22use SqlSemantics\Platform\PostgreSql\Statement\Name\ObjectKind;
23use SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName;
24use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\NameArgument;
25use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Reading;
26use SqlSemantics\Platform\PostgreSql\Statement\Option\KeywordWord;
27use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\KeywordDesignation;
28use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
29use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\TypeKeyword;
30use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
31
32#[CoversClass(NameArgument::class)]
33#[Small]
34final class NameArgumentTest extends TestCase
35{
36    public function testPartsAnswersTheNamesTheServerReads(): void
37    {
38        self::assertSame(['s', 'f'], array_map(static fn (Name $part): string => $part->value, (new NameArgument(ObjectKind::Function, new DottedName([new Name('s'), new Name('f')])))->parts()));
39        self::assertSame(['pg_catalog', 'int4'], array_map(static fn (Name $part): string => $part->value, (new NameArgument(ObjectKind::Function, new TypeName(new KeywordDesignation(TypeKeyword::Integer))))->parts()));
40        self::assertSame(['s', '+'], array_map(static fn (Name $part): string => $part->value, (new NameArgument(ObjectKind::Operator, new OperatorName(new Name('+'), [new Name('s')])))->parts()));
41        self::assertSame(['F'], array_map(static fn (Name $part): string => $part->value, (new NameArgument(ObjectKind::Function, new StringConstant('F')))->parts()));
42        self::assertSame(['none'], array_map(static fn (Name $part): string => $part->value, (new NameArgument(ObjectKind::Operator, new KeywordWord(new Name('none'))))->parts()));
43    }
44
45    public function testFitsAReadingOfTheSameKind(): void
46    {
47        $name = new NameArgument(ObjectKind::Function, new DottedName([new Name('f')]));
48        self::assertSame([true, false], [$name->fits(Reading::Function), $name->fits(Reading::Operator)]);
49    }
50
51    public function testDeriveClauseDerivesTheModifiersOfATypeName(): void
52    {
53        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
54        $modifier = new Constant(new IntegerConstant('1'));
55        (new NameArgument(ObjectKind::Function, new TypeName(new NamedDesignation(new DottedName([new Name('f')]), [$modifier]))))->deriveClause($derivation, $derivation->environment());
56        self::assertTrue($derivation->facts()->covers($modifier));
57    }
58
59    public function testRenderWritesADottedNameWhereATypeNameIsRead(): void
60    {
61        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
62        (new NameArgument(ObjectKind::Function, new DottedName([new Name('S'), new Name('f')])))->render($out);
63        self::assertSame('"S".f', (new Lexical())->join($out->pieces()));
64    }
65
66    public function testRenderWritesOtherSpellingsAsWritten(): void
67    {
68        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
69        (new NameArgument(ObjectKind::Function, new StringConstant('f')))->render($out);
70        self::assertSame("'f'", (new Lexical())->join($out->pieces()));
71    }
72
73    public function testRejectsAPlainTypeName(): void
74    {
75        $this->expectExceptionMessage('A plain name is written as a dotted name, not as a type name.');
76        new NameArgument(ObjectKind::Function, new TypeName(new NamedDesignation(new DottedName([new Name('f')]))));
77    }
78
79    public function testRejectsAKindNoAttributeNames(): void
80    {
81        $this->expectExceptionMessage('A definition attribute names a function, an operator, an operator class, a collation, a text search object or a type.');
82        new NameArgument(ObjectKind::Table, new DottedName([new Name('t')]));
83    }
84}
85