2009-07-17 22:13:03 +04:00
< ? php
2009-07-18 15:41:37 +04:00
namespace Doctrine\Tests\ORM\Functional\Locking ;
2009-07-17 22:13:03 +04:00
use Doctrine\ORM\Mapping\ClassMetadata ;
2010-05-11 01:51:56 +04:00
use Doctrine\ORM\OptimisticLockException ;
2009-07-17 22:13:03 +04:00
use Doctrine\Common\EventManager ;
use Doctrine\ORM\Mapping\ClassMetadataFactory ;
use Doctrine\Tests\TestUtil ;
2009-07-18 15:41:37 +04:00
require_once __DIR__ . '/../../../TestInit.php' ;
2009-07-17 22:13:03 +04:00
class OptimisticTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp ()
{
parent :: setUp ();
try {
$this -> _schemaTool -> createSchema ( array (
2009-07-18 15:41:37 +04:00
$this -> _em -> getClassMetadata ( 'Doctrine\Tests\ORM\Functional\Locking\OptimisticJoinedParent' ),
$this -> _em -> getClassMetadata ( 'Doctrine\Tests\ORM\Functional\Locking\OptimisticJoinedChild' ),
2009-08-24 21:06:12 +04:00
$this -> _em -> getClassMetadata ( 'Doctrine\Tests\ORM\Functional\Locking\OptimisticStandard' ),
$this -> _em -> getClassMetadata ( 'Doctrine\Tests\ORM\Functional\Locking\OptimisticTimestamp' )
2009-07-17 22:13:03 +04:00
));
} catch ( \Exception $e ) {
// Swallow all exceptions. We do not test the schema tool here.
}
$this -> _conn = $this -> _em -> getConnection ();
}
2009-07-18 01:55:56 +04:00
public function testJoinedChildInsertSetsInitialVersionValue ()
{
$test = new OptimisticJoinedChild ();
$test -> name = 'child' ;
$test -> whatever = 'whatever' ;
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $test );
2009-07-18 01:55:56 +04:00
$this -> _em -> flush ();
$this -> assertEquals ( 1 , $test -> version );
2010-05-11 01:51:56 +04:00
return $test ;
2009-07-18 01:55:56 +04:00
}
/**
2010-05-11 01:51:56 +04:00
* @ depends testJoinedChildInsertSetsInitialVersionValue
2009-07-18 01:55:56 +04:00
*/
2010-05-11 01:51:56 +04:00
public function testJoinedChildFailureThrowsException ( OptimisticJoinedChild $child )
2009-07-18 01:55:56 +04:00
{
2010-05-11 01:51:56 +04:00
$q = $this -> _em -> createQuery ( 'SELECT t FROM Doctrine\Tests\ORM\Functional\Locking\OptimisticJoinedChild t WHERE t.id = :id' );
$q -> setParameter ( 'id' , $child -> id );
2009-07-18 01:55:56 +04:00
$test = $q -> getSingleResult ();
// Manually update/increment the version so we can try and save the same
2011-12-20 01:56:19 +04:00
// $test and make sure the exception is thrown saying the record was
2009-07-18 01:55:56 +04:00
// changed or updated since you read it
2010-04-01 01:13:34 +04:00
$this -> _conn -> executeQuery ( 'UPDATE optimistic_joined_parent SET version = ? WHERE id = ?' , array ( 2 , $test -> id ));
2009-07-18 01:55:56 +04:00
// Now lets change a property and try and save it again
$test -> whatever = 'ok' ;
2010-05-11 01:51:56 +04:00
try {
$this -> _em -> flush ();
} catch ( OptimisticLockException $e ) {
$this -> assertSame ( $test , $e -> getEntity ());
}
2009-07-18 01:55:56 +04:00
}
public function testJoinedParentInsertSetsInitialVersionValue ()
2009-07-17 22:13:03 +04:00
{
$test = new OptimisticJoinedParent ();
2009-07-18 01:55:56 +04:00
$test -> name = 'parent' ;
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $test );
2009-07-17 22:13:03 +04:00
$this -> _em -> flush ();
$this -> assertEquals ( 1 , $test -> version );
2010-05-11 01:51:56 +04:00
return $test ;
2009-07-17 22:13:03 +04:00
}
/**
2010-05-11 01:51:56 +04:00
* @ depends testJoinedParentInsertSetsInitialVersionValue
2009-07-17 22:13:03 +04:00
*/
2010-05-11 01:51:56 +04:00
public function testJoinedParentFailureThrowsException ( OptimisticJoinedParent $parent )
2009-07-17 22:13:03 +04:00
{
2010-05-11 01:51:56 +04:00
$q = $this -> _em -> createQuery ( 'SELECT t FROM Doctrine\Tests\ORM\Functional\Locking\OptimisticJoinedParent t WHERE t.id = :id' );
$q -> setParameter ( 'id' , $parent -> id );
2009-07-17 22:13:03 +04:00
$test = $q -> getSingleResult ();
// Manually update/increment the version so we can try and save the same
2011-12-20 01:56:19 +04:00
// $test and make sure the exception is thrown saying the record was
2009-07-17 22:13:03 +04:00
// changed or updated since you read it
2010-04-01 01:13:34 +04:00
$this -> _conn -> executeQuery ( 'UPDATE optimistic_joined_parent SET version = ? WHERE id = ?' , array ( 2 , $test -> id ));
2009-07-17 22:13:03 +04:00
// Now lets change a property and try and save it again
$test -> name = 'WHATT???' ;
2010-05-11 01:51:56 +04:00
try {
$this -> _em -> flush ();
} catch ( OptimisticLockException $e ) {
$this -> assertSame ( $test , $e -> getEntity ());
}
2009-07-17 22:13:03 +04:00
}
2010-11-10 01:15:14 +03:00
public function testMultipleFlushesDoIncrementalUpdates ()
{
$test = new OptimisticStandard ();
for ( $i = 0 ; $i < 5 ; $i ++ ) {
$test -> name = 'test' . $i ;
$this -> _em -> persist ( $test );
$this -> _em -> flush ();
2011-02-20 00:50:58 +03:00
$this -> assertInternalType ( 'int' , $test -> getVersion ());
2010-11-10 01:15:14 +03:00
$this -> assertEquals ( $i + 1 , $test -> getVersion ());
}
}
2009-07-17 22:13:03 +04:00
public function testStandardInsertSetsInitialVersionValue ()
{
$test = new OptimisticStandard ();
$test -> name = 'test' ;
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $test );
2009-07-17 22:13:03 +04:00
$this -> _em -> flush ();
2011-02-20 00:50:58 +03:00
$this -> assertInternalType ( 'int' , $test -> getVersion ());
2010-02-14 13:48:25 +03:00
$this -> assertEquals ( 1 , $test -> getVersion ());
2010-05-11 01:51:56 +04:00
return $test ;
2009-07-17 22:13:03 +04:00
}
/**
2010-05-11 01:51:56 +04:00
* @ depends testStandardInsertSetsInitialVersionValue
2009-07-17 22:13:03 +04:00
*/
2010-05-11 01:51:56 +04:00
public function testStandardFailureThrowsException ( OptimisticStandard $entity )
2009-07-17 22:13:03 +04:00
{
2010-05-11 01:51:56 +04:00
$q = $this -> _em -> createQuery ( 'SELECT t FROM Doctrine\Tests\ORM\Functional\Locking\OptimisticStandard t WHERE t.id = :id' );
$q -> setParameter ( 'id' , $entity -> id );
2009-07-17 22:13:03 +04:00
$test = $q -> getSingleResult ();
// Manually update/increment the version so we can try and save the same
2011-12-20 01:56:19 +04:00
// $test and make sure the exception is thrown saying the record was
2009-07-17 22:13:03 +04:00
// changed or updated since you read it
2010-04-01 01:13:34 +04:00
$this -> _conn -> executeQuery ( 'UPDATE optimistic_standard SET version = ? WHERE id = ?' , array ( 2 , $test -> id ));
2009-07-17 22:13:03 +04:00
// Now lets change a property and try and save it again
$test -> name = 'WHATT???' ;
2010-05-11 01:51:56 +04:00
try {
$this -> _em -> flush ();
} catch ( OptimisticLockException $e ) {
$this -> assertSame ( $test , $e -> getEntity ());
}
2009-07-17 22:13:03 +04:00
}
2009-08-24 21:06:12 +04:00
public function testOptimisticTimestampSetsDefaultValue ()
{
$test = new OptimisticTimestamp ();
$test -> name = 'Testing' ;
2010-11-10 01:15:14 +03:00
$this -> assertNull ( $test -> version , " Pre-Condition " );
2009-08-24 21:06:12 +04:00
$this -> _em -> persist ( $test );
$this -> _em -> flush ();
2011-02-20 00:50:58 +03:00
$this -> assertInstanceOf ( 'DateTime' , $test -> version );
2010-05-11 01:51:56 +04:00
return $test ;
2009-08-24 21:06:12 +04:00
}
/**
2010-05-11 01:51:56 +04:00
* @ depends testOptimisticTimestampSetsDefaultValue
2009-08-24 21:06:12 +04:00
*/
2010-05-11 01:51:56 +04:00
public function testOptimisticTimestampFailureThrowsException ( OptimisticTimestamp $entity )
2009-08-24 21:06:12 +04:00
{
2010-05-11 01:51:56 +04:00
$q = $this -> _em -> createQuery ( 'SELECT t FROM Doctrine\Tests\ORM\Functional\Locking\OptimisticTimestamp t WHERE t.id = :id' );
$q -> setParameter ( 'id' , $entity -> id );
2009-08-24 21:06:12 +04:00
$test = $q -> getSingleResult ();
2011-02-20 00:50:58 +03:00
$this -> assertInstanceOf ( 'DateTime' , $test -> version );
2009-11-30 22:02:05 +03:00
2009-08-24 21:06:12 +04:00
// Manually increment the version datetime column
2009-08-28 21:25:28 +04:00
$format = $this -> _em -> getConnection () -> getDatabasePlatform () -> getDateTimeFormatString ();
2010-04-01 01:13:34 +04:00
$this -> _conn -> executeQuery ( 'UPDATE optimistic_timestamp SET version = ? WHERE id = ?' , array ( date ( $format , strtotime ( $test -> version -> format ( $format )) + 3600 ), $test -> id ));
2009-08-24 21:06:12 +04:00
// Try and update the record and it should throw an exception
$test -> name = 'Testing again' ;
2010-05-11 01:51:56 +04:00
try {
$this -> _em -> flush ();
} catch ( OptimisticLockException $e ) {
$this -> assertSame ( $test , $e -> getEntity ());
}
2009-08-24 21:06:12 +04:00
}
2009-07-17 22:13:03 +04:00
}
/**
* @ Entity
* @ Table ( name = " optimistic_joined_parent " )
* @ InheritanceType ( " JOINED " )
* @ DiscriminatorColumn ( name = " discr " , type = " string " )
2009-08-17 21:58:16 +04:00
* @ DiscriminatorMap ({ " parent " = " OptimisticJoinedParent " , " child " = " OptimisticJoinedChild " })
2009-07-17 22:13:03 +04:00
*/
class OptimisticJoinedParent
{
/**
* @ Id @ Column ( type = " integer " )
* @ GeneratedValue ( strategy = " AUTO " )
*/
public $id ;
/**
* @ Column ( type = " string " , length = 255 )
*/
public $name ;
/**
* @ Version @ Column ( type = " integer " )
*/
public $version ;
}
/**
* @ Entity
* @ Table ( name = " optimistic_joined_child " )
*/
class OptimisticJoinedChild extends OptimisticJoinedParent
{
/**
* @ Column ( type = " string " , length = 255 )
*/
2009-07-18 01:55:56 +04:00
public $whatever ;
2009-07-17 22:13:03 +04:00
}
/**
* @ Entity
* @ Table ( name = " optimistic_standard " )
*/
class OptimisticStandard
{
/**
* @ Id @ Column ( type = " integer " )
* @ GeneratedValue ( strategy = " AUTO " )
*/
public $id ;
/**
* @ Column ( type = " string " , length = 255 )
*/
public $name ;
/**
* @ Version @ Column ( type = " integer " )
*/
2010-02-14 13:48:25 +03:00
private $version ;
2011-12-20 01:56:19 +04:00
2010-02-14 13:48:25 +03:00
function getVersion () { return $this -> version ;}
2009-08-24 21:06:12 +04:00
}
/**
* @ Entity
* @ Table ( name = " optimistic_timestamp " )
*/
class OptimisticTimestamp
{
/**
* @ Id @ Column ( type = " integer " )
* @ GeneratedValue ( strategy = " AUTO " )
*/
public $id ;
/**
* @ Column ( type = " string " , length = 255 )
*/
public $name ;
/**
* @ Version @ Column ( type = " datetime " )
*/
public $version ;
2009-07-17 22:13:03 +04:00
}