packages/sql-semantics-mysql/tests/Unit/Statement/Call/Json/JsonValueCallTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Call\Json;
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\Type\Known;
16use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\MySql\Platform;
18use SqlSemantics\Platform\MySql\Rules\Call\TypeClass;
19use SqlSemantics\Platform\MySql\Statement\Call\Json\JsonResponse;
20use SqlSemantics\Platform\MySql\Statement\Call\Json\JsonResponseKind;
21use SqlSemantics\Platform\MySql\Statement\Call\Json\JsonValueCall;
22use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
23use SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral;
24use SqlSemantics\Platform\MySql\Statement\Name\ColumnUse;
25use SqlSemantics\Platform\MySql\Statement\Type\CastTarget;
26use SqlSemantics\Platform\MySql\Statement\Type\Kind\CastKind;
27
28#[CoversClass(JsonValueCall::class)]
29#[Small]
30final class JsonValueCallTest extends TestCase
31{
32 public function testDeriveScalarAnswersTheReturningType(): void
33 {
34 $platform = new Platform();
35 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
36 $derivation = new Derivation($platform->context($profile, null, [], false));
37 $fact = $derivation->scalar(new JsonValueCall(new StringLiteral(['x']), new StringLiteral(['$.a']), new CastTarget(CastKind::Unsigned), new JsonResponse(JsonResponseKind::Default, new NumberLiteral('1'))), $derivation->environment());
38
39 self::assertEquals(new Known(new CastTarget(CastKind::Unsigned)), $fact->type);
40 self::assertSame(Nullability::Nullable, $fact->nullability);
41 }
42
43 public function testDeriveScalarAnswersACharacterStringWithoutReturning(): void
44 {
45 $platform = new Platform();
46 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
47 $derivation = new Derivation($platform->context($profile, null, [], false));
48 $fact = $derivation->scalar(new JsonValueCall(new StringLiteral(['x']), new StringLiteral(['$.a'])), $derivation->environment());
49
50 self::assertEquals(new Known(TypeClass::Character->descriptor()), $fact->type);
51 }
52
53 public function testRenderWritesTheResponses(): void
54 {
55 $platform = new Platform();
56 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
57 $out = new Output($platform->codec($profile));
58 (new JsonValueCall(new ColumnUse(new Name('a')), new StringLiteral(['$.a']), null, new JsonResponse(JsonResponseKind::Null), new JsonResponse(JsonResponseKind::Error)))->render($out);
59
60 self::assertSame("JSON_VALUE(a, '$.a' NULL ON EMPTY ERROR ON ERROR)", (new Lexical())->join($out->pieces()));
61 }
62}
63