Skip to content

Commit

Permalink
Revert return to null if manager is not found; add bool to throw exce…
Browse files Browse the repository at this point in the history
…ption instead
  • Loading branch information
TomHAnderson committed Nov 1, 2024
1 parent 936cadc commit faf63df
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/IlluminateRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public function getRepository(string $persistentObject, string|null $persistentM
*
* @param class-string $className A persistent object class name.
*/
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 +335,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 faf63df

Please sign in to comment.