Skip to content
This repository has been archived by the owner on Jul 3, 2023. It is now read-only.

Commit

Permalink
feature: allow multiple variables and variables with additional text
Browse files Browse the repository at this point in the history
  • Loading branch information
gimler committed Jan 23, 2020
1 parent e2fd454 commit 0e15e5e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
27 changes: 16 additions & 11 deletions src/GherkinParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class GherkinParam extends \Codeception\Extension
* @var array RegExp for parsing steps
*/
private static $regEx = [
'match' => '/^{{\s?[A-z0-9_:-]+\s?}}$/',
'match' => '/{{\s?[A-z0-9_:-]+\s?}}/',
'filter' => '/[{}]/',
'config' => '/(?:^config)?:([A-z0-9_-]+)+(?=:|$)/',
'array' => '/^(?P<var>[A-z0-9_-]+)(?:\[(?P<key>.+)])$/'
Expand All @@ -52,18 +52,23 @@ class GherkinParam extends \Codeception\Extension
*/
final protected function getValueFromParam(string $param)
{
if (preg_match(self::$regEx['match'], $param)) {
$arg = trim(preg_filter(self::$regEx['filter'], '', $param));
if (preg_match(self::$regEx['config'], $arg)) {
return $this->getValueFromConfig($arg);
} elseif (preg_match(self::$regEx['array'], $arg)) {
return $this->getValueFromArray($arg);
} else {
return Fixtures::get($arg);
if (preg_match_all(self::$regEx['match'], $param, $variables)){
$values = [];
foreach ($variables[0] as $variable)
{
$variableName = trim(preg_filter(self::$regEx['filter'], '', $variable));
if (preg_match(self::$regEx['config'], $variableName)) {
$values[] = $this->getValueFromConfig($variableName);
} elseif (preg_match(self::$regEx['array'], $variableName)) {
$values[] = $this->getValueFromArray($variableName);
} else {
$values[] = Fixtures::get($variableName);
}
}
} else {
return $param;
$param = str_replace($variables[0], $values, $param);
}

return $param;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/_support/AcceptanceTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function iHaveAParameterWithValue($param, $value)
}

/**
* @Then /^I should see "({{\s?[A-z0-9\[\]_:-]+\s?}})" equals (?:to )?(?:")?([^"]+)(?:")?$/i
* @Then /^I should see "([^"]+)" equals (?:to )?(?:")?([^"]+)(?:")?$/i
*/
public function iSeeEqual($arg1, $arg2)
{
Expand Down
7 changes: 6 additions & 1 deletion tests/acceptance/GherkinParam.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Feature: Parametrize Gherkin Feature
Then I should see "{{test}}" equals "42"
Then I should see "{{ test }}" equals "42"

Scenario: Scenario with multiple values
Given I have a parameter "first_param" with value "first"
Given I have a parameter "second_param" with value "second"
Then I should see "{{first_param}} and {{second_param}}" equals "first and second"

Scenario: Scenario using table parameter
Given I have a parameter "my_param" with value "This is a test"
And I have a parameter "another_param" with value "3.14"
Expand Down Expand Up @@ -52,4 +57,4 @@ Feature: Parametrize Gherkin Feature
| shape | color |
| circle | green |
| square | yellow |
| triangle | blue |
| triangle | blue |
1 change: 1 addition & 0 deletions tests/acceptance/GherkinParamArray.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Feature: Parametrize Gherkin Feature (Array)
And I should see "{{test[2]}}" equals to 3.14
And I should see "{{test[3]}}" equals to "IV"
And I should see "{{test[4]}}" equals to "101"
And I should see "{{test[4]}} and {{test[1]}}" equals "101 and two"

Scenario: Key not exist (exception)
Given I have an array "test" with values [1, two, 3.14, IV, 101]
Expand Down
1 change: 1 addition & 0 deletions tests/acceptance/GherkinParamConfig.feature
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Feature: Parametrize Gherkin Feature (Config)
"""
When I execute a scenario calling the parameter 'my_param:user'
Then I should see "{{config:my_param:user}}" equals "mylogin"
Then I should see "{{config:my_param:user}}:{{config:my_param:password}}" equals "mylogin:mypassword"

Scenario: Suite parameters from acceptance.suite.yml
Given I have a configuration file "acceptance.suite.yml"
Expand Down

0 comments on commit 0e15e5e

Please sign in to comment.