packages/sql-semantics-sqlite/tests/Unit/Model/Statement/Shape/RowShapeTest.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\Identifier\Name;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Missing\UndeclaredRelation;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\OutputSlot;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\RowShape;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
16use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\Sqlite\Semantics;
18use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
19
20#[CoversClass(RowShape::class)]
21#[Medium]
22final class RowShapeTest extends TestCase
23{
24    public function testCompleteHoldsWhenNoInputIsMissing(): void
25    {
26        $slot = new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull);
27
28        $shape = new RowShape([$slot]);
29
30        self::assertTrue($shape->complete());
31        self::assertSame([$slot], $shape->slots);
32        self::assertSame([], $shape->missing);
33    }
34
35    public function testCompleteFailsWhileAnInputIsMissingAndTheKnownSlotsStayExact(): void
36    {
37        $slot = new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull);
38        $missing = new UndeclaredRelation(new QualifiedName(new Name('t')));
39
40        $shape = new RowShape([$slot], [$missing]);
41
42        self::assertFalse($shape->complete());
43        self::assertSame([$slot], $shape->slots);
44        self::assertSame([$missing], $shape->missing);
45    }
46
47    public function testCompleteOfAnAnalyzedStarOverDeclaredTables(): void
48    {
49        $semantics = new Semantics();
50        $table = $semantics->analyze('CREATE TABLE t (a INTEGER, b TEXT)');
51
52        $shape = $semantics->analyze('SELECT * FROM t', [$table])->shape();
53
54        self::assertNotNull($shape);
55        self::assertTrue($shape->complete());
56        self::assertSame(['a', 'b'], array_map(static fn (OutputSlot $slot): ?string => $slot->name?->value, $shape->slots));
57    }
58}
59