Skip to content

Commit

Permalink
Merge pull request #1 from fhuitelec/master
Browse files Browse the repository at this point in the history
Add closeEMOnRollback property on DoctrineEntityManager
  • Loading branch information
remi-san committed Sep 27, 2016
2 parents 781518f + 32f6e6b commit 948cbb5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Doctrine/DoctrineEntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ final class DoctrineEntityManager implements Transactional
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var bool
*/
private $closeEntityManagerOnRollback;

/**
* Constructor.
*
* @param EntityManagerInterface $entityManager
* @param bool $closeEntityManagerOnRollback
*/
public function __construct(EntityManagerInterface $entityManager)
public function __construct(EntityManagerInterface $entityManager, $closeEntityManagerOnRollback = false)
{
$this->entityManager = $entityManager;
$this->closeEntityManagerOnRollback = $closeEntityManagerOnRollback;
}

/**
Expand Down Expand Up @@ -64,5 +70,9 @@ public function rollback()
} catch (\Exception $e) {
throw new RollbackException('Cannot rollback Doctrine ORM transaction', $e->getCode(), $e);
}

if ($this->closeEntityManagerOnRollback) {
$this->entityManager->close();
}
}
}
20 changes: 20 additions & 0 deletions tests/spec/Doctrine/DoctrineEntityManagerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,24 @@ function it_should_throw_an_exception_if_em_transaction_rollback_failed(EntityMa
$this->shouldThrow('\RemiSan\TransactionManager\Exception\RollbackException')
->duringRollback();
}

function it_should_close_em_if_em_transaction_rollback_is_on_closeEntityManagerOnRollback_mode(EntityManagerInterface $entityManager)
{
$this->beConstructedWith($entityManager, true);

$entityManager->rollback()->shouldBeCalledTimes(1);
$entityManager->close()->shouldBeCalledTimes(1);

$this->rollback();
}

function it_should_not_close_em_if_em_transaction_rollback_is_not_on_closeEntityManagerOnRollback_mode(EntityManagerInterface $entityManager)
{
$this->beConstructedWith($entityManager, false);

$entityManager->rollback()->shouldBeCalledTimes(1);
$entityManager->close()->shouldBeCalledTimes(0);

$this->rollback();
}
}

0 comments on commit 948cbb5

Please sign in to comment.