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

svn at limb-project.com svn at limb-project.com
Fri Sep 14 00:38:57 MSD 2007


Author: pachanga
Date: 2007-09-14 00:38:57 +0400 (Fri, 14 Sep 2007)
New Revision: 6297
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6297

Modified:
   3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php
   3.x/trunk/limb/active_record/tests/cases/lmbARImportTest.class.php
Log:
-- lmbActiveRecord :: _onAfterImport() hook added, it allows to customize imported stuff
-- lmbActiveRecord :: import() experiments: a) it returns import success status b) it catches all exceptions and puts them into error list which c) can be passed into this method as a third optional argument


Modified: 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php
===================================================================
--- 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php	2007-09-13 10:25:36 UTC (rev 6296)
+++ 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php	2007-09-13 20:38:57 UTC (rev 6297)
@@ -262,6 +262,11 @@
     return $this->_error_list;
   }
 
+  function setErrorList($error_list)
+  {
+    $this->_error_list = $error_list;
+  }
+
   protected function _defineRelations(){}
 
   protected function _hasOne($relation_name, $info)
@@ -1060,6 +1065,8 @@
 
   protected function _onValidate(){}
 
+  protected function _onAfterImport(){}
+
   protected function _validateInsert()
   {
     return $this->_validate($this->_createInsertValidator());
@@ -1524,6 +1531,14 @@
     $this->_is_being_destroyed = false;
   }
 
+  function remove($name)
+  {
+    parent :: remove($name);
+
+    if(isset($this->raw_value_objects[$name]))
+      unset($this->raw_value_objects[$name]);
+  }
+
   protected function _deleteDbRecord()
   {
     $this->_db_table->deleteById($this->getId());
@@ -1677,7 +1692,7 @@
    *  </code>
    *  @param array|object
    */
-  function import($source)
+  function import($source, $error_list = null)
   {
     if(is_object($source))
     {
@@ -1685,29 +1700,42 @@
       {
         $this->importRaw($source->exportRaw());
         $this->setIsNew($source->isNew());
+        return true;//is this assumption correct?
       }
       else
-        $this->import($source->export());
-      return;
+        return $this->import($source->export(), $error_list);
     }
 
-    foreach($source as $property => $value)
+    if($error_list)
+      $this->_error_list = $error_list;
+
+    try
     {
-      if(isset($this->_composed_of[$property]))
-        $this->_importValueObject($property, $value);
-      elseif(isset($this->_has_many[$property]))
-        $this->_importCollection($property, $value, $this->_has_many[$property]['class']);
-      elseif(isset($this->_has_many_to_many[$property]))
-        $this->_importCollection($property, $value, $this->_has_many_to_many[$property]['class']);
-      elseif(isset($this->_belongs_to[$property]))
-        $this->_importEntity($property, $value, $this->_belongs_to[$property]['class']);
-      elseif(isset($this->_many_belongs_to[$property]))
-        $this->_importEntity($property, $value, $this->_many_belongs_to[$property]['class']);
-      elseif(isset($this->_has_one[$property]))
-        $this->_importEntity($property, $value, $this->_has_one[$property]['class']);
-      elseif($this->_canImportProperty($property))
-        $this->set($property, $value);
+      foreach($source as $property => $value)
+      {
+        if(isset($this->_composed_of[$property]))
+          $this->_importValueObject($property, $value);
+        elseif(isset($this->_has_many[$property]))
+          $this->_importCollection($property, $value, $this->_has_many[$property]['class']);
+        elseif(isset($this->_has_many_to_many[$property]))
+          $this->_importCollection($property, $value, $this->_has_many_to_many[$property]['class']);
+        elseif(isset($this->_belongs_to[$property]))
+          $this->_importEntity($property, $value, $this->_belongs_to[$property]['class']);
+        elseif(isset($this->_many_belongs_to[$property]))
+          $this->_importEntity($property, $value, $this->_many_belongs_to[$property]['class']);
+        elseif(isset($this->_has_one[$property]))
+          $this->_importEntity($property, $value, $this->_has_one[$property]['class']);
+        elseif($this->_canImportProperty($property))
+          $this->set($property, $value);
+      }
+      $this->_onAfterImport();
     }
+    catch(Exception $e)
+    {
+      $this->_error_list->addError($e->getMessage());
+    }
+
+    return $this->_error_list->isValid();
   }
   /**
    *  Plain import of data into object
@@ -1762,7 +1790,10 @@
   protected function _importValueObject($property, $obj)
   {
     if(!is_object($obj))
-      $this->set($property, $this->_createValueObject($this->_composed_of[$property]['class'], $obj));
+    {
+      $class = $this->_composed_of[$property]['class'];
+      $this->set($property, $this->_createValueObject($class, $obj));
+    }
     else
       $this->set($property, $obj);
   }

Modified: 3.x/trunk/limb/active_record/tests/cases/lmbARImportTest.class.php
===================================================================
--- 3.x/trunk/limb/active_record/tests/cases/lmbARImportTest.class.php	2007-09-13 10:25:36 UTC (rev 6296)
+++ 3.x/trunk/limb/active_record/tests/cases/lmbARImportTest.class.php	2007-09-13 20:38:57 UTC (rev 6297)
@@ -16,6 +16,27 @@
 require_once(dirname(__FILE__) . '/lmbARValueObjectTest.class.php');
 require_once(dirname(__FILE__) . '/lmbARAttributesLazyLoadingTest.class.php');
 
+class LessonForTestWithCustomImporter extends lmbActiveRecord
+{
+  protected $_db_table_name = 'lesson_for_test';
+  protected $_composed_of = array('date_start' => array('field' => 'date_start',
+                                                        'class' => 'TestingValueObject',
+                                                        'getter' => 'getValue'),
+                                  'date_end' => array('field' => 'date_end',
+                                                      'class' => 'TestingValueObject',
+                                                      'getter' => 'getValue'));
+  public $echo_on_after_import;
+  public $raise;
+
+  function _onAfterImport()
+  {
+    if($this->echo_on_after_import)
+      echo $this->echo_on_after_import;
+    if($this->raise)
+      throw new Exception($this->raise);
+  }
+}
+
 class lmbARImportTest extends UnitTestCase
 {
   protected $db;
@@ -46,7 +67,8 @@
   function testImportingObjectCallsItsExportMethod()
   {
     $object = new TestOneTableObject();
-    $object->import(new lmbSet(array('annotation' => 'Some annotation')));
+    $res = $object->import(new lmbSet(array('annotation' => 'Some annotation')));
+    $this->assertTrue($res);
     $this->assertEqual($object->getAnnotation(), 'Some annotation');
   }
 
@@ -57,7 +79,8 @@
     $object1->setAnnotation($annotation = 'Some annotation');
 
     $object2 = new TestOneTableObject();
-    $object2->import($object1);
+    $res = $object2->import($object1);
+    $this->assertTrue($res);
     $this->assertEqual($object2->getId(), $object1->getId());
     $this->assertEqual($object2->getAnnotation(), $annotation);
     $this->assertTrue($object2->isNew());
@@ -71,7 +94,8 @@
     $object1->save();
 
     $object2 = new TestOneTableObject();
-    $object2->import($object1);
+    $res = $object2->import($object1);
+    $this->assertTrue($res);
     $this->assertEqual($object2->getId(), $object1->getId());
     $this->assertEqual($object2->getAnnotation(), $annotation);
     $this->assertFalse($object2->isNew());
@@ -101,7 +125,8 @@
     $object1 = new LazyTestOneTableObject();
     $object2 = new LazyTestOneTableObject($object->getId());
 
-    $object1->import($object2);
+    $res = $object1->import($object2);
+    $this->assertTrue($res);
     $this->assertFalse($object1->hasAttribute('annotation'));
     $this->assertEqual($object1->getAnnotation(), $annotation);
     $this->assertFalse($object1->hasAttribute('content'));
@@ -118,7 +143,8 @@
                     'content' => 'Some content',
                     );
 
-    $object->import($source);
+    $res = $object->import($source);
+    $this->assertTrue($res);
     $this->assertEqual($object->getId(), 1000);
     $this->assertEqual($object->getAnnotation(), 'Some annotation');
     $this->assertEqual($object->getContent(), 'Some content');
@@ -137,7 +163,8 @@
                     'content' => 'Some content',
                     );
 
-    $object->import($source);
+    $res = $object->import($source);
+    $this->assertTrue($res);
     $this->assertEqual($object->getId(), $id);
     $this->assertNotEqual($object->getId(), 1000);// just one extra check
     $this->assertEqual($object->getAnnotation(), 'Some annotation');
@@ -176,7 +203,8 @@
                     'lectures' => array($l1->getId(), $l2->getId()));
 
     $course2 = new CourseForTest();
-    $course2->import($source);
+    $res = $course2->import($source);
+    $this->assertTrue($res);
     $this->assertEqual($course2->getTitle(), $course->getTitle());
     $this->assertEqual($course2->getLectures()->count(), 2);
     $this->assertEqual($course2->getLectures()->at(0)->getTitle(), $l1->getTitle());
@@ -202,7 +230,8 @@
                     'lectures' => array($l1->getId(), $l2));
 
     $course2 = new CourseForTest();
-    $course2->import($source);
+    $res = $course2->import($source);
+    $this->assertTrue($res);
     $this->assertEqual($course2->getTitle(), $course->getTitle());
     $this->assertEqual($course2->getLectures()->count(), 2);
     $this->assertEqual($course2->getLectures()->at(0)->getTitle(), $l1->getTitle());
@@ -229,7 +258,8 @@
 
     $course2 = new CourseForTest($course->getId());
 
-    $course2->import($source);
+    $res = $course2->import($source);
+    $this->assertTrue($res);
     $this->assertEqual($course2->getTitle(), $course->getTitle());
     $this->assertEqual($course2->getLectures()->count(), 1);
     $this->assertEqual($course2->getLectures()->at(0)->getTitle(), $l2->getTitle());
@@ -254,7 +284,8 @@
                     'users' => array($u2->getId()));
 
     $group2 = new GroupForTest($group->getId());
-    $group2->import($source);
+    $res = $group2->import($source);
+    $this->assertTrue($res);
     $this->assertEqual($group2->getTitle(), $group->getTitle());
     $this->assertEqual($group2->getUsers()->count(), 1);
     $this->assertEqual($group2->getUsers()->at(0)->getFirstName(), $u2->getFirstName());
@@ -275,7 +306,8 @@
                     'course' => $course->getId());
 
     $l2 = new LectureForTest();
-    $l2->import($source);
+    $res = $l2->import($source);
+    $this->assertTrue($res);
 
     $this->assertEqual($l2->getTitle(), $l->getTitle());
     $this->assertEqual($l2->getCourse()->getTitle(), $course->getTitle());
@@ -296,7 +328,8 @@
                     'course' => $course);
 
     $l2 = new LectureForTest();
-    $l2->import($source);
+    $res = $l2->import($source);
+    $this->assertTrue($res);
     $this->assertEqual($l2->getTitle(), $l->getTitle());
     $this->assertEqual($l2->getCourse()->getTitle(), $course->getTitle());
   }
@@ -319,7 +352,8 @@
                     'groups' => array($g1->getId(), $g2->getId()));
 
     $user2 = new UserForTest();
-    $user2->import($source);
+    $res = $user2->import($source);
+    $this->assertTrue($res);
     $this->assertEqual($user2->getFirstName(), $user1->getFirstName());
     $this->assertEqual($user2->getGroups()->count(), 2);
     $this->assertEqual($user2->getGroups()->at(0)->getTitle(), $g1->getTitle());
@@ -344,7 +378,8 @@
                     'groups' => array($g1->getId(), $g2));
 
     $user2 = new UserForTest();
-    $user2->import($source);
+    $res = $user2->import($source);
+    $this->assertTrue($res);
     $this->assertEqual($user2->getFirstName(), $user1->getFirstName());
     $this->assertEqual($user2->getGroups()->count(), 2);
     $this->assertEqual($user2->getGroups()->at(0)->getTitle(), $g1->getTitle());
@@ -364,7 +399,8 @@
                     'person' => $person->getId());
 
     $number2 = new SocialSecurityForTest();
-    $number2->import($source);
+    $res = $number2->import($source);
+    $this->assertTrue($res);
     $this->assertEqual($number2->getCode(), $number->getCode());
     $this->assertEqual($number2->getPerson()->getName(), $person->getName());
   }
@@ -382,7 +418,8 @@
                     'person' => $person);
 
     $number2 = new SocialSecurityForTest();
-    $number2->import($source);
+    $res = $number2->import($source);
+    $this->assertTrue($res);
     $this->assertEqual($number2->getCode(), $number->getCode());
     $this->assertEqual($number2->getPerson()->getName(), $person->getName());
   }
@@ -400,7 +437,8 @@
                     'social_security' => $number->getId());
 
     $person2 = new PersonForTest();
-    $person2->import($source);
+    $res = $person2->import($source);
+    $this->assertTrue($res);
     $this->assertEqual($person2->getName(), $person->getName());
     $this->assertEqual($person2->getSocialSecurity()->getCode(), $number->getCode());
   }
@@ -418,7 +456,8 @@
                     'social_security' => $number);
 
     $person2 = new PersonForTest();
-    $person2->import($source);
+    $res = $person2->import($source);
+    $this->assertTrue($res);
     $this->assertEqual($person2->getName(), $person->getName());
     $this->assertEqual($person2->getSocialSecurity()->getCode(), $number->getCode());
   }
@@ -436,7 +475,8 @@
                     'social_security' => null);
 
     $person2 = clone $person;
-    $person2->import($source);
+    $res = $person2->import($source);
+    $this->assertTrue($res);
     $this->assertEqual($person2->getName(), $person->getName());
     $this->assertNull($person2->getSocialSecurity());
   }
@@ -454,7 +494,8 @@
                     'social_security' => 'null');
 
     $person2 = clone $person;
-    $person2->import($source);
+    $res = $person2->import($source);
+    $this->assertTrue($res);
     $this->assertEqual($person2->getName(), $person->getName());
     $this->assertNull($person2->getSocialSecurity());
   }
@@ -472,7 +513,8 @@
                     'social_security' => '');
 
     $person2 = clone $person;
-    $person2->import($source);
+    $res = $person2->import($source);
+    $this->assertTrue($res);
     $this->assertEqual($person2->getName(), $person->getName());
     $this->assertNull($person2->getSocialSecurity());
   }
@@ -481,12 +523,41 @@
   {
     $lesson = new LessonForTest();
 
-    $lesson->import(array('date_start' => $v1 = time() - 100,
+    $res = $lesson->import(array('date_start' => $v1 = time() - 100,
                           'date_end' => $v2 = time() + 100));
+    $this->assertTrue($res);
 
     $this->assertEqual($lesson->getDateStart()->getValue(), $v1);
     $this->assertEqual($lesson->getDateEnd()->getValue(), $v2);
   }
+
+  function testOnAfterImport()
+  {
+    $lesson = new LessonForTestWithCustomImporter();
+    $lesson->echo_on_after_import = 'Halo!';
+
+    ob_start();
+    $lesson->import(array('date_start' => 100));
+    $what = ob_get_contents();
+    ob_end_clean();
+
+    $this->assertEqual($what, 'Halo!');
+  }
+
+  function testImportWritesExceptionToErrorList()
+  {
+    $lesson = new LessonForTestWithCustomImporter();
+    $lesson->raise = 'Booo';
+
+    $res = $lesson->import(array('date_start' => 100));
+    $this->assertFalse($res);
+
+    $error_list = $lesson->getErrorList();
+    $this->assertFalse($error_list->isValid());
+    
+    $this->assertEqual($error_list[0]->getMessage(), 'Booo');
+    $this->assertEqual(sizeof($error_list), 1);
+  }
 }
 
 



More information about the limb-svn mailing list