Skip to content

Commit

Permalink
Implement ProxyResolverInterface (#776)
Browse files Browse the repository at this point in the history
Co-authored-by: Antonymous <[email protected]>
  • Loading branch information
antonymous and Antonymous authored Sep 9, 2022
1 parent 0f78b62 commit be127ca
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as MongoDBClassMetadata;
use Doctrine\ODM\MongoDB\Query\Builder;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use Doctrine\Persistence\Mapping\MappingException;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface as BaseProxyQueryInterface;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Sonata\AdminBundle\Model\ProxyResolverInterface;
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery;
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQueryInterface;
use Symfony\Bridge\Doctrine\ManagerRegistry;
Expand All @@ -30,7 +32,7 @@
*
* @template-implements ModelManagerInterface<T>
*/
final class ModelManager implements ModelManagerInterface
final class ModelManager implements ModelManagerInterface, ProxyResolverInterface
{
public const ID_SEPARATOR = '-';

Expand Down Expand Up @@ -240,6 +242,22 @@ public function reverseTransform(object $object, array $array = []): void
}
}

public function getRealClass(object $object): string
{
$class = \get_class($object);

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

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

/**
* @param MongoDBClassMetadata<T> $metadata
*/
Expand Down
42 changes: 42 additions & 0 deletions tests/Model/ModelManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sonata\DoctrineMongoDBAdminBundle\Tests\Model;

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Query\Builder;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use PHPUnit\Framework\MockObject\Stub;
Expand Down Expand Up @@ -148,6 +149,47 @@ public function testSupportsQuery(bool $expected, object $object): void
static::assertSame($expected, $modelManager->supportsQuery($object));
}

public function testGetRealClassWithProxyObject(): void
{
$proxyClass = TestDocument::class;
/** @var class-string $baseClass */
$baseClass = 'BaseTestDocument';

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

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

$registry = $this->createMock(ManagerRegistry::class);
$registry->expects(static::once())
->method('getManagerForClass')
->with($proxyClass)
->willReturn($documentManager);

$modelManager = new ModelManager($registry, $this->propertyAccessor);

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

public function testGetRealClassWithNonProxyObject(): void
{
$registry = $this->createMock(ManagerRegistry::class);
$registry->expects(static::once())
->method('getManagerForClass')
->with(\DateTime::class)
->willReturn(null);

$modelManager = new ModelManager($registry, $this->propertyAccessor);

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

/**
* @phpstan-return iterable<array{bool, object}>
*/
Expand Down

0 comments on commit be127ca

Please sign in to comment.