2009-10-01 23:04:18 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class TransactionTest extends SapphireTest {
|
2011-03-22 23:49:26 +01:00
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'TransactionTest_Object'
|
|
|
|
);
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
function testCreateWithTransaction() {
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
if(DB::getConn()->supportsTransactions()==true){
|
2011-01-12 00:01:42 +01:00
|
|
|
DB::getConn()->transactionStart();
|
2011-03-22 23:49:26 +01:00
|
|
|
$obj=new TransactionTest_Object();
|
|
|
|
$obj->Title='First page';
|
|
|
|
$obj->write();
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2011-03-22 23:49:26 +01:00
|
|
|
$obj=new TransactionTest_Object();
|
|
|
|
$obj->Title='Second page';
|
|
|
|
$obj->write();
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
//Create a savepoint here:
|
|
|
|
DB::getConn()->transactionSavepoint('rollback');
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2011-03-22 23:49:26 +01:00
|
|
|
$obj=new TransactionTest_Object();
|
|
|
|
$obj->Title='Third page';
|
|
|
|
$obj->write();
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2011-03-22 23:49:26 +01:00
|
|
|
$obj=new TransactionTest_Object();
|
2011-10-29 02:34:45 +02:00
|
|
|
$obj->Title='Fourth page';
|
2011-03-22 23:49:26 +01:00
|
|
|
$obj->write();
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
//Revert to a savepoint:
|
|
|
|
DB::getConn()->transactionRollback('rollback');
|
2011-01-12 00:01:42 +01:00
|
|
|
|
|
|
|
DB::getConn()->transactionEnd();
|
|
|
|
|
2011-03-22 23:49:26 +01:00
|
|
|
$first=DataObject::get('TransactionTest_Object', "\"Title\"='First page'");
|
|
|
|
$second=DataObject::get('TransactionTest_Object', "\"Title\"='Second page'");
|
|
|
|
$third=DataObject::get('TransactionTest_Object', "\"Title\"='Third page'");
|
2011-10-29 02:34:45 +02:00
|
|
|
$fourth=DataObject::get('TransactionTest_Object', "\"Title\"='Fourth page'");
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
//These pages should be in the system
|
|
|
|
$this->assertTrue(is_object($first) && $first->exists());
|
|
|
|
$this->assertTrue(is_object($second) && $second->exists());
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
//These pages should NOT exist, we reverted to a savepoint:
|
|
|
|
$this->assertFalse(is_object($third) && $third->exists());
|
2011-10-29 02:34:45 +02:00
|
|
|
$this->assertFalse(is_object($fourth) && $fourth->exists());
|
2011-03-14 04:45:52 +01:00
|
|
|
} else {
|
|
|
|
$this->markTestSkipped('Current database does not support transactions');
|
2009-10-01 23:04:18 +02:00
|
|
|
}
|
|
|
|
}
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2011-03-22 23:49:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class TransactionTest_Object extends DataObject implements TestOnly {
|
|
|
|
static $db = array(
|
|
|
|
'Title' => 'Varchar(255)'
|
|
|
|
);
|
2011-03-14 04:45:52 +01:00
|
|
|
}
|