2009-10-01 23:04:18 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
2009-10-01 23:04:18 +02:00
|
|
|
class TransactionTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-22 23:49:26 +01:00
|
|
|
protected $extraDataObjects = array(
|
2016-10-14 03:30:05 +02:00
|
|
|
TransactionTest\TestObject::class
|
2011-03-22 23:49:26 +01:00
|
|
|
);
|
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();
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj=new TransactionTest\TestObject();
|
2011-03-22 23:49:26 +01:00
|
|
|
$obj->Title='First page';
|
|
|
|
$obj->write();
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj=new TransactionTest\TestObject();
|
2011-03-22 23:49:26 +01:00
|
|
|
$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
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj=new TransactionTest\TestObject();
|
2011-03-22 23:49:26 +01:00
|
|
|
$obj->Title='Third page';
|
|
|
|
$obj->write();
|
2011-01-12 00:01:42 +01:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj=new TransactionTest\TestObject();
|
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
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$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'");
|
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
|
|
|
}
|
|
|
|
|