packages/sql-semantics-mysql/tests/Unit/Statement/Call/Json/PathColumnTest.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\Shape\OutputSlot;
16use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
17use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
18use SqlSemantics\Platform\MySql\Platform;
19use SqlSemantics\Platform\MySql\Statement\Call\Json\JsonResponse;
20use SqlSemantics\Platform\MySql\Statement\Call\Json\JsonResponseKind;
21use SqlSemantics\Platform\MySql\Statement\Call\Json\PathColumn;
22use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
23use SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral;
24use SqlSemantics\Platform\MySql\Statement\Name\CollationName;
25use SqlSemantics\Platform\MySql\Statement\Type\Integral;
26use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
27
28#[CoversClass(PathColumn::class)]
29#[Small]
30final class PathColumnTest extends TestCase
31{
32    public function testDeriveColumnsAnswersTheDeclaredType(): void
33    {
34        $platform = new Platform();
35        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], false));
36        $type = new Integral(IntegralKind::Int);
37        $default = new NumberLiteral('1');
38        $slots = (new PathColumn(new Name('a'), $type, new StringLiteral(['$.a']), false, null, new JsonResponse(JsonResponseKind::Default, $default)))->deriveColumns($derivation, $derivation->environment());
39
40        self::assertEquals([new OutputSlot(new Name('a'), new Known($type), Nullability::Nullable)], $slots);
41        self::assertSame(Nullability::NotNull, $derivation->facts()->scalar($default)->nullability);
42    }
43
44    public function testRenderKeepsTheOrderOfTheResponses(): void
45    {
46        $platform = new Platform();
47        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
48        $out = new Output($platform->codec($profile));
49        (new PathColumn(new Name('a'), new Integral(IntegralKind::Int), new StringLiteral(['$.a']), true, new CollationName(new Name('utf8mb4_bin')), new JsonResponse(JsonResponseKind::Null), new JsonResponse(JsonResponseKind::Error), true))->render($out);
50
51        self::assertSame("a INT COLLATE utf8mb4_bin EXISTS PATH '$.a' ERROR ON ERROR NULL ON EMPTY", (new Lexical())->join($out->pieces()));
52    }
53}
54