1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\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\AbstractFunctionalTest; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\AbstractIntegrationTest; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
13
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaver; |
14
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory; |
15
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\AbstractEntityTest; |
16
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\TestEntityGenerator; |
17
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\EntityValidatorFactory; |
18
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\FullProjectBuildFunctionalTest; |
19
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
20
|
|
|
use Symfony\Component\Validator\Mapping\Cache\DoctrineCache; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class AbstractEntityRepositoryFunctionalTest |
24
|
|
|
* |
25
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\Entity\Repositories |
26
|
|
|
* @see https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/working-with-objects.html#querying |
27
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
28
|
|
|
* @SuppressWarnings(PHPMD.ExcessivePublicCount) |
29
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
30
|
|
|
*/ |
31
|
|
|
class AbstractEntityRepositoryFunctionalTest extends AbstractFunctionalTest |
32
|
|
|
{ |
33
|
|
|
public const WORK_DIR = AbstractIntegrationTest::VAR_PATH . '/' |
34
|
|
|
. self::TEST_TYPE . '/AbstractEntityRepositoryFunctionalTest'; |
35
|
|
|
|
36
|
|
|
private const TEST_ENTITY_FQN = self::TEST_PROJECT_ROOT_NAMESPACE . '\\Entities\\TestEntity'; |
37
|
|
|
|
38
|
|
|
private const TEST_FIELD_FQN_BASE = FullProjectBuildFunctionalTest::TEST_FIELD_NAMESPACE_BASE . '\\Traits'; |
39
|
|
|
|
40
|
|
|
private $fields = []; |
41
|
|
|
|
42
|
|
|
private $generatedEntities = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var AbstractEntityRepository |
46
|
|
|
*/ |
47
|
|
|
private $repository; |
48
|
|
|
|
49
|
|
|
public function setup() |
50
|
|
|
{ |
51
|
|
|
parent::setup(); |
52
|
|
|
$this->generateCode(); |
53
|
|
|
$this->setupCopiedWorkDirAndCreateDatabase(); |
54
|
|
|
$this->generateAndSaveTestEntities(); |
55
|
|
|
$this->repository = $this->getRepository(); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function generateCode(): void |
59
|
|
|
{ |
60
|
|
|
$entityGenerator = $this->getEntityGenerator(); |
61
|
|
|
|
62
|
|
|
$entityGenerator->generateEntity(self::TEST_ENTITY_FQN); |
63
|
|
|
$fieldGenerator = $this->getFieldGenerator(); |
64
|
|
|
foreach (MappingHelper::COMMON_TYPES as $type) { |
65
|
|
|
$this->fields[] = $fieldFqn = $fieldGenerator->generateField( |
66
|
|
|
self::TEST_FIELD_FQN_BASE . '\\' . ucwords($type), |
67
|
|
|
$type |
68
|
|
|
); |
69
|
|
|
$this->getFieldSetter()->setEntityHasField(self::TEST_ENTITY_FQN, $fieldFqn); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function generateAndSaveTestEntities(): void |
74
|
|
|
{ |
75
|
|
|
$entityGenerator = new TestEntityGenerator( |
76
|
|
|
AbstractEntityTest::SEED, |
77
|
|
|
[], |
78
|
|
|
new \ts\Reflection\ReflectionClass(self::TEST_ENTITY_FQN), |
79
|
|
|
new EntitySaverFactory( |
80
|
|
|
$this->getEntityManager(), |
81
|
|
|
new EntitySaver($this->getEntityManager()), |
82
|
|
|
new NamespaceHelper() |
83
|
|
|
), |
84
|
|
|
new EntityValidatorFactory(new DoctrineCache(new ArrayCache())) |
85
|
|
|
); |
86
|
|
|
$this->generatedEntities = $entityGenerator->generateEntities( |
87
|
|
|
$this->getEntityManager(), |
88
|
|
|
$this->getCopiedFqn(self::TEST_ENTITY_FQN), |
89
|
|
|
$this->isQuickTests() ? 2 : 100 |
90
|
|
|
); |
91
|
|
|
$saver = new EntitySaver($this->getEntityManager()); |
92
|
|
|
$saver->saveAll($this->generatedEntities); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function getRepository() |
96
|
|
|
{ |
97
|
|
|
return $this->getEntityManager()->getRepository($this->getCopiedFqn(self::TEST_ENTITY_FQN)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testFind(): void |
101
|
|
|
{ |
102
|
|
|
$expected = $this->generatedEntities[array_rand($this->generatedEntities)]; |
103
|
|
|
$actual = $this->repository->find($expected->getId()); |
|
|
|
|
104
|
|
|
self::assertSame($expected, $actual); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testFindAll(): void |
108
|
|
|
{ |
109
|
|
|
$expected = $this->generatedEntities; |
110
|
|
|
$actual = $this->repository->findAll(); |
111
|
|
|
self::assertSame($expected, $actual); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testFindBy(): void |
115
|
|
|
{ |
116
|
|
|
foreach (MappingHelper::COMMON_TYPES as $key => $property) { |
117
|
|
|
$entity = $this->getEntityByKey($key); |
118
|
|
|
; |
119
|
|
|
$getter = $this->getGetterForType($property); |
120
|
|
|
$criteria = [$property => $entity->$getter()]; |
121
|
|
|
$actual = $this->repository->findBy($criteria); |
122
|
|
|
self::assertTrue($this->arrayContainsEntity($entity, $actual)); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function getGetterForType(string $type): string |
127
|
|
|
{ |
128
|
|
|
$ucType = ucfirst($type); |
129
|
|
|
$getter = "get$ucType"; |
130
|
|
|
if (MappingHelper::TYPE_BOOLEAN === $type) { |
131
|
|
|
$getter = "is$ucType"; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $getter; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
protected function arrayContainsEntity(EntityInterface $expectedEntity, array $array): bool |
138
|
|
|
{ |
139
|
|
|
foreach ($array as $entity) { |
140
|
|
|
if ($entity->getId() === $expectedEntity->getId()) { |
141
|
|
|
return true; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
private function getEntityByKey(int $key): EntityInterface |
149
|
|
|
{ |
150
|
|
|
if ($this->isQuickTests()) { |
151
|
|
|
return $this->generatedEntities[0]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $this->generatedEntities[$key]; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function testFindOneBy(): void |
158
|
|
|
{ |
159
|
|
|
foreach (MappingHelper::COMMON_TYPES as $key => $property) { |
160
|
|
|
$entity = $this->getEntityByKey($key); |
161
|
|
|
$getter = $this->getGetterForType($property); |
162
|
|
|
$value = $entity->$getter(); |
163
|
|
|
$criteria = [ |
164
|
|
|
$property => $value, |
165
|
|
|
'id' => $entity->getId(), |
166
|
|
|
]; |
167
|
|
|
$actual = $this->repository->findOneBy($criteria); |
|
|
|
|
168
|
|
|
self::assertEquals( |
169
|
|
|
$entity, |
170
|
|
|
$actual, |
171
|
|
|
'Failed finding one expected entity (ID' . $entity->getId() . ') with $criteria: ' |
172
|
|
|
. "\n" . var_export($criteria, true) |
173
|
|
|
. "\n and \$actual: " |
174
|
|
|
. "\n" . $actual->__toString() |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function testGetClassName(): void |
180
|
|
|
{ |
181
|
|
|
self::assertSame( |
182
|
|
|
ltrim($this->getCopiedFqn(self::TEST_ENTITY_FQN), '\\'), |
183
|
|
|
$this->repository->getClassName() |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function testMatching(): void |
188
|
|
|
{ |
189
|
|
|
foreach (MappingHelper::COMMON_TYPES as $key => $property) { |
190
|
|
|
$entity = $this->getEntityByKey($key); |
191
|
|
|
; |
192
|
|
|
$getter = $this->getGetterForType($property); |
193
|
|
|
$value = $entity->$getter(); |
194
|
|
|
$criteria = new Criteria(); |
195
|
|
|
$criteria->where(new Comparison($property, '=', $value)); |
196
|
|
|
$criteria->andWhere(new Comparison('id', '=', $entity->getId())); |
197
|
|
|
$actual = $this->repository->matching($criteria); |
198
|
|
|
self::assertTrue($this->collectionContainsEntity($entity, $actual)); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
protected function collectionContainsEntity(EntityInterface $expectedEntity, Collection $collection): bool |
203
|
|
|
{ |
204
|
|
|
foreach ($collection->getIterator() as $entity) { |
205
|
|
|
if ($entity->getId() === $expectedEntity->getId()) { |
206
|
|
|
return true; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return false; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function testCreateQueryBuilder(): void |
214
|
|
|
{ |
215
|
|
|
$this->repository->createQueryBuilder('foo'); |
216
|
|
|
self::assertTrue(true); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function testCreateResultSetMappingBuilder(): void |
220
|
|
|
{ |
221
|
|
|
$this->repository->createResultSetMappingBuilder('foo'); |
222
|
|
|
self::assertTrue(true); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function testCreateNamedQuery(): void |
226
|
|
|
{ |
227
|
|
|
$this->markTestIncomplete( |
228
|
|
|
'Need to add a named query for a test entity somehow in the meta data before we can test this' |
229
|
|
|
); |
230
|
|
|
$this->repository->createNamedQuery('foo'); |
231
|
|
|
self::assertTrue(true); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function testClear(): void |
235
|
|
|
{ |
236
|
|
|
$this->repository->clear(); |
237
|
|
|
self::assertSame( |
238
|
|
|
['AbstractEntityRepositoryFunctionalTest_testClear_\Entities\TestEntity' => []], |
239
|
|
|
$this->getEntityManager()->getUnitOfWork()->getIdentityMap() |
240
|
|
|
); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function testCount(): void |
244
|
|
|
{ |
245
|
|
|
self::assertSame($this->isQuickTests() ? 2 : 100, $this->repository->count([])); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
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..