Allow updating of fixtures in creation steps if they already exist

This commit is contained in:
Ingo Schommer 2015-02-15 12:08:05 +13:00
parent 7e8eaae73a
commit 4297e36762

View File

@ -139,8 +139,17 @@ class FixtureContext extends BehatContext
$class,
array($field => $value)
);
// We should check if this fixture object already exists - if it does, we update it. If not, we create it
if($existingFixture = $this->fixtureFactory->get($class, $id)) {
// Merge existing data with new data, and create new object to replace existing object
foreach($fields as $k => $v) {
$existingFixture->$k = $v;
}
$existingFixture->write();
} else {
$this->fixtureFactory->createObject($class, $id, $fields);
}
}
/**
* Example: Given a "page" "Page 1" with "URL"="page-1" and "Content"="my page 1"
@ -160,8 +169,17 @@ class FixtureContext extends BehatContext
array_combine($matches['key'], $matches['value'])
);
$fields = $this->prepareFixture($class, $id, $fields);
// We should check if this fixture object already exists - if it does, we update it. If not, we create it
if($existingFixture = $this->fixtureFactory->get($class, $id)) {
// Merge existing data with new data, and create new object to replace existing object
foreach($fields as $k => $v) {
$existingFixture->$k = $v;
}
$existingFixture->write();
} else {
$this->fixtureFactory->createObject($class, $id, $fields);
}
}
/**
* Example: And the "page" "Page 2" has the following data