From 6da19dac18c1660294877362d5453f938d31f270 Mon Sep 17 00:00:00 2001 From: madmatt Date: Sat, 14 Mar 2015 01:41:02 +1300 Subject: [PATCH] Fix step when multiple relations exist between the two joined objects --- README.md | 3 +++ .../BehatExtension/Context/FixtureContext.php | 23 +++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1fae23e..1db2c84 100644 --- a/README.md +++ b/README.md @@ -669,6 +669,9 @@ It's based on the `vendor/bin/behat -di @cms` output. Given /^I assign (?:(an|a|the) )"(?[^"]+)" "(?[^"]+)" to (?:(an|a|the) )"(?[^"]+)" "(?[^"]+)"$/ - Example: I assign the "TaxonomyTerm" "For customers" to the "Page" "Page1" + Given /^I assign (?:(an|a|the) )"(?[^"]+)" "(?[^"]+)" to (?:(an|a|the) )"(?[^"]+)" "(?[^"]+)" in the "(?[^"]+)" 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 diff --git a/src/SilverStripe/BehatExtension/Context/FixtureContext.php b/src/SilverStripe/BehatExtension/Context/FixtureContext.php index 3584e44..64b8ffe 100644 --- a/src/SilverStripe/BehatExtension/Context/FixtureContext.php +++ b/src/SilverStripe/BehatExtension/Context/FixtureContext.php @@ -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) )"(?[^"]+)" "(?[^"]+)" to (?:(an|a|the) )"(?[^"]+)" "(?[^"]+)"$/ */ 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) )"(?[^"]+)" "(?[^"]+)" to (?:(an|a|the) )"(?[^"]+)" "(?[^"]+)" in the "(?[^"]+)" 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'!");