packages/sql-semantics-postgres/tests/Unit/Rules/Expression/Typing/CaseTypingTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Expression\Typing;
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\Statement\Type\Known;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
15use SqlSemantics\Platform\PostgreSql\Platform;
16use SqlSemantics\Platform\PostgreSql\Rules\Expression\Typing\CaseTyping;
17use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\CaseBranch;
18use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\CaseExpression;
19use SqlSemantics\Platform\PostgreSql\Statement\Expression\Problem\OperandMismatch;
20use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
21use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
22use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
23
24#[CoversClass(CaseTyping::class)]
25#[Small]
26final class CaseTypingTest extends TestCase
27{
28 public function testDeriveReportsANonBooleanCondition(): void
29 {
30 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
31 $case = new CaseExpression(null, [new CaseBranch(new Constant(new IntegerConstant('1')), new Constant(new IntegerConstant('2')))]);
32 $fact = (new CaseTyping())->derive($derivation, $derivation->environment(), $case);
33 self::assertEquals(new Known(Builtin::Int4), $fact->type);
34 self::assertSame(Nullability::Nullable, $fact->nullability);
35 self::assertEquals([new OperandMismatch('CASE/WHEN', 'boolean', 'integer')], $derivation->facts()->diagnostics);
36 }
37}
38