Fix step when multiple relations exist between the two joined objects

This commit is contained in:
madmatt 2015-03-14 01:41:02 +13:00 committed by Ingo Schommer
parent a02b04c727
commit 6da19dac18
2 changed files with 22 additions and 4 deletions

View File

@ -669,6 +669,9 @@ It's based on the `vendor/bin/behat -di @cms` output.
Given /^I assign (?:(an|a|the) )"(?<type>[^"]+)" "(?<value>[^"]+)" to (?:(an|a|the) )"(?<relationType>[^"]+)" "(?<relationId>[^"]+)"$/
- Example: I assign the "TaxonomyTerm" "For customers" to the "Page" "Page1"
Given /^I assign (?:(an|a|the) )"(?<type>[^"]+)" "(?<value>[^"]+)" to (?:(an|a|the) )"(?<relationType>[^"]+)" "(?<relationId>[^"]+)" in the "(?<relationName>[^"]+)" relation$
- Example: I assign the "TaxonomyTerm" "For customers" to the "Page" "Page1" in the "Terms" relation
Given /^the CMS settings have the following data$/
- Example: Given the CMS settings has the following data
- Note: It only works with the SilverStripe CMS module installed

View File

@ -248,14 +248,26 @@ class FixtureContext extends BehatContext
}
}
/**
* Assign a type of object to another type of object
* The base object will be created if it does not exist already
* Assumption: one object has relationship (has_one, has_many or many_many ) with the other object
/**
* Assign a type of object to another type of object. The base object will be created if it does not exist already.
* If the last part of the string (in the "X" relation) is omitted, then the first matching relation will be used.
*
* @example I assign the "TaxonomyTerm" "For customers" to the "Page" "Page1"
* @Given /^I assign (?:(an|a|the) )"(?<type>[^"]+)" "(?<value>[^"]+)" to (?:(an|a|the) )"(?<relationType>[^"]+)" "(?<relationId>[^"]+)"$/
*/
public function stepIAssignObjToObj($type, $value, $relationType, $relationId) {
self::stepIAssignObjToObjInTheRelation($type, $value, $relationType, $relationId, null);
}
/**
* Assign a type of object to another type of object. The base object will be created if it does not exist already.
* If the last part of the string (in the "X" relation) is omitted, then the first matching relation will be used.
* Assumption: one object has relationship (has_one, has_many or many_many ) with the other object
*
* @example I assign the "TaxonomyTerm" "For customers" to the "Page" "Page1" in the "Terms" relation
* @Given /^I assign (?:(an|a|the) )"(?<type>[^"]+)" "(?<value>[^"]+)" to (?:(an|a|the) )"(?<relationType>[^"]+)" "(?<relationId>[^"]+)" in the "(?<relationName>[^"]+)" relation$/
*/
public function stepIAssignObjToObjInTheRelation($type, $value, $relationType, $relationId, $relationName) {
$class = $this->convertTypeToClass($type);
$relationClass = $this->convertTypeToClass($relationType);
@ -268,12 +280,15 @@ class FixtureContext extends BehatContext
$oneField = null;
if ($relationObj->many_many()) {
$manyField = array_search($class, $relationObj->many_many());
if($manyField && strlen($relationName) > 0) $manyField = $relationName;
}
if(empty($manyField) && $relationObj->has_many()) {
$manyField = array_search($class, $relationObj->has_many());
if($manyField && strlen($relationName) > 0) $manyField = $relationName;
}
if(empty($manyField) && $relationObj->has_one()) {
$oneField = array_search($class, $relationObj->has_one());
if($oneField && strlen($relationName) > 0) $oneField = $relationName;
}
if(empty($manyField) && empty($oneField)) {
throw new \Exception("'$relationClass' has no relationship (has_one, has_many and many_many) with '$class'!");