packages/sql-semantics-postgres/tests/Unit/Statement/Expression/Constructor/ArrayConstructorTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Constructor;
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\Model\Statement\Type\Known;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
18use SqlSemantics\Platform\PostgreSql\Platform;
19use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
20use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\ArrayConstructor;
21use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\ArrayItems;
22use SqlSemantics\Platform\PostgreSql\Statement\Expression\Problem\TypeConflict;
23use SqlSemantics\Platform\PostgreSql\Statement\Literal\BooleanLiteral;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
25use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
26use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
27use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\ArrayOf;
28use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
29
30#[CoversClass(ArrayConstructor::class)]
31#[Small]
32final class ArrayConstructorTest extends TestCase
33{
34    public function testOutputNameIsArray(): void
35    {
36        self::assertSame('array', (new ArrayConstructor(new ArrayItems()))->outputName()->value);
37    }
38
39    public function testDeriveScalarIsAnArrayOfTheCommonType(): void
40    {
41        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
42        $items = new ArrayItems([], [new ArrayItems([new Constant(new StringConstant('1'))]), new ArrayItems([new Constant(new IntegerConstant('2'))])]);
43        $fact = $derivation->scalar(new ArrayConstructor($items), $derivation->environment());
44        self::assertEquals(new Known(new ArrayOf(Builtin::Int4)), $fact->type);
45        self::assertSame(Nullability::NotNull, $fact->nullability);
46    }
47
48    public function testDeriveScalarReportsValuesWithoutACommonType(): void
49    {
50        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
51        $fact = $derivation->scalar(new ArrayConstructor(new ArrayItems([new Constant(new IntegerConstant('1')), new BooleanLiteral(true)])), $derivation->environment());
52        self::assertInstanceOf(Invalid::class, $fact->type);
53        self::assertInstanceOf(TypeConflict::class, $fact->type->cause);
54    }
55
56    public function testRenderWritesArray(): void
57    {
58        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
59        (new ArrayConstructor(new ArrayItems()))->render($out);
60        self::assertSame('ARRAY []', (new Lexical())->join($out->pieces()));
61    }
62}
63