mirror of
https://github.com/Respect/Validation.git
synced 2024-06-09 01:02:16 +02:00
Restore previous error handler when validation fails
The "Call" validator, the "assert()" and "check()" methods, define a custom error handler so it can handle the validation in case of a PHP error. However, it does not restor the previous error handler when the validation fails. Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
3463343b14
commit
7154e90522
|
@ -61,9 +61,9 @@ final class Call extends AbstractRule
|
|||
throw $exception;
|
||||
} catch (Throwable $throwable) {
|
||||
throw $this->reportError($input);
|
||||
} finally {
|
||||
restore_error_handler();
|
||||
}
|
||||
|
||||
restore_error_handler();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -79,9 +79,9 @@ final class Call extends AbstractRule
|
|||
throw $exception;
|
||||
} catch (Throwable $throwable) {
|
||||
throw $this->reportError($input);
|
||||
} finally {
|
||||
restore_error_handler();
|
||||
}
|
||||
|
||||
restore_error_handler();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -14,10 +14,12 @@ declare(strict_types=1);
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Exception;
|
||||
use PHPUnit\Framework\Error\Error;
|
||||
use Respect\Validation\Exceptions\AlwaysInvalidException;
|
||||
use Respect\Validation\Exceptions\CallException;
|
||||
use Respect\Validation\Test\TestCase;
|
||||
use Respect\Validation\Validatable;
|
||||
use function call_user_func;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
|
@ -68,6 +70,26 @@ final class CallTest extends TestCase
|
|||
$sut->assert($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function assertShouldRestorePreviousPhpErrorHandler(): void
|
||||
{
|
||||
$callable = 'trim';
|
||||
|
||||
$rule = $this->createMock(Validatable::class);
|
||||
$rule
|
||||
->expects(self::once())
|
||||
->method('assert');
|
||||
|
||||
$sut = new Call($callable, $rule);
|
||||
$sut->assert('');
|
||||
|
||||
self::expectException(Error::class);
|
||||
|
||||
call_user_func('trim', []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
|
@ -142,6 +164,26 @@ final class CallTest extends TestCase
|
|||
$sut->assert($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function checkShouldRestorePreviousPhpErrorHandler(): void
|
||||
{
|
||||
$callable = 'trim';
|
||||
|
||||
$rule = $this->createMock(Validatable::class);
|
||||
$rule
|
||||
->expects(self::once())
|
||||
->method('check');
|
||||
|
||||
$sut = new Call($callable, $rule);
|
||||
$sut->check('');
|
||||
|
||||
self::expectException(Error::class);
|
||||
|
||||
call_user_func('trim', []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue