Skip to content

Commit

Permalink
Merge pull request #5 from equip/feature/exception-multiple-options
Browse files Browse the repository at this point in the history
Report all missing options at once
  • Loading branch information
shadowhand committed Mar 23, 2016
2 parents 2a98135 + 11022ad commit e9ac321
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ _..._
## 1.2.0 - ???

- Added `getHttpStatus` to command exception
- Report all missing options at once with command exception
- Deprecated `CommandException::missingOption` in favor of `missingOptions`

## 1.1.0 - 2016-03-14

Expand Down
7 changes: 3 additions & 4 deletions src/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ public function options()
$required = $this->requiredOptions();

if ($required) {
foreach ($required as $key) {
if (!isset($this->options[$key])) {
throw CommandException::missingOption($key);
}
$missing = array_diff($required, array_keys($this->options));
if ($missing) {
throw CommandException::missingOptions($missing);
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/CommandException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,27 @@ class CommandException extends RuntimeException
{
const MISSING_OPTION = 2000;

/*
* @param array $names
*
* @return static
*
* @since 1.2.0
*/
public static function missingOptions(array $names)
{
return new static(
sprintf('Required options not defined: `%s`', implode('`, `', $names)),
static::MISSING_OPTION
);
}

/**
* @param string $name
*
* @return static
*
* @deprecated 1.2.0
*/
public static function missingOption($name)
{
Expand Down
5 changes: 3 additions & 2 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ public function testRequiredOptionsFailure()
->method('requiredOptions')
->willReturn([
'user_id',
'article_id',
]);

$this->setExpectedException(
$this->setExpectedExceptionRegExp(
CommandException::class,
'',
'/required options not defined.+user_id.+article_id/i',
CommandException::MISSING_OPTION
);

Expand Down

0 comments on commit e9ac321

Please sign in to comment.