packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Xml/XmlSerializeTest.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\Invalid;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
19use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
20use SqlSemantics\Platform\PostgreSql\Platform;
21use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
22use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Xml\XmlIndent;
23use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Xml\XmlSerialize;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
25use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
26use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
27use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
28use SqlSemantics\Platform\PostgreSql\Statement\Option\XmlOption;
29use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
30use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\KeywordDesignation;
31use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
32use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\TypeKeyword;
33use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
34
35#[CoversClass(XmlSerialize::class)]
36#[Small]
37final class XmlSerializeTest extends TestCase
38{
39    public function testOutputNameIsXmlserialize(): void
40    {
41        self::assertSame('xmlserialize', (new XmlSerialize(XmlOption::Content, new NullLiteral(), new TypeName(new NamedDesignation(new DottedName([new Name('text')])))))->outputName()->value);
42    }
43
44    public function testDeriveScalarIsTheCharacterType(): void
45    {
46        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
47        $fact = $derivation->scalar(new XmlSerialize(XmlOption::Content, new Constant(new StringConstant('<a/>')), new TypeName(new NamedDesignation(new DottedName([new Name('text')])))), $derivation->environment());
48        self::assertEquals(new ScalarFact(new Known(Builtin::Text), Nullability::NotNull), $fact);
49    }
50
51    public function testDeriveScalarReportsATypeThatIsNotACharacterType(): void
52    {
53        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
54        $fact = $derivation->scalar(new XmlSerialize(XmlOption::Content, new Constant(new StringConstant('<a/>')), new TypeName(new KeywordDesignation(TypeKeyword::Integer))), $derivation->environment());
55        self::assertInstanceOf(Invalid::class, $fact->type);
56        self::assertSame('cannot cast XMLSERIALIZE result to integer', $derivation->facts()->diagnostics[0]->message());
57    }
58
59    public function testRenderWritesTheIndentation(): void
60    {
61        $serialize = new XmlSerialize(XmlOption::Document, new Constant(new StringConstant('<a/>')), new TypeName(new NamedDesignation(new DottedName([new Name('text')]))), XmlIndent::NoIndent);
62        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
63        $serialize->render($out);
64        self::assertSame('XMLSERIALIZE(DOCUMENT \'<a/>\' AS text NO INDENT)', (new Lexical())->join($out->pieces()));
65    }
66}
67