diff --git a/changelog.MD b/changelog.MD index 63deef4..63b382e 100644 --- a/changelog.MD +++ b/changelog.MD @@ -1,5 +1,10 @@ # Changelog +## New version + +**Miscellaneous** +* Added a strict mode to the step _options_ method. Default value is _false_. + ## 2.0 **New features** diff --git a/src/Step.php b/src/Step.php index 8e6884a..f878010 100644 --- a/src/Step.php +++ b/src/Step.php @@ -26,15 +26,19 @@ abstract class Step /** * Set the step options. * + * @param bool $strictMode if set to true, will throw an exception if an unknown option is given + * * @return $this */ - public function options(array $options): Step + public function options(array $options, bool $strictMode = false): Step { foreach ($options as $option => $value) { $option = lcfirst(implode('', array_map('ucfirst', explode('_', $option)))); if (in_array($option, $this->availableOptions, true)) { $this->$option = $value; + } elseif (true === $strictMode) { + throw new \InvalidArgumentException("Unknown option: '$option' with value '$value'"); } } diff --git a/tests/StepTest.php b/tests/StepTest.php index 752979c..f61425d 100644 --- a/tests/StepTest.php +++ b/tests/StepTest.php @@ -14,16 +14,29 @@ class StepTest extends TestCase { /** @test */ - public function setOptions(): void + public function setOptionsWithStrictModeDisabled(): void { $step = new FakeStep(); $step->options([ - 'option1' => 'value', - 'option2' => 'value', + 'option1' => 'value1', + 'option2' => 'value2', ]); - static::assertEquals('value', $step->getOption('Option1')); + static::assertEquals('value1', $step->getOption('Option1')); static::assertNull($step->getOption('Option2')); } + + /** @test */ + public function setOptionsWithStrictModeEnabled(): void + { + $step = new FakeStep(); + + static::expectExceptionMessage("Unknown option: 'option2' with value 'value2'"); + + $step->options([ + 'option1' => 'value1', + 'option2' => 'value2', + ], true); + } }