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:
pathumhdes 2020-04-29 05:51:31 +05:30 committed by Henrique Moody
parent 3463343b14
commit 7154e90522
No known key found for this signature in database
GPG key ID: 221E9281655813A6
2 changed files with 46 additions and 4 deletions

View file

@ -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();
}
/**

View file

@ -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
*/