packages/sql-semantics-sqlite/tests/Unit/Model/Statement/Reference/Column/ResolvedColumnTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Model\Statement\Reference\Column;
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\Column\ResolvedColumn;
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\Relation\TableInput;
18use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
19
20#[CoversClass(ResolvedColumn::class)]
21#[Medium]
22final class ResolvedColumnTest extends TestCase
23{
24 public function testDeclarationReachesTheSuppliedColumnThroughTheOccurrence(): void
25 {
26 $semantics = new Semantics();
27 $table = $semantics->analyze('CREATE TABLE t (a INTEGER)');
28 $query = $semantics->analyze('SELECT a FROM t', [$table]);
29
30 $resolution = $query->field('a')->resolution;
31
32 self::assertInstanceOf(ResolvedColumn::class, $resolution);
33 self::assertSame($query->inputRelation(), $resolution->relation);
34 self::assertSame($table->declarations()[0]->columns[0], $resolution->declaration());
35 self::assertSame($query->facts->relation($query->singleNamedInput())->shape->slots[0], $resolution->slot);
36 self::assertSame(0, $resolution->depth);
37 }
38
39 public function testDeclarationIsNullForASlotOfADerivedQuery(): void
40 {
41 $query = (new Semantics())->analyze('SELECT x FROM (SELECT 1 AS x)');
42
43 $resolution = $query->field('x')->resolution;
44
45 self::assertInstanceOf(ResolvedColumn::class, $resolution);
46 self::assertNull($resolution->declaration());
47 }
48
49 public function testDeclarationIsNotReachedForANegativeDepth(): void
50 {
51 $slot = new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull);
52
53 $this->expectExceptionMessage('A correlation depth is not negative.');
54
55 new ResolvedColumn(new TableInput(new QualifiedName(new Name('t'))), $slot, -1);
56 }
57}
58