diff --git a/classes/Validate.php b/classes/Validate.php index 6d31fec..062f920 100644 --- a/classes/Validate.php +++ b/classes/Validate.php @@ -54,7 +54,7 @@ class Validate case 'filter': if (count($rule) < 2) { - throw new Exception('Invalid validation rule, expected: "validate:boolean|email|float|int|ip|regex|url".'); + throw new Exception('Invalid validation rule, expected: "validate:boolean|email|float|int|ip|url".'); } else { @@ -65,13 +65,12 @@ class Validate case 'float': case 'int': case 'ip': - case 'regex': case 'url': $filter = constant('FILTER_VALIDATE_' . strtoupper($rule[1])); break; default: - throw new Exception('Invalid filter, expecting boolean, email, float, int, ip, regex or url.'); + throw new Exception('Invalid filter, expecting boolean, email, float, int, ip or url.'); break; } @@ -103,19 +102,36 @@ class Validate switch ($rule[1]) { - case '<': $valid = $length < $rule[2]; break; - case '<=': $valid = $length <= $rule[2]; break; - case '==': $valid = $length == $rule[2]; break; - case '!=': $valid = $length != $rule[2]; break; - case '>=': $valid = $length >= $rule[2]; break; - case '>': $valid = $length > $rule[2]; break; + case '<': + $valid = $length < $rule[2]; + break; + + case '<=': + $valid = $length <= $rule[2]; + break; + + case '==': + $valid = $length == $rule[2]; + break; + + case '!=': + $valid = $length != $rule[2]; + break; + + case '>=': + $valid = $length >= $rule[2]; + break; + + case '>': + $valid = $length > $rule[2]; + break; default: throw new Exception('Invalid operator, expecting <, <=, ==, !=, >= or >.'); break; } - if ($valid === true) + if (!$valid) { $errors[] = $message; } @@ -137,7 +153,10 @@ class Validate } else { - if ((strtolower($rule[1]) == 'not' && !preg_match($rule[2], $value)) || preg_match($rule[2], $value)) + $rule[1] = strtolower($rule[1]); + + if (($rule[1] == 'is' && preg_match($rule[2], $value)) + || ($rule[1] == 'not' && !preg_match($rule[2], $value))) { $errors[] = $message; } diff --git a/tests/classes/ValidateTest.php b/tests/classes/ValidateTest.php index c618f67..c75654f 100644 --- a/tests/classes/ValidateTest.php +++ b/tests/classes/ValidateTest.php @@ -2,13 +2,200 @@ class ValidateTest extends PHPUnit_Framework_TestCase { - public function testIsValidTooLong() + public function testFilterBoolean() { - $variable = 'really long string'; - $rules = 'length:16'; - - Validate::isValid($variable, $rules); + $this->assertTrue(Validate::isValid(true, ['filter:boolean' => 'error'])); } + + public function testFilterBooleanError() + { + $this->assertEquals(['error'], Validate::isValid(false, ['filter:boolean' => 'error'])); + } + + public function testFilterEmail() + { + $this->assertTrue(Validate::isValid('foo@bar.com', ['filter:email' => 'error'])); + } + + public function testFilterEmailError() + { + $this->assertEquals(['error'], Validate::isValid('invalid', ['filter:email' => 'error'])); + } + + public function testFilterFloat() + { + $this->assertTrue(Validate::isValid(2.231981, ['filter:float' => 'error'])); + } + + public function testFilterFloatError() + { + $this->assertEquals(['error'], Validate::isValid('invalid', ['filter:float' => 'error'])); + } + + public function testFilterInt() + { + $this->assertTrue(Validate::isValid(2231981, ['filter:int' => 'error'])); + } + + public function testFilterIntError() + { + $this->assertEquals(['error'], Validate::isValid('invalid', ['filter:int' => 'error'])); + } + + public function testFilterIP() + { + $this->assertTrue(Validate::isValid('2.23.19.81', ['filter:ip' => 'error'])); + } + + public function testFilterIPError() + { + $this->assertEquals(['error'], Validate::isValid('invalid', ['filter:ip' => 'error'])); + } + + public function testFilterURL() + { + $this->assertTrue(Validate::isValid('http://foo.com/bar?stuff', ['filter:url' => 'error'])); + } + + public function testFilterURLError() + { + $this->assertEquals(['error'], Validate::isValid('invalid', ['filter:url' => 'error'])); + } + + /** + * @expectedException Exception + * @expectedExceptionMessage Invalid validation rule, expected: "validate:boolean|email|float|int|ip|url". + */ + public function testFilterVarInvalidRule() + { + Validate::isValid('value', ['filter' => 'foo']); + } + + /** + * @expectedException Exception + * @expectedExceptionMessage Invalid filter, expecting boolean, email, float, int, ip or url. + */ + public function testFilterVarInvalidFilter() + { + Validate::isValid('value', ['filter:foo' => 'bar']); + } + + public function testLengthLessThan() + { + $this->assertTrue(Validate::isValid('value', ['length:<:10' => 'error'])); + } + + public function testLengthLessThanError() + { + $this->assertEquals(['error'], Validate::isValid('value', ['length:<:1' => 'error'])); + } + + public function testLengthLessThanOrEqual() + { + $this->assertTrue(Validate::isValid('value', ['length:<=:10' => 'error'])); + } + + public function testLengthLessThanOrEqualError() + { + $this->assertEquals(['error'], Validate::isValid('value', ['length:<=:1' => 'error'])); + } + + public function testLengthEqual() + { + $this->assertTrue(Validate::isValid('value', ['length:==:5' => 'error'])); + } + + public function testLengthEqualError() + { + $this->assertEquals(['error'], Validate::isValid('value', ['length:==:1' => 'error'])); + } + + public function testLengthNotEqual() + { + $this->assertTrue(Validate::isValid('value', ['length:!=:1' => 'error'])); + } + + public function testLengthNotEqualError() + { + $this->assertEquals(['error'], Validate::isValid('value', ['length:!=:5' => 'error'])); + } + + public function testLengthGreaterThanOrEqual() + { + $this->assertTrue(Validate::isValid('value', ['length:>=:1' => 'error'])); + } + + public function testLengthGreaterThanOrEqualError() + { + $this->assertEquals(['error'], Validate::isValid('value', ['length:>=:10' => 'error'])); + } + + public function testLengthGreaterThan() + { + $this->assertTrue(Validate::isValid('value', ['length:>:1' => 'error'])); + } + + public function testLengthGreaterThanError() + { + $this->assertEquals(['error'], Validate::isValid('value', ['length:>:10' => 'error'])); + } + + /** + * @expectedException Exception + * @expectedExceptionMessage Invalid validation rule, expected: "length:<|<=|==|!=|>=|>:integer". + */ + public function testLengthInvalidRule() + { + Validate::isValid('value', ['length:16' => 'bar']); + } + + /** + * @expectedException Exception + * @expectedExceptionMessage Invalid length value, expecting an integer. + */ + public function testLengthInvalidLength() + { + Validate::isValid('value', ['length:==:foo' => 'bar']); + } + + /** + * @expectedException Exception + * @expectedExceptionMessage Invalid operator, expecting <, <=, ==, !=, >= or >. + */ + public function testLengthInvalidOperator() + { + Validate::isValid('value', ['length:&&:10' => 'foo']); + } + + public function testRegexIs() + { + $this->assertTrue(Validate::isValid('value', ['regex:is:/^va7ue$/' => 'error'])); + } + + public function testRegexIsError() + { + $this->assertEquals(['error'], Validate::isValid('value', ['regex:is:/^value$/' => 'error'])); + } + + public function testRegexNot() + { + $this->assertTrue(Validate::isValid('value', ['regex:not:/^value$/' => 'error'])); + } + + public function testRegexNotError() + { + $this->assertEquals(['error'], Validate::isValid('value', ['regex:not:/^va7ue$/' => 'error'])); + } + + /** + * @expectedException Exception + * @expectedExceptionMessage Invalid validation rule, expected: "regex:is|not:string". + */ + public function testRegexInvalidRule() + { + Validate::isValid('value', ['regex:/foo/' => 'bar']); + } + } ?>