packages/sql-semantics-postgres/tests/Unit/Statement/Relation/Xml/XmlTableTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Relation\Xml;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Statement\Relation\Xml\XmlTable::class)]
12#[Medium]
13final class XmlTableTest extends TestCase
14{
15 public function testDeriveRelationHasTheColumns(): void
16 {
17 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
18 $query = $semantics->inspect("SELECT * FROM XMLTABLE (XMLNAMESPACES ('u' AS x, DEFAULT 'v'), '/r' PASSING '<r/>' COLUMNS n FOR ORDINALITY, a text PATH 'a' DEFAULT 'z' NOT NULL, b int) AS x");
19 self::assertSame(['n:NotNull', 'a:NotNull', 'b:Nullable'], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\Field $field): string => ($field->name->value ?? '') . ':' . $field->nullability->name, $query->fields()->items ?? []));
20 }
21
22 public function testDeriveRelationReportsAnUnrecognizedOption(): void
23 {
24 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
25 $query = $semantics->inspect("SELECT * FROM XMLTABLE ('/r' PASSING '<r/>' COLUMNS a text foo 'a')");
26 self::assertSame('unrecognized column option "foo"', $query->facts->diagnostics[0]->message());
27 }
28
29 public function testRenderWritesTheNamespacesAndColumns(): void
30 {
31 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
32 $query = $semantics->inspect("SELECT * FROM LATERAL XMLTABLE ('/r' PASSING '<r/>' COLUMNS n FOR ORDINALITY)");
33 self::assertSame("SELECT * FROM LATERAL XMLTABLE ('/r' PASSING '<r/>' COLUMNS n FOR ORDINALITY)", $query->toString());
34 }
35}
36