mirror of
https://github.com/Respect/Validation.git
synced 2024-06-20 06:25:05 +02:00
Apply contribution guidelines to "Finite" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
0be63bd313
commit
23656c9030
|
@ -14,12 +14,13 @@ declare(strict_types=1);
|
|||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
/**
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class FiniteException extends ValidationException
|
||||
final class FiniteException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
|
|
|
@ -13,10 +13,16 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use function is_finite;
|
||||
use function is_numeric;
|
||||
|
||||
/**
|
||||
* Validates if the input is a finite number.
|
||||
*
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class Finite extends AbstractRule
|
||||
final class Finite extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
|
37
tests/integration/rules/finite.phpt
Normal file
37
tests/integration/rules/finite.phpt
Normal file
|
@ -0,0 +1,37 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\FiniteException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::finite()->check('');
|
||||
} catch (FiniteException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::finite())->check(10);
|
||||
} catch (FiniteException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::finite()->assert([12]);
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::finite())->assert('123456');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECT--
|
||||
"" must be a finite number
|
||||
10 must not be a finite number
|
||||
- `{ 12 }` must be a finite number
|
||||
- "123456" must not be a finite number
|
|
@ -13,73 +13,58 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use stdClass;
|
||||
use const INF;
|
||||
use const PHP_INT_MAX;
|
||||
use function acos;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @covers \Respect\Validation\Exceptions\FiniteException
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\Finite
|
||||
*
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class FiniteTest extends TestCase
|
||||
final class FiniteTest extends RuleTestCase
|
||||
{
|
||||
protected $rule;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->rule = new Finite();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForFinite
|
||||
*
|
||||
* @test
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function shouldValidateFiniteNumbers($input): void
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
self::assertTrue($this->rule->validate($input));
|
||||
}
|
||||
$sut = new Finite();
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNonFinite
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function shouldNotValidateNonFiniteNumbers($input): void
|
||||
{
|
||||
self::assertFalse($this->rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Respect\Validation\Exceptions\FiniteException
|
||||
* @expectedExceptionMessage `INF` must be a finite number
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function shouldThrowFiniteExceptionWhenChecking(): void
|
||||
{
|
||||
$this->rule->check(INF);
|
||||
}
|
||||
|
||||
public function providerForFinite()
|
||||
{
|
||||
return [
|
||||
['123456'],
|
||||
[-9],
|
||||
[0],
|
||||
[16],
|
||||
[2],
|
||||
[PHP_INT_MAX],
|
||||
'integer-string' => [$sut, '123456'],
|
||||
'float-string' => [$sut, '123.456'],
|
||||
'zero' => [$sut, 0],
|
||||
'negative integer' => [$sut, PHP_INT_MAX * -1],
|
||||
'positive integer' => [$sut, PHP_INT_MAX],
|
||||
'positive float' => [$sut, 1.3e100],
|
||||
'negative float' => [$sut, 1.3e100 * -1],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForNonFinite()
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
$sut = new Finite();
|
||||
|
||||
return [
|
||||
[' '],
|
||||
[INF],
|
||||
[[]],
|
||||
[new \stdClass()],
|
||||
[null],
|
||||
'empty' => [$sut, ''],
|
||||
'not a number' => [$sut, acos(1.01)],
|
||||
'infinite' => [$sut, INF],
|
||||
'negative infinite' => [$sut, INF * -1],
|
||||
'array' => [$sut, []],
|
||||
'object' => [$sut, new stdClass()],
|
||||
'null' => [$sut, null],
|
||||
'boolean false' => [$sut, false],
|
||||
'boolean true' => [$sut, true],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue