Skip to content

Commit

Permalink
Merge pull request #92 from chadicus/fea/throw-on-error
Browse files Browse the repository at this point in the history
Add FilterOptions::THROW_ON_ERROR
  • Loading branch information
chadicus committed Jul 7, 2020
2 parents 408c7c9 + e2e492f commit 047bc08
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -41,7 +41,8 @@
"require-dev": {
"php-coveralls/php-coveralls": "^2.0",
"phpunit/phpunit": "^6.0",
"squizlabs/php_codesniffer": "^3.2"
"squizlabs/php_codesniffer": "^3.2",
"symfony/yaml": "^3.4"
},
"autoload": {
"psr-4": { "TraderInteractive\\": "src/" }
Expand Down
5 changes: 5 additions & 0 deletions src/FilterOptions.php
Expand Up @@ -28,4 +28,9 @@ final class FilterOptions
* @var string
*/
const USES = 'uses';

/**
* @var string
*/
const THROW_ON_ERROR = 'throwOnError';
}
30 changes: 30 additions & 0 deletions src/Filterer.php
Expand Up @@ -61,6 +61,13 @@ final class Filterer implements FiltererInterface
*/
const RESPONSE_TYPE_FILTER = FilterResponse::class;

/**
* @var string
*/
const INVALID_THROW_ON_ERROR_VALUE_ERROR_FORMAT = (
FilterOptions::THROW_ON_ERROR . " for field '%s' was not a boolean value"
);

/**
* @var array
*/
Expand Down Expand Up @@ -139,6 +146,7 @@ public function execute(array $input) : FilterResponse
$filters = $this->specification[$field];
self::assertFiltersIsAnArray($filters, $field);
$customError = self::validateCustomError($filters, $field);
$throwOnError = self::validateThrowOnError($filters, $field);
unset($filters[FilterOptions::IS_REQUIRED]);//doesn't matter if required since we have this one
unset($filters[FilterOptions::DEFAULT_VALUE]);//doesn't matter if there is a default since we have a value
$conflicts = self::extractConflicts($filters, $field, $conflicts);
Expand All @@ -162,6 +170,10 @@ public function execute(array $input) : FilterResponse
$this->addUsedInputToFilter($uses, $filteredInput, $field, $filter);
$input = call_user_func_array($function, $filter);
} catch (Exception $exception) {
if ($throwOnError) {
throw $exception;
}

$errors = self::handleCustomError($field, $input, $exception, $errors, $customError);
continue 2;//next field
}
Expand Down Expand Up @@ -602,6 +614,24 @@ private static function assertFilterIsArray($filter, string $field)
}
}

private static function validateThrowOnError(array &$filters, string $field) : bool
{
if (!array_key_exists(FilterOptions::THROW_ON_ERROR, $filters)) {
return false;
}

$throwOnError = $filters[FilterOptions::THROW_ON_ERROR];
if ($throwOnError !== true && $throwOnError !== false) {
throw new InvalidArgumentException(
sprintf(self::INVALID_THROW_ON_ERROR_VALUE_ERROR_FORMAT, $field)
);
}

unset($filters[FilterOptions::THROW_ON_ERROR]);

return $throwOnError;
}

private static function validateCustomError(array &$filters, string $field)
{
$customError = null;
Expand Down
42 changes: 42 additions & 0 deletions tests/FiltererTest.php
Expand Up @@ -5,6 +5,7 @@
use Exception;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use stdClass;
use Throwable;
use TraderInteractive\Exceptions\FilterException;
Expand Down Expand Up @@ -380,6 +381,47 @@ function (int $input, int $fieldOneValue) : int {
];
}

/**
* @test
* @covers ::execute
*/
public function executeThrowsOnError()
{
$exception = new RuntimeException('the error');
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage($exception->getMessage());
$filter = function () use ($exception) {
throw $exception;
};

$specification = [
'id' => [
FilterOptions::THROW_ON_ERROR => true,
[$filter],
],
];
$filterer = new Filterer($specification);
$filterer->execute(['id' => 1]);
}

/**
* @test
* @covers ::execute
*/
public function executeValidatesThrowsOnError()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf(Filterer::INVALID_THROW_ON_ERROR_VALUE_ERROR_FORMAT, 'id'));
$specification = [
'id' => [
FilterOptions::THROW_ON_ERROR => 'abc',
['uint'],
],
];
$filterer = new Filterer($specification);
$filterer->execute(['id' => 1]);
}

/**
* @test
* @covers ::filter
Expand Down

0 comments on commit 047bc08

Please sign in to comment.