packages/sql-semantics-postgres/tests/Unit/Statement/Expression/Subquery/InSubqueryTest.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\Operator\Negation;
25use SqlSemantics\Platform\PostgreSql\Statement\Expression\Subquery\InSubquery;
26use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
27use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
28use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
29use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
30
31#[CoversClass(InSubquery::class)]
32#[Small]
33final class InSubqueryTest extends TestCase
34{
35    public function testDeriveScalarComparesWithTheColumn(): void
36    {
37        $context = (new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true);
38        $query = self::createConfiguredStub(Query::class, ['deriveQuery' => new QueryFact([new Field(0, new OutputSlot(new Name('a'), new Known(Builtin::Int4), Nullability::NotNull))], $context->columnNames)]);
39        $derivation = new Derivation($context);
40        $fact = $derivation->scalar(new InSubquery(new Constant(new IntegerConstant('1')), true, $query), $derivation->environment());
41        self::assertEquals(new Known(Builtin::Bool), $fact->type);
42    }
43
44    public function testRenderWritesNotIn(): void
45    {
46        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
47        (new InSubquery(new NullLiteral(), true, self::createStub(Query::class)))->render($out);
48        self::assertSame('NULL NOT IN ()', (new Lexical())->join($out->pieces()));
49    }
50
51    public function testRejectsANegatedValue(): void
52    {
53        $this->expectExceptionMessage('The tested value needs parentheses to keep its place.');
54        new InSubquery(new Negation(new NullLiteral()), false, self::createStub(Query::class));
55    }
56}
57