packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Xml/XmlConcatTest.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\XmlConcat;
21use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
22use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
23
24#[CoversClass(XmlConcat::class)]
25#[Small]
26final class XmlConcatTest extends TestCase
27{
28 public function testRejectsNoValue(): void
29 {
30 $this->expectExceptionMessage('XMLCONCAT takes at least one value.');
31 new XmlConcat([]);
32 }
33
34 public function testOutputNameIsXmlconcat(): void
35 {
36 self::assertSame('xmlconcat', (new XmlConcat([new NullLiteral()]))->outputName()->value);
37 }
38
39 public function testDeriveScalarIsNullableWhenEveryValueIs(): void
40 {
41 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
42 $fact = $derivation->scalar(new XmlConcat([new NullLiteral()]), $derivation->environment());
43 self::assertEquals(new ScalarFact(new Known(Builtin::Xml), Nullability::Nullable), $fact);
44 }
45
46 public function testRenderWritesTheValues(): void
47 {
48 $concat = new XmlConcat([new NullLiteral(), new NullLiteral()]);
49 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
50 $concat->render($out);
51 self::assertSame('XMLCONCAT(NULL, NULL)', (new Lexical())->join($out->pieces()));
52 }
53}
54