From 9d7ba75912e6ed12071158484e987c9d30e9e0a3 Mon Sep 17 00:00:00 2001 From: Gregory Heitz Date: Tue, 4 Feb 2020 22:21:47 +0100 Subject: [PATCH] Add config param for error handling --- codeception.dist.yml | 4 +- src/GherkinParam.php | 46 +++--- tests/_support/AcceptanceTester.php | 141 +++++++++--------- tests/_support/Helper/ConfigHelper.php | 10 ++ tests/acceptance.suite.dist.yml | 3 +- tests/acceptance/GherkinParamError.feature | 6 +- .../GherkinParamErrorNullable.feature | 20 ++- 7 files changed, 131 insertions(+), 99 deletions(-) create mode 100644 tests/_support/Helper/ConfigHelper.php diff --git a/codeception.dist.yml b/codeception.dist.yml index 6bb71e1..ea29998 100644 --- a/codeception.dist.yml +++ b/codeception.dist.yml @@ -26,6 +26,6 @@ coverage: include: - src/* -extensions: +modules: enabled: - - Codeception\Extension\GherkinParam + - Codeception\Extension\GherkinParam \ No newline at end of file diff --git a/src/GherkinParam.php b/src/GherkinParam.php index 63e21dc..bc484e3 100644 --- a/src/GherkinParam.php +++ b/src/GherkinParam.php @@ -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 { /** @@ -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 @@ -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(); } /** @@ -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 diff --git a/tests/_support/AcceptanceTester.php b/tests/_support/AcceptanceTester.php index 661fdbd..c79a647 100644 --- a/tests/_support/AcceptanceTester.php +++ b/tests/_support/AcceptanceTester.php @@ -2,7 +2,6 @@ use Behat\Gherkin\Node\TableNode; use Codeception\Util\Fixtures; - /** * Inherited Methods * @method void wantToTest($text) @@ -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); + } } diff --git a/tests/_support/Helper/ConfigHelper.php b/tests/_support/Helper/ConfigHelper.php new file mode 100644 index 0000000..47e3a3a --- /dev/null +++ b/tests/_support/Helper/ConfigHelper.php @@ -0,0 +1,10 @@ +getModule('Codeception\Extension\GherkinParam')->_reconfigure(array($param => $value)); + } +} \ No newline at end of file diff --git a/tests/acceptance.suite.dist.yml b/tests/acceptance.suite.dist.yml index 97933b2..335576c 100644 --- a/tests/acceptance.suite.dist.yml +++ b/tests/acceptance.suite.dist.yml @@ -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 diff --git a/tests/acceptance/GherkinParamError.feature b/tests/acceptance/GherkinParamError.feature index a5c6101..15d50b8 100644 --- a/tests/acceptance/GherkinParamError.feature +++ b/tests/acceptance/GherkinParamError.feature @@ -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" diff --git a/tests/acceptance/GherkinParamErrorNullable.feature b/tests/acceptance/GherkinParamErrorNullable.feature index 581d3ea..5f1e541 100644 --- a/tests/acceptance/GherkinParamErrorNullable.feature +++ b/tests/acceptance/GherkinParamErrorNullable.feature @@ -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 | @@ -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 "{{}}" is null @@ -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