mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 23:02:16 +02:00
Apply contribution guidelines to "IntType" rule
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
ec8cb734b3
commit
435187298a
|
@ -2,7 +2,7 @@
|
|||
|
||||
- `IntType()`
|
||||
|
||||
Validates whether the type of a value is integer.
|
||||
Validates whether the type of the input is [integer](http://php.net/types.integer).
|
||||
|
||||
```php
|
||||
v::intType()->validate(42); // true
|
||||
|
|
|
@ -13,14 +13,22 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class IntTypeException extends ValidationException
|
||||
/**
|
||||
* Exception class for IntType rule.
|
||||
*
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class IntTypeException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be of the type integer',
|
||||
self::STANDARD => '{{name}} must be of type integer',
|
||||
],
|
||||
self::MODE_NEGATIVE => [
|
||||
self::STANDARD => '{{name}} must not be of the type integer',
|
||||
self::STANDARD => '{{name}} must not be of type integer',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
|
|
@ -13,8 +13,18 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class IntType extends AbstractRule
|
||||
use function is_int;
|
||||
|
||||
/**
|
||||
* Validates whether the type of the input is integer.
|
||||
*
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class IntType extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
return is_int($input);
|
||||
|
|
|
@ -12,4 +12,4 @@ try {
|
|||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
42 must not be of the type integer
|
||||
42 must not be of type integer
|
||||
|
|
37
tests/integration/rules/intType.phpt
Normal file
37
tests/integration/rules/intType.phpt
Normal file
|
@ -0,0 +1,37 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\IntTypeException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::intType()->check(new stdClass());
|
||||
} catch (IntTypeException $exception) {
|
||||
echo $exception->getMainMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::intType())->check(42);
|
||||
} catch (IntTypeException $exception) {
|
||||
echo $exception->getMainMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::intType()->assert(INF);
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::intType())->assert(1234567890);
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
`[object] (stdClass: { })` must be of type integer
|
||||
42 must not be of type integer
|
||||
- `INF` must be of type integer
|
||||
- 1234567890 must not be of type integer
|
|
@ -1,10 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
v::intType()->assert(42);
|
||||
v::intType()->check(1984);
|
||||
?>
|
||||
--EXPECTF--
|
|
@ -1,15 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\IntTypeException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::intType()->check('42');
|
||||
} catch (IntTypeException $exception) {
|
||||
echo $exception->getMainMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
"42" must be of the type integer
|
|
@ -1,15 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::intType()->assert('1984');
|
||||
} catch (AllOfException $exception) {
|
||||
echo $exception->getFullMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
- "1984" must be of the type integer
|
|
@ -1,15 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\IntTypeException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::not(v::intType())->check(42);
|
||||
} catch (IntTypeException $exception) {
|
||||
echo $exception->getMainMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
42 must not be of the type integer
|
|
@ -1,15 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::not(v::intType())->assert(1984);
|
||||
} catch (AllOfException $exception) {
|
||||
echo $exception->getFullMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
- 1984 must not be of the type integer
|
|
@ -13,51 +13,45 @@ 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\IntType
|
||||
*
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class IntTypeTest extends TestCase
|
||||
final class IntTypeTest extends RuleTestCase
|
||||
{
|
||||
public function providerForValidIntType()
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$rule = new IntType();
|
||||
|
||||
return [
|
||||
[0],
|
||||
[123456],
|
||||
[PHP_INT_MAX],
|
||||
[PHP_INT_MAX * -1],
|
||||
[$rule, 0],
|
||||
[$rule, 123456],
|
||||
[$rule, PHP_INT_MAX],
|
||||
[$rule, PHP_INT_MAX * -1],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForValidIntType
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function testShouldValidateInputWhenItIsAValidIntType($input): void
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
$rule = new IntType();
|
||||
|
||||
self::assertTrue($rule->validate($input));
|
||||
}
|
||||
|
||||
public function providerForInvalidIntType()
|
||||
{
|
||||
return [
|
||||
['1'],
|
||||
[1.0],
|
||||
[PHP_INT_MAX + 1],
|
||||
[true],
|
||||
[$rule, '1'],
|
||||
[$rule, 1.0],
|
||||
[$rule, PHP_INT_MAX + 1],
|
||||
[$rule, true],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidIntType
|
||||
*/
|
||||
public function testShouldInvalidateInputWhenItIsNotAValidIntType($input): void
|
||||
{
|
||||
$rule = new IntType();
|
||||
|
||||
self::assertFalse($rule->validate($input));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue