1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Large\Entity\Repositories; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
6
|
|
|
use Doctrine\Common\Collections\Collection; |
7
|
|
|
use Doctrine\Common\Collections\Criteria; |
8
|
|
|
use Doctrine\Common\Collections\Expr\Comparison; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Repositories\AbstractEntityRepository; |
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaver; |
13
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory; |
14
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\AbstractEntityTest; |
15
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityDebugDumper; |
16
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\TestEntityGenerator; |
17
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\EntityValidatorFactory; |
18
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
19
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\AbstractLargeTest; |
20
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\AbstractTest; |
21
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Large\FullProjectBuildLargeTest; |
22
|
|
|
use Symfony\Component\Validator\Mapping\Cache\DoctrineCache; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @see https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/working-with-objects.html#querying |
26
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
27
|
|
|
* @SuppressWarnings(PHPMD.ExcessivePublicCount) |
28
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
29
|
|
|
* @large |
30
|
|
|
* @coversDefaultClass \EdmondsCommerce\DoctrineStaticMeta\Entity\Repositories\AbstractEntityRepository |
31
|
|
|
*/ |
32
|
|
|
class AbstractEntityRepositoryLargeTest extends AbstractLargeTest |
33
|
|
|
{ |
34
|
|
|
public const WORK_DIR = AbstractTest::VAR_PATH . '/' |
35
|
|
|
. self::TEST_TYPE_LARGE . '/AbstractEntityRepositoryLargeTest'; |
36
|
|
|
|
37
|
|
|
private const TEST_ENTITY_FQN = self::TEST_PROJECT_ROOT_NAMESPACE |
38
|
|
|
. '\\Entities\\AbstractEntityRepositoryLargeTestEntity'; |
39
|
|
|
|
40
|
|
|
private const TEST_FIELD_FQN_BASE = FullProjectBuildLargeTest::TEST_FIELD_NAMESPACE_BASE . '\\Traits'; |
41
|
|
|
|
42
|
|
|
private const NUM_ENTITIES_QUICK = 2; |
43
|
|
|
|
44
|
|
|
private const NUM_ENTITIES_FULL = 10; |
45
|
|
|
protected static $buildOnce = true; |
46
|
|
|
private $fields = []; |
47
|
|
|
private $generatedEntities = []; |
48
|
|
|
/** |
49
|
|
|
* @var AbstractEntityRepository |
50
|
|
|
*/ |
51
|
|
|
private $repository; |
52
|
|
|
|
53
|
|
|
public function setup() |
54
|
|
|
{ |
55
|
|
|
parent::setup(); |
56
|
|
|
$this->generateCode(); |
57
|
|
|
$this->setupCopiedWorkDirAndCreateDatabase(); |
58
|
|
|
$this->generateAndSaveTestEntities(); |
59
|
|
|
$this->repository = $this->getRepository(); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function generateCode(): void |
63
|
|
|
{ |
64
|
|
|
if (true === self::$built) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
$entityGenerator = $this->getEntityGenerator(); |
68
|
|
|
|
69
|
|
|
$entityGenerator->generateEntity(self::TEST_ENTITY_FQN); |
70
|
|
|
$fieldGenerator = $this->getFieldGenerator(); |
71
|
|
|
foreach (MappingHelper::COMMON_TYPES as $type) { |
72
|
|
|
$this->fields[] = $fieldFqn = $fieldGenerator->generateField( |
73
|
|
|
self::TEST_FIELD_FQN_BASE . '\\' . ucwords($type), |
74
|
|
|
$type |
75
|
|
|
); |
76
|
|
|
$this->getFieldSetter()->setEntityHasField(self::TEST_ENTITY_FQN, $fieldFqn); |
77
|
|
|
} |
78
|
|
|
self::$built = true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function generateAndSaveTestEntities(): void |
82
|
|
|
{ |
83
|
|
|
$entityGenerator = new TestEntityGenerator( |
84
|
|
|
AbstractEntityTest::SEED, |
85
|
|
|
[], |
86
|
|
|
new \ts\Reflection\ReflectionClass(self::TEST_ENTITY_FQN), |
87
|
|
|
new EntitySaverFactory( |
88
|
|
|
$this->getEntityManager(), |
89
|
|
|
new EntitySaver($this->getEntityManager()), |
90
|
|
|
new NamespaceHelper() |
91
|
|
|
), |
92
|
|
|
new EntityValidatorFactory(new DoctrineCache(new ArrayCache())) |
93
|
|
|
); |
94
|
|
|
$this->generatedEntities = $entityGenerator->generateEntities( |
95
|
|
|
$this->getEntityManager(), |
96
|
|
|
$this->getCopiedFqn(self::TEST_ENTITY_FQN), |
97
|
|
|
$this->isQuickTests() ? self::NUM_ENTITIES_QUICK : self::NUM_ENTITIES_FULL |
98
|
|
|
); |
99
|
|
|
$saver = new EntitySaver($this->getEntityManager()); |
100
|
|
|
$saver->saveAll($this->generatedEntities); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function getRepository() |
104
|
|
|
{ |
105
|
|
|
return $this->getEntityManager()->getRepository($this->getCopiedFqn(self::TEST_ENTITY_FQN)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @test |
110
|
|
|
* @covers ::__construct |
111
|
|
|
* @covers ::initRepository |
112
|
|
|
* @covers ::getEntityFqn |
113
|
|
|
*/ |
114
|
|
|
public function loadWithNullMetaData(): void |
115
|
|
|
{ |
116
|
|
|
$repo = $this->getRepository(); |
117
|
|
|
$repoFqn = \get_class($repo); |
118
|
|
|
new $repoFqn($this->getEntityManager()); |
119
|
|
|
self::assertTrue(true); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @test |
124
|
|
|
* @covers ::find |
125
|
|
|
* @covers ::__construct |
126
|
|
|
*/ |
127
|
|
|
public function find(): void |
128
|
|
|
{ |
129
|
|
|
$expected = $this->generatedEntities[array_rand($this->generatedEntities)]; |
130
|
|
|
$actual = $this->repository->find($expected->getId()); |
|
|
|
|
131
|
|
|
self::assertSame($expected, $actual); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @covers ::get |
136
|
|
|
* @test |
137
|
|
|
*/ |
138
|
|
|
public function get(): void |
139
|
|
|
{ |
140
|
|
|
$expected = $this->generatedEntities[array_rand($this->generatedEntities)]; |
141
|
|
|
$actual = $this->repository->get($expected->getId()); |
142
|
|
|
self::assertSame($expected, $actual); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @covers ::get |
147
|
|
|
* @test |
148
|
|
|
*/ |
149
|
|
|
public function getWillThrowAnExceptionIfNothingIsFound(): void |
150
|
|
|
{ |
151
|
|
|
$this->expectException(\RuntimeException::class); |
152
|
|
|
$this->repository->get(time()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @test |
157
|
|
|
* @covers ::findAll |
158
|
|
|
*/ |
159
|
|
|
public function findAll(): void |
160
|
|
|
{ |
161
|
|
|
$expected = $this->generatedEntities; |
162
|
|
|
$actual = $this->repository->findAll(); |
163
|
|
|
self::assertSame($expected, $actual); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @test |
168
|
|
|
* @covers ::findBy |
169
|
|
|
*/ |
170
|
|
|
public function findBy(): void |
171
|
|
|
{ |
172
|
|
|
foreach (MappingHelper::COMMON_TYPES as $key => $property) { |
173
|
|
|
$entity = $this->getEntityByKey($key); |
174
|
|
|
; |
175
|
|
|
$getter = $this->getGetterForType($property); |
176
|
|
|
$criteria = [$property => $entity->$getter()]; |
177
|
|
|
$actual = $this->repository->findBy($criteria); |
178
|
|
|
self::assertTrue($this->arrayContainsEntity($entity, $actual)); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
private function getEntityByKey(int $key): EntityInterface |
184
|
|
|
{ |
185
|
|
|
if ($this->isQuickTests()) { |
186
|
|
|
return $this->generatedEntities[0]; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $this->generatedEntities[$key]; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
protected function getGetterForType(string $type): string |
193
|
|
|
{ |
194
|
|
|
$ucType = ucfirst($type); |
195
|
|
|
$getter = "get$ucType"; |
196
|
|
|
if (MappingHelper::TYPE_BOOLEAN === $type) { |
197
|
|
|
$getter = "is$ucType"; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $getter; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
protected function arrayContainsEntity(EntityInterface $expectedEntity, array $array): bool |
204
|
|
|
{ |
205
|
|
|
foreach ($array as $entity) { |
206
|
|
|
if ($entity->getId() === $expectedEntity->getId()) { |
207
|
|
|
return true; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return false; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @test |
216
|
|
|
* @covers ::findOneBy |
217
|
|
|
*/ |
218
|
|
|
public function findOneBy(): void |
219
|
|
|
{ |
220
|
|
|
foreach (MappingHelper::COMMON_TYPES as $key => $property) { |
221
|
|
|
$entity = $this->getEntityByKey($key); |
222
|
|
|
$getter = $this->getGetterForType($property); |
223
|
|
|
$value = $entity->$getter(); |
224
|
|
|
$criteria = [ |
225
|
|
|
$property => $value, |
226
|
|
|
'id' => $entity->getId(), |
227
|
|
|
]; |
228
|
|
|
$actual = $this->repository->findOneBy($criteria); |
|
|
|
|
229
|
|
|
self::assertEquals( |
230
|
|
|
$entity, |
231
|
|
|
$actual, |
232
|
|
|
'Failed finding one expected entity (ID' . $entity->getId() . ') with $criteria: ' |
233
|
|
|
. "\n" . var_export($criteria, true) |
234
|
|
|
. "\n and \$actual: " |
235
|
|
|
. "\n" . (new EntityDebugDumper())->dump($actual, $this->getEntityManager()) |
236
|
|
|
); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @covers ::getOneBy |
242
|
|
|
* @test |
243
|
|
|
*/ |
244
|
|
|
public function getOneBy(): void |
245
|
|
|
{ |
246
|
|
|
$entity = $this->getEntityByKey(0); |
247
|
|
|
$getter = $this->getGetterForType(MappingHelper::TYPE_STRING); |
248
|
|
|
$value = $entity->$getter(); |
249
|
|
|
$criteria = [ |
250
|
|
|
MappingHelper::TYPE_STRING => $value, |
251
|
|
|
'id' => $entity->getId(), |
252
|
|
|
]; |
253
|
|
|
$actual = $this->repository->findOneBy($criteria); |
|
|
|
|
254
|
|
|
self::assertEquals( |
255
|
|
|
$entity, |
256
|
|
|
$actual, |
257
|
|
|
'Failed finding one expected entity (ID' . $entity->getId() . ') with $criteria: ' |
258
|
|
|
. "\n" . var_export($criteria, true) |
259
|
|
|
. "\n and \$actual: " |
260
|
|
|
. "\n" . (new EntityDebugDumper())->dump($actual, $this->getEntityManager()) |
261
|
|
|
); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @covers ::getOneBy |
266
|
|
|
* @test |
267
|
|
|
*/ |
268
|
|
|
public function getOneByWillThrowAnExceptionIfNothingIsFound(): void |
269
|
|
|
{ |
270
|
|
|
$property = MappingHelper::TYPE_STRING; |
271
|
|
|
$criteria = [$property => 'not-a-real-vaule']; |
272
|
|
|
$this->expectException(\RuntimeException::class); |
273
|
|
|
$this->repository->getOneBy($criteria); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @test |
278
|
|
|
* @covers ::getClassName |
279
|
|
|
*/ |
280
|
|
|
public function getClassName(): void |
281
|
|
|
{ |
282
|
|
|
self::assertSame( |
283
|
|
|
ltrim($this->getCopiedFqn(self::TEST_ENTITY_FQN), '\\'), |
284
|
|
|
$this->repository->getClassName() |
285
|
|
|
); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @test |
290
|
|
|
* @covers ::matching |
291
|
|
|
*/ |
292
|
|
|
public function matching(): void |
293
|
|
|
{ |
294
|
|
|
foreach (MappingHelper::COMMON_TYPES as $key => $property) { |
295
|
|
|
$entity = $this->getEntityByKey($key); |
296
|
|
|
; |
297
|
|
|
$getter = $this->getGetterForType($property); |
298
|
|
|
$value = $entity->$getter(); |
299
|
|
|
$criteria = new Criteria(); |
300
|
|
|
$criteria->where(new Comparison($property, '=', $value)); |
301
|
|
|
$criteria->andWhere(new Comparison('id', '=', $entity->getId())); |
302
|
|
|
$actual = $this->repository->matching($criteria); |
303
|
|
|
self::assertTrue($this->collectionContainsEntity($entity, $actual)); |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
protected function collectionContainsEntity(EntityInterface $expectedEntity, Collection $collection): bool |
308
|
|
|
{ |
309
|
|
|
foreach ($collection->getIterator() as $entity) { |
310
|
|
|
if ($entity->getId() === $expectedEntity->getId()) { |
311
|
|
|
return true; |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
return false; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @test |
320
|
|
|
* @covers ::createQueryBuilder |
321
|
|
|
*/ |
322
|
|
|
public function createQueryBuilder(): void |
323
|
|
|
{ |
324
|
|
|
$this->repository->createQueryBuilder('foo'); |
325
|
|
|
self::assertTrue(true); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @test |
330
|
|
|
* @covers ::createResultSetMappingBuilder |
331
|
|
|
*/ |
332
|
|
|
public function createResultSetMappingBuilder(): void |
333
|
|
|
{ |
334
|
|
|
$this->repository->createResultSetMappingBuilder('foo'); |
335
|
|
|
self::assertTrue(true); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* @test |
340
|
|
|
* @covers ::createNamedQuery |
341
|
|
|
*/ |
342
|
|
|
public function createNamedQuery(): void |
343
|
|
|
{ |
344
|
|
|
$this->markTestIncomplete( |
345
|
|
|
'Need to add a named query for a test entity somehow in the meta data before we can test this' |
346
|
|
|
); |
347
|
|
|
$this->repository->createNamedQuery('foo'); |
348
|
|
|
self::assertTrue(true); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @test |
353
|
|
|
* @covers ::clear |
354
|
|
|
*/ |
355
|
|
|
public function clear(): void |
356
|
|
|
{ |
357
|
|
|
$this->repository->clear(); |
358
|
|
|
self::assertSame( |
359
|
|
|
['AbstractEntityRepositoryLargeTest_clear_\Entities\AbstractEntityRepositoryLargeTestEntity' => []], |
360
|
|
|
$this->getEntityManager()->getUnitOfWork()->getIdentityMap() |
361
|
|
|
); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @covers ::count |
366
|
|
|
*/ |
367
|
|
|
public function testCount(): void |
368
|
|
|
{ |
369
|
|
|
self::assertSame( |
370
|
|
|
$this->isQuickTests() ? self::NUM_ENTITIES_QUICK : self::NUM_ENTITIES_FULL, |
371
|
|
|
$this->repository->count([]) |
372
|
|
|
); |
373
|
|
|
} |
374
|
|
|
} |
375
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..