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