Skip to content

Commit 04915c1

Browse files
authored
Merge pull request #644 from TomHAnderson/hotfix/get-manager-for-class
Revert return to null if manager is not found; add bool to throw exce…
2 parents 936cadc + 30b6b16 commit 04915c1

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/IlluminateRegistry.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,10 @@ public function getRepository(string $persistentObject, string|null $persistentM
301301
* Gets the object manager associated with a given class.
302302
*
303303
* @param class-string $className A persistent object class name.
304+
*
305+
* @return ($throwExceptionIfNotFound is true ? ObjectManager : ObjectManager|null)
304306
*/
305-
public function getManagerForClass(string $className): ObjectManager|null
307+
public function getManagerForClass(string $className, bool $throwExceptionIfNotFound = false): ObjectManager|null
306308
{
307309
// Check for namespace alias
308310
if (strpos($className, ':') !== false) {
@@ -335,7 +337,11 @@ public function getManagerForClass(string $className): ObjectManager|null
335337
}
336338
}
337339

338-
throw new RuntimeException('No manager found for class ' . $className);
340+
if ($throwExceptionIfNotFound) {
341+
throw new RuntimeException('No manager found for class ' . $className);
342+
}
343+
344+
return null;
339345
}
340346

341347
/**

tests/Feature/IlluminateRegistryTest.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,37 @@ public function testGetManagerForClassWithNamespace(): void
485485
$this->assertEquals($entityManager, $this->registry->getManagerForClass('Alias:Scientist'));
486486
}
487487

488+
public function testGetManagerForClassReturnsNullWhenNotFound(): void
489+
{
490+
$this->container->shouldReceive('singleton');
491+
$this->registry->addManager('default');
492+
493+
$entityManager = m::mock(EntityManagerInterface::class);
494+
$this->container->shouldReceive('make')
495+
->with('doctrine.managers.default')
496+
->andReturn($entityManager);
497+
498+
$metadataFactory = m::mock(ClassMetadataFactory::class);
499+
$metadataFactory->shouldReceive('isTransient')
500+
->with('LaravelDoctrineTest\ORM\Assets\Entity\Scientist')
501+
->once()
502+
->andReturnFalse();
503+
504+
$metadata = m::mock(ClassMetadata::class);
505+
$metadata->shouldReceive('getName')
506+
->once()
507+
->andReturn('LaravelDoctrineTest\ORM\Assets\Entity\Theory');
508+
509+
$metadataFactory->shouldReceive('getAllMetadata')
510+
->once()
511+
->andReturn([$metadata]);
512+
513+
$entityManager->shouldReceive('getMetadataFactory')
514+
->andReturn($metadataFactory);
515+
516+
$this->assertNull($this->registry->getManagerForClass('LaravelDoctrineTest\ORM\Assets\Entity\Scientist'));
517+
}
518+
488519
public function testGetManagerForClassThrowsExceptionWhenNotFound(): void
489520
{
490521
$this->expectException(RuntimeException::class);
@@ -515,7 +546,7 @@ public function testGetManagerForClassThrowsExceptionWhenNotFound(): void
515546
$entityManager->shouldReceive('getMetadataFactory')
516547
->andReturn($metadataFactory);
517548

518-
$this->assertEquals($entityManager, $this->registry->getManagerForClass('LaravelDoctrineTest\ORM\Assets\Entity\Scientist'));
549+
$this->assertEquals($entityManager, $this->registry->getManagerForClass('LaravelDoctrineTest\ORM\Assets\Entity\Scientist', true));
519550
}
520551

521552
public function testGetManagerForClassInvalidClass(): void

0 commit comments

Comments
 (0)