packages/sql-semantics-sqlite/tests/Unit/Model/Statement/Shape/AmbiguousFieldsTest.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\Shape\AmbiguousFields;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\Field;
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(AmbiguousFields::class)]
20#[Medium]
21final class AmbiguousFieldsTest extends TestCase
22{
23 public function testFieldsAreEveryFieldWithTheNameInOutputOrder(): void
24 {
25 $lookup = (new Semantics())->analyze('SELECT 1 AS a, 2 AS b, 3 AS a')->lookupField('a');
26
27 self::assertInstanceOf(AmbiguousFields::class, $lookup);
28 self::assertSame('a', $lookup->name);
29 self::assertSame([0, 2], array_map(static fn (Field $field): int => $field->position, $lookup->fields));
30 }
31
32 public function testFieldsAreAtLeastTwo(): void
33 {
34 $field = new Field(0, new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull));
35
36 $this->expectExceptionMessage('An ambiguous lookup has at least two fields.');
37
38 new AmbiguousFields('a', [$field]);
39 }
40}
41