/** * Mostly rewritten from parent, but allows circular dependencies - goes through the relation loop only after * the dictionary is fully populated. */ public function saveIntoDatabase(DataModel $model) { // Custom plumbing: this has to be executed only once per fixture. $testDataTag = basename($this->fixtureFile); $this->latestVersion = DB::query("SELECT MAX(\"Version\") FROM \"TestDataTag\" WHERE \"FixtureFile\"='{$testDataTag}'")->value(); // We have to disable validation while we import the fixtures, as the order in // which they are imported doesnt guarantee valid relations until after the // import is complete. $validationenabled = DataObject::get_validation_enabled(); DataObject::set_validation_enabled(false); $parser = new Spyc(); $fixtureContent = $parser->loadFile($this->fixtureFile); $this->fixtureDictionary = array(); foreach ($fixtureContent as $dataClass => $items) { if (ClassInfo::exists($dataClass)) { $this->writeDataObject($model, $dataClass, $items); } else { $this->writeSQL($dataClass, $items); } } // Dictionary is now fully built, inject the relations. foreach ($fixtureContent as $dataClass => $items) { if (ClassInfo::exists($dataClass)) { $this->writeRelations($dataClass, $items); } } DataObject::set_validation_enabled($validationenabled); }
function setUp() { // Mark test as being run $this->originalIsRunningTest = self::$is_running_test; self::$is_running_test = true; // Remove password validation $this->originalMemberPasswordValidator = Member::password_validator(); $this->originalRequirements = Requirements::backend(); Member::set_password_validator(null); Cookie::set_report_errors(false); $className = get_class($this); $fixtureFile = eval("return {$className}::\$fixture_file;"); // Set up fixture if($fixtureFile) { if(substr(DB::getConn()->currentDatabase(),0,5) != 'tmpdb') { //echo "Re-creating temp database... "; self::create_temp_db(); //echo "done.\n"; } // This code is a bit misplaced; we want some way of the whole session being reinitialised... Versioned::reading_stage(null); singleton('DataObject')->flushCache(); $dbadmin = new DatabaseAdmin(); $dbadmin->clearAllData(); // We have to disable validation while we import the fixtures, as the order in // which they are imported doesnt guarantee valid relations until after the // import is complete. $validationenabled = DataObject::get_validation_enabled(); DataObject::set_validation_enabled(false); $this->fixture = new YamlFixture($fixtureFile); $this->fixture->saveIntoDatabase(); DataObject::set_validation_enabled($validationenabled); } // Set up email $this->originalMailer = Email::mailer(); $this->mailer = new TestMailer(); Email::set_mailer($this->mailer); }
/** * Load a YAML fixture file into the database. * Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture. * * Caution: In order to support reflexive relations which need a valid object ID, * the record is written twice: first after populating all non-relational fields, * then again after populating all relations (has_one, has_many, many_many). */ public function saveIntoDatabase() { // We have to disable validation while we import the fixtures, as the order in // which they are imported doesnt guarantee valid relations until after the // import is complete. $validationenabled = DataObject::get_validation_enabled(); DataObject::set_validation_enabled(false); $parser = new Spyc(); $fixtureContent = $parser->loadFile(Director::baseFolder() . '/' . $this->fixtureFile); $this->fixtureDictionary = array(); foreach ($fixtureContent as $dataClass => $items) { if (ClassInfo::exists($dataClass)) { $this->writeDataObject($dataClass, $items); } else { $this->writeSQL($dataClass, $items); } } DataObject::set_validation_enabled($validationenabled); }