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

Commit

Permalink
Add config param for error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
edno committed Feb 4, 2020
1 parent 70af169 commit 9d7ba75
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 99 deletions.
4 changes: 2 additions & 2 deletions codeception.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ coverage:
include:
- src/*

extensions:
modules:
enabled:
- Codeception\Extension\GherkinParam
- Codeception\Extension\GherkinParam
46 changes: 27 additions & 19 deletions src/GherkinParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
use \RuntimeException;
use \Codeception\Exception\ExtensionException;
use \Codeception\Configuration;
use \Codeception\Step;

class GherkinParam extends \Codeception\Extension
class GherkinParam extends \Codeception\Module
{

/**
Expand All @@ -34,13 +35,11 @@ class GherkinParam extends \Codeception\Extension
* true: if parameter invalid then replacement value will be null
* false: default behaviour, ie replacement value is parameter {{name}}
*/
//TODO: implement nullable behaviour
private $nullable = false;

protected static $defaultSettings = [
'onErrorThrowException' => false,
'onErrorNull' => false
];
protected $config = ['onErrorThrowException', 'onErrorNull'];

protected $requiredFields = [];

/**
* @var array List events to listen to
Expand Down Expand Up @@ -68,15 +67,25 @@ class GherkinParam extends \Codeception\Extension
];

/**
* Constructor -- only used for reading module settings
*
* @param array $settings
* Initialize module configuration
*/
public function __construct($settings = [])
final public function _initialize()
{
$settings = Configuration::mergeConfigs(self::$defaultSettings, $settings);
$this->throwException = $settings['onErrorThrowException'];
$this->nullable = $settings['onErrorNull'];
if (isset($this->config['onErrorThrowException'])) {
$this->throwException = (bool) $this->config['onErrorThrowException'];
}

if (isset($this->config['onErrorNull'])) {
$this->nullable = (bool) $this->config['onErrorNull'];
}
}

/**
* Dynamic module reconfiguration
*/
final public function onReconfigure()
{
$this->_initialize();
}

/**
Expand Down Expand Up @@ -225,26 +234,25 @@ final protected function getValueFromArray(string $param)
/**
* Capture suite's config before any execution
*
* @param \Codeception\Event\SuiteEvent $e
* @param array $settings
* @return void
*
* @codeCoverageIgnore
* @ignore Codeception specific
*/
final public function beforeSuite(\Codeception\Event\SuiteEvent $e)
final public function _beforeSuite($settings = [])
{
self::$suiteConfig = $e->getSettings();
self::$suiteConfig = $settings;
}

/**
* Parse scenario's step before execution
*
* @param \Codeception\Event\StepEvent $e
* @param \Codeception\Step $step
* @return void
*/
final public function beforeStep(\Codeception\Event\StepEvent $e)
final public function _beforeStep(Step $step)
{
$step = $e->getStep();
// access to the protected property using reflection
$refArgs = new ReflectionProperty(get_class($step), 'arguments');
// change property accessibility to public
Expand Down
141 changes: 74 additions & 67 deletions tests/_support/AcceptanceTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use Behat\Gherkin\Node\TableNode;
use Codeception\Util\Fixtures;


/**
* Inherited Methods
* @method void wantToTest($text)
Expand All @@ -22,81 +21,89 @@ class AcceptanceTester extends \Codeception\Actor
{
use _generated\AcceptanceTesterActions;

/**
* @Given I have a parameter :param with value :value
*/
public function iHaveAParameterWithValue($param, $value)
{
Fixtures::add($param, $value);
}
/**
* @Given I have a parameter :param with value :value
*/
public function iHaveAParameterWithValue(string $param, string $value)
{
Fixtures::add($param, $value);
}

/**
* @Then /^I should see "([^"]+)" equals (?:to )?(?:")?([^"]+)(?:")?$/i
*/
public function iSeeEqual($arg1, $arg2)
{
$this->assertEquals($arg2, $arg1);
}
/**
* @Then /^I should see "([^"]+)" equals (?:to )?(?:")?([^"]+)(?:")?$/i
*/
public function iSeeEqual(string $arg1, string $arg2)
{
$this->assertEquals($arg2, $arg1);
}

/**
* @Then I should see following:
*/
public function iSeeTableEqual(TableNode $table)
{
foreach ($table->getRows() as $idx => $row) {
if ($idx == 0) continue;
$this->assertEquals($row[1], $row[0]);
}
}

/**
* @Then /^I should see "(.+)" is null$/i
*/
public function iSeeNull($arg1)
{
$this->assertNull($arg1);
}

/**
* @Given I have a parameter :param with values
* @param string $param
* @param TableNode $values
*/
public function iHaveParameterWithValues(string $param, TableNode $values)
{
Fixtures::add($param, $values->getHash());
/**
* @Then I should see following:
*/
public function iSeeTableEqual(TableNode $table)
{
foreach ($table->getRows() as $idx => $row) {
if ($idx == 0) continue;
$this->assertEquals($row[1], $row[0]);
}
}

/**
* @Then I should see :param with values
* @param string $param
* @param TableNode $table
*/
public function iSeeParamEquals(string $param, TableNode $table)
/**
* @Then /^I should see "(.+)" is null$/i
*/
public function iSeeNull(string $arg1)
{
$hash = $table->getHash();
foreach (Fixtures::get($param) as $key => $values) {
$this->assertEquals($hash[$key], $values);
}
$this->assertNull($arg1);
}

/**
* @Given I do not have a parameter :param
*/
public function iDoNotHaveAParameterWithValue($param)
{
// do nothing with $param
}
/**
* @Given I have a parameter :param with values
* @param string $param
* @param TableNode $values
*/
public function iHaveParameterWithValues(string $param, TableNode $values)
{
Fixtures::add($param, $values->getHash());
}

/**
* @Then I should see null:
*/
public function iSeeTableNull(TableNode $table)
{
foreach ($table->getRows() as $idx => $row) {
if ($idx == 0) continue;
$this->assertNull($row[0]);
/**
* @Then I should see :param with values
* @param string $param
* @param TableNode $table
*/
public function iSeeParamEquals(string $param, TableNode $table)
{
$hash = $table->getHash();
foreach (Fixtures::get($param) as $key => $values) {
$this->assertEquals($hash[$key], $values);
}
}

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

/**
* @Then I should see null:
*/
public function iSeeTableNull(TableNode $table)
{
foreach ($table->getRows() as $idx => $row) {
if ($idx == 0) continue;
$this->assertNull($row[0]);
}
}

/**
* @Given The configuration parameter :param is set to :value
*/
public function theConfigurationParameterIsSetTo(string $param, bool $value)
{
$this->setConfigParam($param, $value);
}

}
10 changes: 10 additions & 0 deletions tests/_support/Helper/ConfigHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace Helper;

class ConfigHelper extends \Codeception\Module
{
public function setConfigParam(string $param, bool $value)
{
$this->getModule('Codeception\Extension\GherkinParam')->_reconfigure(array($param => $value));
}
}
3 changes: 1 addition & 2 deletions tests/acceptance.suite.dist.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# Codeception Test Suite Configuration
#
# Suite for acceptance tests.
# Perform tests in browser using the WebDriver or PhpBrowser.
# If you need both WebDriver and PHPBrowser tests - create a separate suite.

class_name: AcceptanceTester

modules:
enabled:
- Asserts
- Helper\ConfigHelper
- Helper\Acceptance
- Helper\ExtHelper

Expand Down
6 changes: 5 additions & 1 deletion tests/acceptance/GherkinParamError.feature
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
Feature: Handling Parametrized Gherkin Errors
Feature: Handling Parametrized Gherkin Errors Defaulted
In order to create dynamic Gherkin scenario
As a tester
I need to have consistent behaviour for error cases

Background:
Given The configuration parameter "onErrorThrowException" is set to 0
And The configuration parameter "onErrorNullabe" is set to 0

@standard
Scenario: Simple parameter does not exist
Given I do not have a parameter "test"
Expand Down
20 changes: 12 additions & 8 deletions tests/acceptance/GherkinParamErrorNullable.feature
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
Feature: Handling Parametrized Gherkin Errors
Feature: Handling Parametrized Gherkin Errors Nullable
In order to create dynamic Gherkin scenario
As a tester
I want `null` value when the parameter is invalid

Background:
Given The configuration parameter "onErrorNull" is set to 1
And The configuration parameter "onErrorThrowException" is set to 0

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

@table
Scenario: Scenario parameter does not exist in a table
Scenario: Scenario parameter null when it does not exist in a table
Given I do not have a parameter "test"
Then I should see null:
| parameter | is null |
| {{ test }} | true |

@table-with-helper
Scenario: Table with helper and invalid parameter
Scenario: Table with helper and invalid parameter null when it does not exist
Given I do not have a parameter "test"
When I have parameters
| parameter | value |
Expand All @@ -25,17 +29,17 @@ Scenario: Table with helper and invalid parameter
Then I should see "{{param2}}" is null

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

@array-invalid-key
Scenario: Array with invalid key
Scenario: Array with invalid key null when it does not exist
Given I have an array "test" with values [1, two, 3.14, IV, 101]
Then I should see "{{test[9999]}}" is null

@outline
Scenario Outline: Outline example with parameter that does not exist
Scenario Outline: Outline example with parameter null when it does not exist
Given I do not have a parameter "parameter"
Then I should see "<{{ parameter }}>" is null
And I should see "{{<test>}}" is null
Expand All @@ -44,7 +48,7 @@ Scenario Outline: Outline example with parameter that does not exist
| param | 1010 |

@config
Scenario: Config key does not exist
Scenario: Config key null when does not exist
Given I have a configuration file "codeception.yml"
"""
actor: Tester
Expand Down

0 comments on commit 9d7ba75

Please sign in to comment.