Repository navigation
Expand file tree
/
Copy pathArrayAccessGenericDimensionAssignmentTest.php
More file actions
290 lines (240 loc) · 7.75 KB
/
Copy pathArrayAccessGenericDimensionAssignmentTest.php
File metadata and controls
290 lines (240 loc) · 7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
declare(strict_types=1);
namespace TypePHP\Tests\TypeChecking\Generics;
use ArrayAccess;
use ArrayObject;
use Countable;
use TypePHP\Exception\TypeError;
use TypePHP\Internal\Util\Config;
use TypePHP\Tests\Fixtures\Domain\Car;
use TypePHP\Tests\Fixtures\Domain\Cat;
use TypePHP\Tests\Fixtures\Domain\Dog;
use TypePHP\TypePHP;
/**
* Simulates Ramsey\Collection\Collection<T>
*
* @template T
*
* @implements ArrayAccess<array-key, T>
*/
class ReproGenericCollection implements ArrayAccess, Countable
{
/**
* @var array<array-key, T>
*/
private array $items = [];
/**
* @param string $collectionType
* @param array<array-key, T> $data
*/
public function __construct(
private readonly string $collectionType = 'mixed',
array $data = []
) {
$this->items = $data;
}
/**
* @param T $element
*/
public function add(mixed $element): bool
{
$this[] = $element;
return true;
}
public function offsetExists(mixed $offset): bool
{
return isset($this->items[$offset]);
}
/**
* @return T|null
*/
public function offsetGet(mixed $offset): mixed
{
return $this->items[$offset] ?? null;
}
/**
* @param array-key|null $offset
* @param T $value
*/
public function offsetSet(mixed $offset, mixed $value): void
{
if ($offset === null) {
$this->items[] = $value;
} else {
$this->items[$offset] = $value;
}
}
public function offsetUnset(mixed $offset): void
{
unset($this->items[$offset]);
}
public function count(): int
{
return \count($this->items);
}
}
/**
* Simulates Ramsey\Collection\Map\TypedMap<K, T>
*
* @template K of array-key
* @template T
*
* @implements ArrayAccess<K, T>
*/
class ReproGenericTypedMap implements ArrayAccess, Countable
{
/**
* @var array<K, T>
*/
private array $items = [];
public function __construct(
private readonly string $keyType = 'string',
private readonly string $valueType = 'mixed'
) {
}
/**
* @param K $key
* @param T $value
*/
public function put(mixed $key, mixed $value): void
{
$this[$key] = $value;
}
public function offsetExists(mixed $offset): bool
{
return isset($this->items[$offset]);
}
/**
* @param K $offset
*
* @return T|null
*/
public function offsetGet(mixed $offset): mixed
{
return $this->items[$offset] ?? null;
}
/**
* @param K|null $offset
* @param T $value
*/
public function offsetSet(mixed $offset, mixed $value): void
{
if ($offset === null) {
$this->items[] = $value;
} else {
$this->items[$offset] = $value;
}
}
public function offsetUnset(mixed $offset): void
{
unset($this->items[$offset]);
}
public function count(): int
{
return \count($this->items);
}
}
class ReproPropertyCollectionHolder
{
/**
* @var ReproGenericCollection<Dog>
*/
public ReproGenericCollection $dogs;
/**
* @var ReproGenericTypedMap<non-empty-string, positive-int>
*/
public ReproGenericTypedMap $scores;
public function __construct()
{
$this->dogs = new ReproGenericCollection(Dog::class);
$this->scores = new ReproGenericTypedMap('string', 'int');
}
}
describe('Generic ArrayAccess Dimension Assignment ($collection[] = $val & $map[$k] = $val)', function () {
beforeEach(function () {
Config::set(['array_validation' => 'full']);
});
afterEach(function () {
Config::reset();
});
describe('1. Single-Template Generic Collection<T> (Ramsey Collection Reproduction)', function () {
test('enforces generic type T on add() and array append $dogs[] = ... in full validation mode', function () {
/** @var ReproGenericCollection<Dog> $dogs */
$dogs = new ReproGenericCollection(Dog::class);
$dogs->add(new Dog());
$dogs[] = new Dog();
expect($dogs->count())->toBe(2)
->and(TypePHP::getGenericType($dogs))->toBe(Dog::class)
;
expect(function () use ($dogs) {
$dogs[] = new Car();
})->toThrow(TypeError::class, Dog::class);
});
test('enforces positive-int, array shapes, and unions on Collection<T> with $collection[] = ...', function () {
/** @var ReproGenericCollection<positive-int> $scores */
$scores = new ReproGenericCollection('int');
$scores->add(100);
$scores[] = 200;
expect($scores->count())->toBe(2);
expect(function () use ($scores) {
$scores[] = -50;
})->toThrow(TypeError::class, 'positive-int');
/** @var ReproGenericCollection<Dog|Cat> $pets */
$pets = new ReproGenericCollection('object');
$pets[] = new Dog();
$pets[] = new Cat();
expect($pets->count())->toBe(2);
expect(function () use ($pets) {
$pets[] = new Car();
})->toThrow(TypeError::class);
});
});
describe('2. Two-Template Generic TypedMap<K, T> (Ramsey TypedMap Reproduction)', function () {
test('enforces both Key (K) and Value (T) generics on put() and $map[$k] = ... in full validation mode', function () {
/** @var ReproGenericTypedMap<string, Dog> $map */
$map = new ReproGenericTypedMap('string', Dog::class);
$map->put('primary', new Dog());
$map['secondary'] = new Dog();
expect($map->count())->toBe(2)
->and(TypePHP::getGenericType($map, 'K'))->toBe('string')
->and(TypePHP::getGenericType($map, 'T'))->toBe(Dog::class)
;
expect(function () use ($map) {
$map['tertiary'] = new Car();
})->toThrow(TypeError::class, Dog::class);
expect(function () use ($map) {
$map[123] = new Dog();
})->toThrow(TypeError::class, 'key must be of type string');
});
test('enforces generic key and value on native SPL ArrayObject<K, V> dimension assignments', function () {
/** @var ArrayObject<non-empty-string, positive-int> $ao */
$ao = new ArrayObject();
$ao['alpha'] = 10;
expect($ao['alpha'])->toBe(10);
expect(function () use ($ao) {
$ao[''] = 20;
})->toThrow(TypeError::class, 'key must be of type non-empty-string');
expect(function () use ($ao) {
$ao['beta'] = -5;
})->toThrow(TypeError::class, 'positive-int');
});
});
describe('3. Class Properties Holding Generic ArrayAccess Objects ($this->dogs[] = ...)', function () {
test('validates dimension assignments on object properties without converting property to array', function () {
$holder = new ReproPropertyCollectionHolder();
$holder->dogs[] = new Dog();
expect($holder->dogs->count())->toBe(1);
expect(function () use ($holder) {
$holder->dogs[] = new Car();
})->toThrow(TypeError::class, Dog::class);
$holder->scores['first'] = 100;
expect($holder->scores->count())->toBe(1);
expect(function () use ($holder) {
$holder->scores[''] = 100;
})->toThrow(TypeError::class, 'key must be of type non-empty-string');
expect(function () use ($holder) {
$holder->scores['second'] = -10;
})->toThrow(TypeError::class, 'positive-int');
});
});
});