packages/sql-semantics-postgres/tests/Unit/Statement/Expression/Predicate/DocumentTestTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Predicate;
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\Type\Invalid;
16use SqlSemantics\Platform\PostgreSql\Platform;
17use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
18use SqlSemantics\Platform\PostgreSql\Statement\Expression\Operator\Negation;
19use SqlSemantics\Platform\PostgreSql\Statement\Expression\Predicate\DocumentTest;
20use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
21use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
22use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
23
24#[CoversClass(DocumentTest::class)]
25#[Small]
26final class DocumentTestTest extends TestCase
27{
28 public function testDeriveScalarReportsANonXmlOperand(): void
29 {
30 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
31 $fact = $derivation->scalar(new DocumentTest(new Constant(new IntegerConstant('1')), false), $derivation->environment());
32 self::assertInstanceOf(Invalid::class, $fact->type);
33 }
34
35 public function testRenderWritesTheTest(): void
36 {
37 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
38 (new DocumentTest(new NullLiteral(), true))->render($out);
39 self::assertSame('NULL IS NOT DOCUMENT', (new Lexical())->join($out->pieces()));
40 }
41
42 public function testRejectsANegation(): void
43 {
44 $this->expectExceptionMessage('The tested value needs parentheses to keep its place.');
45 new DocumentTest(new Negation(new NullLiteral()), false);
46 }
47}
48