Skip to content

Commit

Permalink
Merge pull request #644 from TomHAnderson/hotfix/get-manager-for-class
Browse files Browse the repository at this point in the history
Revert return to null if manager is not found; add bool to throw exce…
  • Loading branch information
TomHAnderson authored Nov 1, 2024
2 parents 936cadc + 30b6b16 commit 04915c1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/IlluminateRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,10 @@ public function getRepository(string $persistentObject, string|null $persistentM
* Gets the object manager associated with a given class.
*
* @param class-string $className A persistent object class name.
*
* @return ($throwExceptionIfNotFound is true ? ObjectManager : ObjectManager|null)
*/
public function getManagerForClass(string $className): ObjectManager|null
public function getManagerForClass(string $className, bool $throwExceptionIfNotFound = false): ObjectManager|null
{
// Check for namespace alias
if (strpos($className, ':') !== false) {
Expand Down Expand Up @@ -335,7 +337,11 @@ public function getManagerForClass(string $className): ObjectManager|null
}
}

throw new RuntimeException('No manager found for class ' . $className);
if ($throwExceptionIfNotFound) {
throw new RuntimeException('No manager found for class ' . $className);
}

return null;
}

/**
Expand Down
33 changes: 32 additions & 1 deletion tests/Feature/IlluminateRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,37 @@ public function testGetManagerForClassWithNamespace(): void
$this->assertEquals($entityManager, $this->registry->getManagerForClass('Alias:Scientist'));
}

public function testGetManagerForClassReturnsNullWhenNotFound(): void
{
$this->container->shouldReceive('singleton');
$this->registry->addManager('default');

$entityManager = m::mock(EntityManagerInterface::class);
$this->container->shouldReceive('make')
->with('doctrine.managers.default')
->andReturn($entityManager);

$metadataFactory = m::mock(ClassMetadataFactory::class);
$metadataFactory->shouldReceive('isTransient')
->with('LaravelDoctrineTest\ORM\Assets\Entity\Scientist')
->once()
->andReturnFalse();

$metadata = m::mock(ClassMetadata::class);
$metadata->shouldReceive('getName')
->once()
->andReturn('LaravelDoctrineTest\ORM\Assets\Entity\Theory');

$metadataFactory->shouldReceive('getAllMetadata')
->once()
->andReturn([$metadata]);

$entityManager->shouldReceive('getMetadataFactory')
->andReturn($metadataFactory);

$this->assertNull($this->registry->getManagerForClass('LaravelDoctrineTest\ORM\Assets\Entity\Scientist'));
}

public function testGetManagerForClassThrowsExceptionWhenNotFound(): void
{
$this->expectException(RuntimeException::class);
Expand Down Expand Up @@ -515,7 +546,7 @@ public function testGetManagerForClassThrowsExceptionWhenNotFound(): void
$entityManager->shouldReceive('getMetadataFactory')
->andReturn($metadataFactory);

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

public function testGetManagerForClassInvalidClass(): void
Expand Down

0 comments on commit 04915c1

Please sign in to comment.