Complex classes like ModelManagerTest 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ModelManagerTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 64 | class ModelManagerTest extends TestCase |
||
| 65 | { |
||
| 66 | /** |
||
| 67 | * @var ManagerRegistry|MockObject |
||
| 68 | */ |
||
| 69 | private $registry; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var ModelManager |
||
| 73 | */ |
||
| 74 | private $modelManager; |
||
| 75 | |||
| 76 | public static function setUpBeforeClass(): void |
||
| 77 | { |
||
| 78 | if (!Type::hasType(UuidType::NAME)) { |
||
| 79 | Type::addType(UuidType::NAME, UuidType::class); |
||
| 80 | } |
||
| 81 | if (!Type::hasType(UuidBinaryType::NAME)) { |
||
| 82 | Type::addType(UuidBinaryType::NAME, UuidBinaryType::class); |
||
| 83 | } |
||
| 84 | if (!Type::hasType(ProductIdType::NAME)) { |
||
| 85 | Type::addType(ProductIdType::NAME, ProductIdType::class); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | protected function setup(): void |
||
| 90 | { |
||
| 91 | $this->registry = $this->createMock(ManagerRegistry::class); |
||
| 92 | $this->modelManager = new ModelManager($this->registry, PropertyAccess::createPropertyAccessor()); |
||
| 93 | } |
||
| 94 | |||
| 95 | public function valueObjectDataProvider(): array |
||
| 96 | { |
||
| 97 | return [ |
||
| 98 | 'value object with toString implementation' => [ValueObjectWithToStringImpl::class], |
||
| 99 | 'value object with magic toString implementation' => [ValueObjectWithMagicToStringImpl::class], |
||
| 100 | ]; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @dataProvider valueObjectDataProvider |
||
| 105 | */ |
||
| 106 | public function testGetIdentifierValuesWhenIdentifierIsValueObjectWithToStringMethod(string $vbClassName): void |
||
| 107 | { |
||
| 108 | $entity = new UuidBinaryEntity(new $vbClassName('a7ef873a-e7b5-11e9-81b4-2a2ae2dbcce4')); |
||
| 109 | |||
| 110 | $platform = $this->createMock(MySqlPlatform::class); |
||
| 111 | |||
| 112 | $connection = $this->createMock(Connection::class); |
||
| 113 | $connection->method('getDatabasePlatform')->willReturn($platform); |
||
| 114 | |||
| 115 | $classMetadata = $this->createMock(ClassMetadataInfo::class); |
||
| 116 | $classMetadata->method('getIdentifierValues')->willReturn([$entity->getId()]); |
||
| 117 | $classMetadata->method('getTypeOfField')->willReturn(UuidBinaryType::NAME); |
||
| 118 | |||
| 119 | $classMetadataFactory = $this->createMock(ClassMetadataFactory::class); |
||
| 120 | $classMetadataFactory->method('getMetadataFor')->willReturn($classMetadata); |
||
| 121 | |||
| 122 | $entityManager = $this->createMock(EntityManager::class); |
||
| 123 | $entityManager->method('getMetadataFactory')->willReturn($classMetadataFactory); |
||
| 124 | $entityManager->method('getConnection')->willReturn($connection); |
||
| 125 | |||
| 126 | $this->registry->method('getManagerForClass')->willReturn($entityManager); |
||
|
|
|||
| 127 | |||
| 128 | $this->assertSame( |
||
| 129 | ['a7ef873a-e7b5-11e9-81b4-2a2ae2dbcce4'], |
||
| 130 | $this->modelManager->getIdentifierValues($entity) |
||
| 131 | ); |
||
| 132 | } |
||
| 133 | |||
| 134 | public function testInstantiateWithDeprecatedRegistryInterface(): void |
||
| 135 | { |
||
| 136 | $em = $this->createMock(EntityManagerInterface::class); |
||
| 137 | |||
| 138 | $this->registry->expects($this->once()) |
||
| 139 | ->method('getManagerForClass') |
||
| 140 | ->with('x') |
||
| 141 | ->willReturn($em) |
||
| 142 | ; |
||
| 143 | $this->assertSame($em, $this->modelManager->getEntityManager('x')); |
||
| 144 | } |
||
| 145 | |||
| 146 | public function testSortParameters(): void |
||
| 147 | { |
||
| 148 | $datagrid1 = $this->createMock(Datagrid::class); |
||
| 149 | $datagrid2 = $this->createMock(Datagrid::class); |
||
| 150 | |||
| 151 | $field1 = new FieldDescription(); |
||
| 152 | $field1->setName('field1'); |
||
| 153 | |||
| 154 | $field2 = new FieldDescription(); |
||
| 155 | $field2->setName('field2'); |
||
| 156 | |||
| 157 | $field3 = new FieldDescription(); |
||
| 158 | $field3->setName('field3'); |
||
| 159 | $field3->setOption('sortable', 'field3sortBy'); |
||
| 160 | |||
| 161 | $datagrid1 |
||
| 162 | ->expects($this->any()) |
||
| 163 | ->method('getValues') |
||
| 164 | ->willReturn([ |
||
| 165 | '_sort_by' => $field1, |
||
| 166 | '_sort_order' => 'ASC', |
||
| 167 | ]); |
||
| 168 | |||
| 169 | $datagrid2 |
||
| 170 | ->expects($this->any()) |
||
| 171 | ->method('getValues') |
||
| 172 | ->willReturn([ |
||
| 173 | '_sort_by' => $field3, |
||
| 174 | '_sort_order' => 'ASC', |
||
| 175 | ]); |
||
| 176 | |||
| 177 | $parameters = $this->modelManager->getSortParameters($field1, $datagrid1); |
||
| 178 | |||
| 179 | $this->assertSame('DESC', $parameters['filter']['_sort_order']); |
||
| 180 | $this->assertSame('field1', $parameters['filter']['_sort_by']); |
||
| 181 | |||
| 182 | $parameters = $this->modelManager->getSortParameters($field2, $datagrid1); |
||
| 183 | |||
| 184 | $this->assertSame('ASC', $parameters['filter']['_sort_order']); |
||
| 185 | $this->assertSame('field2', $parameters['filter']['_sort_by']); |
||
| 186 | |||
| 187 | $parameters = $this->modelManager->getSortParameters($field3, $datagrid1); |
||
| 188 | |||
| 189 | $this->assertSame('ASC', $parameters['filter']['_sort_order']); |
||
| 190 | $this->assertSame('field3sortBy', $parameters['filter']['_sort_by']); |
||
| 191 | |||
| 192 | $parameters = $this->modelManager->getSortParameters($field3, $datagrid2); |
||
| 193 | |||
| 194 | $this->assertSame('DESC', $parameters['filter']['_sort_order']); |
||
| 195 | $this->assertSame('field3sortBy', $parameters['filter']['_sort_by']); |
||
| 196 | } |
||
| 197 | |||
| 198 | public function getVersionDataProvider() |
||
| 199 | { |
||
| 200 | return [ |
||
| 201 | [true], |
||
| 202 | [false], |
||
| 203 | ]; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @dataProvider getVersionDataProvider |
||
| 208 | */ |
||
| 209 | public function testGetVersion($isVersioned): void |
||
| 210 | { |
||
| 211 | $object = new VersionedEntity(); |
||
| 212 | |||
| 213 | $modelManager = $this->getMockBuilder(ModelManager::class) |
||
| 214 | ->disableOriginalConstructor() |
||
| 215 | ->setMethods(['getMetadata']) |
||
| 216 | ->getMock(); |
||
| 217 | |||
| 218 | $metadata = $this->getMetadata(\get_class($object), $isVersioned); |
||
| 219 | |||
| 220 | $modelManager->expects($this->any()) |
||
| 221 | ->method('getMetadata') |
||
| 222 | ->willReturn($metadata); |
||
| 223 | |||
| 224 | if ($isVersioned) { |
||
| 225 | $object->version = 123; |
||
| 226 | |||
| 227 | $this->assertNotNull($modelManager->getLockVersion($object)); |
||
| 228 | } else { |
||
| 229 | $this->assertNull($modelManager->getLockVersion($object)); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | public function lockDataProvider() |
||
| 234 | { |
||
| 235 | return [ |
||
| 236 | [true, false], |
||
| 237 | [true, true], |
||
| 238 | [false, false], |
||
| 239 | ]; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @dataProvider lockDataProvider |
||
| 244 | */ |
||
| 245 | public function testLock($isVersioned, $expectsException): void |
||
| 246 | { |
||
| 247 | $object = new VersionedEntity(); |
||
| 248 | |||
| 249 | $em = $this->getMockBuilder(EntityManager::class) |
||
| 250 | ->disableOriginalConstructor() |
||
| 251 | ->setMethods(['lock']) |
||
| 252 | ->getMock(); |
||
| 253 | |||
| 254 | $modelManager = $this->getMockBuilder(ModelManager::class) |
||
| 255 | ->disableOriginalConstructor() |
||
| 256 | ->setMethods(['getMetadata', 'getEntityManager']) |
||
| 257 | ->getMock(); |
||
| 258 | |||
| 259 | $modelManager->expects($this->any()) |
||
| 260 | ->method('getEntityManager') |
||
| 261 | ->willReturn($em); |
||
| 262 | |||
| 263 | $metadata = $this->getMetadata(\get_class($object), $isVersioned); |
||
| 264 | |||
| 265 | $modelManager->expects($this->any()) |
||
| 266 | ->method('getMetadata') |
||
| 267 | ->willReturn($metadata); |
||
| 268 | |||
| 269 | $em->expects($isVersioned ? $this->once() : $this->never()) |
||
| 270 | ->method('lock'); |
||
| 271 | |||
| 272 | if ($expectsException) { |
||
| 273 | $em->expects($this->once()) |
||
| 274 | ->method('lock') |
||
| 275 | ->will($this->throwException(OptimisticLockException::lockFailed($object))); |
||
| 276 | |||
| 277 | $this->expectException(LockException::class); |
||
| 278 | } |
||
| 279 | |||
| 280 | $modelManager->lock($object, 123); |
||
| 281 | } |
||
| 282 | |||
| 283 | public function testGetParentMetadataForProperty(): void |
||
| 284 | { |
||
| 285 | $containerEntityClass = ContainerEntity::class; |
||
| 286 | $associatedEntityClass = AssociatedEntity::class; |
||
| 287 | $embeddedEntityClass = EmbeddedEntity::class; |
||
| 288 | $modelManagerClass = ModelManager::class; |
||
| 289 | |||
| 290 | $em = $this->createMock(EntityManager::class); |
||
| 291 | |||
| 292 | /** @var MockObject|ModelManager $modelManager */ |
||
| 293 | $modelManager = $this->getMockBuilder($modelManagerClass) |
||
| 294 | ->disableOriginalConstructor() |
||
| 295 | ->setMethods(['getMetadata', 'getEntityManager']) |
||
| 296 | ->getMock(); |
||
| 297 | |||
| 298 | $modelManager->expects($this->any()) |
||
| 299 | ->method('getEntityManager') |
||
| 300 | ->willReturn($em); |
||
| 301 | |||
| 302 | $containerEntityMetadata = $this->getMetadataForContainerEntity(); |
||
| 303 | $associatedEntityMetadata = $this->getMetadataForAssociatedEntity(); |
||
| 304 | $embeddedEntityMetadata = $this->getMetadataForEmbeddedEntity(); |
||
| 305 | |||
| 306 | $modelManager->expects($this->any())->method('getMetadata') |
||
| 307 | ->willReturnMap( |
||
| 308 | [ |
||
| 309 | [$containerEntityClass, $containerEntityMetadata], |
||
| 310 | [$embeddedEntityClass, $embeddedEntityMetadata], |
||
| 311 | [$associatedEntityClass, $associatedEntityMetadata], |
||
| 312 | ] |
||
| 313 | ); |
||
| 314 | |||
| 315 | /** @var ClassMetadata $metadata */ |
||
| 316 | list($metadata, $lastPropertyName) = $modelManager |
||
| 317 | ->getParentMetadataForProperty($containerEntityClass, 'plainField'); |
||
| 318 | $this->assertSame($metadata->fieldMappings[$lastPropertyName]['type'], 'integer'); |
||
| 319 | |||
| 320 | list($metadata, $lastPropertyName) = $modelManager |
||
| 321 | ->getParentMetadataForProperty($containerEntityClass, 'associatedEntity.plainField'); |
||
| 322 | $this->assertSame($metadata->fieldMappings[$lastPropertyName]['type'], 'string'); |
||
| 323 | |||
| 324 | list($metadata, $lastPropertyName) = $modelManager |
||
| 325 | ->getParentMetadataForProperty($containerEntityClass, 'embeddedEntity.plainField'); |
||
| 326 | $this->assertSame($metadata->fieldMappings[$lastPropertyName]['type'], 'boolean'); |
||
| 327 | |||
| 328 | list($metadata, $lastPropertyName) = $modelManager |
||
| 329 | ->getParentMetadataForProperty($containerEntityClass, 'associatedEntity.embeddedEntity.plainField'); |
||
| 330 | $this->assertSame($metadata->fieldMappings[$lastPropertyName]['type'], 'boolean'); |
||
| 331 | |||
| 332 | list($metadata, $lastPropertyName) = $modelManager |
||
| 333 | ->getParentMetadataForProperty( |
||
| 334 | $containerEntityClass, |
||
| 335 | 'associatedEntity.embeddedEntity.subEmbeddedEntity.plainField' |
||
| 336 | ); |
||
| 337 | $this->assertSame($metadata->fieldMappings[$lastPropertyName]['type'], 'boolean'); |
||
| 338 | } |
||
| 339 | |||
| 340 | public function getMetadataForEmbeddedEntity() |
||
| 341 | { |
||
| 342 | $metadata = new ClassMetadata(EmbeddedEntity::class); |
||
| 343 | |||
| 344 | $metadata->fieldMappings = [ |
||
| 345 | 'plainField' => [ |
||
| 346 | 'fieldName' => 'plainField', |
||
| 347 | 'columnName' => 'plainField', |
||
| 348 | 'type' => 'boolean', |
||
| 349 | ], |
||
| 350 | ]; |
||
| 351 | |||
| 352 | return $metadata; |
||
| 353 | } |
||
| 354 | |||
| 355 | public function getMetadataForSubEmbeddedEntity() |
||
| 356 | { |
||
| 357 | $metadata = new ClassMetadata(SubEmbeddedEntity::class); |
||
| 358 | |||
| 359 | $metadata->fieldMappings = [ |
||
| 360 | 'plainField' => [ |
||
| 361 | 'fieldName' => 'plainField', |
||
| 362 | 'columnName' => 'plainField', |
||
| 363 | 'type' => 'boolean', |
||
| 364 | ], |
||
| 365 | ]; |
||
| 366 | |||
| 367 | return $metadata; |
||
| 368 | } |
||
| 369 | |||
| 370 | public function getMetadataForAssociatedEntity() |
||
| 371 | { |
||
| 372 | $embeddedEntityClass = EmbeddedEntity::class; |
||
| 373 | $subEmbeddedEntityClass = SubEmbeddedEntity::class; |
||
| 374 | |||
| 375 | $metadata = new ClassMetadata(AssociatedEntity::class); |
||
| 376 | |||
| 377 | $metadata->fieldMappings = [ |
||
| 378 | 'plainField' => [ |
||
| 379 | 'fieldName' => 'plainField', |
||
| 380 | 'columnName' => 'plainField', |
||
| 381 | 'type' => 'string', |
||
| 382 | ], |
||
| 383 | ]; |
||
| 384 | |||
| 385 | $metadata->embeddedClasses['embeddedEntity'] = [ |
||
| 386 | 'class' => $embeddedEntityClass, |
||
| 387 | 'columnPrefix' => 'embedded_entity_', |
||
| 388 | ]; |
||
| 389 | $metadata->embeddedClasses['embeddedEntity.subEmbeddedEntity'] = [ |
||
| 390 | 'class' => $subEmbeddedEntityClass, |
||
| 391 | 'columnPrefix' => 'embedded_entity_sub_embedded_entity_', |
||
| 392 | 'declaredField' => 'embeddedEntity', |
||
| 393 | 'originalField' => 'subEmbeddedEntity', |
||
| 394 | ]; |
||
| 395 | |||
| 396 | $metadata->inlineEmbeddable('embeddedEntity', $this->getMetadataForEmbeddedEntity()); |
||
| 397 | $metadata->inlineEmbeddable('embeddedEntity.subEmbeddedEntity', $this->getMetadataForSubEmbeddedEntity()); |
||
| 398 | |||
| 399 | return $metadata; |
||
| 400 | } |
||
| 401 | |||
| 402 | public function getMetadataForContainerEntity() |
||
| 403 | { |
||
| 404 | $containerEntityClass = ContainerEntity::class; |
||
| 405 | $associatedEntityClass = AssociatedEntity::class; |
||
| 406 | $embeddedEntityClass = EmbeddedEntity::class; |
||
| 407 | $subEmbeddedEntityClass = SubEmbeddedEntity::class; |
||
| 408 | |||
| 409 | $metadata = new ClassMetadata($containerEntityClass); |
||
| 410 | |||
| 411 | $metadata->fieldMappings = [ |
||
| 412 | 'plainField' => [ |
||
| 413 | 'fieldName' => 'plainField', |
||
| 414 | 'columnName' => 'plainField', |
||
| 415 | 'type' => 'integer', |
||
| 416 | ], |
||
| 417 | ]; |
||
| 418 | |||
| 419 | $metadata->associationMappings['associatedEntity'] = [ |
||
| 420 | 'fieldName' => 'associatedEntity', |
||
| 421 | 'targetEntity' => $associatedEntityClass, |
||
| 422 | 'sourceEntity' => $containerEntityClass, |
||
| 423 | ]; |
||
| 424 | |||
| 425 | $metadata->embeddedClasses['embeddedEntity'] = [ |
||
| 426 | 'class' => $embeddedEntityClass, |
||
| 427 | 'columnPrefix' => 'embeddedEntity', |
||
| 428 | ]; |
||
| 429 | $metadata->embeddedClasses['embeddedEntity.subEmbeddedEntity'] = [ |
||
| 430 | 'class' => $subEmbeddedEntityClass, |
||
| 431 | 'columnPrefix' => 'embedded_entity_sub_embedded_entity_', |
||
| 432 | 'declaredField' => 'embeddedEntity', |
||
| 433 | 'originalField' => 'subEmbeddedEntity', |
||
| 434 | ]; |
||
| 435 | |||
| 436 | $metadata->inlineEmbeddable('embeddedEntity', $this->getMetadataForEmbeddedEntity()); |
||
| 437 | $metadata->inlineEmbeddable('embeddedEntity.subEmbeddedEntity', $this->getMetadataForSubEmbeddedEntity()); |
||
| 438 | |||
| 439 | return $metadata; |
||
| 440 | } |
||
| 441 | |||
| 442 | public function testGetIdentifierValuesForIdInObjectTypeBinaryToStringSupport(): void |
||
| 443 | { |
||
| 444 | $uuid = new NonIntegerIdentifierTestClass('efbcfc4b-8c43-4d42-aa4c-d707e55151ac'); |
||
| 445 | |||
| 446 | $entity = new UuidEntity($uuid); |
||
| 447 | |||
| 448 | $meta = $this->createMock(ClassMetadata::class); |
||
| 449 | $meta->expects($this->any()) |
||
| 450 | ->method('getIdentifierValues') |
||
| 451 | ->willReturn([$entity->getId()]); |
||
| 452 | $meta->expects($this->any()) |
||
| 453 | ->method('getTypeOfField') |
||
| 454 | ->willReturn(UuidBinaryType::NAME); //'uuid_binary' |
||
| 455 | |||
| 456 | $mf = $this->createMock(ClassMetadataFactory::class); |
||
| 457 | $mf->expects($this->any()) |
||
| 458 | ->method('getMetadataFor') |
||
| 459 | ->willReturn($meta); |
||
| 460 | |||
| 461 | $platform = $this->createMock(PostgreSqlPlatform::class); |
||
| 462 | $platform->expects($this->any()) |
||
| 463 | ->method('hasDoctrineTypeMappingFor') |
||
| 464 | ->with(UuidBinaryType::NAME) |
||
| 465 | ->willReturn(true); |
||
| 466 | $platform->expects($this->any()) |
||
| 467 | ->method('getDoctrineTypeMapping') |
||
| 468 | ->with(UuidBinaryType::NAME) |
||
| 469 | ->willReturn('binary'); |
||
| 470 | |||
| 471 | $conn = $this->createMock(Connection::class); |
||
| 472 | $conn->expects($this->any()) |
||
| 473 | ->method('getDatabasePlatform') |
||
| 474 | ->willReturn($platform); |
||
| 475 | |||
| 476 | $em = $this->createMock(EntityManager::class); |
||
| 477 | $em->expects($this->any()) |
||
| 478 | ->method('getMetadataFactory') |
||
| 479 | ->willReturn($mf); |
||
| 480 | $em->expects($this->any()) |
||
| 481 | ->method('getConnection') |
||
| 482 | ->willReturn($conn); |
||
| 483 | |||
| 484 | $this->registry->expects($this->any()) |
||
| 485 | ->method('getManagerForClass') |
||
| 486 | ->willReturn($em); |
||
| 487 | |||
| 488 | $result = $this->modelManager->getIdentifierValues($entity); |
||
| 489 | |||
| 490 | $this->assertSame($entity->getId()->toString(), $result[0]); |
||
| 491 | } |
||
| 492 | |||
| 493 | public function testNonIntegerIdentifierType(): void |
||
| 494 | { |
||
| 495 | $uuid = new NonIntegerIdentifierTestClass('efbcfc4b-8c43-4d42-aa4c-d707e55151ac'); |
||
| 496 | $entity = new UuidEntity($uuid); |
||
| 497 | |||
| 498 | $meta = $this->createMock(ClassMetadata::class); |
||
| 499 | $meta->expects($this->any()) |
||
| 500 | ->method('getIdentifierValues') |
||
| 501 | ->willReturn([$entity->getId()]); |
||
| 502 | $meta->expects($this->any()) |
||
| 503 | ->method('getTypeOfField') |
||
| 504 | ->willReturn(UuidType::NAME); |
||
| 505 | |||
| 506 | $mf = $this->createMock(ClassMetadataFactory::class); |
||
| 507 | $mf->expects($this->any()) |
||
| 508 | ->method('getMetadataFor') |
||
| 509 | ->willReturn($meta); |
||
| 510 | |||
| 511 | $platform = $this->createMock(PostgreSqlPlatform::class); |
||
| 512 | $platform->expects($this->any()) |
||
| 513 | ->method('hasDoctrineTypeMappingFor') |
||
| 514 | ->with(UuidType::NAME) |
||
| 515 | ->willReturn(false); |
||
| 516 | $platform->expects($this->never()) |
||
| 517 | ->method('getDoctrineTypeMapping'); |
||
| 518 | |||
| 519 | $conn = $this->createMock(Connection::class); |
||
| 520 | $conn->expects($this->any()) |
||
| 521 | ->method('getDatabasePlatform') |
||
| 522 | ->willReturn($platform); |
||
| 523 | |||
| 524 | $em = $this->createMock(EntityManager::class); |
||
| 525 | $em->expects($this->any()) |
||
| 526 | ->method('getMetadataFactory') |
||
| 527 | ->willReturn($mf); |
||
| 528 | $em->expects($this->any()) |
||
| 529 | ->method('getConnection') |
||
| 530 | ->willReturn($conn); |
||
| 531 | |||
| 532 | $this->registry->expects($this->any()) |
||
| 533 | ->method('getManagerForClass') |
||
| 534 | ->willReturn($em); |
||
| 535 | |||
| 536 | $result = $this->modelManager->getIdentifierValues($entity); |
||
| 537 | |||
| 538 | $this->assertSame($entity->getId()->toString(), $result[0]); |
||
| 539 | } |
||
| 540 | |||
| 541 | public function testIntegerIdentifierType(): void |
||
| 542 | { |
||
| 543 | $id = new ProductId(12345); |
||
| 544 | $entity = new Product($id, 'Some product'); |
||
| 545 | |||
| 546 | $meta = $this->createMock(ClassMetadata::class); |
||
| 547 | $meta->expects($this->any()) |
||
| 548 | ->method('getIdentifierValues') |
||
| 549 | ->willReturn([$entity->getId()]); |
||
| 550 | $meta->expects($this->any()) |
||
| 551 | ->method('getTypeOfField') |
||
| 552 | ->willReturn(ProductIdType::NAME); |
||
| 553 | |||
| 554 | $mf = $this->createMock(ClassMetadataFactory::class); |
||
| 555 | $mf->expects($this->any()) |
||
| 556 | ->method('getMetadataFor') |
||
| 557 | ->willReturn($meta); |
||
| 558 | |||
| 559 | $platform = $this->createMock(PostgreSqlPlatform::class); |
||
| 560 | $platform->expects($this->any()) |
||
| 561 | ->method('hasDoctrineTypeMappingFor') |
||
| 562 | ->with(ProductIdType::NAME) |
||
| 563 | ->willReturn(false); |
||
| 564 | $platform->expects($this->never()) |
||
| 565 | ->method('getDoctrineTypeMapping'); |
||
| 566 | |||
| 567 | $conn = $this->createMock(Connection::class); |
||
| 568 | $conn->expects($this->any()) |
||
| 569 | ->method('getDatabasePlatform') |
||
| 570 | ->willReturn($platform); |
||
| 571 | |||
| 572 | $em = $this->createMock(EntityManager::class); |
||
| 573 | $em->expects($this->any()) |
||
| 574 | ->method('getMetadataFactory') |
||
| 575 | ->willReturn($mf); |
||
| 576 | $em->expects($this->any()) |
||
| 577 | ->method('getConnection') |
||
| 578 | ->willReturn($conn); |
||
| 579 | |||
| 580 | $this->registry->expects($this->any()) |
||
| 581 | ->method('getManagerForClass') |
||
| 582 | ->willReturn($em); |
||
| 583 | |||
| 584 | $result = $this->modelManager->getIdentifierValues($entity); |
||
| 585 | |||
| 586 | $this->assertSame((string) $entity->getId()->getId(), $result[0]); |
||
| 587 | } |
||
| 588 | |||
| 589 | public function testAssociationIdentifierType(): void |
||
| 590 | { |
||
| 591 | $entity = new ContainerEntity(new AssociatedEntity(42, new EmbeddedEntity()), new EmbeddedEntity()); |
||
| 592 | |||
| 593 | $meta = $this->createMock(ClassMetadata::class); |
||
| 594 | $meta->expects($this->any()) |
||
| 595 | ->method('getIdentifierValues') |
||
| 596 | ->willReturn([$entity->getAssociatedEntity()->getPlainField()]); |
||
| 597 | $meta->expects($this->any()) |
||
| 598 | ->method('getTypeOfField') |
||
| 599 | ->willReturn(null); |
||
| 600 | |||
| 601 | $mf = $this->createMock(ClassMetadataFactory::class); |
||
| 602 | $mf->expects($this->any()) |
||
| 603 | ->method('getMetadataFor') |
||
| 604 | ->willReturn($meta); |
||
| 605 | |||
| 606 | $platform = $this->createMock(PostgreSqlPlatform::class); |
||
| 607 | $platform->expects($this->never()) |
||
| 608 | ->method('hasDoctrineTypeMappingFor'); |
||
| 609 | |||
| 610 | $conn = $this->createMock(Connection::class); |
||
| 611 | $conn->expects($this->any()) |
||
| 612 | ->method('getDatabasePlatform') |
||
| 613 | ->willReturn($platform); |
||
| 614 | |||
| 615 | $em = $this->createMock(EntityManager::class); |
||
| 616 | $em->expects($this->any()) |
||
| 617 | ->method('getMetadataFactory') |
||
| 618 | ->willReturn($mf); |
||
| 619 | $em->expects($this->any()) |
||
| 620 | ->method('getConnection') |
||
| 621 | ->willReturn($conn); |
||
| 622 | |||
| 623 | $this->registry->expects($this->any()) |
||
| 624 | ->method('getManagerForClass') |
||
| 625 | ->willReturn($em); |
||
| 626 | |||
| 627 | $result = $this->modelManager->getIdentifierValues($entity); |
||
| 628 | |||
| 629 | $this->assertSame(42, $result[0]); |
||
| 630 | } |
||
| 631 | |||
| 632 | /** |
||
| 633 | * [sortBy, sortOrder, isAddOrderBy]. |
||
| 634 | * |
||
| 635 | * @return array |
||
| 636 | */ |
||
| 637 | public function getSortableInDataSourceIteratorDataProvider() |
||
| 646 | |||
| 647 | /** |
||
| 648 | * @dataProvider getSortableInDataSourceIteratorDataProvider |
||
| 649 | * |
||
| 650 | * @param string|null $sortBy |
||
| 651 | * @param string|null $sortOrder |
||
| 652 | * @param bool $isAddOrderBy |
||
| 653 | */ |
||
| 654 | public function testSortableInDataSourceIterator($sortBy, $sortOrder, $isAddOrderBy): void |
||
| 655 | { |
||
| 656 | $datagrid = $this->getMockForAbstractClass(DatagridInterface::class); |
||
| 657 | $configuration = $this->getMockBuilder(Configuration::class)->getMock(); |
||
| 658 | $configuration->expects($this->any()) |
||
| 659 | ->method('getDefaultQueryHints') |
||
| 660 | ->willReturn([]); |
||
| 661 | |||
| 662 | $em = $this->getMockBuilder(EntityManager::class) |
||
| 663 | ->disableOriginalConstructor() |
||
| 664 | ->getMock(); |
||
| 665 | |||
| 710 | |||
| 711 | public function testModelReverseTransform(): void |
||
| 744 | |||
| 745 | public function testCollections(): void |
||
| 765 | |||
| 766 | public function testModelTransform(): void |
||
| 772 | |||
| 773 | public function testGetPaginationParameters(): void |
||
| 791 | |||
| 792 | public function testGetModelInstanceException(): void |
||
| 798 | |||
| 799 | public function testGetModelInstanceForProtectedEntity(): void |
||
| 803 | |||
| 804 | public function testGetEntityManagerException(): void |
||
| 810 | |||
| 811 | public function testGetNewFieldDescriptionInstanceException(): void |
||
| 817 | |||
| 818 | /** |
||
| 819 | * @dataProvider createUpdateRemoveData |
||
| 820 | */ |
||
| 821 | public function testCreate($exception): void |
||
| 840 | |||
| 841 | public function createUpdateRemoveData() |
||
| 852 | |||
| 853 | /** |
||
| 854 | * @dataProvider createUpdateRemoveData |
||
| 855 | */ |
||
| 856 | public function testUpdate($exception): void |
||
| 875 | |||
| 876 | /** |
||
| 877 | * @dataProvider createUpdateRemoveData |
||
| 878 | */ |
||
| 879 | public function testRemove($exception): void |
||
| 898 | |||
| 899 | /** |
||
| 900 | * NEXT_MAJOR: Remove this method. |
||
| 901 | * |
||
| 902 | * @group legacy |
||
| 903 | * |
||
| 904 | * @expectedDeprecation Passing null as argument 1 for Sonata\DoctrineORMAdminBundle\Model\ModelManager::find() is deprecated since sonata-project/doctrine-orm-admin-bundle 3.20 and will be not allowed in version 4.0. |
||
| 905 | */ |
||
| 906 | public function testFindBadId(): void |
||
| 910 | |||
| 911 | /** |
||
| 912 | * @dataProvider getWrongEntities |
||
| 913 | * |
||
| 914 | * @param mixed $entity |
||
| 915 | */ |
||
| 916 | public function testNormalizedIdentifierException($entity): void |
||
| 922 | |||
| 923 | public function getWrongEntities(): iterable |
||
| 933 | |||
| 934 | /** |
||
| 935 | * NEXT_MAJOR: Remove this method. |
||
| 936 | * |
||
| 937 | * @group legacy |
||
| 938 | * |
||
| 939 | * @expectedDeprecation Passing null as argument 1 for Sonata\DoctrineORMAdminBundle\Model\ModelManager::getNormalizedIdentifier() is deprecated since sonata-project/doctrine-orm-admin-bundle 3.20 and will be not allowed in version 4.0. |
||
| 940 | */ |
||
| 941 | public function testGetUrlsafeIdentifierNull(): void |
||
| 945 | |||
| 946 | private function getMetadata($class, $isVersioned) |
||
| 960 | } |
||
| 961 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.