diff --git a/library/Exceptions/IntValException.php b/library/Exceptions/IntValException.php index a67dc8db..5b57a0f9 100644 --- a/library/Exceptions/IntValException.php +++ b/library/Exceptions/IntValException.php @@ -13,8 +13,16 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; -class IntValException extends ValidationException +/** + * @author Alexandre Gomes Gaigalas + * @author Danilo Benevides + * @author Henrique Moody + */ +final class IntValException extends ValidationException { + /** + * {@inheritdoc} + */ public static $defaultTemplates = [ self::MODE_DEFAULT => [ self::STANDARD => '{{name}} must be an integer number', diff --git a/library/Rules/IntVal.php b/library/Rules/IntVal.php index dd1e4805..8c3387f8 100644 --- a/library/Rules/IntVal.php +++ b/library/Rules/IntVal.php @@ -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 + * @author Danilo Benevides + * @author Henrique Moody + */ +final class IntVal extends AbstractRule { public function validate($input): bool { diff --git a/tests/integration/intVal.phpt b/tests/integration/intVal.phpt new file mode 100644 index 00000000..34e0f692 --- /dev/null +++ b/tests/integration/intVal.phpt @@ -0,0 +1,37 @@ +--FILE-- +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 diff --git a/tests/unit/Rules/IntValTest.php b/tests/unit/Rules/IntValTest.php index 4d9508d8..40b3ace4 100644 --- a/tests/unit/Rules/IntValTest.php +++ b/tests/unit/Rules/IntValTest.php @@ -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 + * @author Danilo Benevides + * @author Gabriel Caruso + * @author Henrique Moody */ -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], ]; } }