2009-10-01 21:04:18 +00:00
|
|
|
<?php
|
2016-06-15 16:03:16 +12:00
|
|
|
|
2016-10-14 14:30:05 +13:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
|
|
|
|
2016-06-15 16:03:16 +12:00
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 10:51:35 +12:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
2016-12-16 17:34:21 +13:00
|
|
|
class TransactionTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
2017-03-25 00:17:26 +13:00
|
|
|
protected static $extra_dataobjects = array(
|
2016-12-16 17:34:21 +13:00
|
|
|
TransactionTest\TestObject::class
|
|
|
|
);
|
|
|
|
|
|
|
|
public function testCreateWithTransaction()
|
|
|
|
{
|
|
|
|
|
|
|
|
if (DB::get_conn()->supportsTransactions()==true) {
|
|
|
|
DB::get_conn()->transactionStart();
|
|
|
|
$obj=new TransactionTest\TestObject();
|
|
|
|
$obj->Title='First page';
|
|
|
|
$obj->write();
|
|
|
|
|
|
|
|
$obj=new TransactionTest\TestObject();
|
|
|
|
$obj->Title='Second page';
|
|
|
|
$obj->write();
|
|
|
|
|
|
|
|
//Create a savepoint here:
|
|
|
|
DB::get_conn()->transactionSavepoint('rollback');
|
|
|
|
|
|
|
|
$obj=new TransactionTest\TestObject();
|
|
|
|
$obj->Title='Third page';
|
|
|
|
$obj->write();
|
|
|
|
|
|
|
|
$obj=new TransactionTest\TestObject();
|
|
|
|
$obj->Title='Fourth page';
|
|
|
|
$obj->write();
|
|
|
|
|
|
|
|
//Revert to a savepoint:
|
|
|
|
DB::get_conn()->transactionRollback('rollback');
|
|
|
|
|
|
|
|
DB::get_conn()->transactionEnd();
|
|
|
|
|
|
|
|
$first=DataObject::get(TransactionTest\TestObject::class, "\"Title\"='First page'");
|
|
|
|
$second=DataObject::get(TransactionTest\TestObject::class, "\"Title\"='Second page'");
|
|
|
|
$third=DataObject::get(TransactionTest\TestObject::class, "\"Title\"='Third page'");
|
|
|
|
$fourth=DataObject::get(TransactionTest\TestObject::class, "\"Title\"='Fourth page'");
|
|
|
|
|
|
|
|
//These pages should be in the system
|
|
|
|
$this->assertTrue(is_object($first) && $first->exists());
|
|
|
|
$this->assertTrue(is_object($second) && $second->exists());
|
|
|
|
|
|
|
|
//These pages should NOT exist, we reverted to a savepoint:
|
|
|
|
$this->assertFalse(is_object($third) && $third->exists());
|
|
|
|
$this->assertFalse(is_object($fourth) && $fourth->exists());
|
|
|
|
} else {
|
|
|
|
$this->markTestSkipped('Current database does not support transactions');
|
|
|
|
}
|
|
|
|
}
|
2011-03-23 11:49:26 +13:00
|
|
|
}
|