| Total Complexity | 43 |
| Total Lines | 365 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| Bugs | 2 | Features | 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 |
||
| 98 | { |
||
| 99 | $expected = $this->generatedEntities[array_rand($this->generatedEntities)]; |
||
| 100 | $actual = $this->repository->find($expected->getId()); |
||
| 101 | self::assertSame($expected, $actual); |
||
| 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 |
||
| 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 |
||
| 152 | { |
||
| 153 | foreach ($array as $entity) { |
||
| 154 | if ($entity->getId() === $expectedEntity->getId()) { |
||
| 155 | return true; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | return false; |
||
| 160 | } |
||
| 161 | |||
| 162 | private function findOneBy(): void |
||
| 163 | { |
||
| 164 | foreach (MappingHelper::COMMON_TYPES as $property) { |
||
| 165 | $entity = current($this->generatedEntities); |
||
| 166 | $getter = $this->getGetterForType($property); |
||
| 167 | $value = $entity->$getter(); |
||
| 168 | $criteria = [ |
||
| 169 | $property => $value, |
||
| 170 | 'id' => $entity->getId(), |
||
| 171 | ]; |
||
| 172 | $actual = $this->repository->findOneBy($criteria); |
||
| 173 | self::assertEquals( |
||
| 174 | $entity, |
||
| 175 | $actual, |
||
| 176 | 'Failed finding one expected entity (ID' . $entity->getId() . ') with $criteria: ' |
||
| 177 | . "\n" . var_export($criteria, true) |
||
| 178 | . "\n and \$actual: " |
||
| 179 | . "\n" . (new EntityDebugDumper())->dump($actual, $this->getEntityManager()) |
||
|
|
|||
| 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 getClassName(): void |
||
| 248 | { |
||
| 249 | self::assertSame( |
||
| 250 | ltrim($this->getCopiedFqn(self::PERSON_ENTITY_FQN), '\\'), |
||
| 251 | $this->repository->getClassName() |
||
| 252 | ); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @test |
||
| 257 | */ |
||
| 258 | public function itCanUseEntitiesInDql(): void |
||
| 259 | { |
||
| 260 | $this->addAssocEntities(); |
||
| 261 | $queryBuilder = $this->repository->createQueryBuilder('fetch'); |
||
| 262 | $queryBuilder->where('fetch.attributesAddress IS NOT NULL'); |
||
| 263 | |||
| 264 | $person = $queryBuilder->getQuery()->execute()[0]; |
||
| 265 | $address = $person->getAttributesAddress(); |
||
| 266 | $secondQuery = $this->repository->createQueryBuilder('second'); |
||
| 267 | $secondQuery->where('second.attributesAddress = :address'); |
||
| 268 | $secondQuery->setParameter('address', $address); |
||
| 269 | $query = $secondQuery->getQuery(); |
||
| 270 | $secondPerson = $query->execute(); |
||
| 271 | self::assertNotEmpty($secondPerson); |
||
| 272 | |||
| 273 | self::assertSame($address->getId()->toString(), $secondPerson[0]->getAttributesAddress()->getId()->toString()); |
||
| 274 | } |
||
| 275 | |||
| 276 | private function addAssocEntities(): void |
||
| 277 | { |
||
| 278 | foreach ($this->generatedEntities as $entity) { |
||
| 279 | $this->entityGenerator->addAssociationEntities($entity); |
||
| 280 | } |
||
| 281 | $saver = new EntitySaver($this->getEntityManager()); |
||
| 282 | $saver->saveAll($this->generatedEntities); |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @test |
||
| 287 | */ |
||
| 288 | public function getWillThrowAnExceptionIfNothingIsFound(): void |
||
| 289 | { |
||
| 290 | $this->expectException(DoctrineStaticMetaException::class); |
||
| 291 | $this->repository->get(time()); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @test |
||
| 296 | */ |
||
| 297 | public function getOneByWillThrowAnExceptionIfNothingIsFound(): void |
||
| 298 | { |
||
| 299 | $property = MappingHelper::TYPE_STRING; |
||
| 300 | $criteria = [$property => 'not-a-real-vaule']; |
||
| 301 | $this->expectException(\RuntimeException::class); |
||
| 302 | $this->repository->getOneBy($criteria); |
||
| 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 | /** |
||
| 373 | * @test |
||
| 374 | */ |
||
| 375 | public function itCanWorkWithGeneratedAliases(): void |
||
| 376 | { |
||
| 377 | $alias = $this->repository->getAlias(); |
||
| 378 | self::assertNotEmpty($alias); |
||
| 379 | $queryBuilder = $this->repository->createQueryBuilderWithAlias(); |
||
| 380 | $queryBuilder->select($this->repository->aliasPrefix('id')); |
||
| 381 | $query = $queryBuilder->getQuery(); |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @test |
||
| 388 | */ |
||
| 389 | public function itCanBuildADeletionQueryBuilder(): void |
||
| 396 |