-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #455 from ampproject/add/454-invalid-option-value-…
…exception
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace AmpProject\Exception; | ||
|
||
use DomainException; | ||
|
||
/** | ||
* Exception thrown when an invalid option value was provided. | ||
* | ||
* @package ampproject/amp-toolbox | ||
*/ | ||
final class InvalidOptionValue extends DomainException implements AmpException | ||
{ | ||
/** | ||
* Instantiate an InvalidOptionValue exception for an invalid option value. | ||
* | ||
* @param string $option Name of the option. | ||
* @param array<string> $accepted Array of acceptable values. | ||
* @param string $actual Value that was actually provided. | ||
* @return self | ||
*/ | ||
public static function forValue($option, $accepted, $actual) | ||
{ | ||
$acceptedString = implode(', ', $accepted); | ||
$message = "The value for the option '{$option}' expected the value to be one of " | ||
. "[{$acceptedString}], got '{$actual}' instead."; | ||
|
||
return new self($message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace AmpProject\Exception; | ||
|
||
use AmpProject\Tests\TestCase; | ||
|
||
/** | ||
* Test the InvalidOptionValue exception. | ||
* | ||
* @covers \AmpProject\Exception\InvalidOptionValue | ||
* @package ampproject/amp-toolbox | ||
*/ | ||
class InvalidOptionValueTest extends TestCase | ||
{ | ||
/** | ||
* Test throwing the exception for an invalid option value. | ||
*/ | ||
public function testThrowingForSpecName() | ||
{ | ||
$this->expectException(InvalidOptionValue::class); | ||
$this->expectExceptionMessage( | ||
"The value for the option 'optionName' expected the value to be one of " | ||
. "[optionA, optionB], got 'invalid' instead." | ||
); | ||
|
||
throw InvalidOptionValue::forValue('optionName', ['optionA', 'optionB'], 'invalid'); | ||
} | ||
} |