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
|
|
|
* @test |
136
|
|
|
* @covers ::findAll |
137
|
|
|
*/ |
138
|
|
|
public function findAll(): void |
139
|
|
|
{ |
140
|
|
|
$expected = $this->generatedEntities; |
141
|
|
|
$actual = $this->repository->findAll(); |
142
|
|
|
self::assertSame($expected, $actual); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @test |
147
|
|
|
* @covers ::findBy |
148
|
|
|
*/ |
149
|
|
|
public function findBy(): void |
150
|
|
|
{ |
151
|
|
|
foreach (MappingHelper::COMMON_TYPES as $key => $property) { |
152
|
|
|
$entity = $this->getEntityByKey($key); |
153
|
|
|
; |
154
|
|
|
$getter = $this->getGetterForType($property); |
155
|
|
|
$criteria = [$property => $entity->$getter()]; |
156
|
|
|
$actual = $this->repository->findBy($criteria); |
157
|
|
|
self::assertTrue($this->arrayContainsEntity($entity, $actual)); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
private function getEntityByKey(int $key): EntityInterface |
162
|
|
|
{ |
163
|
|
|
if ($this->isQuickTests()) { |
164
|
|
|
return $this->generatedEntities[0]; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $this->generatedEntities[$key]; |
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
|
|
|
* @covers ::findOneBy |
195
|
|
|
*/ |
196
|
|
|
public function findOneBy(): void |
197
|
|
|
{ |
198
|
|
|
foreach (MappingHelper::COMMON_TYPES as $key => $property) { |
199
|
|
|
$entity = $this->getEntityByKey($key); |
200
|
|
|
$getter = $this->getGetterForType($property); |
201
|
|
|
$value = $entity->$getter(); |
202
|
|
|
$criteria = [ |
203
|
|
|
$property => $value, |
204
|
|
|
'id' => $entity->getId(), |
205
|
|
|
]; |
206
|
|
|
$actual = $this->repository->findOneBy($criteria); |
|
|
|
|
207
|
|
|
self::assertEquals( |
208
|
|
|
$entity, |
209
|
|
|
$actual, |
210
|
|
|
'Failed finding one expected entity (ID' . $entity->getId() . ') with $criteria: ' |
211
|
|
|
. "\n" . var_export($criteria, true) |
212
|
|
|
. "\n and \$actual: " |
213
|
|
|
. "\n" . (new EntityDebugDumper())->dump($actual, $this->getEntityManager()) |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @test |
220
|
|
|
* @covers ::getClassName |
221
|
|
|
*/ |
222
|
|
|
public function getClassName(): void |
223
|
|
|
{ |
224
|
|
|
self::assertSame( |
225
|
|
|
ltrim($this->getCopiedFqn(self::TEST_ENTITY_FQN), '\\'), |
226
|
|
|
$this->repository->getClassName() |
227
|
|
|
); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @test |
232
|
|
|
* @covers ::matching |
233
|
|
|
*/ |
234
|
|
|
public function matching(): void |
235
|
|
|
{ |
236
|
|
|
foreach (MappingHelper::COMMON_TYPES as $key => $property) { |
237
|
|
|
$entity = $this->getEntityByKey($key); |
238
|
|
|
; |
239
|
|
|
$getter = $this->getGetterForType($property); |
240
|
|
|
$value = $entity->$getter(); |
241
|
|
|
$criteria = new Criteria(); |
242
|
|
|
$criteria->where(new Comparison($property, '=', $value)); |
243
|
|
|
$criteria->andWhere(new Comparison('id', '=', $entity->getId())); |
244
|
|
|
$actual = $this->repository->matching($criteria); |
245
|
|
|
self::assertTrue($this->collectionContainsEntity($entity, $actual)); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
protected function collectionContainsEntity(EntityInterface $expectedEntity, Collection $collection): bool |
250
|
|
|
{ |
251
|
|
|
foreach ($collection->getIterator() as $entity) { |
252
|
|
|
if ($entity->getId() === $expectedEntity->getId()) { |
253
|
|
|
return true; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return false; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @test |
262
|
|
|
* @covers ::createQueryBuilder |
263
|
|
|
*/ |
264
|
|
|
public function createQueryBuilder(): void |
265
|
|
|
{ |
266
|
|
|
$this->repository->createQueryBuilder('foo'); |
267
|
|
|
self::assertTrue(true); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @test |
272
|
|
|
* @covers ::createResultSetMappingBuilder |
273
|
|
|
*/ |
274
|
|
|
public function createResultSetMappingBuilder(): void |
275
|
|
|
{ |
276
|
|
|
$this->repository->createResultSetMappingBuilder('foo'); |
277
|
|
|
self::assertTrue(true); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @test |
282
|
|
|
* @covers ::createNamedQuery |
283
|
|
|
*/ |
284
|
|
|
public function createNamedQuery(): void |
285
|
|
|
{ |
286
|
|
|
$this->markTestIncomplete( |
287
|
|
|
'Need to add a named query for a test entity somehow in the meta data before we can test this' |
288
|
|
|
); |
289
|
|
|
$this->repository->createNamedQuery('foo'); |
290
|
|
|
self::assertTrue(true); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @test |
295
|
|
|
* @covers ::clear |
296
|
|
|
*/ |
297
|
|
|
public function clear(): void |
298
|
|
|
{ |
299
|
|
|
$this->repository->clear(); |
300
|
|
|
self::assertSame( |
301
|
|
|
['AbstractEntityRepositoryLargeTest_clear_\Entities\AbstractEntityRepositoryLargeTestEntity' => []], |
302
|
|
|
$this->getEntityManager()->getUnitOfWork()->getIdentityMap() |
303
|
|
|
); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @covers ::count |
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
|
|
|
|
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..