packages/sql-semantics-sqlite/tests/Unit/Model/Statement/Shape/FieldsTest.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\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Comparison;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Missing\SessionState;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\AbsentField;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\AmbiguousFields;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\DependentField;
16use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\Field;
17use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\Fields;
18use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\OutputSlot;
19use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\UniqueField;
20use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
21use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
22use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
23
24#[CoversClass(Fields::class)]
25#[Small]
26final class FieldsTest extends TestCase
27{
28 public function testCountIsTheNumberOfOutputPositions(): void
29 {
30 $fields = new Fields([
31 new Field(0, new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull)),
32 new Field(1, new OutputSlot(null, new Known(Storage::Text), Nullability::Nullable)),
33 ], Comparison::Sensitive);
34
35 self::assertCount(2, $fields);
36 self::assertSame(2, $fields->count());
37 self::assertCount(0, new Fields([], Comparison::Sensitive));
38 }
39
40 public function testGetIteratorWalksTheFieldsInOutputOrder(): void
41 {
42 $first = new Field(0, new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull));
43 $second = new Field(1, new OutputSlot(new Name('b'), new Known(Storage::Text), Nullability::Nullable));
44 $fields = new Fields([$first, $second], Comparison::Sensitive);
45
46 self::assertSame([$first, $second], iterator_to_array($fields));
47 self::assertSame([$first, $second], $fields->items);
48 }
49
50 public function testAtAnswersTheFieldAtAPosition(): void
51 {
52 $second = new Field(1, new OutputSlot(new Name('b'), new Known(Storage::Text), Nullability::Nullable));
53 $fields = new Fields([new Field(0, new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull)), $second], Comparison::Sensitive);
54
55 self::assertSame($second, $fields->at(1));
56 }
57
58 public function testAtRefusesAPositionOutsideTheList(): void
59 {
60 $fields = new Fields([new Field(0, new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull))], Comparison::Sensitive);
61
62 $this->expectExceptionMessage('No field exists at the requested position.');
63
64 $fields->at(1);
65 }
66
67 public function testLookupTellsOneNoneAndSeveralApart(): void
68 {
69 $first = new Field(0, new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull));
70 $second = new Field(1, new OutputSlot(new Name('A'), new Known(Storage::Text), Nullability::Nullable));
71 $third = new Field(2, new OutputSlot(new Name('b'), new Known(Storage::Text), Nullability::Nullable));
72 $fourth = new Field(3, new OutputSlot(null, new Known(Storage::Text), Nullability::Nullable));
73 $fields = new Fields([$first, $second, $third, $fourth], Comparison::AsciiInsensitive);
74
75 $ambiguous = $fields->lookup('a');
76 $unique = $fields->lookup('B');
77
78 self::assertInstanceOf(AmbiguousFields::class, $ambiguous);
79 self::assertSame([$first, $second], $ambiguous->fields);
80 self::assertInstanceOf(UniqueField::class, $unique);
81 self::assertSame($third, $unique->field);
82 self::assertInstanceOf(AbsentField::class, $fields->lookup('c'));
83 }
84
85 public function testLookupIsSensitiveWhenTheProfileSaysSo(): void
86 {
87 $first = new Field(0, new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull));
88 $second = new Field(1, new OutputSlot(new Name('A'), new Known(Storage::Text), Nullability::Nullable));
89 $fields = new Fields([$first, $second], Comparison::Sensitive);
90
91 $lookup = $fields->lookup('A');
92
93 self::assertInstanceOf(UniqueField::class, $lookup);
94 self::assertSame($second, $lookup->field);
95 }
96
97 public function testLookupIsNotReachedWhenAFieldHoldsAnotherPosition(): void
98 {
99 $this->expectExceptionMessage('Each field holds its own output position.');
100
101 new Fields([new Field(1, new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull))], Comparison::Sensitive);
102 }
103
104 public function testLookupIsDependentWhileAFieldNameDependsOnMissingInputs(): void
105 {
106 $missing = new SessionState('the client character set');
107 $known = new Field(0, new OutputSlot(new Name('a'), new Known(Storage::Integer), Nullability::NotNull));
108 $unnamed = new Field(1, new OutputSlot(null, new Known(Storage::Text), Nullability::Nullable, null, null, [$missing]));
109
110 $lookup = (new Fields([$known, $unnamed], Comparison::AsciiInsensitive))->lookup('a');
111
112 self::assertInstanceOf(DependentField::class, $lookup);
113 self::assertSame([$known], $lookup->candidates);
114 self::assertSame([$missing], $lookup->missing);
115 }
116}
117