[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/core/tests/cases/ -> lmbObjectTest.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/core/src/lmbObject.class.php');
  10  
  11  class ObjectTestVersion extends lmbObject
  12  {
  13    public $bar;
  14    public $_guarded = 'yeah';
  15    protected $protected = 'me';
  16  
  17    function getBar()
  18    {
  19      return $this->bar . '_get_called';
  20    }
  21  
  22    function setBar($value)
  23    {
  24      $this->bar = $value . '_set_called';
  25    }
  26  
  27    function isOk()
  28    {
  29      return true;
  30    }
  31  
  32    function getIsError()
  33    {
  34      return true;
  35    }
  36  
  37    function isError()
  38    {
  39      return false;
  40    }
  41  }
  42  
  43  class lmbObjectTest extends UnitTestCase
  44  {
  45    function testHasAttribute()
  46    {
  47      $object = new lmbObject();
  48      $object->set('bar', 1);
  49  
  50      $this->assertFalse($object->hasAttribute('foo'));
  51      $this->assertTrue($object->hasAttribute('bar'));
  52    }
  53  
  54    function testHasAttributeForExistingButNullProperty()
  55    {
  56      $object = new lmbObject();
  57      $object->set('foo', null);
  58      $this->assertTrue($object->hasAttribute('foo'));
  59    }
  60  
  61    function testHasAttributeForGuardedProperty()
  62    {
  63      $object = new ObjectTestVersion();
  64      $this->assertFalse($object->hasAttribute('_guarded'));
  65    }
  66  
  67    function testGetAttributesNames()
  68    {
  69      $object = new ObjectTestVersion();
  70      $this->assertEqual($object->getAttributesNames(), array('bar', 'protected'));
  71    }
  72  
  73    function testGetNull()
  74    {
  75      $object = new lmbObject();
  76      $this->assertNull($object->get('foo'));
  77    }
  78  
  79    function testSetGet()
  80    {
  81      $object = new lmbObject();
  82      $object->set('foo', 1);
  83  
  84      $this->assertEqual($object->get('foo'), 1);
  85    }
  86  
  87    function testCallGetterForGuardedProperty()
  88    {
  89      $object = new ObjectTestVersion();
  90      $this->assertNull($object->get('_guarded'));
  91    }
  92  
  93    function testNonExistingGetter()
  94    {
  95      $object = new lmbObject();
  96      $object->set('foo_bar_yo', 1);
  97  
  98      $this->assertEqual($object->getFooBarYo(), 1);
  99    }
 100  
 101    function testNonExistingSetter()
 102    {
 103      $object = new lmbObject();
 104      $object->setFooBarYo(1);
 105  
 106      $this->assertEqual($object->getFooBarYo(), 1);
 107    }
 108  
 109    function testCallGetterForPropertyIfItExists()
 110    {
 111      $object = new ObjectTestVersion();
 112      $object->bar = 'BAR';
 113      $this->assertEqual($object->get('bar'), 'BAR_get_called');
 114    }
 115  
 116    function testCallSetterForPropertyIfItExists()
 117    {
 118      $object = new ObjectTestVersion();
 119      $object->set('bar', 'BAR');
 120      $this->assertEqual($object->bar, 'BAR_set_called');
 121    }
 122  
 123    function testGetterForIsPropertyIsMappedToIsMethodIfItExists()
 124    {
 125      $object = new ObjectTestVersion();
 126      $object->set('is_ok', false);
 127      $this->assertTrue($object->get('is_ok'));//isOk overriden in ObjectTestVersion

 128    }
 129  
 130    function testGetterForIsPropertyIsMappedToGetIsMethodFirst()
 131    {
 132      $object = new ObjectTestVersion();
 133      $object->set('is_error', false);
 134      $this->assertTrue($object->get('is_error'));//getIsError overriden in ObjectTestVersion

 135    }
 136  
 137    function testImportMergesWithExistingProps()
 138    {
 139      $object = new lmbObject();
 140      $object->set('foo', 'hey');
 141      $object->set('baz', 'wow');
 142      $object->import(array('foo' => 'test', 'bar' => 'test2'));
 143  
 144      $this->assertEqual($object->get('foo'), 'test');
 145      $this->assertEqual($object->get('bar'), 'test2');
 146      $this->assertEqual($object->get('baz'), 'wow');
 147    }
 148  
 149    function testImportIgnoresGuardedProperties()
 150    {
 151      $object = new ObjectTestVersion();
 152      $object->import(array('_guarded' => 'no'));
 153      $this->assertEqual($object->_guarded, 'yeah');
 154    }
 155  
 156    function testPassAttributesInConstructor()
 157    {
 158      $object = new lmbObject(array('foo' => 'hey', 'baz' => 'wow'));
 159      $this->assertEqual($object->get('foo'), 'hey');
 160      $this->assertEqual($object->get('baz'), 'wow');
 161    }
 162  
 163    function testExport()
 164    {
 165      $object = new lmbObject();
 166      $object->set('foo', 'yo-yo');
 167      $object->set('bar', 'zoo');
 168  
 169      $this->assertEqual($object->export(), array('foo' => 'yo-yo', 'bar' => 'zoo'));
 170    }
 171  
 172    function testExportOnlyNonGuardedProperties()
 173    {
 174      $object = new ObjectTestVersion();
 175      $object->set('foo', 'FOO');
 176  
 177      $this->assertEqual($object->export(), array('bar' => null, 'foo' => 'FOO', 'protected' => 'me'));
 178    }
 179  
 180    function testRemove()
 181    {
 182      $object = new lmbObject();
 183      $object->set('bar', 1);
 184      $object->set('foo', 2);
 185  
 186      $object->remove('bar');
 187  
 188      $this->assertEqual($object->get('foo'), 2);
 189      $this->assertNull($object->get('bar'));
 190      $this->assertFalse($object->hasAttribute('bar'));
 191    }
 192  
 193    function testRemoveForGuardedProperty()
 194    {
 195      $object = new ObjectTestVersion();
 196      $object->remove('_guarded');
 197  
 198      $this->assertEqual($object->_guarded, 'yeah');
 199    }
 200  
 201    function testRemoveAll()
 202    {
 203      $object = new lmbObject();
 204      $object->set('bar', 1);
 205      $object->set('foo', 2);
 206  
 207      $object->removeAll();
 208  
 209      $this->assertEqual($object->export(), array());
 210    }
 211  
 212    function testRemoveAllExceptGuardedProperties()
 213    {
 214      $object = new ObjectTestVersion();
 215      $object->removeAll();
 216      $this->assertEqual($object->_guarded, 'yeah');
 217    }
 218  
 219    function testGetHash()
 220    {
 221      $o1 = new lmbObject();
 222      $o2 = new lmbObject();
 223  
 224      $this->assertNotNull($o1->getHash());
 225      $this->assertEqual($o1->getHash(), $o2->getHash());
 226    }
 227  
 228    function testGetClass()
 229    {
 230      $o1 = new lmbObject();
 231      $this->assertEqual($o1->getClass(), 'lmbObject');
 232  
 233      $o2 = new ObjectTestVersion($this);
 234      $this->assertEqual($o2->getClass(), 'ObjectTestVersion');
 235    }
 236  
 237    function testImplementsArrayAccessInterface()
 238    {
 239      $o = new lmbObject();
 240  
 241      $o->set('foo', 'Bar');
 242      $this->assertEqual($o['foo'], 'Bar');
 243  
 244      $o['foo'] = 'Zoo';
 245      $this->assertEqual($o->get('foo'), 'Zoo');
 246  
 247      unset($o['foo']);
 248      $this->assertNull($o->get('foo'));
 249  
 250      $o->set('foo', 'Bar');
 251      $this->assertTrue(isset($o['foo']));
 252      $this->assertFalse(isset($o['bar']));
 253    }
 254  }
 255  ?>


Generated: Fri Dec 5 04:05:07 2008 Cross-referenced by PHPXref 0.7