Skip to content

Commit

Permalink
Added an optionnal strict mode to the options method (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecourtial authored Feb 6, 2022
1 parent 9064e7e commit 3a25000
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions changelog.MD
Original file line number Diff line number Diff line change
@@ -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**
Expand Down
6 changes: 5 additions & 1 deletion src/Step.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'");
}
}

Expand Down
21 changes: 17 additions & 4 deletions tests/StepTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 3a25000

Please sign in to comment.