packages/sql-semantics-postgres/tests/Unit/Statement/Expression/Subquery/QuantifiedSubqueryTest.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\Fact\QueryFact;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Query;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\Field;
19use SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot;
20use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
21use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
22use SqlSemantics\Platform\PostgreSql\Platform;
23use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
24use SqlSemantics\Platform\PostgreSql\Statement\Expression\BinaryOperation;
25use SqlSemantics\Platform\PostgreSql\Statement\Expression\Subquery\MatchKeyword;
26use SqlSemantics\Platform\PostgreSql\Statement\Expression\Subquery\QuantifiedSubquery;
27use SqlSemantics\Platform\PostgreSql\Statement\Expression\Subquery\Quantifier;
28use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
29use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
30use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
31use SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName;
32use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
33
34#[CoversClass(QuantifiedSubquery::class)]
35#[Small]
36final class QuantifiedSubqueryTest extends TestCase
37{
38    public function testDeriveScalarComparesWithTheColumn(): void
39    {
40        $context = (new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true);
41        $query = self::createConfiguredStub(Query::class, ['deriveQuery' => new QueryFact([new Field(0, new OutputSlot(new Name('a'), new Known(Builtin::Int4), Nullability::NotNull))], $context->columnNames)]);
42        $derivation = new Derivation($context);
43        $fact = $derivation->scalar(new QuantifiedSubquery(new Constant(new IntegerConstant('1')), new OperatorName(new Name('<')), Quantifier::All, $query), $derivation->environment());
44        self::assertEquals(new Known(Builtin::Bool), $fact->type);
45    }
46
47    public function testRenderWritesAKeywordOperator(): void
48    {
49        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
50        (new QuantifiedSubquery(new NullLiteral(), MatchKeyword::NotILike, Quantifier::Some, self::createStub(Query::class)))->render($out);
51        self::assertSame('NULL NOT ILIKE SOME ()', (new Lexical())->join($out->pieces()));
52    }
53
54    public function testRejectsASumBeforeAProductOperator(): void
55    {
56        $sum = new BinaryOperation(new OperatorName(new Name('+')), new NullLiteral(), new NullLiteral());
57        $this->expectExceptionMessage('The compared value needs parentheses to keep its place.');
58        new QuantifiedSubquery($sum, new OperatorName(new Name('*')), Quantifier::Any, self::createStub(Query::class));
59    }
60}
61