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

Commit

Permalink
Update fix & tests for invalid parameter (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
edno committed Jan 31, 2020
1 parent d537951 commit 81df60a
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 32 deletions.
13 changes: 11 additions & 2 deletions src/GherkinParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,17 @@ final protected function getValueFromParam(string $param)
}
// array case
elseif (preg_match(self::$regEx['array'], $variable)) {
$values[] = $this->getValueFromArray($variable);
try {
$values[] = $this->getValueFromArray($variable);
} catch(RuntimeException $e) {
if ($this->throwException) throw new GherkinParamException(null, null, $e);
}
}
// normal case
else {
try {
$values[] = Fixtures::get($variable);
} catch(GherkinParamException $e) {
} catch(RuntimeException $e) {
if ($this->throwException) throw new GherkinParamException(null, null, $e);
}
}
Expand Down Expand Up @@ -130,8 +134,10 @@ final protected function getValueFromParam(string $param)
*
* @return \mixed|null Returns parameter's value if exists, else parameter {{name}}
*/
//TODO: pass param ref to function (&) [performance]
final private function mapParametersToValues(array $matches, array $values, string $param)
{
//TODO: move count() into separate variable [performance]
for ($i=0; $i<count($matches); $i++) {
$search = $matches[$i];
if (\is_string($search)) { // if null then skip
Expand All @@ -142,6 +148,7 @@ final private function mapParametersToValues(array $matches, array $values, stri
if ($this->throwException) throw new GherkinParamException(null);
break;
}
//TODO: replace str_replace by strtr (performance)
$param = \str_replace($search, $replacement, $param);
} else {
if ($this->throwException) throw new GherkinParamException(null);
Expand All @@ -160,6 +167,7 @@ final private function mapParametersToValues(array $matches, array $values, stri
*
* @return \mixed|null Returns parameter's value if exists, else null
*/
//TODO: pass param ref to function (&) [performance]
final protected function getValueFromConfig(string $param)
{
$value = null;
Expand All @@ -186,6 +194,7 @@ final protected function getValueFromConfig(string $param)
*
* @return \mixed|null Returns parameter's value if exists, else null
*/
//TODO: pass param ref to function (&) [performance]
final protected function getValueFromArray(string $param)
{
$value = null;
Expand Down
9 changes: 9 additions & 0 deletions tests/_support/AcceptanceTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,13 @@ public function iSeeParamEquals(string $param, TableNode $table)
$this->assertEquals($hash[$key], $values);
}
}

/**
* @Given I do not have a parameter :param
*/
public function iDoNotHaveAParameterWithValue($param)
{
// do nothing with $param
}

}
8 changes: 8 additions & 0 deletions tests/_support/Helper/Acceptance.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ public function iHaveArray($var, $values)
$array = preg_split('/,\s?/', $values);
Fixtures::add($var, $array);
}

/**
* @When /^I do not have an array "(\w+)"/i
*/
public function iDoNotHaveArray($var)
{
// Do nothing with $var
}
}
2 changes: 1 addition & 1 deletion tests/acceptance/GherkinParam.feature
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Feature: Parametrize Gherkin Feature
| {{ another_param }} | 3.14 |

Scenario Outline: Scenario using example
Given I have a parameter "test" with value "param"
Given I have a parameter "test" with value "parameter"
And I have a parameter "<parameter>" with value "<value>"
Then I should see "{{<parameter>}}" equals "<value>"

Expand Down
4 changes: 0 additions & 4 deletions tests/acceptance/GherkinParamArray.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ Feature: Parametrize Gherkin Feature (Array)
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]
Then I should see "{{test[9999]}}" is null

Scenario: Using array parameters as values of associative array
Given I have an array "shape" with values [triangle, blue]
And I have a parameter "shapes" with values
Expand Down
25 changes: 0 additions & 25 deletions tests/acceptance/GherkinParamConfig.feature
Original file line number Diff line number Diff line change
Expand Up @@ -98,28 +98,3 @@ Feature: Parametrize Gherkin Feature (Config)
"""
When I execute a scenario calling the parameter 'my_edgecase:0:user'
Then I should see "{{config:my_edgecase:0:user}}" equals "edgecase"

Scenario: Config key not exists (expect null)
Given I have a configuration file "codeception.yml"
"""
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
settings:
bootstrap: _bootstrap.php
colors: true
memory_limit: 512M
my_user: 'a value'
extensions:
enabled:
- Codeception\Extension\GherkinParam
"""
When I execute a scenario calling the parameter 'config:not_a_param'
Then I should see "{{config:not_a_param}}" is null
74 changes: 74 additions & 0 deletions tests/acceptance/GherkinParamError.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
Feature: Handling Parametrized Gherkin Errors
In order to create dynamic Gherkin scenario
As a tester
I need to have consistent behaviour for error cases

@standard
Scenario: Simple parameter does not exist
Given I do not have a parameter "test"
Then I should see "{{test}}" equals "{{test}}"

@table
Scenario: Scenario parameter does not exist in a table
Given I do not have a parameter "test"
Then I should see following:
| parameter | equals to |
| {{ test }} | {{ test }} |

@table-with-helper
Scenario: Table with helper and invalid parameter
Given I do not have a parameter "test"
When I have parameters
| parameter | value |
| param1 | Fix Helper |
| param2 | {{ test }} |
Then I should see "{{param2}}" equals "{{ test }}"

@array-invalid-name
Scenario: Array does not exist
Given I do not have an array "test"
Then I should see "{{test[9999]}}" equals "{{test[9999]}}"

@array-invalid-key
Scenario: Array with invalid key
Given I have an array "test" with values [1, two, 3.14, IV, 101]
Then I should see "{{test[9999]}}" equals "{{test[9999]}}"

@associative-array
Scenario: 4

@outline
Scenario Outline: Outline example with parameter that does not exist
Given I do not have a parameter "parameter"
Then I should see "<{{ parameter }}>" equals "<{{ parameter }}>"
And I should see "{{<parameter>}}" equals "{{param}}"

Examples:
| parameter | value |
| param | 1010 |

@config
Scenario: Config key does not exist
Given I have a configuration file "codeception.yml"
"""
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
settings:
bootstrap: _bootstrap.php
colors: true
memory_limit: 512M
my_user: 'a value'
extensions:
enabled:
- Codeception\Extension\GherkinParam
"""
When I execute a scenario calling the parameter 'config:not_a_param'
Then I should see "{{ config:not_a_param }}" equals "{{ config:not_a_param }}"

0 comments on commit 81df60a

Please sign in to comment.