mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 14:52:17 +02:00
Apply contribution guidelines to "IntVal" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
f486d521cb
commit
de53a4b4bd
|
@ -13,8 +13,16 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class IntValException extends ValidationException
|
||||
/**
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Danilo Benevides <danilobenevides01@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class IntValException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be an integer number',
|
||||
|
|
|
@ -13,7 +13,17 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class IntVal extends AbstractRule
|
||||
use function filter_var;
|
||||
use function is_float;
|
||||
|
||||
/**
|
||||
* Validates if the input is an integer.
|
||||
*
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Danilo Benevides <danilobenevides01@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class IntVal extends AbstractRule
|
||||
{
|
||||
public function validate($input): bool
|
||||
{
|
||||
|
|
37
tests/integration/intVal.phpt
Normal file
37
tests/integration/intVal.phpt
Normal file
|
@ -0,0 +1,37 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\intValException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::intVal()->check('42.33');
|
||||
} catch (intValException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::intVal())->check(2);
|
||||
} catch (intValException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::intVal()->assert('Foo');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::intVal())->assert(3);
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
"42.33" must be an integer number
|
||||
2 must not be an integer number
|
||||
- "Foo" must be an integer number
|
||||
- 3 must not be an integer number
|
|
@ -13,64 +13,52 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\IntVal
|
||||
* @covers \Respect\Validation\Exceptions\IntValException
|
||||
*
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Danilo Benevides <danilobenevides01@gmail.com>
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class IntValTest extends TestCase
|
||||
final class IntValTest extends RuleTestCase
|
||||
{
|
||||
protected $intValidator;
|
||||
|
||||
protected function setUp(): void
|
||||
/*
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$this->intValidator = new IntVal();
|
||||
}
|
||||
$rule = new IntVal();
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInt
|
||||
*/
|
||||
public function testValidIntegersShouldReturnTrue($input): void
|
||||
{
|
||||
self::assertTrue($this->intValidator->__invoke($input));
|
||||
$this->intValidator->check($input);
|
||||
$this->intValidator->assert($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNotInt
|
||||
* @expectedException \Respect\Validation\Exceptions\IntValException
|
||||
*/
|
||||
public function testInvalidIntegersShouldThrowIntException($input): void
|
||||
{
|
||||
self::assertFalse($this->intValidator->__invoke($input));
|
||||
$this->intValidator->assert($input);
|
||||
}
|
||||
|
||||
public function providerForInt()
|
||||
{
|
||||
return [
|
||||
[16],
|
||||
['165'],
|
||||
[123456],
|
||||
[PHP_INT_MAX],
|
||||
[$rule, 16],
|
||||
[$rule, '165'],
|
||||
[$rule, 123456],
|
||||
[$rule, PHP_INT_MAX],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForNotInt()
|
||||
/*
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
$rule = new IntVal();
|
||||
|
||||
return [
|
||||
[''],
|
||||
[null],
|
||||
['a'],
|
||||
['1.0'],
|
||||
[1.0],
|
||||
[' '],
|
||||
['Foo'],
|
||||
['1.44'],
|
||||
[1e-5],
|
||||
[$rule, ''],
|
||||
[$rule, null],
|
||||
[$rule, 'a'],
|
||||
[$rule, '1.0'],
|
||||
[$rule, 1.0],
|
||||
[$rule, ' '],
|
||||
[$rule, 'Foo'],
|
||||
[$rule, '1.44'],
|
||||
[$rule, 1e-5],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue