packages/sql-semantics-mysql/tests/Unit/Rules/Call/RoutineCallsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\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\Statement\Fact\ScalarFact;
13use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
15use SqlSemantics\Platform\MySql\Model\Statement\Reference\Missing\UndeclaredRoutine;
16use SqlSemantics\Platform\MySql\Model\Statement\Type\Dependent;
17use SqlSemantics\Platform\MySql\Model\Statement\Type\Invalid;
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\RoutineCalls;
22use SqlSemantics\Platform\MySql\Rules\Call\TypeClass;
23use SqlSemantics\Platform\MySql\Statement\Call\CallArgument;
24use SqlSemantics\Platform\MySql\Statement\Call\FunctionCall;
25use SqlSemantics\Platform\MySql\Statement\Call\Problem\NamedArgument;
26use SqlSemantics\Platform\MySql\Statement\Call\Problem\ReservedFunction;
27use SqlSemantics\Platform\MySql\Statement\Call\Problem\WrongArgumentCount;
28use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
29
30#[CoversClass(RoutineCalls::class)]
31#[Small]
32final class RoutineCallsTest extends TestCase
33{
34    public function testResultTypesANativeFunction(): void
35    {
36        $platform = new Platform();
37        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], false));
38        $int = new ScalarFact(new Known(TypeClass::Integer->descriptor()), Nullability::NotNull);
39        $fact = (new RoutineCalls())->result(new FunctionCall(new Name('Abs'), [new CallArgument(new NumberLiteral('1'))]), [$int], $derivation);
40
41        self::assertEquals($int->type, $fact->type);
42        self::assertSame(Nullability::NotNull, $fact->nullability);
43        self::assertSame([], $derivation->facts()->diagnostics);
44    }
45
46    public function testResultDependsOnAStoredOrLoadableFunction(): void
47    {
48        $platform = new Platform();
49        $derivation = new Derivation($platform->context($platform->profile('mysql-5.6.51', null, ParameterStyle::Native), null, [], false));
50        $stored = (new RoutineCalls())->result(new FunctionCall(new Name('json_array')), [], $derivation);
51        $qualified = (new RoutineCalls())->result(new FunctionCall(new Name('abs'), [], new Name('db')), [], $derivation);
52
53        self::assertInstanceOf(Dependent::class, $stored->type);
54        self::assertInstanceOf(UndeclaredRoutine::class, $stored->type->missing[0]);
55        self::assertInstanceOf(Dependent::class, $qualified->type);
56        self::assertEquals([new UndeclaredRoutine(new QualifiedName(new Name('abs'), new Name('db')))], $qualified->type->missing);
57    }
58
59    public function testResultReportsWhatTheServerRejects(): void
60    {
61        $platform = new Platform();
62        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], false));
63        $int = new ScalarFact(new Known(TypeClass::Integer->descriptor()), Nullability::NotNull);
64        $count = (new RoutineCalls())->result(new FunctionCall(new Name('abs')), [], $derivation);
65        $named = (new RoutineCalls())->result(new FunctionCall(new Name('abs'), [new CallArgument(new NumberLiteral('1'), new Name('x'))]), [$int], $derivation);
66        $reserved = (new RoutineCalls())->result(new FunctionCall(new Name('internal_use_terminology_previous')), [], $derivation);
67
68        self::assertInstanceOf(Invalid::class, $count->type);
69        self::assertInstanceOf(WrongArgumentCount::class, $count->type->cause);
70        self::assertInstanceOf(Invalid::class, $named->type);
71        self::assertInstanceOf(NamedArgument::class, $named->type->cause);
72        self::assertInstanceOf(Invalid::class, $reserved->type);
73        self::assertInstanceOf(ReservedFunction::class, $reserved->type->cause);
74        self::assertCount(3, $derivation->facts()->diagnostics);
75    }
76}
77