packages/sql-semantics-mysql/tests/Unit/Statement/Call/KeywordCallTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Call;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
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\Rules\Call\TypeClass;
18use SqlSemantics\Platform\MySql\Statement\Call\KeywordCall;
19use SqlSemantics\Platform\MySql\Statement\Call\KeywordFunction;
20use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
21use SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral;
22use SqlSemantics\Platform\MySql\Statement\Literal\TemporalForm;
23use SqlSemantics\Platform\MySql\Statement\Literal\TemporalLiteral;
24
25#[CoversClass(KeywordCall::class)]
26#[Small]
27final class KeywordCallTest extends TestCase
28{
29    public function testDeriveScalarTypesTheFunction(): void
30    {
31        $platform = new Platform();
32        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
33        $derivation = new Derivation($platform->context($profile, null, [], false));
34        $fact = $derivation->scalar(new KeywordCall(KeywordFunction::If, [new NumberLiteral('1'), new StringLiteral(['x']), new NumberLiteral('1')]), $derivation->environment());
35
36        self::assertEquals(new Known(TypeClass::Character->descriptor()), $fact->type);
37        self::assertSame(Nullability::NotNull, $fact->nullability);
38    }
39
40    public function testDeriveScalarTypesAddDateAsDateArithmetic(): void
41    {
42        $platform = new Platform();
43        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
44        $derivation = new Derivation($platform->context($profile, null, [], false));
45        $fact = $derivation->scalar(new KeywordCall(KeywordFunction::AddDate, [new TemporalLiteral(TemporalForm::Date, '2024-01-01'), new NumberLiteral('1')]), $derivation->environment());
46
47        self::assertEquals(new Known(TypeClass::Date->descriptor()), $fact->type);
48        self::assertSame(Nullability::Nullable, $fact->nullability);
49    }
50
51    public function testRenderGluesTheKeywordToItsArguments(): void
52    {
53        $platform = new Platform();
54        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
55        $out = new Output($platform->codec($profile));
56        (new KeywordCall(KeywordFunction::CurrentUser, []))->render($out);
57
58        self::assertSame('CURRENT_USER()', (new Lexical())->join($out->pieces()));
59    }
60}
61