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

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Constructor;
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\Known;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
18use SqlSemantics\Platform\PostgreSql\Platform;
19use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
20use SqlSemantics\Platform\PostgreSql\Statement\Expression\ColumnReference;
21use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\CaseBranch;
22use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\CaseExpression;
23use SqlSemantics\Platform\PostgreSql\Statement\Literal\BooleanLiteral;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
25use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
26use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
27use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
28use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
29
30#[CoversClass(CaseExpression::class)]
31#[Small]
32final class CaseExpressionTest extends TestCase
33{
34    public function testOutputNameIsTheElseNameOrCase(): void
35    {
36        $named = new CaseExpression(null, [new CaseBranch(new BooleanLiteral(true), new NullLiteral())], new ColumnReference([new Name('a')]));
37        $unnamed = new CaseExpression(null, [new CaseBranch(new BooleanLiteral(true), new NullLiteral())]);
38        self::assertSame(['a', 'case'], [$named->outputName()?->value, $unnamed->outputName()?->value]);
39    }
40
41    public function testDeriveScalarUnifiesTheResults(): void
42    {
43        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
44        $case = new CaseExpression(new Constant(new IntegerConstant('1')), [new CaseBranch(new Constant(new IntegerConstant('1')), new Constant(new StringConstant('2')))], new Constant(new IntegerConstant('3')));
45        $fact = $derivation->scalar($case, $derivation->environment());
46        self::assertEquals(new Known(Builtin::Int4), $fact->type);
47        self::assertSame(Nullability::NotNull, $fact->nullability);
48    }
49
50    public function testRenderWritesTheBranchesAndElse(): void
51    {
52        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
53        (new CaseExpression(new NullLiteral(), [new CaseBranch(new NullLiteral(), new NullLiteral())], new NullLiteral()))->render($out);
54        self::assertSame('CASE NULL WHEN NULL THEN NULL ELSE NULL END', (new Lexical())->join($out->pieces()));
55    }
56
57    public function testRejectsNoBranch(): void
58    {
59        $this->expectExceptionMessage('A CASE expression has at least one WHEN branch.');
60        new CaseExpression(null, []);
61    }
62}
63