Total Complexity | 41 |
Total Lines | 341 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AbstractEntityRepositoryLargeTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractEntityRepositoryLargeTest, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
29 | class AbstractEntityRepositoryLargeTest extends AbstractLargeTest |
||
30 | { |
||
31 | public const WORK_DIR = AbstractTest::VAR_PATH . '/' |
||
32 | . self::TEST_TYPE_LARGE . '/AbstractEntityRepositoryLargeTest'; |
||
33 | |||
34 | private const PERSON_ENTITY_FQN = self::TEST_ENTITIES_ROOT_NAMESPACE . TestCodeGenerator::TEST_ENTITY_PERSON; |
||
35 | |||
36 | private const NUM_ENTITIES_QUICK = 2; |
||
37 | |||
38 | private const NUM_ENTITIES_FULL = 10; |
||
39 | |||
40 | protected static $buildOnce = true; |
||
41 | |||
42 | private $generatedEntities = []; |
||
43 | /** |
||
44 | * @var AbstractEntityRepository |
||
45 | */ |
||
46 | private $repository; |
||
47 | /** |
||
48 | * @var TestEntityGenerator $entityGenerator |
||
49 | */ |
||
50 | private $entityGenerator; |
||
51 | |||
52 | public function setup() |
||
53 | { |
||
54 | parent::setUp(); |
||
55 | $this->generateTestCode(); |
||
56 | $this->setupCopiedWorkDirAndCreateDatabase(); |
||
57 | $this->repository = $this->getRepository(); |
||
58 | $this->entityGenerator = $this->container->get(TestEntityGeneratorFactory::class) |
||
59 | ->createForEntityFqn($this->getCopiedFqn(self::PERSON_ENTITY_FQN)); |
||
60 | $this->generateAndSaveTestEntities(); |
||
61 | } |
||
62 | |||
63 | protected function getRepository(): AbstractEntityRepository |
||
64 | { |
||
65 | return $this->container->get(RepositoryFactory::class) |
||
66 | ->getRepository($this->getCopiedFqn(self::PERSON_ENTITY_FQN)); |
||
67 | } |
||
68 | |||
69 | protected function generateAndSaveTestEntities(): void |
||
70 | { |
||
71 | $this->generatedEntities = $this->entityGenerator->generateEntities( |
||
72 | $this->isQuickTests() ? self::NUM_ENTITIES_QUICK : self::NUM_ENTITIES_FULL |
||
73 | ); |
||
74 | |||
75 | $saver = new EntitySaver($this->getEntityManager()); |
||
76 | $saver->saveAll($this->generatedEntities); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @test |
||
81 | */ |
||
82 | public function itCanRunAllTheBasicMethods(): void |
||
83 | { |
||
84 | $this->find(); |
||
85 | $this->get(); |
||
86 | $this->findAll(); |
||
87 | $this->findBy(); |
||
88 | $this->findOneBy(); |
||
89 | $this->matching(); |
||
90 | $this->createQueryBuilder(); |
||
91 | $this->createResultSetMappingBuilder(); |
||
92 | $this->get(); |
||
93 | $this->getOneBy(); |
||
94 | $this->getClassName(); |
||
95 | } |
||
96 | |||
97 | private function find(): void |
||
102 | } |
||
103 | |||
104 | private function get(): void |
||
105 | { |
||
106 | $expected = $this->generatedEntities[array_rand($this->generatedEntities)]; |
||
107 | $actual = $this->repository->get($expected->getId()); |
||
108 | self::assertSame($expected, $actual); |
||
109 | } |
||
110 | |||
111 | private function findAll(): void |
||
116 | } |
||
117 | |||
118 | private function sortCollectionById(array $collection): array |
||
119 | { |
||
120 | $return = []; |
||
127 | } |
||
128 | |||
129 | private function findBy(): void |
||
130 | { |
||
131 | foreach (MappingHelper::COMMON_TYPES as $property) { |
||
132 | $entity = current($this->generatedEntities); |
||
133 | $getter = $this->getGetterForType($property); |
||
134 | $criteria = [$property => $entity->$getter()]; |
||
135 | $actual = $this->repository->findBy($criteria); |
||
136 | self::assertTrue($this->arrayContainsEntity($entity, $actual)); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | protected function getGetterForType(string $type): string |
||
149 | } |
||
150 | |||
151 | protected function arrayContainsEntity(EntityInterface $expectedEntity, array $array): bool |
||
160 | } |
||
161 | |||
162 | private function findOneBy(): void |
||
180 | ); |
||
181 | } |
||
182 | } |
||
183 | |||
184 | private function matching(): void |
||
185 | { |
||
186 | foreach (MappingHelper::COMMON_TYPES as $property) { |
||
187 | $entity = current($this->generatedEntities); |
||
188 | $getter = $this->getGetterForType($property); |
||
189 | $value = $entity->$getter(); |
||
190 | $criteria = new Criteria(); |
||
191 | $criteria->where(new Comparison($property, '=', $value)); |
||
192 | // $criteria->andWhere(new Comparison('id', '=', $entity->getId())); |
||
193 | $actual = $this->repository->matching($criteria); |
||
194 | self::assertTrue( |
||
195 | $this->collectionContainsEntity($entity, $actual), |
||
196 | "Failed finding entity by criteria $property = $value" |
||
197 | ); |
||
198 | } |
||
199 | } |
||
200 | |||
201 | protected function collectionContainsEntity(EntityInterface $expectedEntity, Collection $collection): bool |
||
202 | { |
||
203 | foreach ($collection->getIterator() as $entity) { |
||
204 | if ($entity->getId()->toString() === $expectedEntity->getId()->toString()) { |
||
205 | return true; |
||
206 | } |
||
207 | } |
||
208 | |||
209 | return false; |
||
210 | } |
||
211 | |||
212 | private function createQueryBuilder(): void |
||
213 | { |
||
214 | $this->repository->createQueryBuilder('foo'); |
||
215 | self::assertTrue(true); |
||
216 | } |
||
217 | |||
218 | private function createResultSetMappingBuilder(): void |
||
219 | { |
||
220 | $this->repository->createResultSetMappingBuilder('foo'); |
||
221 | self::assertTrue(true); |
||
222 | } |
||
223 | |||
224 | private function getOneBy(): void |
||
225 | { |
||
226 | $entity = current($this->generatedEntities); |
||
227 | $getter = $this->getGetterForType(MappingHelper::TYPE_STRING); |
||
228 | $value = $entity->$getter(); |
||
229 | $criteria = [ |
||
230 | MappingHelper::TYPE_STRING => $value, |
||
231 | 'id' => $entity->getId(), |
||
232 | ]; |
||
233 | $actual = $this->repository->findOneBy($criteria); |
||
234 | self::assertEquals( |
||
235 | $entity, |
||
236 | $actual, |
||
237 | 'Failed finding one expected entity (ID' . $entity->getId() . ') with $criteria: ' |
||
238 | . "\n" . var_export($criteria, true) |
||
239 | . "\n and \$actual: " |
||
240 | . "\n" . (new EntityDebugDumper())->dump($actual, $this->getEntityManager()) |
||
241 | ); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * @test |
||
246 | */ |
||
247 | public function itCanUseEntitiesInDql(): void |
||
248 | { |
||
249 | $this->addAssocEntities(); |
||
250 | $queryBuilder = $this->repository->createQueryBuilder('fetch'); |
||
251 | $queryBuilder->where('fetch.attributesAddress IS NOT NULL'); |
||
252 | |||
253 | $person = $queryBuilder->getQuery()->execute()[0]; |
||
254 | $address = $person->getAttributesAddress(); |
||
255 | $secondQuery = $this->repository->createQueryBuilder('second'); |
||
256 | $secondQuery->where('second.attributesAddress = :address'); |
||
257 | $secondQuery->setParameter('address', $address); |
||
258 | $query = $secondQuery->getQuery(); |
||
259 | $secondPerson = $query->execute(); |
||
260 | self::assertNotEmpty($secondPerson); |
||
261 | |||
262 | self::assertSame($address->getId()->toString(), $secondPerson[0]->getAttributesAddress()->getId()->toString()); |
||
263 | } |
||
264 | |||
265 | private function addAssocEntities(): void |
||
266 | { |
||
267 | foreach ($this->generatedEntities as $entity) { |
||
268 | $this->entityGenerator->addAssociationEntities($entity); |
||
269 | } |
||
270 | $saver = new EntitySaver($this->getEntityManager()); |
||
271 | $saver->saveAll($this->generatedEntities); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * @test |
||
276 | */ |
||
277 | public function getWillThrowAnExceptionIfNothingIsFound(): void |
||
278 | { |
||
279 | $this->expectException(DoctrineStaticMetaException::class); |
||
280 | $this->repository->get(time()); |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * @test |
||
285 | */ |
||
286 | public function getOneByWillThrowAnExceptionIfNothingIsFound(): void |
||
287 | { |
||
288 | $property = MappingHelper::TYPE_STRING; |
||
289 | $criteria = [$property => 'not-a-real-vaule']; |
||
290 | $this->expectException(\RuntimeException::class); |
||
291 | $this->repository->getOneBy($criteria); |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * @test |
||
296 | */ |
||
297 | public function getClassName(): void |
||
302 | ); |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * @test |
||
307 | */ |
||
308 | public function createNamedQuery(): void |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * @test |
||
319 | */ |
||
320 | public function clear(): void |
||
321 | { |
||
322 | $this->repository->clear(); |
||
323 | $map = $this->getEntityManager()->getUnitOfWork()->getIdentityMap(); |
||
324 | self::assertSame( |
||
325 | [], |
||
326 | $map[ltrim($this->getCopiedFqn(self::PERSON_ENTITY_FQN), '\\')] |
||
327 | ); |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | */ |
||
332 | public function testCount(): void |
||
337 | ); |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * @test |
||
342 | */ |
||
343 | public function getRandomBy(): void |
||
344 | { |
||
345 | $criteria = []; |
||
346 | $result = $this->repository->getRandomBy($criteria); |
||
347 | self::assertCount(1, $result); |
||
348 | $result = $this->repository->getRandomBy($criteria, 2); |
||
349 | self::assertCount(2, $result); |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @test |
||
354 | */ |
||
355 | public function getRandomOneBy(): void |
||
370 | } |
||
371 | } |
||
372 |