[limb-svn] r7082 - in 3.x/trunk/limb/validation: src tests/cases

svn at limb-project.com svn at limb-project.com
Thu Jun 26 13:58:41 MSD 2008


Author: conf
Date: 2008-06-26 13:58:41 +0400 (Thu, 26 Jun 2008)
New Revision: 7082
URL: http://fisheye.limb-project.com/changelog/limb/?cs=7082

Modified:
   3.x/trunk/limb/validation/src/lmbValidatorBuilder.class.php
   3.x/trunk/limb/validation/tests/cases/lmbValidatorBuilderTest.class.php
Log:
-- lmbValidatorBuilder code adjusted to limb coding standards
-- adding more tests into lmbValidatorBuilderTest

Modified: 3.x/trunk/limb/validation/src/lmbValidatorBuilder.class.php
===================================================================
--- 3.x/trunk/limb/validation/src/lmbValidatorBuilder.class.php	2008-06-25 21:00:51 UTC (rev 7081)
+++ 3.x/trunk/limb/validation/src/lmbValidatorBuilder.class.php	2008-06-26 09:58:41 UTC (rev 7082)
@@ -17,33 +17,35 @@
  * Builds new or fills with the rules existing lmbValidator object, simplifying constructing rules
  * @package validation
  */
-class lmbValidatorBuilder {
+class lmbValidatorBuilder
+{
 
   /**
    * @todo correct working of common rules shortcuts, i.e. min shortcut does not work now
    *
    * @var array
    */
-  static protected $rules_shortcuts = array(  	
-  	'not_matches' => 'invalid_value',
-  	'min_length' => 'size_range',
-  	'max_length' => 'size_range',
-  	'range_length' => 'size_range',
-  	'mb_min_length' => 'i18_n_size_range',
-  	'mb_max_length' => 'i18_n_size_range',
-  	'mb_range_length' => 'i18_n_size_range',
-  	'min' => 'numeric_value_range',
-  	'max' => 'numeric_value_range',
-  	'range' => 'numeric_value_range'
+  static protected $rules_shortcuts = array(
+    'matches' => 'match',
+    'not_matches' => 'invalid_value',
+    'min_length' => 'size_range',
+    'max_length' => 'size_range',
+    'range_length' => 'size_range',
+    'mb_min_length' => 'i18_n_size_range',
+    'mb_max_length' => 'i18_n_size_range',
+    'mb_range_length' => 'i18_n_size_range',
+    'min' => 'numeric_value_range',
+    'max' => 'numeric_value_range',
+    'range' => 'numeric_value_range'
   );
-  
+
   /**
    * User defined rules, can be filled dynamically using function registerRule
    * 
    * @var array $rule_name => $path
    */
   static protected $user_rules = array();
-  
+
   /**
    * Main function for building rules.
    *
@@ -62,30 +64,37 @@
    * 
    * @param object  $validator  (optional)
    */
-  static function addRules($rules_lists, lmbValidator $validator) {
-  	if(!is_array($rules_lists)) {
-  		return;
-  	}
+  static function addRules($rules_lists, lmbValidator $validator) 
+  {
+    if(!is_array($rules_lists))
+    {
+      return;
+    }
 
-  	foreach($rules_lists as $field => $list) {
-  		
-  		if(is_string($list)) {
-  			$list = explode('|', $list);
-  		}
-  		  		
-  		foreach($list as $rule_name => $rule) { // by default $rule has simple format
-  			$error = '';
-  			
-  			if(is_string($rule_name)) { // extended format  				
-  				$error = $rule;
-  				$rule = $rule_name;  				
-  			}  			
-  			
-  			if($object_rule = self :: parseRule($field, $rule, $error)) {
-  				$validator->addRule($object_rule);	
-  			}  			
-  		}
-  	}
+    foreach($rules_lists as $field => $list)
+    {
+
+      if(is_string($list))
+      {
+        $list = explode('|', $list);
+      }
+
+      foreach($list as $rule_name => $rule) // by default $rule has simple format
+      {
+        $error = '';
+
+        if(is_string($rule_name)) // extended format
+        {
+          $error = $rule;
+          $rule = $rule_name;
+        }
+
+        if($object_rule = self :: parseRule($field, $rule, $error))
+        {
+          $validator->addRule($object_rule);
+        }
+      }
+    }
   }
 
   /**
@@ -97,7 +106,7 @@
     self :: addRules($rules_list, $validator);
     return $validator;
   }
-  
+
   /**
    * Parse text representation of a rule and return rule object
    *
@@ -106,59 +115,75 @@
    * @param string $error
    * @return object 
    */  
-  protected static function parseRule($field, $rule, $error = '') {
-  	
-  	$params = array();
-  	
-  	if(!preg_match('/^([^\[]+)(\[(.+)\])?$/i', $rule, $matches)) { // let's parse the rule
-  		return null;
-  	}
-  	  	
-  	$rule_name = $matches[1];
-  	if(isset($matches[3])) {
-  		$params = explode(',', $matches[3]);  		
-  	}
-  	
-  	array_unshift($params, $field); // field must be the first in params
-  	
-  	if(!empty($error)) {
-  		array_push($params, $error); // but error the last
-  	}
-  	
-  	$params = self :: trim($params);
-  	
-  	$path_to_rule = self :: getPathByRuleName($rule_name);
-  	
-  	return new lmbHandle($path_to_rule, $params);
+  protected static function parseRule($field, $rule, $error = '') 
+  {
+
+    $params = array();
+
+    if(!preg_match('/^([^\[]+)(\[(.+)\])?$/i', $rule, $matches)) // let's parse the rule
+    { 
+      return null;
+    }
+
+    $rule_name = $matches[1];
+    if(isset($matches[3])) 
+    {
+      $params = explode(',', $matches[3]);
+    }
+
+    array_unshift($params, $field); // field must be the first in params
+
+    if(!empty($error)) 
+    {
+      array_push($params, $error); // but error the last
+    }
+
+    $params = self :: trim($params);
+
+    $path_to_rule = self :: getPathByRuleName($rule_name);
+
+    return new lmbHandle($path_to_rule, $params);
   }
-  
-  static function getPathByRuleName($rule_name) {
-  	if(isset(self :: $user_rules[$rule_name])) {
-  		return $user_rules[$rule_name];
-  	} elseif(isset(self :: $rules_shortcuts[$rule_name])) {
-  		return LIMB_RULES_INCLUDE_PATH . '/' . self :: getLmbRule(self :: $rules_shortcuts[$rule_name]);
-  	} elseif(strpos($rule_name, 'lmb') === 0) { // $rule_name is exactly limb filename
-  		return LIMB_RULES_INCLUDE_PATH . '/' . $rule_name;
-  	} else {
-  		return LIMB_RULES_INCLUDE_PATH . '/' . self :: getLmbRule($rule_name);
-  	}
+
+  static function getPathByRuleName($rule_name) 
+  {
+    if(isset(self :: $user_rules[$rule_name])) 
+    {
+      return $user_rules[$rule_name];
+    } 
+    elseif(isset(self :: $rules_shortcuts[$rule_name])) 
+    {
+      return LIMB_RULES_INCLUDE_PATH . '/' . self :: getLmbRule(self :: $rules_shortcuts[$rule_name]);
+    } 
+    elseif(strpos($rule_name, 'lmb') === 0) // $rule_name is exactly limb filename
+    { 
+      return LIMB_RULES_INCLUDE_PATH . '/' . $rule_name;
+    } 
+    else 
+    {
+      return LIMB_RULES_INCLUDE_PATH . '/' . self :: getLmbRule($rule_name);
+    }
   }
-  
-  static function getLmbRule($underscored_name) {
-  	return 'lmb' . lmb_camel_case($underscored_name) . 'Rule.class.php';  	
+
+  static function getLmbRule($underscored_name) 
+  {
+    return 'lmb' . lmb_camel_case($underscored_name) . 'Rule.class.php';
   }
-  
-  static function registerRule($name, $path) {
-  	self :: $user_rules[$name] = $path;
+
+  static function registerRule($name, $path) 
+  {
+    self :: $user_rules[$name] = $path;
   }
-  
-  static function trim($arr) {  	
-  	$trimmed = array();
-  	
-  	foreach($arr as $key => $value) {
-  		$trimmed[$key] = trim($value);
-  	}
-  	
-  	return $trimmed;
+
+  static function trim($arr) 
+  {
+    $trimmed = array();
+
+    foreach($arr as $key => $value) 
+    {
+      $trimmed[$key] = trim($value);
+    }
+
+    return $trimmed;
   }
 }

Modified: 3.x/trunk/limb/validation/tests/cases/lmbValidatorBuilderTest.class.php
===================================================================
--- 3.x/trunk/limb/validation/tests/cases/lmbValidatorBuilderTest.class.php	2008-06-25 21:00:51 UTC (rev 7081)
+++ 3.x/trunk/limb/validation/tests/cases/lmbValidatorBuilderTest.class.php	2008-06-26 09:58:41 UTC (rev 7082)
@@ -22,10 +22,117 @@
 
   function testAddRulesFromSimpleString()
   {
-    $rules = array("login" => "required"); 
-    $this->validator->expectOnce("addRule", array(new EqualExpectation(new lmbHandle('limb/validation/src/rule/lmbRequiredRule.class.php', array('login')))));
+    $rules = array();
+    $rules['login'] = "required|matches[bbb]|size_range[5, 8]|lmbIdentifierRule.class.php";
+    
+    $calls_counter = 0;
+    
+    // no params
+    $this->validator->expectAt(
+      $calls_counter++,
+      "addRule", 
+      array(
+        new EqualExpectation(
+          new lmbHandle('limb/validation/src/rule/lmbRequiredRule.class.php', array('login'))
+        )
+      )
+    );
+    
+    // 1 param
+    $this->validator->expectAt(
+      $calls_counter++,
+      "addRule", 
+      array(
+        new EqualExpectation(
+          new lmbHandle('limb/validation/src/rule/lmbMatchRule.class.php', array('login', 'bbb'))
+        )
+      )
+    );
+    
+    // 2 (or more) params
+    $this->validator->expectAt(
+      $calls_counter++,
+      "addRule", 
+      array(
+        new EqualExpectation(
+          new lmbHandle('limb/validation/src/rule/lmbSizeRangeRule.class.php', array('login', 5, 8))
+        )
+      )
+    );
+    
+    // rule name is exactly matches to file name
+    $this->validator->expectAt(
+      $calls_counter++,
+      "addRule", 
+      array(
+        new EqualExpectation(
+          new lmbHandle('limb/validation/src/rule/lmbIdentifierRule.class.php', array('login'))
+        )
+      )
+    );
+        
     lmbValidatorBuilder :: addRules($rules, $this->validator);
   }
+    
+  function testAddRulesFromArrayWithCustomError()
+  {
+    $errors = array(
+      'email' => 'Email error',
+      'pattern' => 'Not a digit'    
+    );
+        
+    $rules = array();
+    
+    $rules['login'] = array(
+      'required',
+      'size_range[5, 8]',
+      'email' => $errors['email'],
+      'pattern[/\d+/]' => $errors['pattern'],
+    );
+    
+    $calls_counter = 0;
+    
+    // no params
+    $this->validator->expectAt(
+      $calls_counter++,
+      "addRule", 
+      array(
+        new EqualExpectation(
+          new lmbHandle('limb/validation/src/rule/lmbRequiredRule.class.php', array('login'))
+        )
+      )
+    );
+
+   $this->validator->expectAt(
+      $calls_counter++,
+      "addRule", 
+      array(
+        new EqualExpectation(
+          new lmbHandle('limb/validation/src/rule/lmbSizeRangeRule.class.php', array('login', 5, 8))
+        )
+      )
+    );
+    
+    $this->validator->expectAt(
+      $calls_counter++,
+      "addRule", 
+      array(
+        new EqualExpectation(
+          new lmbHandle('limb/validation/src/rule/lmbEmailRule.class.php', array('login', $errors['email']))
+        )
+      )
+    );
+        
+    $this->validator->expectAt(
+      $calls_counter++,
+      "addRule", 
+      array(
+        new EqualExpectation(
+          new lmbHandle('limb/validation/src/rule/lmbPatternRule.class.php', array('login', '/\d+/', $errors['pattern']))
+        )
+      )
+    );
+        
+    lmbValidatorBuilder :: addRules($rules, $this->validator);
+  }
 }
-
-



More information about the limb-svn mailing list