[limb-svn] r5863 - in 3.x/trunk/limb/active_record: src tests/cases

svn at limb-project.com svn at limb-project.com
Fri May 11 16:56:42 MSD 2007


Author: pachanga
Date: 2007-05-11 16:56:42 +0400 (Fri, 11 May 2007)
New Revision: 5863
URL: http://fisheye.limb-project.com/changelog/limb/?cs=5863

Modified:
   3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php
   3.x/trunk/limb/active_record/tests/cases/lmbActiveRecordTest.class.php
   3.x/trunk/limb/active_record/tests/cases/lmbActiveRecordValidationTest.class.php
Log:
-- lmbActiveRecord :: _onValidate() hook added, it's called right before validation
-- lmbActiveRecord asks error list if validation was ok, not just validator, this allows to add errors separately from validation rules in, for example, new _onValidate() hook
-- $_error_list property is setup in constructor, not lazy

Modified: 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php
===================================================================
--- 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php	2007-05-11 09:39:45 UTC (rev 5862)
+++ 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php	2007-05-11 12:56:42 UTC (rev 5863)
@@ -159,6 +159,7 @@
 
     $this->_db_table = $this->_db_meta_info->getDbTable();
     $this->_db_table_name = $this->_db_table->getTableName();
+    $this->_error_list = new lmbErrorList();
 
     if(is_int($magic_params))
       $this->loadById($magic_params);
@@ -213,10 +214,6 @@
    */
   function getErrorList()
   {
-    if($this->_error_list)
-      return $this->_error_list;
-
-    $this->_error_list = new lmbErrorList();
     return $this->_error_list;
   }
   /**
@@ -336,7 +333,7 @@
     $object = $this->_getRaw($property);
     if(is_object($object))
     {
-      $object->save($this->getErrorList());
+      $object->save($this->_error_list);
       $object_id = $object->getId();
       if($this->_getRaw($info['field']) != $object_id)
         $this->_setRaw($info['field'], $object->getId());
@@ -362,7 +359,7 @@
   {
     $collection = $this->_getRaw($property);
     if(is_object($collection))
-      $collection->save($this->getErrorList());
+      $collection->save($this->_error_list);
   }
 
   protected function _savePostRelationObject($property, $info)
@@ -371,7 +368,7 @@
     if(is_object($object))
     {
       $object->set($info['field'], $this->getId());
-      $object->save($this->getErrorList());
+      $object->save($this->_error_list);
     }
   }
 
@@ -741,7 +738,7 @@
 
       if($need_validation && !$this->_validateUpdate())
         throw new lmbValidationException('ActiveRecord "' . get_class($this) . '" validation failed',
-                                         $this->getErrorList());
+                                         $this->_error_list);
 
       $this->_onSave();
 
@@ -763,7 +760,7 @@
 
       if($need_validation && !$this->_validateInsert())
         throw new lmbValidationException('ActiveRecord "' . get_class($this) . '" validation failed',
-                                         $this->getErrorList());
+                                         $this->_error_list);
 
       $this->_onSave();
 
@@ -929,6 +926,8 @@
 
   protected function _onAfterDestroy(){}
 
+  protected function _onValidate(){}
+
   protected function _validateInsert()
   {
     return $this->_validate($this->_createInsertValidator());
@@ -941,8 +940,11 @@
 
   protected function _validate($validator)
   {
-    $validator->setErrorList($this->getErrorList());
-    return $validator->validate($this);
+    $this->_onValidate();
+
+    $validator->setErrorList($this->_error_list);
+    $validator->validate($this);
+    return $this->_error_list->isValid();
   }
 
   protected static function _isCriteria($params)

Modified: 3.x/trunk/limb/active_record/tests/cases/lmbActiveRecordTest.class.php
===================================================================
--- 3.x/trunk/limb/active_record/tests/cases/lmbActiveRecordTest.class.php	2007-05-11 09:39:45 UTC (rev 5862)
+++ 3.x/trunk/limb/active_record/tests/cases/lmbActiveRecordTest.class.php	2007-05-11 12:56:42 UTC (rev 5863)
@@ -32,6 +32,11 @@
 
 class TestOneTableObjectWithHooks extends TestOneTableObject
 {
+  protected function _onValidate()
+  {
+    echo '|on_validate|';
+  }
+
   protected function _onBeforeUpdate()
   {
     echo '|on_before_update|';
@@ -217,7 +222,7 @@
     $object->save();
     $str = ob_get_contents();
     ob_end_clean();
-    $this->assertEqual($str, '|on_before_save||on_before_create||on_save||on_create||on_after_create||on_after_save|');
+    $this->assertEqual($str, '|on_before_save||on_before_create||on_validate||on_save||on_create||on_after_create||on_after_save|');
   }
 
   function testProperOrderOfUpdateHooksCalls()
@@ -234,7 +239,7 @@
     $object->save();
     $str = ob_get_contents();
     ob_end_clean();
-    $this->assertEqual($str, '|on_before_save||on_before_update||on_save||on_update||on_after_update||on_after_save|');
+    $this->assertEqual($str, '|on_before_save||on_before_update||on_validate||on_save||on_update||on_after_update||on_after_save|');
   }
 
   function testProperOrderOfDestroyHooksCalls()

Modified: 3.x/trunk/limb/active_record/tests/cases/lmbActiveRecordValidationTest.class.php
===================================================================
--- 3.x/trunk/limb/active_record/tests/cases/lmbActiveRecordValidationTest.class.php	2007-05-11 09:39:45 UTC (rev 5862)
+++ 3.x/trunk/limb/active_record/tests/cases/lmbActiveRecordValidationTest.class.php	2007-05-11 12:56:42 UTC (rev 5863)
@@ -14,6 +14,7 @@
 lmb_require('limb/validation/src/lmbErrorList.class.php');
 
 Mock :: generate('lmbValidator', 'MockValidator');
+Mock :: generate('lmbErrorList', 'MockErrorList');
 
 class lmbActiveRecordValidationStub extends lmbActiveRecord
 {
@@ -115,7 +116,8 @@
     $object->setInsertValidator($insert_validator);
 
     $insert_validator->expectOnce('setErrorList', array($error_list));
-    $insert_validator->setReturnValue('validate', false);
+    $insert_validator->expectOnce('validate', array(new ReferenceExpectation($object)));
+    $error_list->addError('foo');//simulating validation error
 
     $this->assertFalse($object->validate($error_list));
   }
@@ -149,7 +151,8 @@
     $object->setUpdateValidator($update_validator);
 
     $update_validator->expectOnce('setErrorList', array($error_list));
-    $update_validator->setReturnValue('validate', false);
+    $update_validator->expectOnce('validate', array(new ReferenceExpectation($object)));
+    $error_list->addError('foo');//simulating validation error
 
     $this->assertFalse($object->validate($error_list));
   }
@@ -170,7 +173,7 @@
 
     $validator->expectOnce('setErrorList', array($error_list));
     $validator->expectOnce('validate', array(new ReferenceExpectation($object)));
-    $validator->setReturnValue('validate', false);
+    $error_list->addError('foo');//simulating validation error
 
     try
     {
@@ -221,7 +224,7 @@
 
     $validator->expectOnce('setErrorList', array($error_list));
     $validator->expectOnce('validate', array(new ReferenceExpectation($object)));
-    $validator->setReturnValue('validate', false);
+    $error_list->addError('foo');//simulating validation error
 
     try
     {



More information about the limb-svn mailing list