2010-04-11 18:43:33 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional\Locking;
|
|
|
|
|
|
|
|
use Doctrine\Tests\Models\CMS\CmsArticle,
|
|
|
|
Doctrine\Tests\Models\CMS\CmsUser,
|
2010-05-15 13:48:20 +04:00
|
|
|
Doctrine\DBAL\LockMode,
|
2010-05-02 15:02:44 +04:00
|
|
|
Doctrine\ORM\EntityManager;
|
2010-04-11 18:43:33 +04:00
|
|
|
|
|
|
|
require_once __DIR__ . '/../../../TestInit.php';
|
|
|
|
|
2010-05-02 15:02:44 +04:00
|
|
|
class LockTest extends \Doctrine\Tests\OrmFunctionalTestCase {
|
|
|
|
protected function setUp() {
|
2010-04-11 18:43:33 +04:00
|
|
|
$this->useModelSet('cms');
|
|
|
|
parent::setUp();
|
2010-05-02 15:02:44 +04:00
|
|
|
$this->handles = array();
|
2010-04-11 18:43:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-178
|
|
|
|
* @group locking
|
|
|
|
*/
|
2010-05-02 15:02:44 +04:00
|
|
|
public function testLockVersionedEntity() {
|
2010-04-11 18:43:33 +04:00
|
|
|
$article = new CmsArticle();
|
|
|
|
$article->text = "my article";
|
|
|
|
$article->topic = "Hello";
|
|
|
|
|
|
|
|
$this->_em->persist($article);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->_em->lock($article, LockMode::OPTIMISTIC, $article->version);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-178
|
|
|
|
* @group locking
|
|
|
|
*/
|
2010-05-02 15:02:44 +04:00
|
|
|
public function testLockVersionedEntity_MissmatchThrowsException() {
|
2010-04-11 18:43:33 +04:00
|
|
|
$article = new CmsArticle();
|
|
|
|
$article->text = "my article";
|
|
|
|
$article->topic = "Hello";
|
|
|
|
|
|
|
|
$this->_em->persist($article);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->setExpectedException('Doctrine\ORM\OptimisticLockException');
|
|
|
|
$this->_em->lock($article, LockMode::OPTIMISTIC, $article->version + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-178
|
|
|
|
* @group locking
|
|
|
|
*/
|
2010-05-02 15:02:44 +04:00
|
|
|
public function testLockUnversionedEntity_ThrowsException() {
|
2010-04-11 18:43:33 +04:00
|
|
|
$user = new CmsUser();
|
|
|
|
$user->name = "foo";
|
|
|
|
$user->status = "active";
|
|
|
|
$user->username = "foo";
|
|
|
|
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->setExpectedException('Doctrine\ORM\OptimisticLockException');
|
|
|
|
$this->_em->lock($user, LockMode::OPTIMISTIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-178
|
|
|
|
* @group locking
|
|
|
|
*/
|
2010-05-02 15:02:44 +04:00
|
|
|
public function testLockUnmanagedEntity_ThrowsException() {
|
2010-04-11 18:43:33 +04:00
|
|
|
$article = new CmsArticle();
|
|
|
|
|
2011-10-22 14:49:33 +04:00
|
|
|
$this->setExpectedException('InvalidArgumentException', 'Entity Doctrine\Tests\Models\CMS\CmsArticle');
|
2010-04-11 18:43:33 +04:00
|
|
|
$this->_em->lock($article, LockMode::OPTIMISTIC, $article->version + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-178
|
|
|
|
* @group locking
|
|
|
|
*/
|
2010-05-02 15:02:44 +04:00
|
|
|
public function testLockPessimisticRead_NoTransaction_ThrowsException() {
|
2010-04-11 18:43:33 +04:00
|
|
|
$article = new CmsArticle();
|
|
|
|
$article->text = "my article";
|
|
|
|
$article->topic = "Hello";
|
|
|
|
|
|
|
|
$this->_em->persist($article);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->setExpectedException('Doctrine\ORM\TransactionRequiredException');
|
|
|
|
$this->_em->lock($article, LockMode::PESSIMISTIC_READ);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-178
|
|
|
|
* @group locking
|
|
|
|
*/
|
2010-05-02 15:02:44 +04:00
|
|
|
public function testLockPessimisticWrite_NoTransaction_ThrowsException() {
|
2010-04-11 18:43:33 +04:00
|
|
|
$article = new CmsArticle();
|
|
|
|
$article->text = "my article";
|
|
|
|
$article->topic = "Hello";
|
|
|
|
|
|
|
|
$this->_em->persist($article);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->setExpectedException('Doctrine\ORM\TransactionRequiredException');
|
|
|
|
$this->_em->lock($article, LockMode::PESSIMISTIC_WRITE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-178
|
|
|
|
* @group locking
|
|
|
|
*/
|
2010-05-02 15:02:44 +04:00
|
|
|
public function testLockPessimisticWrite() {
|
2010-04-11 18:43:33 +04:00
|
|
|
$writeLockSql = $this->_em->getConnection()->getDatabasePlatform()->getWriteLockSql();
|
|
|
|
if (strlen($writeLockSql) == 0) {
|
|
|
|
$this->markTestSkipped('Database Driver has no Write Lock support.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$article = new CmsArticle();
|
|
|
|
$article->text = "my article";
|
|
|
|
$article->topic = "Hello";
|
|
|
|
|
|
|
|
$this->_em->persist($article);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->_em->beginTransaction();
|
2010-06-07 02:14:43 +04:00
|
|
|
try {
|
|
|
|
$this->_em->lock($article, LockMode::PESSIMISTIC_WRITE);
|
|
|
|
$this->_em->commit();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->_em->rollback();
|
|
|
|
throw $e;
|
|
|
|
}
|
2010-04-11 18:43:33 +04:00
|
|
|
|
|
|
|
$query = array_pop( $this->_sqlLoggerStack->queries );
|
|
|
|
$this->assertContains($writeLockSql, $query['sql']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-178
|
|
|
|
* @group locking
|
|
|
|
*/
|
2010-05-02 15:02:44 +04:00
|
|
|
public function testLockPessimisticRead() {
|
2010-04-11 18:43:33 +04:00
|
|
|
$readLockSql = $this->_em->getConnection()->getDatabasePlatform()->getReadLockSql();
|
|
|
|
if (strlen($readLockSql) == 0) {
|
|
|
|
$this->markTestSkipped('Database Driver has no Write Lock support.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$article = new CmsArticle();
|
|
|
|
$article->text = "my article";
|
|
|
|
$article->topic = "Hello";
|
|
|
|
|
|
|
|
$this->_em->persist($article);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->_em->beginTransaction();
|
2010-06-07 02:14:43 +04:00
|
|
|
try {
|
|
|
|
$this->_em->lock($article, LockMode::PESSIMISTIC_READ);
|
|
|
|
$this->_em->commit();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->_em->rollback();
|
|
|
|
throw $e;
|
|
|
|
}
|
2010-04-11 18:43:33 +04:00
|
|
|
|
|
|
|
$query = array_pop( $this->_sqlLoggerStack->queries );
|
|
|
|
$this->assertContains($readLockSql, $query['sql']);
|
|
|
|
}
|
|
|
|
}
|