packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Xml/XmlElementTest.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\Fact\ScalarFact;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
19use SqlSemantics\Platform\PostgreSql\Platform;
20use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
21use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Problem\XmlProblem;
22use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Problem\XmlProblemKind;
23use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Xml\XmlAttribute;
24use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Xml\XmlElement;
25use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
26use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
27use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
28use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
29
30#[CoversClass(XmlElement::class)]
31#[Small]
32final class XmlElementTest extends TestCase
33{
34 public function testOutputNameIsXmlelement(): void
35 {
36 self::assertSame('xmlelement', (new XmlElement(new Name('a')))->outputName()->value);
37 }
38
39 public function testDeriveScalarReportsAnUnnamedAttribute(): void
40 {
41 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
42 $fact = $derivation->scalar(new XmlElement(new Name('a'), [new XmlAttribute(new Constant(new IntegerConstant('1')))]), $derivation->environment());
43 self::assertEquals(new ScalarFact(new Known(Builtin::Xml), Nullability::NotNull), $fact);
44 self::assertEquals([new XmlProblem(XmlProblemKind::UnnamedAttribute)], $derivation->facts()->diagnostics);
45 }
46
47 public function testRenderWritesAttributesAndContent(): void
48 {
49 $element = new XmlElement(new Name('a'), [new XmlAttribute(new Constant(new IntegerConstant('1')), new Name('b'))], [new Constant(new StringConstant('c'))]);
50 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
51 $element->render($out);
52 self::assertSame('XMLELEMENT(NAME a, XMLATTRIBUTES(1 AS b), \'c\')', (new Lexical())->join($out->pieces()));
53 }
54}
55