packages/sql-semantics-mysql/tests/Unit/Lowering/Call/JsonRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Call;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Lowering\Call\JsonRule;
11use SqlSemantics\Platform\MySql\Lowering\Lowering;
12use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
13use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
14use SqlSemantics\Platform\MySql\Platform;
15use SqlSemantics\Platform\MySql\Statement\Call\Json\JsonResponseKind;
16use SqlSemantics\Platform\MySql\Statement\Call\Json\NestedColumns;
17use SqlSemantics\Platform\MySql\Statement\Call\Json\OrdinalityColumn;
18use SqlSemantics\Platform\MySql\Statement\Call\Json\PathColumn;
19use SqlSemantics\Platform\MySql\Statement\Type\Kind\CastKind;
20
21#[CoversClass(JsonRule::class)]
22#[Small]
23final class JsonRuleTest extends TestCase
24{
25    public function testValueLowersJsonValue(): void
26    {
27        $platform = new Platform();
28        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
29        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
30        $node = $platform->parser($profile)->parse("SELECT JSON_VALUE(a, '$.b' RETURNING CHAR(3) DEFAULT 'x' ON EMPTY ERROR ON ERROR)")->find('function_call_keyword')[0];
31        $rule = new JsonRule($lowering);
32        $call = $rule->value($lowering->form($node));
33
34        self::assertSame(CastKind::Char, $call->returning?->kind);
35        self::assertSame(JsonResponseKind::Default, $call->onEmpty?->kind);
36        self::assertSame(JsonResponseKind::Error, $call->onError?->kind);
37    }
38
39    public function testReturningLowersNoType(): void
40    {
41        $platform = new Platform();
42        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
43        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
44        $node = $platform->parser($profile)->parse("SELECT JSON_VALUE(a, '$.b')")->find('opt_returning_type')[0];
45        $rule = new JsonRule($lowering);
46        self::assertNull($rule->returning($node));
47    }
48
49    public function testResponsesLowersOnError(): void
50    {
51        $platform = new Platform();
52        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
53        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
54        $node = $platform->parser($profile)->parse("SELECT JSON_VALUE(a, '$.b' NULL ON ERROR)")->find('opt_on_empty_or_error')[0];
55        $rule = new JsonRule($lowering);
56        $responses = $rule->responses($node);
57
58        self::assertNull($responses[0]);
59        self::assertSame(JsonResponseKind::Null, $responses[1]?->kind);
60    }
61
62    public function testResponseLowersOneClause(): void
63    {
64        $platform = new Platform();
65        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
66        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
67        $node = $platform->parser($profile)->parse("SELECT JSON_VALUE(a, '$.b' ERROR ON EMPTY)")->find('on_empty')[0];
68        $rule = new JsonRule($lowering);
69        self::assertSame(JsonResponseKind::Error, $rule->response($node, 'on_empty: json_on_response ON_SYM EMPTY_SYM')->kind);
70    }
71
72    public function testColumnsLowersEveryColumnForm(): void
73    {
74        $platform = new Platform();
75        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
76        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
77        $node = $platform->parser($profile)->parse("SELECT * FROM JSON_TABLE('[]', '$[*]' COLUMNS (n FOR ORDINALITY, a INT EXISTS PATH '$.a', NESTED PATH '$.b[*]' COLUMNS (b TEXT PATH '$')))")->find('columns_clause')[0];
78        $rule = new JsonRule($lowering);
79        $columns = $rule->columns($node);
80
81        self::assertInstanceOf(OrdinalityColumn::class, $columns[0]);
82        self::assertInstanceOf(PathColumn::class, $columns[1]);
83        self::assertTrue($columns[1]->exists);
84        self::assertInstanceOf(NestedColumns::class, $columns[2]);
85    }
86
87    public function testColumnLowersAnOrdinalityColumn(): void
88    {
89        $platform = new Platform();
90        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
91        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
92        $node = $platform->parser($profile)->parse("SELECT * FROM JSON_TABLE('[]', '$' COLUMNS (n FOR ORDINALITY))")->find('jt_column')[0];
93        $rule = new JsonRule($lowering);
94        self::assertInstanceOf(OrdinalityColumn::class, $rule->column($node));
95    }
96
97    public function testPathKeepsTheReversedOrder(): void
98    {
99        $platform = new Platform();
100        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
101        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
102        $node = $platform->parser($profile)->parse("SELECT * FROM JSON_TABLE('[]', '$' COLUMNS (a INT PATH '$' NULL ON ERROR DEFAULT '1' ON EMPTY))")->find('jt_column')[0];
103        $rule = new JsonRule($lowering);
104        $column = $rule->path($lowering->form($node));
105
106        self::assertTrue($column->errorFirst);
107        self::assertSame(JsonResponseKind::Default, $column->onEmpty?->kind);
108    }
109}
110