Skip to content

Commit

Permalink
Make getRealClass more robust (#1733)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 committed Apr 1, 2023
1 parent 78520b0 commit 87a84a1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
14 changes: 13 additions & 1 deletion src/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Doctrine\ORM\Tools\Pagination\Paginator;
use Doctrine\ORM\UnitOfWork;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\Mapping\MappingException;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface as BaseProxyQueryInterface;
use Sonata\AdminBundle\Exception\LockException;
use Sonata\AdminBundle\Exception\ModelManagerException;
Expand Down Expand Up @@ -57,7 +58,18 @@ public function __construct(

public function getRealClass(object $object): string
{
return $this->getMetadata($object::class)->getName();
$class = $object::class;

$em = $this->registry->getManagerForClass($class);
if (null === $em) {
return $class;
}

try {
return $em->getClassMetadata($class)->getName();
} catch (MappingException) {
return $class;
}
}

public function create(object $object): void
Expand Down
37 changes: 29 additions & 8 deletions tests/Model/ModelManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,38 @@ protected function setUp(): void
$this->modelManager = new ModelManager($this->registry, PropertyAccess::createPropertyAccessor());
}

public function testGetRealClass(): void
public function testGetRealClassWithProxyObject(): void
{
$proxyClass = User::class;
$baseClass = \stdClass::class;

$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->method('getName')->willReturn(User::class);
$classMetadata->expects(static::once())
->method('getName')
->willReturn($baseClass);

$entityManager = $this->createMock(EntityManagerInterface::class);
$entityManager->method('getClassMetadata')->willReturn($classMetadata);
$entityManager->expects(static::once())
->method('getClassMetadata')
->with($proxyClass)
->willReturn($classMetadata);

$this->registry->method('getManagerForClass')->willReturn($entityManager);
$this->registry->expects(static::once())
->method('getManagerForClass')
->with($proxyClass)
->willReturn($entityManager);

static::assertSame($baseClass, $this->modelManager->getRealClass(new User()));
}

static::assertSame(User::class, $this->modelManager->getRealClass(new User()));
public function testGetRealClassWithNonProxyObject(): void
{
$this->registry->expects(static::once())
->method('getManagerForClass')
->with(\DateTime::class)
->willReturn(null);

static::assertSame(\DateTime::class, $this->modelManager->getRealClass(new \DateTime()));
}

/**
Expand Down Expand Up @@ -443,7 +464,7 @@ public function testCreate(\Throwable $exception): void

$entityManager = $this->createMock(EntityManagerInterface::class);

$this->registry->expects(static::once())
$this->registry->expects(static::atLeastOnce())
->method('getManagerForClass')
->willReturn($entityManager);

Expand Down Expand Up @@ -488,7 +509,7 @@ public function testUpdate(\Throwable $exception): void

$entityManager = $this->createMock(EntityManagerInterface::class);

$this->registry->expects(static::once())
$this->registry->expects(static::atLeastOnce())
->method('getManagerForClass')
->willReturn($entityManager);

Expand Down Expand Up @@ -518,7 +539,7 @@ public function testRemove(\Throwable $exception): void

$entityManager = $this->createMock(EntityManagerInterface::class);

$this->registry->expects(static::once())
$this->registry->expects(static::atLeastOnce())
->method('getManagerForClass')
->willReturn($entityManager);

Expand Down

0 comments on commit 87a84a1

Please sign in to comment.