function testGetTagOldWithExisting() { $exporter = new TestDataExporter(); $exporter->currentFixtureFile = 'output.yml'; // Fake some previous tag. $testdatatag = new TestDataTag(); $testdatatag->Class = 'Page'; $testdatatag->RecordID = '123'; $testdatatag->FixtureID = 'Page100'; $testdatatag->FixtureFile = 'output.yml'; $testdatatag->Version = 1; $testdatatag->write(); $page = new Page(); $page->ID = 123; $this->assertEquals($exporter->getTag($page), 'Page100', "Provides exsiting tag if record already in TestDataTag table."); }
/** * Prepares an automatic YML tag for the object. * Reuses an existing tag, if present from the previous import so we can reexport correctly. */ function getTag($object) { $existingTag = TestDataTag::get()->filter(array('Class' => $object->ClassName, 'RecordID' => $object->ID, 'FixtureFile' => $this->currentFixtureFile))->sort('Version', 'DESC')->First(); if ($existingTag) { // Use existing YML tag. return $existingTag->FixtureID; } else { // Create a new YML tag. // First, extract the highest present numeric suffix for a same-class same-file tag. $existingTags = DB::query("SELECT DISTINCT \"FixtureID\" FROM \"TestDataTag\" WHERE \"Class\"='{$object->ClassName}' AND \"FixtureFile\"='{$this->currentFixtureFile}'")->column("FixtureID"); foreach ($existingTags as $key => $tag) { $existingTags[$key] = (int) preg_replace("/[^0-9]/", '', $tag); } $newTagID = 1; if (count($existingTags)) { rsort($existingTags); $newTagID = $existingTags[0] + 1; } // Pull the exported records into the TestDataTag table. $tag = new TestDataTag(); $tag->Class = $object->ClassName; $tag->RecordID = $object->ID; $tag->FixtureFile = $this->currentFixtureFile; $tag->FixtureID = $object->ClassName . $newTagID; $tag->Version = 1; $tag->write(); return $tag->FixtureID; } }