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

Migrate to wrapInTransaction() #392

Merged
merged 1 commit into from
Apr 24, 2022
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
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
"doctrine/persistence": "^1.3.3|^2.0|^3.0"
},
"conflict": {
"doctrine/phpcr-odm": "<1.3.0",
"doctrine/dbal": "<2.13"
"doctrine/dbal": "<2.13",
"doctrine/orm": "<2.10",
"doctrine/phpcr-odm": "<1.3.0"
},
"require-dev": {
"ext-sqlite3": "*",
"doctrine/coding-standard": "^9.0",
"doctrine/dbal": "^2.13 || ^3.0",
"doctrine/mongodb-odm": "^1.3.0 || ^2.0.0",
"doctrine/orm": "^2.7.0",
"doctrine/orm": "^2.10.0",
"jangregor/phpstan-prophecy": "^1",
"phpstan/phpstan": "^1.5",
"phpunit/phpunit": "^8.5 || ^9.5",
Expand Down
20 changes: 16 additions & 4 deletions lib/Doctrine/Common/DataFixtures/Executor/ORMExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@
use Doctrine\Common\DataFixtures\Event\Listener\ORMReferenceListener;
use Doctrine\Common\DataFixtures\Purger\ORMPurgerInterface;
use Doctrine\Common\DataFixtures\ReferenceRepository;
use Doctrine\ORM\Decorator\EntityManagerDecorator;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;

/**
* Class responsible for executing data fixtures.
*/
class ORMExecutor extends AbstractExecutor
{
/** @var EntityManagerInterface */
/** @var EntityManager|EntityManagerDecorator */
private $em;

/** @var EntityManagerInterface */
private $originalManager;

/** @var ORMReferenceListener */
private $listener;

Expand All @@ -27,7 +32,14 @@ class ORMExecutor extends AbstractExecutor
*/
public function __construct(EntityManagerInterface $em, ?ORMPurgerInterface $purger = null)
{
$this->em = $em;
$this->originalManager = $em;
// Make sure, wrapInTransaction() exists on the EM.
// To be removed when dropping support for ORM 2
$this->em = $em instanceof EntityManager || $em instanceof EntityManagerDecorator
? $em
: new class ($em) extends EntityManagerDecorator {
};

if ($purger !== null) {
$this->purger = $purger;
$this->purger->setEntityManager($em);
Expand All @@ -45,7 +57,7 @@ public function __construct(EntityManagerInterface $em, ?ORMPurgerInterface $pur
*/
public function getObjectManager()
{
return $this->em;
return $this->originalManager;
}

/** @inheritDoc */
Expand All @@ -65,7 +77,7 @@ public function setReferenceRepository(ReferenceRepository $referenceRepository)
public function execute(array $fixtures, $append = false)
{
$executor = $this;
$this->em->transactional(static function (EntityManagerInterface $em) use ($executor, $fixtures, $append) {
$this->em->wrapInTransaction(static function (EntityManagerInterface $em) use ($executor, $fixtures, $append) {
if ($append === false) {
$executor->purge();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

namespace Doctrine\Tests\Common\DataFixtures\Executor;

use Closure;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\Common\EventManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Tests\Common\DataFixtures\BaseTest;
use PHPUnit\Framework\MockObject\MockObject;

Expand Down Expand Up @@ -56,6 +59,16 @@ public function testExecuteTransaction(): void
$executor->execute([$fixture], true);
}

public function testCustomLegacyEntityManager(): void
{
$em = $this->createMock(EntityManagerInterface::class);
$em->method('getEventManager')->willReturn($this->createMock(EventManager::class));
$em->expects($this->once())->method('transactional')->with(self::isInstanceOf(Closure::class));

$executor = new ORMExecutor($em);
@$executor->execute([]);
}

/**
* @return FixtureInterface&MockObject
*/
Expand Down