packages/sql-semantics-sqlite/tests/Unit/Model/Statement/Shape/OutputSlotTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Model\Statement\Shape;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Declaration\Column;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Missing\SessionState;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\OutputSlot;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
16use SqlSemantics\Platform\Sqlite\Semantics;
17use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
18
19#[CoversClass(OutputSlot::class)]
20#[Medium]
21final class OutputSlotTest extends TestCase
22{
23    public function testDeclarationFollowsReExposedSlotsToTheColumn(): void
24    {
25        $column = new Column(new Name('a'), Storage::Integer, Nullability::NotNull);
26        $base = new OutputSlot($column->name, new Known($column->type), $column->nullability, $column);
27        $joined = new OutputSlot($column->name, $base->type, Nullability::Nullable, null, $base);
28        $projected = new OutputSlot(new Name('x'), $joined->type, $joined->nullability, null, $joined);
29
30        self::assertSame($column, $projected->declaration());
31        self::assertNull($projected->column);
32        self::assertSame(Nullability::Nullable, $projected->nullability);
33    }
34
35    public function testDeclarationIsNullWhenNoColumnIsReached(): void
36    {
37        $computed = new OutputSlot(null, new Known(Storage::Text), Nullability::NotNull);
38
39        self::assertNull($computed->declaration());
40        self::assertNull((new OutputSlot(new Name('x'), $computed->type, $computed->nullability, null, $computed))->declaration());
41    }
42
43    public function testDeclarationOfAnOuterJoinSlotIsTheUnchangedColumn(): void
44    {
45        $semantics = new Semantics();
46        $t = $semantics->analyze('CREATE TABLE t (a INTEGER NOT NULL)')->declarations()[0];
47        $u = $semantics->analyze('CREATE TABLE u (a INTEGER)')->declarations()[0];
48
49        $slot = $semantics->analyze('SELECT t.a FROM u LEFT JOIN t ON u.a = t.a', [$t, $u])->field(0)->slot;
50
51        self::assertSame(Nullability::Nullable, $slot->nullability);
52        self::assertSame($t->columns[0], $slot->declaration());
53        self::assertSame(Nullability::NotNull, $t->columns[0]->nullability);
54        self::assertNotNull($slot->origin);
55    }
56
57    public function testUnnamedKeepsTheInputsAnUnknownNameDependsOn(): void
58    {
59        $missing = new SessionState('the client character set');
60
61        $slot = new OutputSlot(null, new Known(Storage::Text), Nullability::Nullable, null, null, [$missing]);
62
63        self::assertSame([$missing], $slot->unnamed);
64    }
65
66    public function testUnnamedRefusesInputsForAPositionThatHasAName(): void
67    {
68        $this->expectExceptionMessage('Only a position without a name depends on inputs for its name.');
69
70        new OutputSlot(new Name('a'), new Known(Storage::Text), Nullability::Nullable, null, null, [new SessionState('the client character set')]);
71    }
72}
73