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
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @see https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/working-with-objects.html#querying |
23
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
24
|
|
|
* @SuppressWarnings(PHPMD.ExcessivePublicCount) |
25
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
26
|
|
|
* @large |
27
|
|
|
* @covers \EdmondsCommerce\DoctrineStaticMeta\Entity\Repositories\AbstractEntityRepository |
28
|
|
|
*/ |
29
|
|
|
class AbstractEntityRepositoryLargeTest extends AbstractLargeTest |
30
|
|
|
{ |
31
|
|
|
public const WORK_DIR = AbstractTest::VAR_PATH . '/' |
32
|
|
|
. self::TEST_TYPE_LARGE . '/AbstractEntityRepositoryLargeTest'; |
33
|
|
|
|
34
|
|
|
private const TEST_ENTITY_FQN = self::TEST_ENTITIES_ROOT_NAMESPACE . TestCodeGenerator::TEST_ENTITY_PERSON; |
35
|
|
|
|
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 = |
64
|
|
|
$this->container->get(TestEntityGeneratorFactory::class) |
65
|
|
|
->createForEntityFqn($this->getCopiedFqn(self::TEST_ENTITY_FQN)); |
66
|
|
|
$this->generatedEntities = $entityGenerator->generateEntities( |
67
|
|
|
$this->isQuickTests() ? self::NUM_ENTITIES_QUICK : self::NUM_ENTITIES_FULL |
68
|
|
|
); |
69
|
|
|
$saver = new EntitySaver($this->getEntityManager()); |
70
|
|
|
$saver->saveAll($this->generatedEntities); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function getRepository(): AbstractEntityRepository |
74
|
|
|
{ |
75
|
|
|
return $this->container->get(RepositoryFactory::class) |
76
|
|
|
->getRepository($this->getCopiedFqn(self::TEST_ENTITY_FQN)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @test |
82
|
|
|
*/ |
83
|
|
|
public function find(): void |
84
|
|
|
{ |
85
|
|
|
$expected = $this->generatedEntities[array_rand($this->generatedEntities)]; |
86
|
|
|
$actual = $this->repository->find($expected->getId()); |
87
|
|
|
self::assertSame($expected, $actual); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @test |
92
|
|
|
*/ |
93
|
|
|
public function get(): void |
94
|
|
|
{ |
95
|
|
|
$expected = $this->generatedEntities[array_rand($this->generatedEntities)]; |
96
|
|
|
$actual = $this->repository->get($expected->getId()); |
97
|
|
|
self::assertSame($expected, $actual); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @test |
102
|
|
|
*/ |
103
|
|
|
public function getWillThrowAnExceptionIfNothingIsFound(): void |
104
|
|
|
{ |
105
|
|
|
$this->expectException(DoctrineStaticMetaException::class); |
106
|
|
|
$this->repository->get(time()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @test |
111
|
|
|
*/ |
112
|
|
|
public function findAll(): void |
113
|
|
|
{ |
114
|
|
|
$expected = $this->sortCollectionById($this->generatedEntities); |
115
|
|
|
$actual = $this->sortCollectionById($this->repository->findAll()); |
116
|
|
|
self::assertEquals($expected, $actual); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function sortCollectionById(array $collection): array |
120
|
|
|
{ |
121
|
|
|
$return = []; |
122
|
|
|
foreach ($collection as $item) { |
123
|
|
|
$return[(string)$item->getId()] = $item; |
124
|
|
|
} |
125
|
|
|
ksort($return); |
126
|
|
|
|
127
|
|
|
return $return; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @test |
132
|
|
|
*/ |
133
|
|
|
public function findBy(): void |
134
|
|
|
{ |
135
|
|
|
foreach (MappingHelper::COMMON_TYPES as $property) { |
136
|
|
|
$entity = current($this->generatedEntities); |
137
|
|
|
$getter = $this->getGetterForType($property); |
138
|
|
|
$criteria = [$property => $entity->$getter()]; |
139
|
|
|
$actual = $this->repository->findBy($criteria); |
140
|
|
|
self::assertTrue($this->arrayContainsEntity($entity, $actual)); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
protected function getGetterForType(string $type): string |
145
|
|
|
{ |
146
|
|
|
$ucType = ucfirst($type); |
147
|
|
|
$getter = "get$ucType"; |
148
|
|
|
if (MappingHelper::TYPE_BOOLEAN === $type) { |
149
|
|
|
$getter = "is$ucType"; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $getter; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
protected function arrayContainsEntity(EntityInterface $expectedEntity, array $array): bool |
156
|
|
|
{ |
157
|
|
|
foreach ($array as $entity) { |
158
|
|
|
if ($entity->getId() === $expectedEntity->getId()) { |
159
|
|
|
return true; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return false; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @test |
168
|
|
|
*/ |
169
|
|
|
public function findOneBy(): void |
170
|
|
|
{ |
171
|
|
|
foreach (MappingHelper::COMMON_TYPES as $property) { |
172
|
|
|
$entity = current($this->generatedEntities); |
173
|
|
|
$getter = $this->getGetterForType($property); |
174
|
|
|
$value = $entity->$getter(); |
175
|
|
|
$criteria = [ |
176
|
|
|
$property => $value, |
177
|
|
|
'id' => $entity->getId(), |
178
|
|
|
]; |
179
|
|
|
$actual = $this->repository->findOneBy($criteria); |
180
|
|
|
self::assertEquals( |
181
|
|
|
$entity, |
182
|
|
|
$actual, |
183
|
|
|
'Failed finding one expected entity (ID' . $entity->getId() . ') with $criteria: ' |
184
|
|
|
. "\n" . var_export($criteria, true) |
185
|
|
|
. "\n and \$actual: " |
186
|
|
|
. "\n" . (new EntityDebugDumper())->dump($actual, $this->getEntityManager()) |
|
|
|
|
187
|
|
|
); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @test |
193
|
|
|
*/ |
194
|
|
|
public function getOneBy(): void |
195
|
|
|
{ |
196
|
|
|
$entity = current($this->generatedEntities); |
197
|
|
|
$getter = $this->getGetterForType(MappingHelper::TYPE_STRING); |
198
|
|
|
$value = $entity->$getter(); |
199
|
|
|
$criteria = [ |
200
|
|
|
MappingHelper::TYPE_STRING => $value, |
201
|
|
|
'id' => $entity->getId(), |
202
|
|
|
]; |
203
|
|
|
$actual = $this->repository->findOneBy($criteria); |
204
|
|
|
self::assertEquals( |
205
|
|
|
$entity, |
206
|
|
|
$actual, |
207
|
|
|
'Failed finding one expected entity (ID' . $entity->getId() . ') with $criteria: ' |
208
|
|
|
. "\n" . var_export($criteria, true) |
209
|
|
|
. "\n and \$actual: " |
210
|
|
|
. "\n" . (new EntityDebugDumper())->dump($actual, $this->getEntityManager()) |
|
|
|
|
211
|
|
|
); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @test |
216
|
|
|
*/ |
217
|
|
|
public function getOneByWillThrowAnExceptionIfNothingIsFound(): void |
218
|
|
|
{ |
219
|
|
|
$property = MappingHelper::TYPE_STRING; |
220
|
|
|
$criteria = [$property => 'not-a-real-vaule']; |
221
|
|
|
$this->expectException(\RuntimeException::class); |
222
|
|
|
$this->repository->getOneBy($criteria); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @test |
227
|
|
|
*/ |
228
|
|
|
public function getClassName(): void |
229
|
|
|
{ |
230
|
|
|
self::assertSame( |
231
|
|
|
ltrim($this->getCopiedFqn(self::TEST_ENTITY_FQN), '\\'), |
232
|
|
|
$this->repository->getClassName() |
233
|
|
|
); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @test |
238
|
|
|
*/ |
239
|
|
|
public function matching(): void |
240
|
|
|
{ |
241
|
|
|
foreach (MappingHelper::COMMON_TYPES as $property) { |
242
|
|
|
$entity = current($this->generatedEntities); |
243
|
|
|
$getter = $this->getGetterForType($property); |
244
|
|
|
$value = $entity->$getter(); |
245
|
|
|
$criteria = new Criteria(); |
246
|
|
|
$criteria->where(new Comparison($property, '=', $value)); |
247
|
|
|
$criteria->andWhere(new Comparison('id', '=', $entity->getId())); |
248
|
|
|
$actual = $this->repository->matching($criteria); |
249
|
|
|
self::assertTrue($this->collectionContainsEntity($entity, $actual)); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
protected function collectionContainsEntity(EntityInterface $expectedEntity, Collection $collection): bool |
254
|
|
|
{ |
255
|
|
|
foreach ($collection->getIterator() as $entity) { |
256
|
|
|
if ($entity->getId() === $expectedEntity->getId()) { |
257
|
|
|
return true; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return false; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @test |
266
|
|
|
*/ |
267
|
|
|
public function createQueryBuilder(): void |
268
|
|
|
{ |
269
|
|
|
$this->repository->createQueryBuilder('foo'); |
270
|
|
|
self::assertTrue(true); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @test |
275
|
|
|
*/ |
276
|
|
|
public function createResultSetMappingBuilder(): void |
277
|
|
|
{ |
278
|
|
|
$this->repository->createResultSetMappingBuilder('foo'); |
279
|
|
|
self::assertTrue(true); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @test |
284
|
|
|
*/ |
285
|
|
|
public function createNamedQuery(): void |
286
|
|
|
{ |
287
|
|
|
$this->markTestIncomplete( |
288
|
|
|
'Need to add a named query for a test entity somehow in the meta data before we can test this' |
289
|
|
|
); |
290
|
|
|
$this->repository->createNamedQuery('foo'); |
291
|
|
|
self::assertTrue(true); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @test |
296
|
|
|
*/ |
297
|
|
|
public function clear(): void |
298
|
|
|
{ |
299
|
|
|
$this->repository->clear(); |
300
|
|
|
$map = $this->getEntityManager()->getUnitOfWork()->getIdentityMap(); |
301
|
|
|
self::assertSame( |
302
|
|
|
[], |
303
|
|
|
$map[ltrim($this->getCopiedFqn(self::TEST_ENTITY_FQN), '\\')] |
304
|
|
|
); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
*/ |
309
|
|
|
public function testCount(): void |
310
|
|
|
{ |
311
|
|
|
self::assertSame( |
312
|
|
|
$this->isQuickTests() ? self::NUM_ENTITIES_QUICK : self::NUM_ENTITIES_FULL, |
313
|
|
|
$this->repository->count([]) |
314
|
|
|
); |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|