Merge pull request #8179 from open-sausages/pulls/4/safer-test-clear

BUG Make regression in #7839 safer
This commit is contained in:
Ingo Schommer 2018-06-14 17:28:13 +12:00 committed by GitHub
commit 1c5876acf0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 7 deletions

View File

@ -201,15 +201,20 @@ class FixtureFactory
* If the $class argument is set, limit clearing to items of this class.
*
* @param string $limitToClass
* @param bool $metadata Clear internal mapping as well as data.
* Set to false by default since sometimes data is rolled back by translations.
*/
public function clear($limitToClass = null)
public function clear($limitToClass = null, $metadata = false)
{
$classes = ($limitToClass) ? array($limitToClass) : array_keys($this->fixtures);
foreach ($classes as $class) {
$ids = $this->fixtures[$class];
foreach ($ids as $id => $dbId) {
if (class_exists($class)) {
$class::get()->byId($dbId)->delete();
$instance = DataObject::get($class)->byId($dbId);
if ($instance) {
$instance->delete();
}
} else {
$table = $class;
$delete = new SQLDelete("\"$table\"", array(
@ -218,7 +223,9 @@ class FixtureFactory
$delete->execute();
}
unset($this->fixtures[$class][$id]);
if ($metadata) {
unset($this->fixtures[$class][$id]);
}
}
}
}

View File

@ -136,19 +136,25 @@ class FixtureFactoryTest extends SapphireTest
public function testClear()
{
$factory = new FixtureFactory();
$table = TestDataObject::singleton()->baseTable();
$obj1Id = $factory->createRaw(
TestDataObject::singleton()->baseTable(),
$table,
'one',
array('Name' => 'My Name')
);
$obj2 = $factory->createObject(TestDataObject::class, 'two');
// Clear models only
$factory->clear();
$this->assertFalse($factory->getId(TestDataObject::class, 'one'));
$this->assertEquals($obj1Id, $factory->getId($table, 'one'));
$this->assertNull(TestDataObject::get()->byID($obj1Id));
$this->assertFalse($factory->getId(TestDataObject::class, 'two'));
$this->assertEquals($obj2->ID, $factory->getId(TestDataObject::class, 'two'));
$this->assertNull(TestDataObject::get()->byID($obj2->ID));
// Force metadata clear
$factory->clear(null, true);
$this->assertFalse($factory->getId($table, 'one'));
$this->assertFalse($factory->getId(TestDataObject::class, 'two'));
}
public function testClearWithClass()