Skip to content

Commit

Permalink
Merge pull request #455 from ampproject/add/454-invalid-option-value-…
Browse files Browse the repository at this point in the history
…exception
  • Loading branch information
schlessera authored Jan 4, 2022
2 parents b4d2d6f + 54c994e commit 77a0f92
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Exception/InvalidOptionValue.php
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);
}
}
28 changes: 28 additions & 0 deletions tests/Exception/InvalidOptionValueTest.php
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');
}
}

0 comments on commit 77a0f92

Please sign in to comment.