Skip to content

Commit

Permalink
Update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chosroes committed Jan 7, 2020
1 parent 06df782 commit 479f8c8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 32 deletions.
9 changes: 8 additions & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Upgrade to 2.8

## Minor BC BREAK: Failed commit now throw OptimisticLockException

Method `Doctrine\ORM\UnitOfWork#commit()` can throw an OptimisticLockException when a commit silently fails and returns false
since `Doctrine\DBAL\Connection#commit()` signature changed from returning void to boolean

# Upgrade to 2.7

## Added `Doctrine\ORM\AbstractQuery#enableResultCache()` and `Doctrine\ORM\AbstractQuery#disableResultCache()` methods
Expand All @@ -24,7 +31,7 @@ Method `Doctrine\ORM\AbstractQuery#useResultCache()` is deprecated because it is
and `disableResultCache()`. It will be removed in 3.0.

## Deprecated code generators and related console commands

These console commands have been deprecated:

* `orm:convert-mapping`
Expand Down
3 changes: 2 additions & 1 deletion lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use Throwable;
use UnexpectedValueException;
use function get_class;
use function is_object;

/**
* The UnitOfWork is responsible for tracking changes to objects during an
Expand Down Expand Up @@ -425,7 +426,7 @@ public function commit($entity = null)
}

// Commit failed silently
if ( ! $conn->commit()) {
if (false === $conn->commit()) {
$object = is_object($entity) ? $entity : null;

throw new OptimisticLockException('Commit failed', $object);
Expand Down
22 changes: 0 additions & 22 deletions tests/Doctrine/Tests/Mocks/ConnectionCommitFailMock.php

This file was deleted.

16 changes: 8 additions & 8 deletions tests/Doctrine/Tests/ORM/UnitOfWorkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMInvalidArgumentException;
use Doctrine\ORM\UnitOfWork;
use Doctrine\Tests\Mocks\ConnectionCommitFailMock;
use Doctrine\Tests\Mocks\ConnectionMock;
use Doctrine\Tests\Mocks\DriverMock;
use Doctrine\Tests\Mocks\EntityManagerMock;
Expand Down Expand Up @@ -795,22 +794,23 @@ public function testPreviousDetectedIllegalNewNonCascadedEntitiesAreCleanedUpOnS
/**
* @group #7946 Throw OptimisticLockException when connection::commit() returns false.
*/
public function testCommitThrowOptimisticLockExceptionWhenConnectionCommitReturnFalse(): void
public function testCommitThrowOptimisticLockExceptionWhenConnectionCommitReturnFalse() : void
{
// Set another connection mock that fail on commit
$this->_connectionMock = new ConnectionCommitFailMock([], new DriverMock());
$this->eventManager = $this->getMockBuilder(EventManager::class)->getMock();
$this->_emMock = EntityManagerMock::create($this->_connectionMock, null, $this->eventManager);
$this->_unitOfWork = new UnitOfWorkMock($this->_emMock);
$this->_emMock->setUnitOfWork($this->_unitOfWork);
$this->_connectionMock = $this->getMockBuilder(ConnectionMock::class)
->setConstructorArgs([[], new DriverMock()])
->setMethods(['commit'])
->getMock();

$this->_connectionMock->method('commit')->willReturn(false);

// Setup fake persister and id generator
$userPersister = new EntityPersisterMock($this->_emMock, $this->_emMock->getClassMetadata(ForumUser::class));
$userPersister->setMockIdGeneratorType(ClassMetadata::GENERATOR_TYPE_IDENTITY);
$this->_unitOfWork->setEntityPersister(ForumUser::class, $userPersister);

// Create a test user
$user = new ForumUser();
$user = new ForumUser();
$user->username = 'Jasper';
$this->_unitOfWork->persist($user);

Expand Down

0 comments on commit 479f8c8

Please sign in to comment.