packages/sql-semantics-postgres/tests/Unit/Lowering/Invocation/XmlRuleTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Invocation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlParser\PostgreSql\PostgreSqlParser;
11use SqlSemantics\Platform\PostgreSql\Lowering\Invocation\SyntaxRule;
12use SqlSemantics\Platform\PostgreSql\Lowering\Invocation\XmlRule;
13use SqlSemantics\Platform\PostgreSql\Lowering\Lowering;
14use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
15use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
16use SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves;
17use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
18use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
19use SqlSemantics\Platform\PostgreSql\Platform;
20use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
21use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Xml\XmlIndent;
22
23#[CoversClass(XmlRule::class)]
24#[Small]
25final class XmlRuleTest extends TestCase
26{
27 public function testExpressionLowersEveryXmlFunction(): void
28 {
29 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
30 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT XMLELEMENT(NAME a, XMLATTRIBUTES(1 AS b), 2), XMLELEMENT(NAME a, 1), XMLPARSE(CONTENT \'x\' PRESERVE WHITESPACE), XMLPI(NAME p), XMLSERIALIZE(DOCUMENT \'x\' AS text INDENT)');
31 $node = array_map(static fn ($target) => (new SyntaxRule($lowering))->subexpression($target->find('func_expr_common_subexpr')[0]), $tree->find('target_list')[0]->find('target_el'));
32 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
33 $out->list($node);
34 self::assertSame('XMLELEMENT(NAME a, XMLATTRIBUTES(1 AS b), 2), XMLELEMENT(NAME a, 1), XMLPARSE(CONTENT \'x\' PRESERVE WHITESPACE), XMLPI(NAME p), XMLSERIALIZE(DOCUMENT \'x\' AS text INDENT)', (new Lexical())->join($out->pieces()));
35 }
36
37 public function testRootLowersTheVersionAndStandalone(): void
38 {
39 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
40 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT XMLROOT(\'x\', VERSION \'1.0\', STANDALONE YES)');
41 $node = (new SyntaxRule($lowering))->subexpression($tree->find('func_expr_common_subexpr')[0]);
42 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
43 $out->node($node);
44 self::assertSame('XMLROOT(\'x\', VERSION \'1.0\', STANDALONE YES)', (new Lexical())->join($out->pieces()));
45 }
46
47 public function testOptionAnswersTheTableEntry(): void
48 {
49 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
50 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT XMLSERIALIZE(CONTENT 1 AS text INDENT)');
51 self::assertSame(XmlIndent::Indent, (new XmlRule($lowering))->option(['xml_indent_option: INDENT' => XmlIndent::Indent], $tree->find('xml_indent_option')[0]));
52 }
53
54 public function testWrappedLowersXmlAttributes(): void
55 {
56 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
57 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT XMLELEMENT(NAME a, XMLATTRIBUTES(1 AS b, 2 AS c))');
58 $node = (new XmlRule($lowering))->wrapped($tree->find('xml_attributes')[0]);
59 self::assertCount(2, $node);
60 }
61
62 public function testAttributesLowersUnnamedValues(): void
63 {
64 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
65 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT XMLFOREST(a)');
66 $node = (new XmlRule($lowering))->attributes($tree->find('xml_attribute_list')[0]);
67 self::assertNull($node[0]->name);
68 }
69
70 public function testPassingDropsThePassingMechanism(): void
71 {
72 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
73 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT XMLEXISTS(\'x\' PASSING BY REF \'y\')');
74 $node = (new XmlRule($lowering))->passing($tree->find('xmlexists_argument')[0]);
75 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
76 $out->node($node);
77 self::assertSame('PASSING \'y\'', (new Lexical())->join($out->pieces()));
78 }
79}
80