packages/sql-semantics-mysql/tests/Unit/Statement/Call/FunctionCallTest.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\Identifier\Name;
15use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
16use SqlSemantics\Platform\MySql\Model\Statement\Reference\Missing\UndeclaredRoutine;
17use SqlSemantics\Platform\MySql\Model\Statement\Type\Dependent;
18use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
19use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
20use SqlSemantics\Platform\MySql\Platform;
21use SqlSemantics\Platform\MySql\Rules\Call\TypeClass;
22use SqlSemantics\Platform\MySql\Statement\Call\CallArgument;
23use SqlSemantics\Platform\MySql\Statement\Call\FunctionCall;
24use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
25use SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral;
26
27#[CoversClass(FunctionCall::class)]
28#[Small]
29final class FunctionCallTest extends TestCase
30{
31    public function testNamedTellsWhetherAnArgumentHasAnAlias(): void
32    {
33        self::assertTrue((new FunctionCall(new Name('f'), [new CallArgument(new NumberLiteral('1'), new Name('x'))]))->named());
34        self::assertFalse((new FunctionCall(new Name('f'), [new CallArgument(new NumberLiteral('1'))]))->named());
35    }
36
37    public function testDeriveScalarTypesANativeFunction(): void
38    {
39        $platform = new Platform();
40        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
41        $derivation = new Derivation($platform->context($profile, null, [], false));
42        $fact = $derivation->scalar(new FunctionCall(new Name('concat'), [new CallArgument(new StringLiteral(['x'])), new CallArgument(new NumberLiteral('1'))]), $derivation->environment());
43
44        self::assertEquals(new Known(TypeClass::Character->descriptor()), $fact->type);
45        self::assertSame(Nullability::NotNull, $fact->nullability);
46    }
47
48    public function testDeriveScalarDependsOnAStoredFunction(): void
49    {
50        $platform = new Platform();
51        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
52        $derivation = new Derivation($platform->context($profile, null, [], false));
53        $fact = $derivation->scalar(new FunctionCall(new Name('score'), [new CallArgument(new NumberLiteral('1'))], new Name('db')), $derivation->environment());
54
55        self::assertEquals(new Dependent([new UndeclaredRoutine(new QualifiedName(new Name('score'), new Name('db')))]), $fact->type);
56        self::assertSame(Nullability::Dependent, $fact->nullability);
57    }
58
59    public function testRenderGluesTheNameToItsParenthesis(): void
60    {
61        $platform = new Platform();
62        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
63        $out = new Output($platform->codec($profile));
64        (new FunctionCall(new Name('score'), [new CallArgument(new NumberLiteral('1'))], new Name('db')))->render($out);
65
66        self::assertSame('db.score(1)', (new Lexical())->join($out->pieces()));
67    }
68
69    public function testRenderQuotesAKeywordName(): void
70    {
71        $platform = new Platform();
72        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
73        $out = new Output($platform->codec($profile));
74        (new FunctionCall(new Name('count'), []))->render($out);
75
76        self::assertSame('`count`()', (new Lexical())->join($out->pieces()));
77    }
78}
79