packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Xml/XmlForestTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Invocation\Xml;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
13use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
14use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
17use SqlSemantics\Platform\PostgreSql\Platform;
18use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
19use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Problem\XmlProblem;
20use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Problem\XmlProblemKind;
21use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Xml\XmlAttribute;
22use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Xml\XmlForest;
23use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
25use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
26use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
27
28#[CoversClass(XmlForest::class)]
29#[Small]
30final class XmlForestTest extends TestCase
31{
32    public function testRejectsNoValue(): void
33    {
34        $this->expectExceptionMessage('XMLFOREST takes at least one value.');
35        new XmlForest([]);
36    }
37
38    public function testOutputNameIsXmlforest(): void
39    {
40        self::assertSame('xmlforest', (new XmlForest([new XmlAttribute(new NullLiteral(), new Name('a'))]))->outputName()->value);
41    }
42
43    public function testDeriveScalarReportsAnUnnamedValue(): void
44    {
45        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
46        $fact = $derivation->scalar(new XmlForest([new XmlAttribute(new Constant(new IntegerConstant('1')))]), $derivation->environment());
47        self::assertEquals([new XmlProblem(XmlProblemKind::UnnamedElement)], $derivation->facts()->diagnostics);
48        self::assertEquals(new Known(Builtin::Xml), $fact->type);
49    }
50
51    public function testRenderWritesTheValues(): void
52    {
53        $forest = new XmlForest([new XmlAttribute(new Constant(new IntegerConstant('1')), new Name('a'))]);
54        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
55        $forest->render($out);
56        self::assertSame('XMLFOREST(1 AS a)', (new Lexical())->join($out->pieces()));
57    }
58}
59