From cca733919c9f272ffe162b75fcc0043f5b93ea1a Mon Sep 17 00:00:00 2001 From: Kennedy Tedesco Date: Sat, 31 Oct 2015 10:41:09 -0200 Subject: [PATCH] Throw an exception when age is not an integer --- library/Rules/MinimumAge.php | 9 +++++---- tests/unit/Rules/MininumAgeTest.php | 9 +++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/library/Rules/MinimumAge.php b/library/Rules/MinimumAge.php index 66ceec51b..e51d495e2 100644 --- a/library/Rules/MinimumAge.php +++ b/library/Rules/MinimumAge.php @@ -12,6 +12,7 @@ namespace Respect\Validation\Rules; use DateTime; +use Respect\Validation\Exceptions\ComponentException; class MinimumAge extends AbstractRule { @@ -20,16 +21,16 @@ class MinimumAge extends AbstractRule public function __construct($age, $format = null) { + if (!filter_var($age, FILTER_VALIDATE_INT)) { + throw new ComponentException('The age must be a integer value.'); + } + $this->age = $age; $this->format = $format; } public function validate($input) { - if (!filter_var($this->age, FILTER_VALIDATE_INT)) { - return false; - } - if ($input instanceof DateTime) { $birthday = new \DateTime('now - '.$this->age.' year'); diff --git a/tests/unit/Rules/MininumAgeTest.php b/tests/unit/Rules/MininumAgeTest.php index fcbb2fa7c..61d5e005b 100644 --- a/tests/unit/Rules/MininumAgeTest.php +++ b/tests/unit/Rules/MininumAgeTest.php @@ -51,6 +51,15 @@ public function testInvalidDateShouldNotPass($age, $format, $input) $this->assertFalse($minimumAge->assert($input)); } + /** + * @expectedException Respect\Validation\Exceptions\ComponentException + * @expectedExceptionMessage The age must be a integer value + */ + public function testShouldNotAcceptNonIntegerAgeOnConstructor() + { + new MinimumAge('L12'); + } + public function providerForValidDateValidMinimumAge() { return [