|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author SignpostMarv |
|
4
|
|
|
*/ |
|
5
|
|
|
declare(strict_types=1); |
|
6
|
|
|
|
|
7
|
|
|
namespace SignpostMarv\DaftObject\Tests; |
|
8
|
|
|
|
|
9
|
|
|
use Generator; |
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
use RuntimeException; |
|
12
|
|
|
use SignpostMarv\DaftObject\DaftObjectMemoryRepository; |
|
13
|
|
|
use SignpostMarv\DaftObject\DaftObjectRepository; |
|
14
|
|
|
use SignpostMarv\DaftObject\DaftObjectRepositoryTypeException; |
|
15
|
|
|
use SignpostMarv\DaftObject\DefinesOwnIdPropertiesInterface; |
|
16
|
|
|
use SignpostMarv\DaftObject\ReadOnly; |
|
17
|
|
|
use SignpostMarv\DaftObject\ReadWrite; |
|
18
|
|
|
use SignpostMarv\DaftObject\ReadWriteTwoColumnPrimaryKey; |
|
19
|
|
|
|
|
20
|
|
|
class DaftObjectRepositoryTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
public static function DaftObjectRepositoryByType( |
|
23
|
|
|
string $type |
|
24
|
|
|
) : DaftObjectRepository { |
|
25
|
|
|
return DaftObjectMemoryRepository::DaftObjectRepositoryByType( |
|
26
|
|
|
$type |
|
27
|
|
|
); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public static function DaftObjectRepositoryByDaftObject( |
|
31
|
|
|
DefinesOwnIdPropertiesInterface $object |
|
32
|
|
|
) : DaftObjectRepository { |
|
33
|
|
|
return DaftObjectMemoryRepository::DaftObjectRepositoryByDaftObject( |
|
34
|
|
|
$object |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function RepositoryDataProvider() : Generator |
|
39
|
|
|
{ |
|
40
|
|
|
$arrayParams = $this->RepositoryDataProviderParams(); |
|
41
|
|
|
foreach ( |
|
42
|
|
|
[ |
|
43
|
|
|
ReadWrite::class, |
|
44
|
|
|
ReadWriteTwoColumnPrimaryKey::class, |
|
45
|
|
|
] as $className |
|
46
|
|
|
) { |
|
47
|
|
|
yield array_merge( |
|
48
|
|
|
[ |
|
49
|
|
|
$className, |
|
50
|
|
|
true, |
|
51
|
|
|
true, |
|
52
|
|
|
], |
|
53
|
|
|
$arrayParams |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function DaftObjectRepositoryTypeExceptionForgetRemoveDataProvider( |
|
59
|
|
|
) : array { |
|
60
|
|
|
return [ |
|
61
|
|
|
[ |
|
62
|
|
|
ReadWrite::class, |
|
63
|
|
|
ReadOnly::class, |
|
64
|
|
|
[ |
|
65
|
|
|
'Foo' => '1', |
|
66
|
|
|
], |
|
67
|
|
|
[ |
|
68
|
|
|
'Foo' => '1', |
|
69
|
|
|
], |
|
70
|
|
|
], |
|
71
|
|
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @dataProvider RepositoryDataProvider |
|
76
|
|
|
*/ |
|
77
|
|
|
public function testRepositoryForImplementaion( |
|
78
|
|
|
string $objImplementation, |
|
79
|
|
|
bool $readable, |
|
|
|
|
|
|
80
|
|
|
bool $writeable, |
|
81
|
|
|
array ...$paramsArray |
|
82
|
|
|
) : void { |
|
83
|
|
|
$interfaceCheck = $objImplementation; |
|
84
|
|
|
|
|
85
|
|
|
if ( |
|
86
|
|
|
false === is_a( |
|
87
|
|
|
$interfaceCheck, |
|
88
|
|
|
DefinesOwnIdPropertiesInterface::class, |
|
89
|
|
|
true |
|
90
|
|
|
) |
|
91
|
|
|
) { |
|
92
|
|
|
throw new InvalidArgumentException( |
|
93
|
|
|
sprintf( |
|
94
|
|
|
'Argument 1 passed to %s must be an implementation of %s', |
|
95
|
|
|
__METHOD__, |
|
96
|
|
|
DefinesOwnIdPropertiesInterface::class |
|
97
|
|
|
) |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$repo = static::DaftObjectRepositoryByType( |
|
102
|
|
|
$objImplementation |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
$idProps = []; |
|
106
|
|
|
|
|
107
|
|
|
foreach ($objImplementation::DaftObjectIdProperties() as $idProp) { |
|
108
|
|
|
$idProps[] = $idProp; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
foreach ($paramsArray as $params) { |
|
112
|
|
|
/** |
|
113
|
|
|
* @var DefinesOwnIdPropertiesInterface $obj |
|
114
|
|
|
*/ |
|
115
|
|
|
$obj = new $objImplementation($params, $writeable); |
|
116
|
|
|
|
|
117
|
|
|
$repoByObject = static::DaftObjectRepositoryByDaftObject( |
|
118
|
|
|
$obj |
|
119
|
|
|
); |
|
120
|
|
|
|
|
121
|
|
|
$this->assertSame(get_class($repo), get_class($repoByObject)); |
|
122
|
|
|
|
|
123
|
|
|
$ids = []; |
|
124
|
|
|
|
|
125
|
|
|
$repo->RememberDaftObject($obj); |
|
126
|
|
|
|
|
127
|
|
|
$props = array_values($idProps); |
|
128
|
|
|
|
|
129
|
|
|
foreach ($props as $prop) { |
|
130
|
|
|
$ids[] = $obj->$prop; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
if (1 === count($ids)) { |
|
134
|
|
|
$this->assertSame($obj, $repo->RecallDaftObject($ids[0])); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$this->assertSame($obj, $repo->RecallDaftObject($ids)); |
|
138
|
|
|
|
|
139
|
|
|
if (count($ids) < 1) { |
|
140
|
|
|
throw new RuntimeException( |
|
141
|
|
|
'Insufficient Id properties found!' |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$repo->ForgetDaftObject($obj); |
|
146
|
|
|
|
|
147
|
|
|
$retrieved = $repo->RecallDaftObject($ids); |
|
148
|
|
|
|
|
149
|
|
|
$this->assertNotNull($retrieved); |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @var DefinesOwnIdPropertiesInterface $retrieved |
|
153
|
|
|
*/ |
|
154
|
|
|
$retrieved = $retrieved; |
|
155
|
|
|
|
|
156
|
|
|
$this->assertSame( |
|
157
|
|
|
$objImplementation::DaftObjectIdHash($obj), |
|
158
|
|
|
$objImplementation::DaftObjectIdHash($retrieved) |
|
159
|
|
|
); |
|
160
|
|
|
$this->assertSame(get_class($obj), get_class($retrieved)); |
|
161
|
|
|
$this->assertNotSame($obj, $retrieved); |
|
162
|
|
|
|
|
163
|
|
|
foreach ($objImplementation::DaftObjectProperties() as $prop) { |
|
164
|
|
|
if ( |
|
165
|
|
|
true === method_exists($obj, 'Get' . ucfirst($prop)) && |
|
166
|
|
|
true === method_exists($retrieved, 'Get' . ucfirst($prop)) |
|
167
|
|
|
) { |
|
168
|
|
|
$this->assertSame($obj->$prop, $retrieved->$prop); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
$repo->RemoveDaftObject($obj); |
|
173
|
|
|
|
|
174
|
|
|
$this->assertNull($repo->RecallDaftObject($ids)); |
|
175
|
|
|
|
|
176
|
|
|
foreach ($objImplementation::DaftObjectProperties() as $prop) { |
|
177
|
|
|
if ( |
|
178
|
|
|
false === in_array($prop, $idProps, true) && |
|
179
|
|
|
true === method_exists($obj, 'Set' . ucfirst($prop)) && |
|
180
|
|
|
true === method_exists( |
|
181
|
|
|
$retrieved, 'Get' . ucfirst($prop) |
|
182
|
|
|
) && |
|
183
|
|
|
true === is_numeric($obj->$prop) |
|
184
|
|
|
) { |
|
185
|
|
|
$retrieved->$prop *= 2; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
$repo->RememberDaftObject($retrieved); |
|
190
|
|
|
$repo->ForgetDaftObject($obj); |
|
191
|
|
|
$repo->ForgetDaftObject($retrieved); |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @var DefinesOwnIdPropertiesInterface $retrieved |
|
195
|
|
|
*/ |
|
196
|
|
|
$retrieved = $repo->RecallDaftObject($ids); |
|
197
|
|
|
|
|
198
|
|
|
$this->assertTrue( |
|
199
|
|
|
is_a($retrieved, $objImplementation, true), |
|
200
|
|
|
( |
|
201
|
|
|
get_class($repo) . |
|
202
|
|
|
'::RecallDaftObject() must return an implementation of ' . |
|
203
|
|
|
$objImplementation |
|
204
|
|
|
) |
|
205
|
|
|
); |
|
206
|
|
|
|
|
207
|
|
|
foreach ($objImplementation::DaftObjectProperties() as $prop) { |
|
208
|
|
|
if ( |
|
209
|
|
|
false === in_array($prop, $idProps, true) && |
|
210
|
|
|
true === method_exists($obj, 'Set' . ucfirst($prop)) && |
|
211
|
|
|
true === method_exists( |
|
212
|
|
|
$retrieved, 'Get' . ucfirst($prop) |
|
213
|
|
|
) && |
|
214
|
|
|
true === is_numeric($obj->$prop) |
|
215
|
|
|
) { |
|
216
|
|
|
$this->assertSame($obj->$prop * 2, $retrieved->$prop); |
|
217
|
|
|
$retrieved->$prop /= 2; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
$repo->RememberDaftObject($retrieved); |
|
222
|
|
|
|
|
223
|
|
|
$repo->RemoveDaftObject($retrieved); |
|
224
|
|
|
|
|
225
|
|
|
$this->assertNull($repo->RecallDaftObject($ids)); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @dataProvider DaftObjectRepositoryTypeExceptionForgetRemoveDataProvider |
|
231
|
|
|
*/ |
|
232
|
|
|
public function testForgetDaftObjectRepositoryTypeException( |
|
233
|
|
|
string $objectTypeA, |
|
234
|
|
|
string $objectTypeB, |
|
235
|
|
|
array $dataTypeA, |
|
236
|
|
|
array $dataTypeB |
|
237
|
|
|
) : void { |
|
238
|
|
|
/** |
|
239
|
|
|
* @var DefinesOwnIdPropertiesInterface $A |
|
240
|
|
|
*/ |
|
241
|
|
|
$A = new $objectTypeA($dataTypeA); |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @var DefinesOwnIdPropertiesInterface $B |
|
245
|
|
|
*/ |
|
246
|
|
|
$B = new $objectTypeB($dataTypeB); |
|
247
|
|
|
|
|
248
|
|
|
$repo = static::DaftObjectRepositoryByDaftObject($A); |
|
249
|
|
|
|
|
250
|
|
|
$this->expectException(DaftObjectRepositoryTypeException::class); |
|
251
|
|
|
$this->expectExceptionMessage( |
|
252
|
|
|
'Argument 1 passed to ' . |
|
253
|
|
|
get_class($repo) . |
|
254
|
|
|
'::ForgetDaftObject() must be an implementation of ' . |
|
255
|
|
|
$objectTypeA . |
|
256
|
|
|
', ' . |
|
257
|
|
|
$objectTypeB . |
|
258
|
|
|
' given.' |
|
259
|
|
|
); |
|
260
|
|
|
|
|
261
|
|
|
$repo->ForgetDaftObject($B); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @dataProvider DaftObjectRepositoryTypeExceptionForgetRemoveDataProvider |
|
266
|
|
|
*/ |
|
267
|
|
|
public function testRemoveDaftObjectRepositoryTypeException( |
|
268
|
|
|
string $objectTypeA, |
|
269
|
|
|
string $objectTypeB, |
|
270
|
|
|
array $dataTypeA, |
|
271
|
|
|
array $dataTypeB |
|
272
|
|
|
) : void { |
|
273
|
|
|
/** |
|
274
|
|
|
* @var DefinesOwnIdPropertiesInterface $A |
|
275
|
|
|
*/ |
|
276
|
|
|
$A = new $objectTypeA($dataTypeA); |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* @var DefinesOwnIdPropertiesInterface $B |
|
280
|
|
|
*/ |
|
281
|
|
|
$B = new $objectTypeB($dataTypeB); |
|
282
|
|
|
|
|
283
|
|
|
$repo = static::DaftObjectRepositoryByDaftObject($A); |
|
284
|
|
|
|
|
285
|
|
|
$this->expectException(DaftObjectRepositoryTypeException::class); |
|
286
|
|
|
$this->expectExceptionMessage( |
|
287
|
|
|
'Argument 1 passed to ' . |
|
288
|
|
|
get_class($repo) . |
|
289
|
|
|
'::RemoveDaftObject() must be an implementation of ' . |
|
290
|
|
|
$objectTypeA . |
|
291
|
|
|
', ' . |
|
292
|
|
|
$objectTypeB . |
|
293
|
|
|
' given.' |
|
294
|
|
|
); |
|
295
|
|
|
|
|
296
|
|
|
$repo->RemoveDaftObject($B); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* @dataProvider DaftObjectRepositoryTypeExceptionForgetRemoveDataProvider |
|
301
|
|
|
*/ |
|
302
|
|
|
public function testRememberDaftObjectRepositoryTypeException( |
|
303
|
|
|
string $objectTypeA, |
|
304
|
|
|
string $objectTypeB, |
|
305
|
|
|
array $dataTypeA, |
|
306
|
|
|
array $dataTypeB |
|
307
|
|
|
) : void { |
|
308
|
|
|
/** |
|
309
|
|
|
* @var DefinesOwnIdPropertiesInterface $A |
|
310
|
|
|
*/ |
|
311
|
|
|
$A = new $objectTypeA($dataTypeA); |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* @var DefinesOwnIdPropertiesInterface $B |
|
315
|
|
|
*/ |
|
316
|
|
|
$B = new $objectTypeB($dataTypeB); |
|
317
|
|
|
|
|
318
|
|
|
$repo = static::DaftObjectRepositoryByDaftObject($A); |
|
319
|
|
|
|
|
320
|
|
|
$this->expectException(DaftObjectRepositoryTypeException::class); |
|
321
|
|
|
$this->expectExceptionMessage( |
|
322
|
|
|
'Argument 1 passed to ' . |
|
323
|
|
|
get_class($repo) . |
|
324
|
|
|
'::RememberDaftObject() must be an implementation of ' . |
|
325
|
|
|
$objectTypeA . |
|
326
|
|
|
', ' . |
|
327
|
|
|
$objectTypeB . |
|
328
|
|
|
' given.' |
|
329
|
|
|
); |
|
330
|
|
|
|
|
331
|
|
|
$repo->RememberDaftObject($B); |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
protected function RepositoryDataProviderParams() : array |
|
335
|
|
|
{ |
|
336
|
|
|
return [ |
|
337
|
|
|
[ |
|
338
|
|
|
'Foo' => '1', |
|
339
|
|
|
'Bar' => 1.0, |
|
340
|
|
|
'Baz' => 1, |
|
341
|
|
|
], |
|
342
|
|
|
[ |
|
343
|
|
|
'Foo' => '2', |
|
344
|
|
|
'Bar' => 2.0, |
|
345
|
|
|
'Baz' => 2, |
|
346
|
|
|
], |
|
347
|
|
|
[ |
|
348
|
|
|
'Foo' => '3', |
|
349
|
|
|
'Bar' => 3.0, |
|
350
|
|
|
'Baz' => 3, |
|
351
|
|
|
], |
|
352
|
|
|
[ |
|
353
|
|
|
'Foo' => '4', |
|
354
|
|
|
'Bar' => 4.0, |
|
355
|
|
|
'Baz' => 4, |
|
356
|
|
|
], |
|
357
|
|
|
[ |
|
358
|
|
|
'Foo' => '5', |
|
359
|
|
|
'Bar' => 5.0, |
|
360
|
|
|
'Baz' => 5, |
|
361
|
|
|
], |
|
362
|
|
|
[ |
|
363
|
|
|
'Foo' => '6', |
|
364
|
|
|
'Bar' => 6.0, |
|
365
|
|
|
'Baz' => 6, |
|
366
|
|
|
], |
|
367
|
|
|
[ |
|
368
|
|
|
'Foo' => '7', |
|
369
|
|
|
'Bar' => 7.0, |
|
370
|
|
|
'Baz' => 7, |
|
371
|
|
|
], |
|
372
|
|
|
[ |
|
373
|
|
|
'Foo' => '8', |
|
374
|
|
|
'Bar' => 8.0, |
|
375
|
|
|
'Baz' => 8, |
|
376
|
|
|
], |
|
377
|
|
|
[ |
|
378
|
|
|
'Foo' => '9', |
|
379
|
|
|
'Bar' => 9.0, |
|
380
|
|
|
'Baz' => 9, |
|
381
|
|
|
], |
|
382
|
|
|
[ |
|
383
|
|
|
'Foo' => '10', |
|
384
|
|
|
'Bar' => 10.0, |
|
385
|
|
|
'Baz' => 10, |
|
386
|
|
|
], |
|
387
|
|
|
]; |
|
388
|
|
|
} |
|
389
|
|
|
} |
|
390
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.