packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Xml/XmlParseTest.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\Type\Known;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
18use SqlSemantics\Platform\PostgreSql\Platform;
19use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
20use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Xml\XmlParse;
21use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Xml\XmlWhitespace;
22use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
23use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
25use SqlSemantics\Platform\PostgreSql\Statement\Option\XmlOption;
26use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
27
28#[CoversClass(XmlParse::class)]
29#[Small]
30final class XmlParseTest extends TestCase
31{
32 public function testOutputNameIsXmlparse(): void
33 {
34 self::assertSame('xmlparse', (new XmlParse(XmlOption::Content, new NullLiteral()))->outputName()->value);
35 }
36
37 public function testDeriveScalarIsNullWhenTheValueIs(): void
38 {
39 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
40 $fact = $derivation->scalar(new XmlParse(XmlOption::Content, new NullLiteral()), $derivation->environment());
41 self::assertEquals(new ScalarFact(new Known(Builtin::Xml), Nullability::Nullable), $fact);
42 }
43
44 public function testRenderWritesTheWhitespaceOption(): void
45 {
46 $parse = new XmlParse(XmlOption::Document, new Constant(new StringConstant('<a/>')), XmlWhitespace::Strip);
47 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
48 $parse->render($out);
49 self::assertSame('XMLPARSE(DOCUMENT \'<a/>\' STRIP WHITESPACE)', (new Lexical())->join($out->pieces()));
50 }
51}
52