[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/tree/tests/cases/ -> lmbTreeTestBase.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/dbal/src/lmbSimpleDb.class.php');
  10  lmb_require('limb/tree/src/exception/lmbTreeException.class.php');
  11  lmb_require('limb/tree/src/exception/lmbTreeInvalidNodeException.class.php');
  12  lmb_require('limb/tree/src/exception/lmbTreeConsistencyException.class.php');
  13  
  14  abstract class lmbTreeTestBase extends UnitTestCase
  15  {
  16    protected $db;
  17    protected $conn;
  18    protected $imp;
  19  
  20    abstract function _createTreeImp();
  21    abstract function _cleanUp();
  22  
  23    function setUp()
  24    {
  25      $toolkit = lmbToolkit :: instance();
  26      $this->conn = $toolkit->getDefaultDbConnection();
  27      $this->db = new lmbSimpleDb($this->conn);
  28      $this->imp = $this->_createTreeImp();
  29  
  30      $this->_cleanUp();
  31    }
  32  
  33    function tearDown()
  34    {
  35      $this->_cleanUp();
  36    }
  37  
  38    function testInitTree()
  39    {
  40      $id = $this->imp->initTree();
  41      $node = $this->imp->getRootNode();
  42      $this->assertEqual($id, $node['id']);
  43    }
  44  
  45    function testGetRootNodeFailed()
  46    {
  47      $this->assertNull($this->imp->getRootNode());
  48    }
  49  
  50    function testGetRootNode()
  51    {
  52      $id = $this->imp->initTree();
  53  
  54      $root_node = $this->imp->getRootNode();
  55      $this->assertEqual($id, $root_node['id']);
  56    }
  57  
  58    function testGetNodeFailed()
  59    {
  60      $this->assertNull($this->imp->getNode(10000));
  61    }
  62  
  63    function testGetNode()
  64    {
  65      $id = $this->imp->initTree();
  66  
  67      $node = $this->imp->getNode($id);
  68      $this->assertEqual($node['id'], $id);
  69    }
  70  
  71    function testNodeContainsParentId()
  72    {
  73      $root_id = $this->imp->initTree();
  74      $node_1 = $this->imp->createNode($root_id, array('identifier'=>'node_1'));
  75  
  76      $node = $this->imp->getNode($node_1);
  77      $this->assertEqual($node['id'], $node_1);
  78      $this->assertEqual($node['parent_id'], $root_id);
  79    }
  80  
  81    function testNodeContainsLevel()
  82    {
  83      $root_id = $this->imp->initTree();
  84      $node_1 = $this->imp->createNode($root_id, array('identifier'=>'node_1'));
  85  
  86      $node = $this->imp->getNode($root_id);
  87      $this->assertEqual($node['level'], 0);
  88  
  89      $node = $this->imp->getNode($node_1);
  90      $this->assertEqual($node['level'], 1);
  91    }
  92  
  93    function testGetNodeByNode()
  94    {
  95      $id = $this->imp->initTree();
  96  
  97      $node = $this->imp->getNode($id);
  98      $sec_node = $this->imp->getNode($node);
  99  
 100      $this->assertEqual($node->export(), $sec_node->export());
 101    }
 102  
 103    function testGetNodeByIdsReturnsOrderedNodes()
 104    {
 105      $root_id = $this->imp->initTree();
 106      $node_1 = $this->imp->createNode($root_id, array('identifier'=>'node_1'));
 107      $node_2 = $this->imp->createNode($root_id, array('identifier'=>'node_2'));
 108      $node_1_1 = $this->imp->createNode($node_1, array('identifier'=>'node_1_1'));
 109  
 110      $arr = $this->imp->getNodesByIds(array($node_2, $node_1, $root_id, $node_1_1));
 111      $this->assertEqual($arr[0]['id'], $root_id);
 112      $this->assertEqual($arr[1]['id'], $node_1);
 113      $this->assertEqual($arr[2]['id'], $node_1_1);
 114      $this->assertEqual($arr[3]['id'], $node_2);
 115    }
 116  
 117    function testIsNodeFailed()
 118    {
 119      $this->assertFalse($this->imp->isNode(10000));
 120      $this->assertFalse($this->imp->isNode('/'));
 121    }
 122  
 123    function testIsNodeForRootNode()
 124    {
 125      $id = $this->imp->initTree();
 126      $this->assertTrue($this->imp->isNode($id));
 127      $this->assertTrue($this->imp->isNode('/'));
 128    }
 129  
 130    function testIsNode()
 131    {
 132      $root_id = $this->imp->initTree();
 133      $node_1 = $this->imp->createNode($root_id, array('identifier'=>'node_1'));
 134      $node_2 = $this->imp->createNode($root_id, array('identifier'=>'node_2'));
 135      $node_1_1 = $this->imp->createNode($node_1, array('identifier'=>'node_1_1'));
 136  
 137      $this->assertTrue($this->imp->isNode($root_id));
 138      $this->assertTrue($this->imp->isNode($node_1));
 139      $this->assertTrue($this->imp->isNode($node_2));
 140      $this->assertTrue($this->imp->isNode($node_1_1));
 141    }
 142  
 143    function testIsNodeByPath()
 144    {
 145      $root_id = $this->imp->initTree();
 146      $node_1 = $this->imp->createNode($root_id, array('identifier'=>'node_1'));
 147      $node_2 = $this->imp->createNode($root_id, array('identifier'=>'node_2'));
 148      $node_1_1 = $this->imp->createNode($node_1, array('identifier'=>'node_1_1'));
 149  
 150      $this->assertTrue($this->imp->isNode('/'));
 151      $this->assertTrue($this->imp->isNode('/node_1'));
 152      $this->assertTrue($this->imp->isNode('/node_2'));
 153      $this->assertTrue($this->imp->isNode('/node_1/node_1_1'));
 154      $this->assertFalse($this->imp->isNode('/node_1_1'));
 155    }
 156  
 157    function testGetParentFailed()
 158    {
 159      try
 160      {
 161        $this->imp->getParent(1000);
 162        $this->assertTrue(false);
 163      }
 164      catch(lmbTreeInvalidNodeException $e){}
 165    }
 166  
 167    function testGetParentReturnsNullForRootNode()
 168    {
 169      $id = $this->imp->initTree();
 170      $this->assertNull($this->imp->getParent($id));
 171    }
 172  
 173    function testGetParent()
 174    {
 175      $root_id = $this->imp->initTree();
 176      $parent_node_id = $this->imp->createNode($root_id, array('identifier' => 'node_1'));
 177      $node_id = $this->imp->createNode($parent_node_id, array('identifier' => 'node_1_1'));
 178  
 179      $parent_node = $this->imp->getParent($node_id);
 180      $this->assertEqual($parent_node['id'], $parent_node_id);
 181      $this->assertEqual($parent_node['identifier'], 'node_1');
 182    }
 183  
 184    function testGetParentByPath()
 185    {
 186      $root_id = $this->imp->initTree();
 187      $parent_node_id = $this->imp->createNode($root_id, array('identifier' => 'node_1'));
 188      $node_id = $this->imp->createNode($parent_node_id, array('identifier' => 'node_1_1'));
 189  
 190      $parent_node = $this->imp->getParent('/node_1/node_1_1');
 191      $this->assertEqual($parent_node['id'], $parent_node_id);
 192      $this->assertEqual($parent_node['identifier'], 'node_1');
 193    }
 194  
 195    function testCreateNodeThrowsInvalidNodeException()
 196    {
 197      try
 198      {
 199        $this->imp->createNode(100, array('identifier'=>'node_1'));
 200        $this->assertTrue(false);
 201      }
 202      catch(lmbTreeInvalidNodeException $e){}
 203    }
 204  
 205    function testCreateNodeFailsWithDuplicateIdentifier()
 206    {
 207      $root_id = $this->imp->initTree();
 208      $node_1 = $this->imp->createNode($root_id, array('identifier'=>'node_1'));
 209      $node_2 = $this->imp->createNode($root_id, array('identifier'=>'node_2'));
 210  
 211      try
 212      {
 213        $this->imp->createNode($root_id, array('identifier'=>'node_2'));
 214        $this->assertTrue(false);
 215      }
 216      catch(lmbTreeConsistencyException $e){}
 217    }
 218  
 219    function testCreateNodeFailsWithEmptyIdentifier()
 220    {
 221      $root_id = $this->imp->initTree();
 222      try
 223      {
 224        $this->imp->createNode($root_id, array('identifier'=>''));
 225        $this->assertTrue(false);
 226      }
 227      catch(lmbTreeConsistencyException $e){}
 228    }
 229  
 230    function testCreateNode()
 231    {
 232      $root_id = $this->imp->initTree();
 233      $node_1 = $this->imp->createNode($root_id, array('identifier'=>'node_1'));
 234      $node_2 = $this->imp->createNode($root_id, array('identifier'=>'node_2'));
 235      $node_1_1 = $this->imp->createNode($node_1, array('identifier'=>'node_1_1'));
 236  
 237      $node = $this->imp->getNode($node_1_1);
 238      $parent_node = $this->imp->getParent($node);
 239  
 240      $this->assertEqual($node_1_1, $node['id']);
 241      $this->assertEqual(2, count($this->imp->getParents($node)));
 242      $this->assertEqual($node_1, $parent_node['id']);
 243    }
 244  
 245    function testUpdateRootWithIdentifierFailed()
 246    {
 247      $root_id = $this->imp->initTree();
 248  
 249      try
 250      {
 251        $this->imp->updateNode($root_id, array('identifier' => 'hey'));
 252        $this->assertTrue(false);
 253      }
 254      catch(lmbTreeConsistencyException $e){}
 255    }
 256  
 257    function testUpdateRootWithEmptyIdentifier()
 258    {
 259      $root_id = $this->imp->initTree();
 260      $this->imp->updateNode($root_id, array('identifier' => ''));
 261    }
 262  
 263    function testUpdateNodeFailed()
 264    {
 265      try
 266      {
 267        $this->imp->updateNode(1000, array('junk'));
 268        $this->assertTrue(false);
 269      }
 270      catch(lmbTreeInvalidNodeException $e){}
 271    }
 272  
 273    function testUpdateNodeFailedWithDuplicateIdentifier()
 274    {
 275      $root_id = $this->imp->initTree();
 276      $node_1 = $this->imp->createNode($root_id, array('identifier'=>'node_1'));
 277      $node_2 = $this->imp->createNode($root_id, array('identifier'=>'node_2'));
 278  
 279      try
 280      {
 281        $this->imp->updateNode($node_1, array('identifier' => 'node_2'));
 282        $this->assertTrue(false);
 283      }
 284      catch(lmbTreeConsistencyException $e){}
 285    }
 286  
 287    function testGetNodeByInvalidArray()
 288    {
 289      $this->assertNull($this->imp->getNode(array('identifier' => 'node_1')));
 290    }
 291  
 292    function testGetNodeByArrayWithId()
 293    {
 294      $root_id = $this->imp->initTree();
 295      $node_1 = $this->imp->createNode($root_id, array('identifier'=>'node_1'));
 296      $node = $this->imp->getNode(array('id' => $node_1));
 297      $this->assertEqual($node['id'], $node_1);
 298      $this->assertEqual($node['identifier'], 'node_1');
 299    }
 300  
 301    function testGetNodeByInvalidObject()
 302    {
 303      $obj = new lmbSet();
 304      $this->assertNull($this->imp->getNode($obj));
 305    }
 306  
 307    function testGetNodeByObjectWithId()
 308    {
 309      $root_id = $this->imp->initTree();
 310      $node_1 = $this->imp->createNode($root_id, array('identifier'=>'node_1'));
 311      $node = $this->imp->getNode(new lmbSet(array('id' => $node_1)));
 312      $this->assertEqual($node['id'], $node_1);
 313      $this->assertEqual($node['identifier'], 'node_1');
 314    }
 315  
 316    function testGetNodeByStringCallsGetNodeByPath()
 317    {
 318      $root_id = $this->imp->initTree();
 319      $node_id = $this->imp->createNode($root_id, array('identifier' => 'node_1'));
 320      $this->assertEqual($this->imp->getNode('/node_1')->export(),
 321                         $this->imp->getNode($node_id)->export());
 322    }
 323  
 324    function testGetNodeByInvalidPath()
 325    {
 326      $this->assertNull($this->imp->getNodeByPath('/blah'));
 327      $this->assertNull($this->imp->getNodeByPath('/\'\''));
 328      $this->assertNull($this->imp->getNodeByPath('/""'));
 329    }
 330  
 331    function testGetRootNodeByPath()
 332    {
 333      $id = $this->imp->initTree();
 334      $node = $this->imp->getNodeByPath('/');
 335      $this->assertEqual($node['id'], $id);
 336      $this->assertEqual($node['identifier'], '');
 337    }
 338  
 339    function testGetNodeByPathWithExcessiveSlashes()
 340    {
 341      $root_id = $this->imp->initTree();
 342      $node_1 = $this->imp->createNode($root_id, array('identifier'=>'node_1'));
 343      $node_2 = $this->imp->createNode($root_id, array('identifier'=>'node_2'));
 344      $node_1_1 = $this->imp->createNode($node_1, array('identifier'=>'node_1_1'));
 345  
 346      $node = $this->imp->getNodeByPath('////');
 347      $this->assertEqual($node['id'], $root_id);
 348  
 349      $node = $this->imp->getNodeByPath('/node_1///');
 350      $this->assertE