packages/sql-semantics-postgres/tests/Unit/Statement/Expression/CollationTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression;
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\Model\Statement\Type\Invalid;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
18use SqlSemantics\Platform\PostgreSql\Platform;
19use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
20use SqlSemantics\Platform\PostgreSql\Statement\Expression\Collation;
21use SqlSemantics\Platform\PostgreSql\Statement\Expression\ColumnReference;
22use SqlSemantics\Platform\PostgreSql\Statement\Expression\Operator\Negation;
23use SqlSemantics\Platform\PostgreSql\Statement\Expression\Problem\NoCollation;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
25use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
26use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
27use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
28use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
29
30#[CoversClass(Collation::class)]
31#[Small]
32final class CollationTest extends TestCase
33{
34    public function testOutputNameIsTheOperandName(): void
35    {
36        self::assertSame('a', (new Collation(new ColumnReference([new Name('a')]), new DottedName([new Name('C')])))->outputName()?->value);
37    }
38
39    public function testDeriveScalarKeepsAStringType(): void
40    {
41        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
42        $fact = $derivation->scalar(new Collation(new Constant(new StringConstant('a')), new DottedName([new Name('C')])), $derivation->environment());
43        self::assertEquals(new Known(Builtin::Unknown), $fact->type);
44    }
45
46    public function testDeriveScalarReportsATypeWithoutCollations(): void
47    {
48        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
49        $fact = $derivation->scalar(new Collation(new Constant(new IntegerConstant('1')), new DottedName([new Name('C')])), $derivation->environment());
50        self::assertInstanceOf(Invalid::class, $fact->type);
51        self::assertInstanceOf(NoCollation::class, $fact->type->cause);
52    }
53
54    public function testRenderWritesTheCollationName(): void
55    {
56        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
57        (new Collation(new Constant(new StringConstant('a')), new DottedName([new Name('pg_catalog'), new Name('C')])))->render($out);
58        self::assertSame("'a' COLLATE pg_catalog.\"C\"", (new Lexical())->join($out->pieces()));
59    }
60
61    public function testRejectsAnOperandThatBindsWeaker(): void
62    {
63        $this->expectExceptionMessage('The operand of COLLATE needs parentheses to keep its place.');
64        new Collation(new Negation(new ColumnReference([new Name('a')])), new DottedName([new Name('C')]));
65    }
66}
67