Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report all missing options at once #5

Merged
merged 1 commit into from
Mar 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ameech fixed

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Looks good.

CommandException::MISSING_OPTION
);

Expand Down