Skip to content

Commit

Permalink
Throw an exception when age is not an integer
Browse files Browse the repository at this point in the history
  • Loading branch information
KennedyTedesco authored and henriquemoody committed Apr 24, 2016
1 parent 9460a4c commit cca7339
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 5 additions & 4 deletions library/Rules/MinimumAge.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Respect\Validation\Rules;

use DateTime;
use Respect\Validation\Exceptions\ComponentException;

class MinimumAge extends AbstractRule
{
Expand All @@ -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');

Expand Down
9 changes: 9 additions & 0 deletions tests/unit/Rules/MininumAgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down

0 comments on commit cca7339

Please sign in to comment.