When the OptimisticLockingException is generated with the static function lockFailedVersionMismatch and the passed parameters are DateTime instances, the exception could not be thrown because the DateTime object is not implicitly converted to a string.
This commit is contained in:
parent
0f3679f034
commit
192bb6fd21
@ -73,6 +73,8 @@ class OptimisticLockException extends ORMException
|
|||||||
*/
|
*/
|
||||||
public static function lockFailedVersionMismatch($entity, $expectedLockVersion, $actualLockVersion)
|
public static function lockFailedVersionMismatch($entity, $expectedLockVersion, $actualLockVersion)
|
||||||
{
|
{
|
||||||
|
$expectedLockVersion = (is_object($expectedLockVersion) && $expectedLockVersion instanceof \DateTime) ? $expectedLockVersion->getTimestamp() : $expectedLockVersion;
|
||||||
|
$actualLockVersion = (is_object($actualLockVersion) && $actualLockVersion instanceof \DateTime) ? $actualLockVersion->getTimestamp() : $actualLockVersion;
|
||||||
return new self("The optimistic lock failed, version " . $expectedLockVersion . " was expected, but is actually ".$actualLockVersion, $entity);
|
return new self("The optimistic lock failed, version " . $expectedLockVersion . " was expected, but is actually ".$actualLockVersion, $entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,8 @@ use Doctrine\ORM\OptimisticLockException;
|
|||||||
use Doctrine\Common\EventManager;
|
use Doctrine\Common\EventManager;
|
||||||
use Doctrine\ORM\Mapping\ClassMetadataFactory;
|
use Doctrine\ORM\Mapping\ClassMetadataFactory;
|
||||||
use Doctrine\Tests\TestUtil;
|
use Doctrine\Tests\TestUtil;
|
||||||
|
use Doctrine\DBAL\LockMode;
|
||||||
|
use DateTime;
|
||||||
|
|
||||||
require_once __DIR__ . '/../../../TestInit.php';
|
require_once __DIR__ . '/../../../TestInit.php';
|
||||||
|
|
||||||
@ -181,13 +183,44 @@ class OptimisticTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
$this->_conn->executeQuery('UPDATE optimistic_timestamp SET version = ? WHERE id = ?', array(date($format, strtotime($test->version->format($format)) + 3600), $test->id));
|
$this->_conn->executeQuery('UPDATE optimistic_timestamp SET version = ? WHERE id = ?', array(date($format, strtotime($test->version->format($format)) + 3600), $test->id));
|
||||||
|
|
||||||
// Try and update the record and it should throw an exception
|
// Try and update the record and it should throw an exception
|
||||||
|
$caughtException = null;
|
||||||
$test->name = 'Testing again';
|
$test->name = 'Testing again';
|
||||||
try {
|
try {
|
||||||
$this->_em->flush();
|
$this->_em->flush();
|
||||||
} catch (OptimisticLockException $e) {
|
} catch (OptimisticLockException $e) {
|
||||||
$this->assertSame($test, $e->getEntity());
|
$caughtException = $e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->assertNotNull($caughtException, "No OptimisticLockingException was thrown");
|
||||||
|
$this->assertSame($test, $caughtException->getEntity());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testOptimisticTimestampSetsDefaultValue
|
||||||
|
*/
|
||||||
|
public function testOptimisticTimestampLockFailureThrowsException(OptimisticTimestamp $entity)
|
||||||
|
{
|
||||||
|
$q = $this->_em->createQuery('SELECT t FROM Doctrine\Tests\ORM\Functional\Locking\OptimisticTimestamp t WHERE t.id = :id');
|
||||||
|
$q->setParameter('id', $entity->id);
|
||||||
|
$test = $q->getSingleResult();
|
||||||
|
|
||||||
|
$this->assertInstanceOf('DateTime', $test->version);
|
||||||
|
|
||||||
|
// Try to lock the record with an older timestamp and it should throw an exception
|
||||||
|
$caughtException = null;
|
||||||
|
try {
|
||||||
|
$expectedVersionExpired = DateTime::createFromFormat('U', $test->version->getTimestamp()-3600);
|
||||||
|
$this->_em->lock($test, LockMode::OPTIMISTIC, $expectedVersionExpired);
|
||||||
|
} catch (OptimisticLockException $e) {
|
||||||
|
$caughtException = $e;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertNotNull($caughtException, "No OptimisticLockingException was thrown");
|
||||||
|
$this->assertSame($test, $caughtException->getEntity());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user