packages/sql-semantics-mysql/tests/Unit/Statement/Expression/Branching/CaseExpressionTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Branching;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
11use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
12use SqlSemantics\Platform\MySql\Model\Rendering\Lexical;
13use SqlSemantics\Platform\MySql\Model\Rendering\Output;
14use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
16use SqlSemantics\Platform\MySql\Platform;
17use SqlSemantics\Platform\MySql\Statement\Expression\Branching\CaseBranch;
18use SqlSemantics\Platform\MySql\Statement\Expression\Branching\CaseExpression;
19use SqlSemantics\Platform\MySql\Statement\Expression\Row;
20use SqlSemantics\Platform\MySql\Statement\Literal\NullLiteral;
21use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
22use SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral;
23use SqlSemantics\Platform\MySql\Statement\Type\Decimal;
24
25#[CoversClass(CaseExpression::class)]
26#[Medium]
27final class CaseExpressionTest extends TestCase
28{
29    public function testDeriveScalarAggregatesTheResultsAndIsNullWithoutElse(): void
30    {
31        $platform = new Platform();
32        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
33        $fact = $derivation->scalar(new CaseExpression(null, [new CaseBranch(new NumberLiteral('1'), new NumberLiteral('2')), new CaseBranch(new NumberLiteral('0'), new NumberLiteral('2.5'))]), $derivation->environment());
34
35        self::assertEquals(new Known(new Decimal()), $fact->type);
36        self::assertSame(Nullability::Nullable, $fact->nullability);
37    }
38
39    public function testDeriveScalarReportsAValueOfAnotherWidthThanTheOperand(): void
40    {
41        $platform = new Platform();
42        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
43        $derivation->scalar(new CaseExpression(new NumberLiteral('1'), [new CaseBranch(new Row([new NumberLiteral('1'), new NumberLiteral('2')]), new NullLiteral())], new StringLiteral(['x'])), $derivation->environment());
44
45        self::assertSame('Operand should contain 1 column(s), not 2.', $derivation->facts()->diagnostics[0]->message());
46    }
47
48    public function testRenderWritesTheSimpleForm(): void
49    {
50        $platform = new Platform();
51        $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
52        (new CaseExpression(new NumberLiteral('1'), [new CaseBranch(new NumberLiteral('1'), new StringLiteral(['a']))], new StringLiteral(['b'])))->render($out);
53
54        self::assertSame("CASE 1 WHEN 1 THEN 'a' ELSE 'b' END", (new Lexical())->join($out->pieces()));
55    }
56
57    public function testACaseWithoutBranchesIsRejected(): void
58    {
59        $this->expectExceptionMessage('A CASE expression has at least one WHEN branch.');
60
61        new CaseExpression(null, []);
62    }
63}
64