Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert return to null if manager is not found; add bool to throw exce… #644

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add @return annotation, so static tools (e.g. phpstan) will understand that return type depends on argument

* @return ($throwExceptionIfNotFound is true ? ObjectManager : ObjectManager|null)

*
* @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
Loading