Apply contribution guidelines to "IntType" rule

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2018-03-26 22:29:49 +02:00
parent ec8cb734b3
commit 435187298a
No known key found for this signature in database
GPG key ID: 221E9281655813A6
11 changed files with 84 additions and 105 deletions

View file

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

View file

@ -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',
],
];
}

View file

@ -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);

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
42 must not be of the type integer
42 must not be of type integer

View 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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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