databaseExists($dbname)) { $dbname = 'tmpdb' . rand(1000000,9999999); } $dbConn->selectDatabase($dbname); // This code is a bit misplaced; we want some way of the whole session being reinitialised... Versioned::reading_stage(null); $dbConn->createDatabase(); singleton('DataObject')->flushCache(); $dbadmin = new DatabaseAdmin(); $dbadmin->doBuild(true, false); // Load the fixture into the database $className = get_class($this); $fixtureFile = eval("return {$className}::\$fixture_file;"); $this->loadFixture($fixtureFile); } /** * Array of */ protected $fixtureDictionary; /** * Get the ID of an object from the fixture. * @param $className The data class, as specified in your fixture file. Parent classes won't work * @param $identifier The identifier string, as provided in your fixture file */ protected function idFromFixture($className, $identifier) { return $this->fixtureDictionary["$className.$identifier"]; } /** * Get an object from the fixture. * @param $className The data class, as specified in your fixture file. Parent classes won't work * @param $identifier The identifier string, as provided in your fixture file */ protected function objFromFixture($className, $identifier) { return DataObject::get_by_id($className, $this->idFromFixture($className, $identifier)); } /** * Load a YAML fixture file into the database. * Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture * @param $fixtureFile The location of the .yml fixture file, relative to the site base dir */ function loadFixture($fixtureFile) { $parser = new Spyc(); $fixtureContent = $parser->load(Director::baseFolder().'/'.$fixtureFile); $this->fixtureDictionary = array(); foreach($fixtureContent as $dataClass => $items) { foreach($items as $identifier => $fields) { $obj = new $dataClass(); foreach($fields as $fieldName => $fieldVal) { // Parse a dictionary reference - used to set foreign keys if(substr($fieldVal,0,2) == '=>') { $obj->$fieldName = $this->fixtureDictionary[ substr($fieldVal,2) ]; // Regular field value setting } else { $obj->$fieldName = $fieldVal; } } $obj->write(); // Populate the dictionary with the ID $this->fixtureDictionary[$dataClass.'.'.$identifier] = $obj->ID; } } } function tearDown() { // Delete our temporary database $dbConn = DB::getConn(); if(substr($dbConn->currentDatabase(),0,5) == 'tmpdb') { // echo "Deleted temp database " . $dbConn->currentDatabase() . "\n"; $dbConn->dropDatabase(); } } } } else { // Stub class SapphireTest extends Object {} } ?>