Skip to content

Commit

Permalink
Fix step when multiple relations exist between the two joined objects
Browse files Browse the repository at this point in the history
  • Loading branch information
madmatt authored and chillu committed Apr 21, 2015
1 parent a02b04c commit 6da19da
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
23 changes: 19 additions & 4 deletions src/SilverStripe/BehatExtension/Context/FixtureContext.php
Expand Up @@ -248,14 +248,26 @@ public function stepUpdateRecordRelation($type, $id, $relation, $relationType, $
}
}

/**
* 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);

Expand All @@ -268,12 +280,15 @@ public function stepIAssignObjToObj($type, $value, $relationType, $relationId) {
$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'!");
Expand Down

0 comments on commit 6da19da

Please sign in to comment.