1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SlayerBirden\DFCodeGeneration\Generator\Tests; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
8
|
|
|
|
9
|
|
|
class BeforeTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var ObjectProphecy[] |
13
|
|
|
*/ |
14
|
|
|
private $providers = []; |
15
|
|
|
|
16
|
|
|
protected function setUp() |
17
|
|
|
{ |
18
|
|
|
$provider1 = $this->getUserProvider(1, [ |
19
|
|
|
[ |
20
|
|
|
'name' => 'id', |
21
|
|
|
'type' => 'integer', |
22
|
|
|
'nullable' => false, |
23
|
|
|
], |
24
|
|
|
[ |
25
|
|
|
'name' => 'name', |
26
|
|
|
'type' => 'string', |
27
|
|
|
'nullable' => false, |
28
|
|
|
], |
29
|
|
|
[ |
30
|
|
|
'name' => 'email', |
31
|
|
|
'type' => 'string', |
32
|
|
|
'nullable' => false, |
33
|
|
|
], |
34
|
|
|
[ |
35
|
|
|
'name' => 'group', |
36
|
|
|
'type' => 'manytoone', |
37
|
|
|
'entity' => 'Dummy\\Group', |
38
|
|
|
'nullable' => false, |
39
|
|
|
], |
40
|
|
|
], [ |
41
|
|
|
'name' => 'Bob', |
42
|
|
|
'email' => '[email protected]', |
43
|
|
|
'group' => 1, |
44
|
|
|
], [ |
45
|
|
|
'id' => 1, |
46
|
|
|
'name' => 'Bob', |
47
|
|
|
'email' => '[email protected]', |
48
|
|
|
]); |
49
|
|
|
|
50
|
|
|
$provider2 = $this->getGroupProvider(1, [ |
51
|
|
|
[ |
52
|
|
|
'name' => 'id', |
53
|
|
|
'type' => 'integer', |
54
|
|
|
'nullable' => false, |
55
|
|
|
], |
56
|
|
|
[ |
57
|
|
|
'name' => 'name', |
58
|
|
|
'type' => 'string', |
59
|
|
|
'nullable' => false, |
60
|
|
|
], |
61
|
|
|
], [ |
62
|
|
|
'name' => 'Default', |
63
|
|
|
], [ |
64
|
|
|
'id' => 1, |
65
|
|
|
'name' => 'Default', |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
$this->providers = [$provider1, $provider2]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function getUserProvider(int $id, array $spec, array $post, array $params) |
72
|
|
|
{ |
73
|
|
|
$provider = $this->prophesize(EntityProviderInterface::class); |
74
|
|
|
$provider->getId()->willReturn($id); |
75
|
|
|
$provider->getEntitySpec()->willReturn($spec); |
76
|
|
|
$provider->getPostParams()->willReturn($post); |
77
|
|
|
$provider->getParams()->willReturn($params); |
78
|
|
|
$provider->getBaseName()->willReturn('User'); |
79
|
|
|
$provider->getShortName()->willReturn('user'); |
80
|
|
|
$provider->getEntityClassName()->willReturn('Dummy\\User'); |
81
|
|
|
$provider->hasUnique()->willReturn(true); |
82
|
|
|
$provider->getIdName()->willReturn('id'); |
83
|
|
|
|
84
|
|
|
return $provider; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function getGroupProvider(int $id, array $spec, array $post, array $params) |
88
|
|
|
{ |
89
|
|
|
$provider = $this->prophesize(EntityProviderInterface::class); |
90
|
|
|
$provider->getId()->willReturn($id); |
91
|
|
|
$provider->getEntitySpec()->willReturn($spec); |
92
|
|
|
$provider->getPostParams()->willReturn($post); |
93
|
|
|
$provider->getParams()->willReturn($params); |
94
|
|
|
$provider->getBaseName()->willReturn('Group'); |
95
|
|
|
$provider->getShortName()->willReturn('group'); |
96
|
|
|
$provider->getEntityClassName()->willReturn('Dummy\\Group'); |
97
|
|
|
$provider->hasUnique()->willReturn(false); |
98
|
|
|
$provider->getIdName()->willReturn('id'); |
99
|
|
|
|
100
|
|
|
return $provider; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return EntityProviderFactoryInterface |
105
|
|
|
*/ |
106
|
|
|
private function getFactory() |
107
|
|
|
{ |
108
|
|
|
return new class($this->providers) implements EntityProviderFactoryInterface |
109
|
|
|
{ |
110
|
|
|
private $providers; |
111
|
|
|
|
112
|
|
|
public function __construct($providers) |
113
|
|
|
{ |
114
|
|
|
$this->providers = $providers; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function create(string $entityClassName): EntityProviderInterface |
118
|
|
|
{ |
119
|
|
|
foreach ($this->providers as $id => $provider) { |
120
|
|
|
/** @var EntityProviderInterface $entityProvider */ |
121
|
|
|
$entityProvider = $provider->reveal(); |
122
|
|
|
if ($entityProvider->getEntityClassName() == $entityClassName) { |
123
|
|
|
unset($this->providers[$id]); |
124
|
|
|
return $entityProvider; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
throw new \LogicException('Could not create provider.'); |
129
|
|
|
} |
130
|
|
|
}; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testManyToOne() |
134
|
|
|
{ |
135
|
|
|
$expected = <<<'BODY' |
136
|
|
|
$I->haveInRepository('Dummy\Group', array ( |
137
|
|
|
'id' => 1, |
138
|
|
|
'name' => 'Default', |
139
|
|
|
)); |
140
|
|
|
$group = $I->grabEntityFromRepository('Dummy\Group', ['id' => 1]); |
141
|
|
|
$I->haveInRepository('Dummy\User', array ( |
142
|
|
|
'id' => 1, |
143
|
|
|
'name' => 'Bob', |
144
|
|
|
'email' => '[email protected]', |
145
|
|
|
'group' => $group, |
146
|
|
|
)); |
147
|
|
|
|
148
|
|
|
BODY; |
149
|
|
|
|
150
|
|
|
$actual = (new class('Dummy\\User', $this->getFactory()) extends AbstractTest |
151
|
|
|
{ |
152
|
|
|
|
153
|
|
|
public function generate(): string |
154
|
|
|
{ |
155
|
|
|
return $this->generateHaveInRepo(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function getClassName(): string |
159
|
|
|
{ |
160
|
|
|
return 'TestCest'; |
161
|
|
|
} |
162
|
|
|
})->generate(); |
163
|
|
|
|
164
|
|
|
$this->assertEquals($expected, $actual); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testManyToOneMultiple() |
168
|
|
|
{ |
169
|
|
|
$provider = $this->getUserProvider(2, [ |
170
|
|
|
[ |
171
|
|
|
'name' => 'id', |
172
|
|
|
'type' => 'integer', |
173
|
|
|
'nullable' => false, |
174
|
|
|
], |
175
|
|
|
[ |
176
|
|
|
'name' => 'name', |
177
|
|
|
'type' => 'string', |
178
|
|
|
'nullable' => false, |
179
|
|
|
], |
180
|
|
|
[ |
181
|
|
|
'name' => 'email', |
182
|
|
|
'type' => 'string', |
183
|
|
|
'nullable' => false, |
184
|
|
|
], |
185
|
|
|
[ |
186
|
|
|
'name' => 'group', |
187
|
|
|
'type' => 'manytoone', |
188
|
|
|
'entity' => 'Dummy\\Group', |
189
|
|
|
'nullable' => false, |
190
|
|
|
], |
191
|
|
|
], [ |
192
|
|
|
'name' => 'Joe', |
193
|
|
|
'email' => '[email protected]', |
194
|
|
|
'group' => 1, |
195
|
|
|
], [ |
196
|
|
|
'id' => 2, |
197
|
|
|
'name' => 'Joe', |
198
|
|
|
'email' => '[email protected]', |
199
|
|
|
]); |
200
|
|
|
$this->providers[] = $provider; |
201
|
|
|
|
202
|
|
|
$expected = <<<'BODY' |
203
|
|
|
$I->haveInRepository('Dummy\Group', array ( |
204
|
|
|
'id' => 1, |
205
|
|
|
'name' => 'Default', |
206
|
|
|
)); |
207
|
|
|
$group = $I->grabEntityFromRepository('Dummy\Group', ['id' => 1]); |
208
|
|
|
$I->haveInRepository('Dummy\User', array ( |
209
|
|
|
'id' => 1, |
210
|
|
|
'name' => 'Bob', |
211
|
|
|
'email' => '[email protected]', |
212
|
|
|
'group' => $group, |
213
|
|
|
)); |
214
|
|
|
$I->haveInRepository('Dummy\User', array ( |
215
|
|
|
'id' => 2, |
216
|
|
|
'name' => 'Joe', |
217
|
|
|
'email' => '[email protected]', |
218
|
|
|
'group' => $group, |
219
|
|
|
)); |
220
|
|
|
|
221
|
|
|
BODY; |
222
|
|
|
|
223
|
|
|
$actual = (new class('Dummy\\User', $this->getFactory(), 2) extends AbstractTest |
224
|
|
|
{ |
225
|
|
|
/** |
226
|
|
|
* @var int |
227
|
|
|
*/ |
228
|
|
|
private $times; |
229
|
|
|
|
230
|
|
|
public function __construct( |
231
|
|
|
string $entityClassName, |
232
|
|
|
EntityProviderFactoryInterface $entityProviderFactory, |
233
|
|
|
int $times |
234
|
|
|
) { |
235
|
|
|
parent::__construct($entityClassName, $entityProviderFactory); |
236
|
|
|
$this->times = $times; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function generate(): string |
240
|
|
|
{ |
241
|
|
|
return $this->generateHaveInRepo($this->times); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function getClassName(): string |
245
|
|
|
{ |
246
|
|
|
return 'TestCest'; |
247
|
|
|
} |
248
|
|
|
})->generate(); |
249
|
|
|
|
250
|
|
|
$this->assertEquals($expected, $actual); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function testManyToManyMultiple() |
254
|
|
|
{ |
255
|
|
|
$spec = [ |
256
|
|
|
[ |
257
|
|
|
'name' => 'id', |
258
|
|
|
'type' => 'integer', |
259
|
|
|
'nullable' => false, |
260
|
|
|
], |
261
|
|
|
[ |
262
|
|
|
'name' => 'name', |
263
|
|
|
'type' => 'string', |
264
|
|
|
'nullable' => false, |
265
|
|
|
], |
266
|
|
|
[ |
267
|
|
|
'name' => 'email', |
268
|
|
|
'type' => 'string', |
269
|
|
|
'nullable' => false, |
270
|
|
|
], |
271
|
|
|
[ |
272
|
|
|
'name' => 'groups', |
273
|
|
|
'type' => 'manytomany', |
274
|
|
|
'entity' => 'Dummy\\Group', |
275
|
|
|
'nullable' => false, |
276
|
|
|
], |
277
|
|
|
]; |
278
|
|
|
$provider1 = $this->providers[0]; |
279
|
|
|
$provider1->getEntitySpec()->willReturn($spec); |
280
|
|
|
$provider1->getPostParams()->willReturn([ |
281
|
|
|
'name' => 'Bob', |
282
|
|
|
'email' => '[email protected]', |
283
|
|
|
'groups' => [1], |
284
|
|
|
]); |
285
|
|
|
|
286
|
|
|
$provider3 = $this->getUserProvider(2, $spec, [ |
287
|
|
|
'name' => 'Joe', |
288
|
|
|
'email' => '[email protected]', |
289
|
|
|
'groups' => "1,2", |
290
|
|
|
], [ |
291
|
|
|
'id' => 2, |
292
|
|
|
'name' => 'Joe', |
293
|
|
|
'email' => '[email protected]', |
294
|
|
|
]); |
295
|
|
|
$this->providers[] = $provider3; |
296
|
|
|
|
297
|
|
|
$provider4 = $this->getGroupProvider(2, [ |
298
|
|
|
[ |
299
|
|
|
'name' => 'id', |
300
|
|
|
'type' => 'integer', |
301
|
|
|
'nullable' => false, |
302
|
|
|
], |
303
|
|
|
[ |
304
|
|
|
'name' => 'name', |
305
|
|
|
'type' => 'string', |
306
|
|
|
'nullable' => false, |
307
|
|
|
], |
308
|
|
|
], [ |
309
|
|
|
'name' => 'Special', |
310
|
|
|
], [ |
311
|
|
|
'id' => 2, |
312
|
|
|
'name' => 'Special', |
313
|
|
|
]); |
314
|
|
|
$this->providers[] = $provider4; |
315
|
|
|
|
316
|
|
|
$actual = (new class('Dummy\\User', $this->getFactory(), 2) extends AbstractTest |
317
|
|
|
{ |
318
|
|
|
/** |
319
|
|
|
* @var int |
320
|
|
|
*/ |
321
|
|
|
private $times; |
322
|
|
|
|
323
|
|
|
public function __construct( |
324
|
|
|
string $entityClassName, |
325
|
|
|
EntityProviderFactoryInterface $entityProviderFactory, |
326
|
|
|
int $times |
327
|
|
|
) { |
328
|
|
|
parent::__construct($entityClassName, $entityProviderFactory); |
329
|
|
|
$this->times = $times; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
public function generate(): string |
333
|
|
|
{ |
334
|
|
|
return $this->generateHaveInRepo($this->times); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
public function getClassName(): string |
338
|
|
|
{ |
339
|
|
|
return 'TestCest'; |
340
|
|
|
} |
341
|
|
|
})->generate(); |
342
|
|
|
|
343
|
|
|
$expected = <<<'BODY' |
344
|
|
|
$I->haveInRepository('Dummy\Group', array ( |
345
|
|
|
'id' => 1, |
346
|
|
|
'name' => 'Default', |
347
|
|
|
)); |
348
|
|
|
$group = $I->grabEntityFromRepository('Dummy\Group', ['id' => 1]); |
349
|
|
|
$I->haveInRepository('Dummy\User', array ( |
350
|
|
|
'id' => 1, |
351
|
|
|
'name' => 'Bob', |
352
|
|
|
'email' => '[email protected]', |
353
|
|
|
'groups' => [$group], |
354
|
|
|
)); |
355
|
|
|
$I->haveInRepository('Dummy\User', array ( |
356
|
|
|
'id' => 2, |
357
|
|
|
'name' => 'Joe', |
358
|
|
|
'email' => '[email protected]', |
359
|
|
|
'groups' => [$group], |
360
|
|
|
)); |
361
|
|
|
|
362
|
|
|
BODY; |
363
|
|
|
$this->assertEquals($expected, $actual); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
public function testRelationsNullable() |
367
|
|
|
{ |
368
|
|
|
$spec = [ |
369
|
|
|
[ |
370
|
|
|
'name' => 'id', |
371
|
|
|
'type' => 'integer', |
372
|
|
|
'nullable' => false, |
373
|
|
|
], |
374
|
|
|
[ |
375
|
|
|
'name' => 'name', |
376
|
|
|
'type' => 'string', |
377
|
|
|
'nullable' => false, |
378
|
|
|
], |
379
|
|
|
[ |
380
|
|
|
'name' => 'email', |
381
|
|
|
'type' => 'string', |
382
|
|
|
'nullable' => false, |
383
|
|
|
], |
384
|
|
|
[ |
385
|
|
|
'name' => 'group', |
386
|
|
|
'type' => 'manytoone', |
387
|
|
|
'entity' => 'Dummy\\Group', |
388
|
|
|
'nullable' => true, |
389
|
|
|
], |
390
|
|
|
]; |
391
|
|
|
$provider1 = $this->providers[0]; |
392
|
|
|
$provider1->getEntitySpec()->willReturn($spec); |
393
|
|
|
|
394
|
|
|
$actual = (new class('Dummy\\User', $this->getFactory(), .0) extends AbstractTest |
395
|
|
|
{ |
396
|
|
|
|
397
|
|
|
public function generate(): string |
398
|
|
|
{ |
399
|
|
|
return $this->generateHaveInRepo(); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
public function getClassName(): string |
403
|
|
|
{ |
404
|
|
|
return 'TestCest'; |
405
|
|
|
} |
406
|
|
|
})->generate(); |
407
|
|
|
|
408
|
|
|
$expected = <<<'BODY' |
409
|
|
|
$I->haveInRepository('Dummy\User', array ( |
410
|
|
|
'id' => 1, |
411
|
|
|
'name' => 'Bob', |
412
|
|
|
'email' => '[email protected]', |
413
|
|
|
)); |
414
|
|
|
|
415
|
|
|
BODY; |
416
|
|
|
$this->assertEquals($expected, $actual); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
public function testNullableColumn() |
420
|
|
|
{ |
421
|
|
|
$spec = [ |
422
|
|
|
[ |
423
|
|
|
'name' => 'id', |
424
|
|
|
'type' => 'integer', |
425
|
|
|
'nullable' => false, |
426
|
|
|
], |
427
|
|
|
[ |
428
|
|
|
'name' => 'name', |
429
|
|
|
'type' => 'string', |
430
|
|
|
'nullable' => true, |
431
|
|
|
], |
432
|
|
|
[ |
433
|
|
|
'name' => 'email', |
434
|
|
|
'type' => 'string', |
435
|
|
|
'nullable' => false, |
436
|
|
|
], |
437
|
|
|
[ |
438
|
|
|
'name' => 'group', |
439
|
|
|
'type' => 'manytoone', |
440
|
|
|
'entity' => 'Dummy\\Group', |
441
|
|
|
'nullable' => true, |
442
|
|
|
], |
443
|
|
|
]; |
444
|
|
|
$provider1 = $this->providers[0]; |
445
|
|
|
$provider1->getEntitySpec()->willReturn($spec); |
446
|
|
|
|
447
|
|
|
$actual = (new class('Dummy\\User', $this->getFactory(), .0) extends AbstractTest |
448
|
|
|
{ |
449
|
|
|
|
450
|
|
|
public function generate(): string |
451
|
|
|
{ |
452
|
|
|
return $this->generateHaveInRepo(); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
public function getClassName(): string |
456
|
|
|
{ |
457
|
|
|
return 'TestCest'; |
458
|
|
|
} |
459
|
|
|
})->generate(); |
460
|
|
|
|
461
|
|
|
$expected = <<<'BODY' |
462
|
|
|
$I->haveInRepository('Dummy\User', array ( |
463
|
|
|
'id' => 1, |
464
|
|
|
'email' => '[email protected]', |
465
|
|
|
)); |
466
|
|
|
|
467
|
|
|
BODY; |
468
|
|
|
$this->assertEquals($expected, $actual); |
469
|
|
|
} |
470
|
|
|
} |
471
|
|
|
|