[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/active_record/tests/cases/ -> lmbActiveRecordTest.class.php (source)

   1  <?php
   2  /*

   3   * Limb PHP Framework

   4   *

   5   * @link http://limb-project.com

   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)

   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html

   8   */
   9  lmb_require('limb/active_record/src/lmbActiveRecord.class.php');
  10  lmb_require('limb/dbal/src/lmbSimpleDb.class.php');
  11  lmb_require('limb/dbal/src/lmbTableGateway.class.php');
  12  
  13  class TestOneTableObject extends lmbActiveRecord
  14  {
  15    protected $_db_table_name = 'test_one_table_object';
  16    protected $dummy;
  17  }
  18  
  19  class TestOneTableObjectFailing extends lmbActiveRecord
  20  {
  21    var $fail;
  22    protected $_db_table_name = 'test_one_table_object';
  23  
  24    protected function _onAfterSave()
  25    {
  26      if(is_object($this->fail))
  27        throw $this->fail;
  28    }
  29  }
  30  
  31  class TestOneTableObjectWithCustomDestroy extends lmbActiveRecord
  32  {
  33    protected $_db_table_name = 'test_one_table_object';
  34  
  35    function destroy()
  36    {
  37      parent :: destroy();
  38      echo "destroyed!";
  39    }
  40  }
  41  
  42  class TestOneTableObjectWithHooks extends TestOneTableObject
  43  {
  44    protected function _onValidate()
  45    {
  46      echo '|on_validate|';
  47    }
  48  
  49    protected function _onBeforeUpdate()
  50    {
  51      echo '|on_before_update|';
  52    }
  53  
  54    protected function _onBeforeCreate()
  55    {
  56      echo '|on_before_create|';
  57    }
  58  
  59    protected function _onBeforeSave()
  60    {
  61      echo '|on_before_save|';
  62    }
  63  
  64    protected function _onAfterSave()
  65    {
  66      echo '|on_after_save|';
  67    }
  68  
  69    protected function _onSave()
  70    {
  71      echo '|on_save|';
  72    }
  73  
  74    protected function _onUpdate()
  75    {
  76      echo '|on_update|';
  77    }
  78  
  79    protected function _onCreate()
  80    {
  81      echo '|on_create|';
  82    }
  83  
  84    protected function _onAfterUpdate()
  85    {
  86      echo '|on_after_update|';
  87    }
  88  
  89    protected function _onAfterCreate()
  90    {
  91      echo '|on_after_create|';
  92    }
  93  
  94    protected function _onBeforeDestroy()
  95    {
  96      echo '|on_before_destroy|';
  97    }
  98  
  99    protected function _onAfterDestroy()
 100    {
 101      echo '|on_after_destroy|';
 102    }
 103  }
 104  
 105  class TestOneTableObjectWithSortParams extends TestOneTableObject
 106  {
 107    protected $_default_sort_params = array('id' => 'DESC');
 108  }
 109  
 110  class lmbActiveRecordTest extends UnitTestCase
 111  {
 112    var $conn;
 113    var $db;
 114    var $class_name = 'TestOneTableObject';
 115  
 116    function setUp()
 117    {
 118      $toolkit = lmbToolkit :: save();
 119      $this->conn = $toolkit->getDefaultDbConnection();
 120      $this->db = new lmbSimpleDb($this->conn);
 121  
 122      $this->_cleanUp();
 123    }
 124  
 125    function tearDown()
 126    {
 127      $this->_cleanUp();
 128  
 129      lmbToolkit :: restore();
 130    }
 131  
 132    function _cleanUp()
 133    {
 134      $this->db->delete('test_one_table_object');
 135    }
 136  
 137    function testSaveNewRecord()
 138    {
 139      $object = new TestOneTableObject();
 140      $object->set('annotation', $annotation = 'Super annotation');
 141      $object->set('content', $content = 'Super content');
 142      $object->set('news_date', $news_date = '2005-01-10');
 143  
 144      $this->assertTrue($object->isNew());
 145  
 146      $id = $object->save();
 147  
 148      $this->assertFalse($object->isNew());
 149      $this->assertNotNull($object->getId());
 150      $this->assertEqual($object->getId(), $id);
 151  
 152      $this->assertEqual($this->db->count('test_one_table_object'), 1);
 153  
 154      $record = $this->db->getFirstRecordFrom('test_one_table_object');
 155      $this->assertEqual($record->get('id'), $id);
 156      $this->assertEqual($record->get('annotation'), $annotation);
 157      $this->assertEqual($record->get('content'), $content);
 158      $this->assertEqual($record->get('news_date'), $news_date);
 159      $this->assertEqual($record->get('id'), $object->getId());
 160    }
 161  
 162    function testDontCreateNewRecordTwice()
 163    {
 164      $object = $this->_initActiveRecordWithData(new TestOneTableObject());
 165  
 166      $object->save();
 167      $object->save();
 168  
 169      $this->assertTrue($object->getId());
 170  
 171      $this->assertEqual($this->db->count('test_one_table_object'), 1);
 172    }
 173  
 174    function testIsNew()
 175    {
 176      $object = $this->_initActiveRecordWithData(new TestOneTableObject());
 177      $this->assertTrue($object->isNew());
 178  
 179      $object->save();
 180      $this->assertFalse($object->isNew());
 181  
 182      $object->setIsNew();
 183  
 184      $this->assertTrue($object->isNew());
 185    }
 186  
 187    function testDetach()
 188    {
 189      $object = $this->_initActiveRecordWithData(new TestOneTableObject());
 190      $this->assertTrue($object->isNew());
 191  
 192      $object->save();
 193      $this->assertFalse($object->isNew());
 194      $this->assertNotNull($object->getId());
 195  
 196      $object->detach();
 197  
 198      $this->assertTrue($object->isNew());
 199      $this->assertNull($object->getId());
 200  
 201      $object->save();
 202  
 203      $this->assertEqual($this->db->count('test_one_table_object'), 2);
 204    }
 205  
 206    function testSaveUpdate()
 207    {
 208      $object = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
 209  
 210      $object->set('annotation', $annotation = 'Other annotation');
 211      $object->set('content', $content = 'Other content');
 212      $object->set('news_date', $news_date = '2005-10-20');
 213      $object->save();
 214  
 215      $this->assertEqual($this->db->count('test_one_table_object'), 1);
 216  
 217      $record = $this->db->getFirstRecordFrom('test_one_table_object');
 218  
 219      $this->assertEqual($record->get('annotation'), $annotation);
 220      $this->assertEqual($record->get('content'), $content);
 221      $this->assertEqual($record->get('news_date'), $news_date);
 222      $this->assertEqual($record->get('id'), $object->getId());
 223    }
 224  
 225    function testProperOrderOfCreateHooksCalls()
 226    {
 227      $object = new TestOneTableObjectWithHooks();
 228      $object->setContent('whatever');
 229  
 230      ob_start();
 231      $object->save();
 232      $str = ob_get_contents();
 233      ob_end_clean();
 234      $this->assertEqual($str, '|on_before_save||on_before_create||on_validate||on_save||on_create||on_after_create||on_after_save|');
 235    }
 236  
 237    function testProperOrderOfUpdateHooksCalls()
 238    {
 239      $object = new TestOneTableObjectWithHooks();
 240      $object->setContent('whatever');
 241      ob_start();
 242      $object->save();
 243      ob_end_clean();
 244  
 245      $object->setContent('other content');
 246  
 247      ob_start();
 248      $object->save();
 249      $str = ob_get_contents();
 250      ob_end_clean();
 251      $this->assertEqual($str, '|on_before_save||on_before_update||on_validate||on_save||on_update||on_after_update||on_after_save|');
 252    }
 253  
 254    function testProperOrderOfDestroyHooksCalls()
 255    {
 256      $object = new TestOneTableObjectWithHooks();
 257      $object->setContent('whatever');
 258      ob_start();
 259      $object->save();
 260      ob_clean();
 261  
 262      $object->destroy();
 263      $str = ob_get_contents();
 264      ob_end_clean();
 265      $this->assertEqual($str, '|on_before_destroy||on_after_destroy|');
 266    }
 267  
 268    function testFindById()
 269    {
 270      $object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
 271      $object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
 272  
 273      $object3 = lmbActiveRecord :: findById($this->class_name, $object2->getId());
 274      $this->assertEqual($object3->export(), $object2->export());
 275    }
 276  
 277    function testFindByIdThrowsExceptionIfNotFound()
 278    {
 279      try
 280      {
 281        lmbActiveRecord :: findById($this->class_name, -1000);
 282        $this->assertTrue(false);
 283      }
 284      catch(lmbARException $e){}
 285    }
 286  
 287    function testFindByIdReturnsNullIfNotFound()
 288    {
 289      $this->assertNull(lmbActiveRecord :: findById($this->class_name, -1000, false));
 290    }
 291  
 292    function testLoadById()
 293    {
 294      $object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
 295      $object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
 296  
 297      $object3 = new TestOneTableObject();
 298      $object3->loadById($object2->getId());
 299      $this->assertEqual($object3->export(), $object2->export());
 300      $this->assertFalse($object3->isNew());
 301    }
 302  
 303    function testPassingIntToConstructorLoadsObject()
 304    {
 305      $object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
 306  
 307      $object2 = new TestOneTableObject($object1->getId());
 308      $this->assertEqual($object2->export(), $object1->export());
 309      $this->assertFalse($object2->isNew());
 310    }
 311  
 312    function tesFindFirst()
 313    {
 314      $object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
 315      $object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
 316  
 317      $object3 = lmbActiveRecord :: findFirst($this->class_name, array('criteria' => 'id=' . $object1->getId()));
 318  
 319      $this->assertFalse($object2->isNew());
 320      $this->assertEqual($object3->get('annotation'), $object1->get('annotation'));
 321      $this->assertEqual($object3->get('content'), $object1->get('content'));
 322      $this->assertEqual($object3->get('news_date'), $object1->get('news_date'));
 323      $this->assertEqual($object3->get('id'), $object1->getId());
 324    }
 325  
 326    function tesFindFirstConvertStringToCriteria()
 327    {
 328      $object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
 329      $object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
 330  
 331      $object3 = lmbActiveRecord :: findFirst($this->class_name, 'id=' . $object1->getId());
 332  
 333      $this->assertFalse($object2->isNew());
 334      $this->assertEqual($object3->get('annotation'), $object1->get('annotation'));
 335      $this->assertEqual($object3->get('content'), $object1->get('content'));
 336      $this->assertEqual($object3->get('news_date'), $object1->get('news_date'));
 337      $this->assertEqual($object3->get('id'), $object1->getId());
 338    }
 339  
 340    function tesFindFirstConvertObjectToCriteria()
 341    {
 342      $object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
 343      $object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
 344  
 345      $object3 = lmbActiveRecord :: findFirst($this->class_name, new lmbSQLRawCriteria('id=' . $object1->getId()));
 346  
 347      $this->assertFalse($object2->isNew());
 348      $this->assertEqual($object3->get('annotation'), $object1->get('annotation'));
 349      $this->assertEqual($object3->get('content'), $object1->get('content'));
 350      $this->assertEqual($object3->get('news_date'), $object1->get('news_date'));
 351      $this->assertEqual($object3->get('id'), $object1->getId());
 352    }
 353  
 354    function tesFindFirstConvertArrayToCriteria()
 355    {
 356      $object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
 357      $object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
 358  
 359      $object3 = lmbActiveRecord :: findFirst($this->class_name, array('id=?', $object1->getId()));
 360  
 361      $this->assertFalse($object2->isNew());
 362      $this->assertEqual($object3->get('annotation'), $object1->get('annotation'));
 363      $this->assertEqual($object3->get('content'), $object1->get('content'));
 364      $this->assertEqual($object3->get('news_date'), $object1->get('news_date'));
 365      $this->assertEqual($object3->get('id'), $object1->getId());
 366    }
 367  
 368    function testFindFirstWithSortParams()
 369    {
 370      $object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
 371      $object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
 372  
 373      $object3 = lmbActiveRecord :: findFirst($this->class_name, array('sort' => array('id' => 'DESC')));
 374  
 375      $this->assertEqual($object3->get('id'), $object2->getId());
 376    }
 377  
 378    function testFindFirstWithDefaultSortParams()
 379    {
 380      $object1 = new TestOneTableObjectWithSortParams();
 381      $object1->setContent('Content'.mt_rand());
 382      $object1->save();
 383  
 384      $object2 = new TestOneTableObjectWithSortParams();
 385      $object2->setContent('Content'.mt_rand());
 386      $object2->save();
 387  
 388      $object3 = lmbActiveRecord :: findFirst('TestOneTableObjectWithSortParams');
 389      $this->assertEqual($object3->get('id'), $object2->getId());
 390    }
 391  
 392    function testFindOneAlias()
 393    {
 394      $object1 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
 395      $object2 = $this->_initActiveRecordWithDataAndSave(new TestOneTableObject());
 396  
 397      $object3 = lmbActiveRecord :: findOne($this->class_name, 'id=' . $object1->getId());
 398  
 399      $this->assertFalse($object2->isNew());
 400      $this->assertEqual($object3->get('annotation'), $object1->