packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Conditional/MinMaxTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Invocation\Conditional;
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\Fact\ScalarFact;
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\Invocation\Conditional\MinMax;
21use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Conditional\MinMaxKind;
22use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
23use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
25use SqlSemantics\Platform\PostgreSql\Statement\Literal\NumericConstant;
26use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
27
28#[CoversClass(MinMax::class)]
29#[Small]
30final class MinMaxTest extends TestCase
31{
32 public function testRejectsNoValue(): void
33 {
34 $this->expectExceptionMessage('GREATEST and LEAST take at least one value.');
35 new MinMax(MinMaxKind::Least, []);
36 }
37
38 public function testOutputNameIsTheFunction(): void
39 {
40 self::assertSame('least', (new MinMax(MinMaxKind::Least, [new NullLiteral()]))->outputName()->value);
41 }
42
43 public function testDeriveScalarIsTheCommonType(): void
44 {
45 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
46 $fact = $derivation->scalar(new MinMax(MinMaxKind::Greatest, [new Constant(new IntegerConstant('1')), new Constant(new NumericConstant('9999999999'))]), $derivation->environment());
47 self::assertEquals(new ScalarFact(new Known(Builtin::Int8), Nullability::NotNull), $fact);
48 }
49
50 public function testRenderWritesTheKeyword(): void
51 {
52 $greatest = new MinMax(MinMaxKind::Greatest, [new Constant(new IntegerConstant('1')), new Constant(new IntegerConstant('1'))]);
53 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
54 $greatest->render($out);
55 self::assertSame('GREATEST(1, 1)', (new Lexical())->join($out->pieces()));
56 }
57}
58