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;
|
2018-02-08 22:28:32 +01:00
|
|
|
use SilverStripe\ORM\Tests\TransactionTest\TestObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class TransactionTest extends SapphireTest
|
|
|
|
{
|
2018-02-08 22:28:32 +01:00
|
|
|
protected $usesDatabase = true;
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-02-08 22:28:32 +01:00
|
|
|
protected static $extra_dataobjects = [
|
|
|
|
TransactionTest\TestObject::class,
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-02-08 22:28:32 +01:00
|
|
|
public static function setUpBeforeClass()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2018-02-08 22:28:32 +01:00
|
|
|
parent::setUpBeforeClass();
|
|
|
|
if (!DB::get_conn()->supportsTransactions()) {
|
|
|
|
static::markTestSkipped('Current database does not support transactions');
|
|
|
|
}
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-02-08 22:28:32 +01:00
|
|
|
public function testNestedTransaction()
|
|
|
|
{
|
|
|
|
$this->assertCount(0, TestObject::get());
|
|
|
|
try {
|
|
|
|
DB::get_conn()->withTransaction(function () {
|
|
|
|
$obj = TransactionTest\TestObject::create();
|
|
|
|
$obj->Title = 'Test';
|
|
|
|
$obj->write();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-02-08 22:28:32 +01:00
|
|
|
$this->assertCount(1, TestObject::get());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-02-08 22:28:32 +01:00
|
|
|
DB::get_conn()->withTransaction(function () {
|
|
|
|
$obj = TransactionTest\TestObject::create();
|
|
|
|
$obj->Title = 'Test2';
|
|
|
|
$obj->write();
|
|
|
|
$this->assertCount(2, TestObject::get());
|
|
|
|
});
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-02-08 22:28:32 +01:00
|
|
|
throw new \Exception('roll back transaction');
|
|
|
|
});
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->assertEquals('roll back transaction', $e->getMessage());
|
|
|
|
}
|
|
|
|
$this->assertCount(0, TestObject::get());
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-02-08 22:28:32 +01:00
|
|
|
public function testCreateWithTransaction()
|
|
|
|
{
|
|
|
|
DB::get_conn()->transactionStart();
|
|
|
|
$obj = new TransactionTest\TestObject();
|
|
|
|
$obj->Title = 'First page';
|
|
|
|
$obj->write();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-02-08 22:28:32 +01:00
|
|
|
$obj = new TransactionTest\TestObject();
|
|
|
|
$obj->Title = 'Second page';
|
|
|
|
$obj->write();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-02-08 22:28:32 +01:00
|
|
|
//Create a savepoint here:
|
|
|
|
DB::get_conn()->transactionSavepoint('rollback');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-02-08 22:28:32 +01:00
|
|
|
$obj = new TransactionTest\TestObject();
|
|
|
|
$obj->Title = 'Third page';
|
|
|
|
$obj->write();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-02-08 22:28:32 +01:00
|
|
|
$obj = new TransactionTest\TestObject();
|
|
|
|
$obj->Title = 'Fourth page';
|
|
|
|
$obj->write();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-02-08 22:28:32 +01:00
|
|
|
//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());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2011-03-22 23:49:26 +01:00
|
|
|
}
|