2009-10-01 23:04:18 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-10-01 23:04:18 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class TransactionTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-22 23:49:26 +01:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'TransactionTest_Object'
|
|
|
|
);
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testCreateWithTransaction() {
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
if(DB::get_conn()->supportsTransactions()==true){
|
|
|
|
DB::get_conn()->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:
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::get_conn()->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:
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::get_conn()->transactionRollback('rollback');
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::get_conn()->transactionEnd();
|
2011-01-12 00:01:42 +01:00
|
|
|
|
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 {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2011-03-22 23:49:26 +01:00
|
|
|
'Title' => 'Varchar(255)'
|
|
|
|
);
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|