Skip to content

Commit

Permalink
Merge 4.x into 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
SonataCI committed Mar 27, 2023
2 parents 97f9182 + ad386f2 commit fa7465d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
20 changes: 9 additions & 11 deletions src/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Sonata\DoctrineORMAdminBundle\Model;

use Doctrine\Common\Util\ClassUtils;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\LockMode;
use Doctrine\DBAL\Platforms\AbstractPlatform;
Expand Down Expand Up @@ -58,7 +57,7 @@ public function __construct(

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

public function create(object $object): void
Expand All @@ -69,7 +68,7 @@ public function create(object $object): void
$entityManager->flush();
} catch (\PDOException|Exception $exception) {
throw new ModelManagerException(
sprintf('Failed to create object: %s', ClassUtils::getClass($object)),
sprintf('Failed to create object: %s', $this->getRealClass($object)),
(int) $exception->getCode(),
$exception
);
Expand All @@ -84,7 +83,7 @@ public function update(object $object): void
$entityManager->flush();
} catch (\PDOException|Exception $exception) {
throw new ModelManagerException(
sprintf('Failed to update object: %s', ClassUtils::getClass($object)),
sprintf('Failed to update object: %s', $this->getRealClass($object)),
(int) $exception->getCode(),
$exception
);
Expand All @@ -99,7 +98,7 @@ public function delete(object $object): void
$entityManager->flush();
} catch (\PDOException|Exception $exception) {
throw new ModelManagerException(
sprintf('Failed to delete object: %s', ClassUtils::getClass($object)),
sprintf('Failed to delete object: %s', $this->getRealClass($object)),
(int) $exception->getCode(),
$exception
);
Expand All @@ -108,7 +107,7 @@ public function delete(object $object): void

public function getLockVersion(object $object)
{
$metadata = $this->getMetadata(ClassUtils::getClass($object));
$metadata = $this->getMetadata($object::class);

if (!$metadata->isVersioned || !isset($metadata->reflFields[$metadata->versionField])) {
return null;
Expand All @@ -119,7 +118,7 @@ public function getLockVersion(object $object)

public function lock(object $object, ?int $expectedVersion): void
{
$metadata = $this->getMetadata(ClassUtils::getClass($object));
$metadata = $this->getMetadata($object::class);

if (!$metadata->isVersioned) {
return;
Expand Down Expand Up @@ -236,9 +235,8 @@ public function executeQuery(object $query)

public function getIdentifierValues(object $model): array
{
$class = ClassUtils::getClass($model);
$metadata = $this->getMetadata($class);
$platform = $this->getEntityManager($class)->getConnection()->getDatabasePlatform();
$metadata = $this->getMetadata($model::class);
$platform = $this->getEntityManager($model::class)->getConnection()->getDatabasePlatform();

$identifiers = [];

Expand All @@ -256,7 +254,7 @@ public function getIdentifierValues(object $model): array
continue;
}

$identifierMetadata = $this->getMetadata(ClassUtils::getClass($value));
$identifierMetadata = $this->getMetadata($value::class);

foreach ($identifierMetadata->getIdentifierValues($value) as $identifierValue) {
$identifiers[] = $identifierValue;
Expand Down
53 changes: 41 additions & 12 deletions tests/Model/ModelManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ protected function setUp(): void

public function testGetRealClass(): void
{
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->method('getName')->willReturn(User::class);

$entityManager = $this->createMock(EntityManagerInterface::class);
$entityManager->method('getClassMetadata')->willReturn($classMetadata);

$this->registry->method('getManagerForClass')->willReturn($entityManager);

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

Expand Down Expand Up @@ -430,19 +438,26 @@ public function testGetEntityManagerException(): void
*/
public function testCreate(\Throwable $exception): void
{
$entityManger = $this->createMock(EntityManagerInterface::class);
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->method('getName')->willReturn(VersionedEntity::class);

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

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

$entityManger->expects(static::once())
$entityManager->expects(static::once())
->method('persist');

$entityManger->expects(static::once())
$entityManager->expects(static::once())
->method('flush')
->willThrowException($exception);

$entityManager->expects(static::once())
->method('getClassMetadata')
->willReturn($classMetadata);

$this->expectException(ModelManagerException::class);

$this->modelManager->create(new VersionedEntity());
Expand All @@ -468,19 +483,26 @@ public function createUpdateRemoveData(): iterable
*/
public function testUpdate(\Throwable $exception): void
{
$entityManger = $this->createMock(EntityManagerInterface::class);
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->method('getName')->willReturn(VersionedEntity::class);

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

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

$entityManger->expects(static::once())
$entityManager->expects(static::once())
->method('persist');

$entityManger->expects(static::once())
$entityManager->expects(static::once())
->method('flush')
->willThrowException($exception);

$entityManager->expects(static::once())
->method('getClassMetadata')
->willReturn($classMetadata);

$this->expectException(ModelManagerException::class);

$this->modelManager->update(new VersionedEntity());
Expand All @@ -491,19 +513,26 @@ public function testUpdate(\Throwable $exception): void
*/
public function testRemove(\Throwable $exception): void
{
$entityManger = $this->createMock(EntityManagerInterface::class);
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->method('getName')->willReturn(VersionedEntity::class);

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

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

$entityManger->expects(static::once())
$entityManager->expects(static::once())
->method('remove');

$entityManger->expects(static::once())
$entityManager->expects(static::once())
->method('flush')
->willThrowException($exception);

$entityManager->expects(static::once())
->method('getClassMetadata')
->willReturn($classMetadata);

$this->expectException(ModelManagerException::class);

$this->modelManager->delete(new VersionedEntity());
Expand Down

0 comments on commit fa7465d

Please sign in to comment.