mirror of
https://github.com/Respect/Validation.git
synced 2024-06-08 16:52:16 +02:00
Improve unit tests for "Call" rule
I can't explain why, but depending on the environment, some tests of the "Call" rule was failing. This commit will add a test for the "validate()" method, and also use a more clean way to trigger and handle a PHP error. Co-authored-by: Casey McLaughlin <caseyamcl@gmail.com> Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
f3faf50340
commit
6f3979cb51
|
@ -13,13 +13,15 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use ErrorException;
|
||||
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;
|
||||
use function restore_error_handler;
|
||||
use function set_error_handler;
|
||||
use function trigger_error;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
|
@ -33,6 +35,31 @@ use function call_user_func;
|
|||
*/
|
||||
final class CallTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var ErrorException
|
||||
*/
|
||||
private $errorException;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->errorException = new ErrorException('This is a PHP error');
|
||||
|
||||
set_error_handler(function (): void {
|
||||
throw $this->errorException;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
restore_error_handler();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
|
@ -85,9 +112,9 @@ final class CallTest extends TestCase
|
|||
$sut = new Call($callable, $rule);
|
||||
$sut->assert('');
|
||||
|
||||
self::expectException(Error::class);
|
||||
self::expectExceptionObject($this->errorException);
|
||||
|
||||
call_user_func('trim', []);
|
||||
trigger_error('Forcing PHP to trigger an error');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -179,9 +206,9 @@ final class CallTest extends TestCase
|
|||
$sut = new Call($callable, $rule);
|
||||
$sut->check('');
|
||||
|
||||
self::expectException(Error::class);
|
||||
self::expectExceptionObject($this->errorException);
|
||||
|
||||
call_user_func('trim', []);
|
||||
trigger_error('Forcing PHP to trigger an error');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -264,4 +291,24 @@ final class CallTest extends TestCase
|
|||
|
||||
self::assertFalse($sut->validate('something'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function validateShouldRestorePreviousPhpErrorHandler(): void
|
||||
{
|
||||
$callable = 'trim';
|
||||
|
||||
$rule = $this->createMock(Validatable::class);
|
||||
$rule
|
||||
->expects(self::once())
|
||||
->method('check');
|
||||
|
||||
$sut = new Call($callable, $rule);
|
||||
$sut->validate('');
|
||||
|
||||
self::expectExceptionObject($this->errorException);
|
||||
|
||||
trigger_error('Forcing PHP to trigger an error');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue