packages/sql-semantics-sqlite/tests/Unit/Model/Validation/ValueGraphTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Model\Validation;
6
7use ArrayIterator;
8use PHPUnit\Framework\Attributes\CoversClass;
9use PHPUnit\Framework\Attributes\Small;
10use PHPUnit\Framework\TestCase;
11use SqlSemantics\Platform\Sqlite\Model\Construction\Derivation;
12use SqlSemantics\Platform\Sqlite\Model\Contract\SearchPath;
13use SqlSemantics\Platform\Sqlite\Model\Lowering\Leaves;
14use SqlSemantics\Platform\Sqlite\Model\Rendering\Output;
15use SqlSemantics\Platform\Sqlite\Model\Resolution\Environment;
16use SqlSemantics\Platform\Sqlite\Model\Resolution\VisibleRelation;
17use SqlSemantics\Platform\Sqlite\Model\Statement\Fact\RelationFact;
18use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
19use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
20use SqlSemantics\Platform\Sqlite\Model\Statement\Relation;
21use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\RowShape;
22use SqlSemantics\Platform\Sqlite\Model\Validation\ValueGraph;
23use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\IntegerLiteral;
24use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
25use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
26use SqlSemantics\Platform\Sqlite\Statement\Query\SetQuantifier;
27use SqlSemantics\Platform\Sqlite\Statement\Relation\TableInput;
28
29#[CoversClass(ValueGraph::class)]
30#[Small]
31final class ValueGraphTest extends TestCase
32{
33 public function testObjectsListsEveryReachableObjectOnce(): void
34 {
35 $name = new Name('a');
36 $literal = new IntegerLiteral('1');
37 $statement = new Select([new ResultColumn($literal, $name)], null, null, [], null, [], [], null, SetQuantifier::Distinct);
38
39 $objects = (new ValueGraph(['SqlSemantics\\Platform\\Sqlite\\Model\\Statement\\', 'SqlSemantics\\Platform\\Sqlite\\Statement\\']))->objects($statement);
40
41 self::assertCount(5, $objects);
42 self::assertSame($statement, $objects[0]);
43 self::assertContains($literal, $objects);
44 self::assertContains($name, $objects);
45 self::assertContains(SetQuantifier::Distinct, $objects);
46 }
47
48 public function testObjectsRefusesAClassOutsideTheAdmittedNamespaces(): void
49 {
50 $graph = new ValueGraph(['SqlSemantics\\Platform\\Sqlite\\Model\\Statement\\', 'SqlSemantics\\Platform\\Sqlite\\Statement\\Query\\']);
51
52 $this->expectExceptionMessage('A value of class ' . IntegerLiteral::class . ' is not part of the closed semantic value domain.');
53
54 $graph->objects(new Select([new ResultColumn(new IntegerLiteral('1'))]));
55 }
56
57 public function testObjectsRefusesAPhpReferenceInsideAList(): void
58 {
59 $hidden = [1];
60 $alias = &$hidden[0];
61 $relation = new VisibleRelation(new TableInput(new QualifiedName(new Name('t'))), new RowShape([]), null, null, $hidden);
62
63 $this->expectExceptionMessage('A semantic value holds no PHP reference.');
64
65 (new ValueGraph(['SqlSemantics\\']))->objects($relation);
66 }
67
68 public function testObjectsRefusesAnAnonymousClassWhateverInterfaceItImplements(): void
69 {
70 $foreign = new class () implements Relation {
71 public function render(Output $out): void
72 {
73 }
74
75 public function deriveRelation(Derivation $derivation, Environment $environment): RelationFact
76 {
77 return new RelationFact(new RowShape([]));
78 }
79 };
80 $statement = new Select([new ResultColumn(new IntegerLiteral('1'))], $foreign);
81
82 $this->expectExceptionMessageMatches('/Semantic value class ' . preg_quote(Relation::class, '/') . '@anonymous.* must be final\.$/');
83
84 (new ValueGraph(['SqlSemantics\\Platform\\Sqlite\\Model\\Statement\\', 'SqlSemantics\\Platform\\Sqlite\\Statement\\']))->objects($statement);
85 }
86
87 public function testMembersRefusesAClassThatIsNotFinal(): void
88 {
89 $graph = new ValueGraph([ArrayIterator::class]);
90
91 $this->expectExceptionMessage('Semantic value class ArrayIterator must be final.');
92
93 $graph->members(new ArrayIterator([]));
94 }
95
96 public function testMembersRefusesAClassWithoutTheSnapshotTrait(): void
97 {
98 $graph = new ValueGraph(['SqlSemantics\\']);
99
100 $this->expectExceptionMessage('Semantic value class ' . SearchPath::class . ' must close cloning, dynamic properties and unserialization with the Snapshot trait.');
101
102 $graph->members(new SearchPath('main'));
103 }
104
105 public function testMembersRefusesAClassWithWritableProperties(): void
106 {
107 $graph = new ValueGraph(['SqlSemantics\\']);
108
109 $this->expectExceptionMessage('Semantic value class ' . Leaves::class . ' must have readonly properties only.');
110
111 $graph->members(new Leaves());
112 }
113
114 public function testMembersAnswersTheReadonlyPropertiesAndNothingForAnEnumCase(): void
115 {
116 $graph = new ValueGraph(['SqlSemantics\\Platform\\Sqlite\\Statement\\']);
117
118 self::assertSame(['digits'], array_map(static fn ($property): string => $property->getName(), $graph->members(new IntegerLiteral('1'))));
119 self::assertSame([], $graph->members(SetQuantifier::All));
120 }
121}
122