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

svn at limb-project.com svn at limb-project.com
Wed Feb 27 16:05:09 MSK 2008


Author: serega
Date: 2008-02-27 16:05:08 +0300 (Wed, 27 Feb 2008)
New Revision: 6811
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6811

Modified:
   3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php
   3.x/trunk/limb/active_record/tests/cases/lmbARTestingObjectMother.class.php
   3.x/trunk/limb/active_record/tests/cases/lmbARTransactionTest.class.php
   3.x/trunk/limb/active_record/tests/cases/lmbARValidationTest.class.php
Log:
-- internal flag $_is_being_saved is reset if validation failed

Modified: 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php
===================================================================
--- 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php	2008-02-27 12:19:01 UTC (rev 6810)
+++ 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php	2008-02-27 13:05:08 UTC (rev 6811)
@@ -950,8 +950,11 @@
         $this->_invokeListeners(self :: ON_BEFORE_UPDATE);
 
         if($need_validation && !$this->_validateUpdate())
+        {
+          $this->_is_being_saved = false;
           throw new lmbValidationException('ActiveRecord "' . get_class($this) . '" validation failed',
                                            $this->_error_list);
+        }
 
         $this->_onSave();
 
@@ -974,8 +977,11 @@
         $this->_invokeListeners(self :: ON_BEFORE_CREATE);
 
         if($need_validation && !$this->_validateInsert())
+        {
+          $this->_is_being_saved = false;
           throw new lmbValidationException('ActiveRecord "' . get_class($this) . '" validation failed',
                                            $this->_error_list);
+        }
 
         $this->_onSave();
 

Modified: 3.x/trunk/limb/active_record/tests/cases/lmbARTestingObjectMother.class.php
===================================================================
--- 3.x/trunk/limb/active_record/tests/cases/lmbARTestingObjectMother.class.php	2008-02-27 12:19:01 UTC (rev 6810)
+++ 3.x/trunk/limb/active_record/tests/cases/lmbARTestingObjectMother.class.php	2008-02-27 13:05:08 UTC (rev 6811)
@@ -4,6 +4,18 @@
   protected $_db_table_name = 'test_one_table_object';
 }
 
+class TestOneTableObjectFailing extends lmbActiveRecord
+{
+  var $fail;
+  protected $_db_table_name = 'test_one_table_object';
+
+  protected function _onAfterSave()
+  {
+    if(is_object($this->fail))
+      throw $this->fail;
+  }
+}
+
 class PersonForTest extends lmbActiveRecord
 {
   public $save_count = 0;

Modified: 3.x/trunk/limb/active_record/tests/cases/lmbARTransactionTest.class.php
===================================================================
--- 3.x/trunk/limb/active_record/tests/cases/lmbARTransactionTest.class.php	2008-02-27 12:19:01 UTC (rev 6810)
+++ 3.x/trunk/limb/active_record/tests/cases/lmbARTransactionTest.class.php	2008-02-27 13:05:08 UTC (rev 6811)
@@ -8,18 +8,6 @@
  */
 require_once(dirname(__FILE__) . '/lmbActiveRecordTest.class.php');//need TestOneTableObjectFailing
 
-class TestOneTableObjectFailing extends lmbActiveRecord
-{
-  var $fail;
-  protected $_db_table_name = 'test_one_table_object';
-
-  protected function _onAfterSave()
-  {
-    if(is_object($this->fail))
-      throw $this->fail;
-  }
-}
-
 class lmbARTransactionTest extends lmbARBaseTestCase
 {
   protected $tables_to_cleanup = array('test_one_table_object');

Modified: 3.x/trunk/limb/active_record/tests/cases/lmbARValidationTest.class.php
===================================================================
--- 3.x/trunk/limb/active_record/tests/cases/lmbARValidationTest.class.php	2008-02-27 12:19:01 UTC (rev 6810)
+++ 3.x/trunk/limb/active_record/tests/cases/lmbARValidationTest.class.php	2008-02-27 13:05:08 UTC (rev 6811)
@@ -179,13 +179,44 @@
 
     $validator->expectOnce('setErrorList', array($error_list));
     $validator->expectOnce('validate', array(new ReferenceExpectation($object)));
-    $validator->setReturnValue('validate', true);
 
     $object->save($error_list);
 
     $this->assertEqual($this->db->count('test_one_table_object'), 1);
   }
 
+  function testDoubleInsert_FirstSaveValidationError_But_SecondSaveIsOk()
+  {
+    $object = $this->_createActiveRecord();
+
+    $validator = new MockValidator();
+    $object->setInsertValidator($validator);
+
+    $object->set('annotation', $annotation = 'Super annotation');
+    $object->set('content', $content = 'Super content');
+    $object->set('news_date', $news_date = '2005-01-10');
+
+    $error_list = new MockErrorList();
+    $error_list->setReturnValueAt(0, 'isValid', false);
+    $error_list->setReturnValueAt(1, 'isValid', true);
+    
+    try
+    {
+      $object->save($error_list);
+      $this->assertTrue(false);
+    }
+    catch(lmbValidationException $e)
+    {
+      $this->assertTrue(true);
+    }
+
+    $this->assertEqual($this->db->count('test_one_table_object'), 0);
+    
+    $object->save($error_list);
+
+    $this->assertEqual($this->db->count('test_one_table_object'), 1);
+  }
+  
   function testDontUpdateOnValidationError()
   {
     $object = $this->_createActiveRecordWithDataAndSave();
@@ -236,7 +267,39 @@
     $record = $this->db->selectRecord('test_one_table_object');
     $this->assertEqual($record->get('annotation'), $annotation);
   }
+  
+  function testDoubleUpdate_FirstSaveValidationError_But_SecondSaveIsOk()
+  {
+    $object = $this->_createActiveRecordWithDataAndSave();
 
+    $validator = new MockValidator();
+    $object->setUpdateValidator($validator);
+
+    $object->set('annotation', $annotation = 'Other annotation');
+
+    $error_list = new MockErrorList();
+    $error_list->setReturnValueAt(0, 'isValid', false);
+    $error_list->setReturnValueAt(1, 'isValid', true);
+    
+    try
+    {
+      $object->save($error_list);
+      $this->assertTrue(false);
+    }
+    catch(lmbValidationException $e)
+    {
+      $this->assertTrue(true);
+    }
+
+    $record = $this->db->selectRecord('test_one_table_object');
+    $this->assertNotEqual($record->get('annotation'), $annotation);
+    
+    $object->save($error_list);
+
+    $record = $this->db->selectRecord('test_one_table_object');
+    $this->assertEqual($record->get('annotation'), $annotation);
+  }  
+
   function testSaveSkipValidation()
   {
     $object = $this->_createActiveRecordWithDataAndSave();



More information about the limb-svn mailing list