packages/sql-semantics-postgres/tests/Unit/Statement/Expression/Subquery/QuantifiedArrayTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Subquery;
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\Identifier\Name;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Query;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Invalid;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
19use SqlSemantics\Platform\PostgreSql\Platform;
20use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
21use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\ArrayConstructor;
22use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\ArrayItems;
23use SqlSemantics\Platform\PostgreSql\Statement\Expression\Problem\OperandMismatch;
24use SqlSemantics\Platform\PostgreSql\Statement\Expression\Subquery\QuantifiedArray;
25use SqlSemantics\Platform\PostgreSql\Statement\Expression\Subquery\Quantifier;
26use SqlSemantics\Platform\PostgreSql\Statement\Expression\Subquery\ScalarSubquery;
27use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
28use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
29use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
30use SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName;
31use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
32
33#[CoversClass(QuantifiedArray::class)]
34#[Small]
35final class QuantifiedArrayTest extends TestCase
36{
37 public function testDeriveScalarComparesWithTheElementType(): void
38 {
39 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
40 $array = new ArrayConstructor(new ArrayItems([new Constant(new IntegerConstant('2'))]));
41 $fact = $derivation->scalar(new QuantifiedArray(new Constant(new IntegerConstant('1')), new OperatorName(new Name('=')), Quantifier::Any, $array), $derivation->environment());
42 self::assertEquals(new Known(Builtin::Bool), $fact->type);
43 }
44
45 public function testDeriveScalarReportsANonArray(): void
46 {
47 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
48 $fact = $derivation->scalar(new QuantifiedArray(new Constant(new IntegerConstant('1')), new OperatorName(new Name('=')), Quantifier::Any, new Constant(new IntegerConstant('2'))), $derivation->environment());
49 self::assertInstanceOf(OperandMismatch::class, $fact->type instanceof Invalid ? $fact->type->cause : null);
50 }
51
52 public function testRenderWritesTheQuantifier(): void
53 {
54 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
55 (new QuantifiedArray(new NullLiteral(), new OperatorName(new Name('<>')), Quantifier::All, new NullLiteral()))->render($out);
56 self::assertSame('NULL <> ALL (NULL)', (new Lexical())->join($out->pieces()));
57 }
58
59 public function testRejectsAScalarSubquery(): void
60 {
61 $this->expectExceptionMessage('A scalar subquery after ANY or ALL is read as a subquery comparison.');
62 new QuantifiedArray(new NullLiteral(), new OperatorName(new Name('=')), Quantifier::Any, new ScalarSubquery(self::createStub(Query::class)));
63 }
64}
65