Permalink
Browse files

Removing unused validators in `util\Validator`, refactoring `Validato…

…r` rules engine to replace pre- and post-filter hooks with core filter system.
  • Loading branch information...
nateabele committed Feb 12, 2010
1 parent e17f657 commit f7a9ba6f79ed2d1ed092eb650fa63af881c406e5
Showing with 119 additions and 349 deletions.
  1. +31 −65 libraries/lithium/tests/cases/util/ValidatorTest.php
  2. +88 −284 libraries/lithium/util/Validator.php
@@ -38,22 +38,16 @@ public function testCustomMethodDispatching() {
* @return void
*/
public function testAddCustomRegexMethods() {
- // $this->expectException("Rule 'foo' is not a validation rule");
- // $this->assertNull(Validator::isFoo('foo'));
+ $this->assertNull(Validator::rules('foo'));
Validator::add('foo', '/^foo$/');
$this->assertTrue(Validator::isFoo('foo'));
$this->assertFalse(Validator::isFoo('bar'));
+ $this->assertTrue(in_array('foo', Validator::rules()));
+ $this->assertEqual('/^foo$/', Validator::rules('foo'));
- // $this->expectException("Rule 'uuid' is not a validation rule");
- // $this->assertNull(Validator::isUuid('1c0a5830-6025-11de-8a39-0800200c9a66'));
-
- $uuid = '/[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}/';
- Validator::add('uuid', $uuid);
- $this->assertTrue(Validator::isUuid('1c0a5830-6025-11de-8a39-0800200c9a66'));
- $this->assertTrue(Validator::isUuid('1c0a5831-6025-11de-8a39-0800200c9a66'));
- $this->assertTrue(Validator::isUuid('1c0a5832-6025-11de-8a39-0800200c9a66'));
- $this->assertFalse(Validator::isUuid('zc0a5832-6025-11de-8a39-0800200c9a66'));
+ $this->expectException("Rule 'bar' is not a validation rule");
+ $this->assertNull(Validator::isBar('foo'));
}
/**
@@ -65,6 +59,18 @@ public function testStateReset() {
Validator::__init();
}
+ /**
+ * Tests that valid and invalid UUIDs are properly detected.
+ *
+ * @return void
+ */
+ public function testUuid() {
+ $this->assertTrue(Validator::isUuid('1c0a5830-6025-11de-8a39-0800200c9a66'));
+ $this->assertTrue(Validator::isUuid('1c0a5831-6025-11de-8a39-0800200c9a66'));
+ $this->assertTrue(Validator::isUuid('1c0a5832-6025-11de-8a39-0800200c9a66'));
+ $this->assertFalse(Validator::isUuid('zc0a5832-6025-11de-8a39-0800200c9a66'));
+ }
+
/**
* Tests that new formats can be added to existing regex methods using Validator::add().
*
@@ -909,74 +915,34 @@ public function testCheckMultiplePasses() {
$this->assertEqual($expected, $result);
}
- public function testCompare() {
- $result = Validator::compare(1, '>', 0);
- $this->assertTrue($result);
-
- $result = Validator::compare(0, '<', 1);
- $this->assertTrue($result);
-
- $result = Validator::compare(1, '>=', 1);
- $this->assertTrue($result);
-
- $result = Validator::compare(1, '<=', 1);
- $this->assertTrue($result);
-
- $result = Validator::compare(1, '==', 1);
- $this->assertTrue($result);
-
- $result = Validator::compare(1, '==', '1');
- $this->assertTrue($result);
-
- $result = Validator::compare(1, '!=', 0);
+ public function testIsInRange() {
+ $value = 5;
+ $lower = 1;
+ $upper = 10;
+ $result = Validator::isInRange($value, null, compact('lower', 'upper'));
$this->assertTrue($result);
- $result = Validator::compare(1, '!=', '1');
+ $value = 0;
+ $result = Validator::isInRange($value, null, compact('lower', 'upper'));
$this->assertFalse($result);
- $result = Validator::compare(1, '===', 1);
- $this->assertTrue($result);
-
- $result = Validator::compare(1, '===', '1');
+ $value = 11;
+ $result = Validator::isInRange($value, null, compact('lower', 'upper'));
$this->assertFalse($result);
- }
- public function testHasMinLength() {
- $string = 'hello world';
- $minLength = 1;
- $result = Validator::hasMinLength($string, $minLength);
+ $result = Validator::isInRange(-1, null, array('upper' => 1));
$this->assertTrue($result);
- $minLength = 20;
- $result = Validator::hasMinLength($string, $minLength);
- $this->assertFalse($result);
- }
-
- public function testHasMaxLength() {
- $string = 'hello world';
- $maxLength = 1;
- $result = Validator::hasMaxLength($string, $maxLength);
+ $result = Validator::isInRange(2, null, array('upper' => 1));
$this->assertFalse($result);
- $maxLength = 20;
- $result = Validator::hasMaxLength($string, $maxLength);
- $this->assertTrue($result);
- }
-
- public function testIsInRange() {
- $value = 5;
- $lower = 1;
- $upper = 10;
- $result = Validator::isInRange($value, $lower, $upper);
+ $result = Validator::isInRange(2, null, array('lower' => 1));
$this->assertTrue($result);
- $value = 0;
- $result = Validator::isInRange($value, $lower, $upper);
+ $result = Validator::isInRange(1, null, array('lower' => 1));
$this->assertFalse($result);
- $value = 11;
- $result = Validator::isInRange($value, $lower, $upper);
- $this->assertFalse($result);
+ $this->assertTrue(Validator::isInRange(0));
}
}
Oops, something went wrong.

0 comments on commit f7a9ba6

Please sign in to comment.