Merge pull request #81 from silverstripe-iterators/pulls/fixture-tweaks

Fixture tweaks
This commit is contained in:
Damian Mooyman 2015-03-10 09:40:18 +13:00
commit 30c0b9e5f1

View File

@ -139,13 +139,23 @@ class FixtureContext extends BehatContext
$class, $class,
array($field => $value) 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); $this->fixtureFactory->createObject($class, $id, $fields);
} }
}
/** /**
* Example: Given a "page" "Page 1" with "URL"="page-1" and "Content"="my page 1" * Example: Given a "page" "Page 1" with "URL"="page-1" and "Content"="my page 1"
* Example: Given the "page" "Page 1" has "URL"="page-1" and "Content"="my page 1"
* *
* @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" with (?<data>.*)$/ * @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" (?:(with|has)) (?<data>".*)$/
*/ */
public function stepCreateRecordWithData($type, $id, $data) { public function stepCreateRecordWithData($type, $id, $data) {
$class = $this->convertTypeToClass($type); $class = $this->convertTypeToClass($type);
@ -159,8 +169,17 @@ class FixtureContext extends BehatContext
array_combine($matches['key'], $matches['value']) array_combine($matches['key'], $matches['value'])
); );
$fields = $this->prepareFixture($class, $id, $fields); $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); $this->fixtureFactory->createObject($class, $id, $fields);
} }
}
/** /**
* Example: And the "page" "Page 2" has the following data * Example: And the "page" "Page 2" has the following data
@ -196,22 +215,31 @@ class FixtureContext extends BehatContext
*/ */
public function stepUpdateRecordRelation($type, $id, $relation, $relationType, $relationId) { public function stepUpdateRecordRelation($type, $id, $relation, $relationType, $relationId) {
$class = $this->convertTypeToClass($type); $class = $this->convertTypeToClass($type);
$relationClass = $this->convertTypeToClass($relationType); $relationClass = $this->convertTypeToClass($relationType);
$obj = $this->fixtureFactory->get($class, $id);
if(!$obj) $obj = $this->fixtureFactory->createObject($class, $id);
$relationObj = $this->fixtureFactory->get($relationClass, $relationId); $relationObj = $this->fixtureFactory->get($relationClass, $relationId);
if(!$relationObj) $relationObj = $this->fixtureFactory->createObject($relationClass, $relationId); if(!$relationObj) $relationObj = $this->fixtureFactory->createObject($relationClass, $relationId);
$data = array();
if($relation == 'child') {
$data['ParentID'] = $relationObj->ID;
}
$obj = $this->fixtureFactory->get($class, $id);
if($obj) {
$obj->update($data);
$obj->write();
} else {
$obj = $this->fixtureFactory->createObject($class, $id, $data);
}
switch($relation) { switch($relation) {
case 'parent': case 'parent':
$relationObj->ParentID = $obj->ID; $relationObj->ParentID = $obj->ID;
$relationObj->write(); $relationObj->write();
break; break;
case 'child': case 'child':
$obj->ParentID = $relationObj->ID; // already written through $data above
$obj->write();
break; break;
default: default:
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(sprintf(