2008-11-10 00:56:24 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage Testing
|
|
|
|
*/
|
|
|
|
class DatabaseTest extends SapphireTest {
|
2010-04-12 04:03:16 +02:00
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'DatabaseTest_MyObject',
|
|
|
|
);
|
2011-01-12 01:10:38 +01:00
|
|
|
|
2009-11-30 02:56:05 +01:00
|
|
|
protected $usesDatabase = true;
|
2011-01-12 01:10:38 +01:00
|
|
|
|
2008-11-10 00:56:24 +01:00
|
|
|
function testDontRequireField() {
|
|
|
|
$conn = DB::getConn();
|
|
|
|
$this->assertArrayHasKey(
|
|
|
|
'MyField',
|
|
|
|
$conn->fieldList('DatabaseTest_MyObject')
|
|
|
|
);
|
2011-01-12 01:10:38 +01:00
|
|
|
|
2008-11-10 00:56:24 +01:00
|
|
|
$conn->dontRequireField('DatabaseTest_MyObject', 'MyField');
|
2011-01-12 01:10:38 +01:00
|
|
|
|
2008-11-10 00:56:24 +01:00
|
|
|
$this->assertArrayHasKey(
|
|
|
|
'_obsolete_MyField',
|
|
|
|
$conn->fieldList('DatabaseTest_MyObject'),
|
|
|
|
'Field is renamed to _obsolete_<fieldname> through dontRequireField()'
|
|
|
|
);
|
2011-01-12 01:10:38 +01:00
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
$this->resetDBSchema(true);
|
2008-11-10 00:56:24 +01:00
|
|
|
}
|
2011-01-12 01:10:38 +01:00
|
|
|
|
2008-11-10 00:56:24 +01:00
|
|
|
function testRenameField() {
|
|
|
|
$conn = DB::getConn();
|
2011-01-12 01:10:38 +01:00
|
|
|
|
2011-03-11 02:22:59 +01:00
|
|
|
$conn->clearCachedFieldlist();
|
2011-01-12 01:10:38 +01:00
|
|
|
|
2008-11-10 00:56:24 +01:00
|
|
|
$conn->renameField('DatabaseTest_MyObject', 'MyField', 'MyRenamedField');
|
|
|
|
|
|
|
|
$this->assertArrayHasKey(
|
|
|
|
'MyRenamedField',
|
|
|
|
$conn->fieldList('DatabaseTest_MyObject'),
|
|
|
|
'New fieldname is set through renameField()'
|
|
|
|
);
|
|
|
|
$this->assertArrayNotHasKey(
|
|
|
|
'MyField',
|
|
|
|
$conn->fieldList('DatabaseTest_MyObject'),
|
|
|
|
'Old fieldname isnt preserved through renameField()'
|
|
|
|
);
|
2010-04-12 04:03:16 +02:00
|
|
|
|
|
|
|
$this->resetDBSchema(true);
|
2008-11-10 00:56:24 +01:00
|
|
|
}
|
2011-01-12 01:10:38 +01:00
|
|
|
|
2009-05-19 05:55:14 +02:00
|
|
|
function testMySQLCreateTableOptions() {
|
|
|
|
if(DB::getConn() instanceof MySQLDatabase) {
|
|
|
|
$ret = DB::query(sprintf(
|
|
|
|
'SHOW TABLE STATUS WHERE "Name" = \'%s\'',
|
|
|
|
'DatabaseTest_MyObject'
|
|
|
|
))->first();
|
|
|
|
$this->assertEquals($ret['Engine'],'InnoDB',
|
|
|
|
"MySQLDatabase tables can be changed to InnoDB through DataObject::\$create_table_options"
|
|
|
|
);
|
|
|
|
}
|
2010-04-13 01:43:10 +02:00
|
|
|
}
|
2011-01-12 01:10:38 +01:00
|
|
|
|
2010-04-13 01:43:10 +02:00
|
|
|
function testSchemaUpdateChecking() {
|
|
|
|
$db = DB::getConn();
|
|
|
|
|
|
|
|
// Initially, no schema changes necessary
|
|
|
|
$db->beginSchemaUpdate();
|
|
|
|
$this->assertFalse($db->doesSchemaNeedUpdating());
|
2011-01-12 01:10:38 +01:00
|
|
|
|
2010-04-13 01:43:10 +02:00
|
|
|
// If we make a change, then the schema will need updating
|
|
|
|
$db->transCreateTable("TestTable");
|
|
|
|
$this->assertTrue($db->doesSchemaNeedUpdating());
|
|
|
|
|
|
|
|
// If we make cancel the change, then schema updates are no longer necessary
|
|
|
|
$db->cancelSchemaUpdate();
|
|
|
|
$this->assertFalse($db->doesSchemaNeedUpdating());
|
2009-05-19 05:55:14 +02:00
|
|
|
}
|
2011-01-12 01:10:38 +01:00
|
|
|
|
2010-10-15 04:57:14 +02:00
|
|
|
function testHasTable() {
|
|
|
|
$this->assertTrue(DB::getConn()->hasTable('DatabaseTest_MyObject'));
|
|
|
|
$this->assertFalse(DB::getConn()->hasTable('asdfasdfasdf'));
|
|
|
|
}
|
2011-09-22 16:28:58 +02:00
|
|
|
|
|
|
|
function testGetAndReleaseLock() {
|
|
|
|
$db = DB::getConn();
|
|
|
|
|
|
|
|
if(!$db->supportsLocks()) {
|
|
|
|
return $this->markTestSkipped('Tested database doesn\'t support application locks');
|
|
|
|
}
|
2011-01-12 01:10:38 +01:00
|
|
|
|
2011-09-22 16:28:58 +02:00
|
|
|
$this->assertTrue($db->getLock('DatabaseTest'), 'Can aquire lock');
|
|
|
|
// $this->assertFalse($db->getLock('DatabaseTest'), 'Can\'t repeatedly aquire the same lock');
|
|
|
|
$this->assertTrue($db->getLock('DatabaseTest'), 'The same lock can be aquired multiple times in the same connection');
|
|
|
|
|
|
|
|
$this->assertTrue($db->getLock('DatabaseTestOtherLock'), 'Can aquire different lock');
|
|
|
|
$db->releaseLock('DatabaseTestOtherLock');
|
|
|
|
|
|
|
|
// Release potentially stacked locks from previous getLock() invocations
|
|
|
|
$db->releaseLock('DatabaseTest');
|
|
|
|
$db->releaseLock('DatabaseTest');
|
|
|
|
|
|
|
|
$this->assertTrue($db->getLock('DatabaseTest'), 'Can aquire lock after releasing it');
|
|
|
|
$db->releaseLock('DatabaseTest');
|
|
|
|
}
|
|
|
|
|
|
|
|
function testCanLock() {
|
|
|
|
$db = DB::getConn();
|
|
|
|
|
|
|
|
if(!$db->supportsLocks()) {
|
|
|
|
return $this->markTestSkipped('Database doesn\'t support locks');
|
|
|
|
}
|
|
|
|
|
|
|
|
if($db instanceof MSSQLDatabase) {
|
|
|
|
return $this->markTestSkipped('MSSQLDatabase doesn\'t support inspecting locks');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertTrue($db->canLock('DatabaseTest'), 'Can lock before first aquiring one');
|
|
|
|
$db->getLock('DatabaseTest');
|
|
|
|
$this->assertFalse($db->canLock('DatabaseTest'), 'Can\'t lock after aquiring one');
|
|
|
|
$db->releaseLock('DatabaseTest');
|
|
|
|
$this->assertTrue($db->canLock('DatabaseTest'), 'Can lock again after releasing it');
|
|
|
|
}
|
|
|
|
|
2008-11-10 00:56:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class DatabaseTest_MyObject extends DataObject implements TestOnly {
|
2011-01-12 01:10:38 +01:00
|
|
|
|
2009-05-19 05:55:14 +02:00
|
|
|
static $create_table_options = array('MySQLDatabase' => 'ENGINE=InnoDB');
|
2011-01-12 01:10:38 +01:00
|
|
|
|
2008-11-10 00:56:24 +01:00
|
|
|
static $db = array(
|
|
|
|
'MyField' => 'Varchar'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
?>
|