packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Xml/XmlExistsTest.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\Expression\BinaryOperation;
22use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Xml\XmlExists;
23use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Xml\XmlPassing;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
25use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
26use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
27use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
28use SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName;
29use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
30
31#[CoversClass(XmlExists::class)]
32#[Small]
33final class XmlExistsTest extends TestCase
34{
35 public function testRejectsAPathThatIsNotPrimary(): void
36 {
37 $this->expectExceptionMessage('The path of XMLEXISTS is a primary expression; group it.');
38 new XmlExists(new BinaryOperation(new OperatorName(new Name('=')), new Constant(new IntegerConstant('1')), new Constant(new IntegerConstant('1'))), new XmlPassing(new NullLiteral()));
39 }
40
41 public function testOutputNameIsXmlexists(): void
42 {
43 self::assertSame('xmlexists', (new XmlExists(new NullLiteral(), new XmlPassing(new NullLiteral())))->outputName()->value);
44 }
45
46 public function testDeriveScalarIsBoolean(): void
47 {
48 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
49 $fact = $derivation->scalar(new XmlExists(new Constant(new StringConstant('//a')), new XmlPassing(new Constant(new StringConstant('<a/>')))), $derivation->environment());
50 self::assertEquals(new ScalarFact(new Known(Builtin::Bool), Nullability::NotNull), $fact);
51 }
52
53 public function testRenderWritesThePassingClause(): void
54 {
55 $exists = new XmlExists(new Constant(new StringConstant('//a')), new XmlPassing(new Constant(new StringConstant('<a/>'))));
56 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
57 $exists->render($out);
58 self::assertSame('XMLEXISTS(\'//a\' PASSING \'<a/>\')', (new Lexical())->join($out->pieces()));
59 }
60}
61