[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/active_record/tests/cases/ -> lmbARValidationTest.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/validation/src/lmbValidator.class.php');
  11  lmb_require('limb/validation/src/lmbErrorList.class.php');
  12  require_once(dirname(__FILE__) . '/lmbActiveRecordTest.class.php');//need TestOneTableObjectFailing

  13  
  14  Mock :: generate('lmbValidator', 'MockValidator');
  15  Mock :: generate('lmbErrorList', 'MockErrorList');
  16  
  17  class lmbActiveRecordValidationStub extends lmbActiveRecord
  18  {
  19    protected $_db_table_name = 'test_one_table_object';
  20    protected $_insert_validator;
  21    protected $_update_validator;
  22  
  23    function setInsertValidator($validator)
  24    {
  25      $this->_insert_validator = $validator;
  26    }
  27  
  28    function setUpdateValidator($validator)
  29    {
  30      $this->_update_validator = $validator;
  31    }
  32  
  33    protected function _createInsertValidator()
  34    {
  35      return is_object($this->_insert_validator) ? $this->_insert_validator : new lmbValidator();
  36    }
  37  
  38    protected function _createUpdateValidator()
  39    {
  40      return is_object($this->_update_validator) ? $this->_update_validator : new lmbValidator();
  41    }
  42  }
  43  
  44  class lmbARValidationTest extends UnitTestCase
  45  {
  46    protected $db = null;
  47  
  48    function setUp()
  49    {
  50      $toolkit = lmbToolkit :: save();
  51      $this->db = new lmbSimpleDb($toolkit->getDefaultDbConnection());
  52  
  53      $this->_cleanUp();
  54    }
  55  
  56    function tearDown()
  57    {
  58      $this->_cleanUp();
  59  
  60      lmbToolkit :: restore();
  61    }
  62  
  63    function _cleanUp()
  64    {
  65      $this->db->delete('test_one_table_object');
  66    }
  67  
  68    function testGetErrorListReturnDefaultErrorList()
  69    {
  70      $object = $this->_createActiveRecord();
  71      $this->assertIsA($object->getErrorList(), 'lmbErrorList');
  72    }
  73  
  74    function testValidateNew()
  75    {
  76      $error_list = new lmbErrorList();
  77      $insert_validator = new MockValidator();
  78      $update_validator = new MockValidator();
  79  
  80      $object = $this->_createActiveRecord();
  81      $object->setInsertValidator($insert_validator);
  82      $object->setUpdateValidator($update_validator);
  83  
  84      $object->set('annotation', 'blah-blah');
  85  
  86      $insert_validator->expectOnce('setErrorList', array($error_list));
  87      $insert_validator->expectOnce('validate', array(new ReferenceExpectation($object)));
  88      $insert_validator->setReturnValue('validate', true);
  89  
  90      $update_validator->expectNever('setErrorList');
  91      $update_validator->expectNever('validate');
  92  
  93      $this->assertTrue($object->validate($error_list));
  94    }
  95  
  96    function testGetErrorListReturnLastErrorListUsed()
  97    {
  98      $error_list = new lmbErrorList();
  99      $insert_validator = new MockValidator();
 100      $object = $this->_createActiveRecord();
 101      $object->setInsertValidator($insert_validator);
 102      $insert_validator->setReturnValue('validate', true);
 103      $object->validate($error_list);
 104  
 105      $this->assertReference($object->getErrorList(), $error_list);
 106    }
 107  
 108    function testValidateNewFailed()
 109    {
 110      $error_list = new lmbErrorList();
 111      $insert_validator = new MockValidator();
 112  
 113      $object = $this->_createActiveRecord();
 114      $object->setInsertValidator($insert_validator);
 115  
 116      $insert_validator->expectOnce('setErrorList', array($error_list));
 117      $insert_validator->expectOnce('validate', array(new ReferenceExpectation($object)));
 118      $error_list->addError('foo');//simulating validation error

 119  
 120      $this->assertFalse($object->validate($error_list));
 121    }
 122  
 123    function testValidateExisting()
 124    {
 125      $error_list = new lmbErrorList();
 126      $insert_validator = new MockValidator();
 127      $update_validator = new MockValidator();
 128  
 129      $object = $this->_createActiveRecordWithDataAndSave();
 130      $object->setInsertValidator($insert_validator);
 131      $object->setUpdateValidator($update_validator);
 132  
 133      $update_validator->expectOnce('setErrorList', array($error_list));
 134      $update_validator->expectOnce('validate', array(new ReferenceExpectation($object)));
 135      $update_validator->setReturnValue('validate', true);
 136  
 137      $insert_validator->expectNever('setErrorList');
 138      $insert_validator->expectNever('validate');
 139  
 140      $this->assertTrue($object->validate($error_list));
 141    }
 142  
 143    function testValidateExistingFailed()
 144    {
 145      $error_list = new lmbErrorList();
 146      $update_validator = new MockValidator();
 147  
 148      $object = $this->_createActiveRecordWithDataAndSave();
 149      $object->setUpdateValidator($update_validator);
 150  
 151      $update_validator->expectOnce('setErrorList', array($error_list));
 152      $update_validator->expectOnce('validate', array(new ReferenceExpectation($object)));
 153      $error_list->addError('foo');//simulating validation error

 154  
 155      $this->assertFalse($object->validate($error_list));
 156    }
 157  
 158    function testDontInsertOnValidationError()
 159    {
 160      $object = $this->_createActiveRecord();
 161  
 162      $error_list = new lmbErrorList();
 163  
 164      $validator = new MockValidator();
 165  
 166      $object->setInsertValidator($validator);
 167  
 168      $object->set('annotation', $annotation = 'Super annotation');
 169      $object->set('content', $content = 'Super content');
 170      $object->set('news_date', $news_date = '2005-01-10');
 171  
 172      $validator->expectOnce('setErrorList', array($error_list));
 173      $validator->expectOnce('validate', array(new ReferenceExpectation($object)));
 174      $error_list->addError('foo');//simulating validation error

 175  
 176      try
 177      {
 178        $object->save($error_list);
 179        $this->assertTrue(false);
 180      }
 181      catch(lmbValidationException $e)
 182      {
 183        $this->assertEqual($e->getErrorList()->export(), $error_list->getReadable()->export());
 184      }
 185  
 186      $this->assertEqual($this->db->count('test_one_table_object'), 0);
 187    }
 188  
 189    function testInsertOnValidationSuccess()
 190    {
 191      $object = $this->_createActiveRecord();
 192  
 193      $error_list = new lmbErrorList();
 194  
 195      $validator = new MockValidator();
 196      $object->setInsertValidator($validator);
 197  
 198      $object->set('annotation', $annotation = 'Super annotation');
 199      $object->set('content', $content = 'Super content');
 200      $object->set('news_date', $news_date = '2005-01-10');
 201  
 202      $validator->expectOnce('setErrorList', array($error_list));
 203      $validator->expectOnce('validate', array(new ReferenceExpectation($object)));
 204      $validator->setReturnValue('validate', true);
 205  
 206      $object->save($error_list);
 207  
 208      $this->assertEqual($this->db->count('test_one_table_object'), 1);
 209    }
 210  
 211    function testDontUpdateOnValidationError()
 212    {
 213      $object = $this->_createActiveRecordWithDataAndSave();
 214      $old_annotation = $object->get('annotation');
 215  
 216      $error_list = new lmbErrorList();
 217  
 218      $validator = new MockValidator();
 219      $object->setUpdateValidator($validator);
 220  
 221      $object->set('annotation', $annotation = 'New annotation ' . time());
 222  
 223      $validator->expectOnce('setErrorList', array($error_list));
 224      $validator->expectOnce('validate', array(new ReferenceExpectation($object)));
 225      $error_list->addError('foo');//simulating validation error

 226  
 227      try
 228      {
 229        $object->save($error_list);
 230        $this->assertTrue(false);
 231      }
 232      catch(lmbValidationException $e)
 233      {
 234        $this->assertEqual($e->getErrorList()->export(), $error_list->getReadable()->export());
 235      }
 236  
 237      $record = $this->db->getFirstRecordFrom('test_one_table_object');
 238      $this->assertEqual($record->get('annotation'), $old_annotation);
 239    }
 240  
 241    function testUpdateOnValidationSuccess()
 242    {
 243      $object = $this->_createActiveRecordWithDataAndSave();
 244  
 245      $error_list = new lmbErrorList();
 246  
 247      $validator = new MockValidator();
 248      $object->setUpdateValidator($validator);
 249  
 250      $object->set('annotation', $annotation = 'New annotation ' . time());
 251  
 252      $validator->expectOnce('setErrorList', array($error_list));
 253      $validator->expectOnce('validate', array(new ReferenceExpectation($object)));
 254      $validator->setReturnValue('validate', true);
 255  
 256      $object->save($error_list);
 257  
 258      $record = $this->db->getFirstRecordFrom('test_one_table_object');
 259      $this->assertEqual($record->get('annotation'), $annotation);
 260    }
 261  
 262    function testSaveSkipValidation()
 263    {
 264      $object = $this->_createActiveRecordWithDataAndSave();
 265  
 266      $validator = new MockValidator();
 267      $object->setUpdateValidator($validator);
 268  
 269      $object->set('annotation', $annotation = 'New annotation ' . time());
 270  
 271      $validator->expectNever('validate');
 272  
 273      $object->saveSkipValidation();
 274  
 275      $record = $this->db->getFirstRecordFrom('test_one_table_object');
 276      $this->assertEqual($record->get('annotation'), $annotation);
 277    }
 278  
 279    function testIsValid()
 280    {
 281      $object = $this->_createActiveRecordWithDataAndSave();
 282      $this->assertTrue($object->isValid());
 283    }
 284  
 285    function testIsNotValid()
 286    {
 287      $error_list = new lmbErrorList();
 288  
 289      $object = $this->_createActiveRecordWithDataAndSave();
 290      $this->assertTrue($object->isValid());
 291  
 292      $error_list->addError('whatever');//actually it's a dirty simulation but that's how it works really

 293  
 294      $object->save($error_list);
 295      $this->assertFalse($object->isValid());
 296    }
 297  
 298    function testValidationExceptionIsNotAddedToErrorList()
 299    {
 300      $error_list = new lmbErrorList();
 301  
 302      $object = new TestOneTableObjectFailing();
 303      $object->setContent('A-a-a-a');
 304      $object->fail = new lmbValidationException('foo', $error_list);
 305  
 306      $this->assertFalse($object->trySave($error_list));
 307      $this->assertTrue($error_list->isEmpty());
 308    }
 309  
 310    function testNonValidationExceptionIsAddedToErrorList()
 311    {
 312      $error_list = new lmbErrorList();
 313  
 314      $object = new TestOneTableObjectFailing();
 315      $object->setContent('A-a-a-a');
 316      $object->fail = new Exception('yo-yo');
 317  
 318      $this->assertFalse($object->trySave($error_list));
 319      $this->assertFalse($error_list->isEmpty());
 320      $this->assertEqual(sizeof($error_list), 1);
 321      $this->assertPattern('~yo-yo~', $error_list[0]->getMessage());
 322    }
 323  
 324    function _createActiveRecord()
 325    {
 326      $object = new lmbActiveRecordValidationStub();
 327      return $object;
 328    }
 329  
 330    protected function _createActiveRecordWithDataAndSave()
 331    {
 332      $object = $this->_createActiveRecord();
 333      $object->set('annotation', 'Annotation ' . time());
 334      $object->set('content', 'Content ' . time());
 335      $object->set('news_date', date("Y-m-d", time()));
 336      $object->save();
 337      return $object;
 338    }
 339  }
 340  ?>