[ Index ]
 

Code source de SPIP Agora 1.4

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/Pear/DB/tests/NestedSet/ -> query.php (source)

   1  <?php
   2  /**
   3   * UnitTest
   4   * Query method tests
   5   *
   6   * @author Daniel Khan <dk@webcluster.at>
   7   * @package DB_NestedSetTest
   8   * @version $Revision: 1.5 $
   9   * @access public
  10   */
  11  
  12  class tests_NestedSet_query extends DB_NestedSetTest {
  13      // +----------------------------------------------+
  14      // | Testing query methods                        |
  15      // |----------------------------------------------+
  16      // | [PUBLIC]                                     |
  17      // +----------------------------------------------+
  18      var $addSQL = array('where' => '1');
  19  
  20      /**
  21       * tests_NestedSet_common::test_getAllNodes()
  22       *
  23       * Creates some nodes and verifies the result
  24       *
  25       * @access public
  26       * @return bool True on completion
  27       */
  28      function test_getAllNodes() {
  29          $rnc = 3;
  30          $depth = 2;
  31          $npl = 3;
  32          $this->_createSubNode($rnc, $depth, $npl);
  33  
  34          $allnodes = $this->_NeSe->getAllNodes(true, true, $this->addSQL);
  35          $rootnodes = $this->_NeSe->getRootNodes(true, true, $this->addSQL);
  36          $exp_cct = 0;
  37          foreach($rootnodes AS $rid => $rootnode) {
  38              $exp_cct = $exp_cct + floor(($rootnode['r'] - $rootnode['l']) / 2);
  39          }
  40          // Does it really return all nodes?
  41          $cct = count($allnodes);
  42          $exp_cct = $exp_cct + count($rootnodes);
  43          $this->assertEquals($exp_cct, $cct, 'Total node count returned is wrong');
  44          // Verify the result agains pickNode()
  45          foreach($allnodes AS $nid => $node) {
  46              $this->assertEquals($this->_NeSe->pickNode($nid, true), $node, 'Result differs from pickNode()');
  47          }
  48  
  49          return true;
  50      }
  51  
  52      /**
  53       * tests_NestedSet_common::test_getRootNodes()
  54       *
  55       * Create 2 sets of rootnodes (ordered and mixed) and see if the result matches
  56       * getRootNodes()
  57       *
  58       * @access public
  59       * @see _createRootNodes
  60       * @return bool True on completion
  61       */
  62      function test_getRootNodes() {
  63          // Create a simple set of rootnodes
  64          $rootnodes_exp = $this->_createRootNodes(15);
  65          $rootnodes = $this->_NeSe->getRootNodes(true, true, $this->addSQL);
  66          $this->assertEquals($rootnodes_exp, $rootnodes, 'getRootNodes() failed');
  67          // Create a mixed order set of rootnodes
  68          $rootnodes_exp = $this->_createRootNodes(15, true);
  69          $rootnodes = $this->_NeSe->getRootNodes(true, true, $this->addSQL);
  70          $this->assertEquals($rootnodes_exp, $rootnodes, 'getRootNodes() failed on mixed set');
  71          return true;
  72      }
  73  
  74      /**
  75       * tests_NestedSet_common::test_getParents()
  76       *
  77       * Handcraft the parent tree using the relation tree from _createSubNode()
  78       * and compare it against getParents()
  79       *
  80       * @access public
  81       * @see _traverseParentRelations
  82       * @return bool True on completion
  83       */
  84      function test_getParents() {
  85          $rnc = 3;
  86          $depth = 2;
  87          $npl = 3;
  88          // Create a new tree
  89          $relationTree = $this->_createSubNode($rnc, $depth, $npl);
  90          $allnodes = $this->_NeSe->getAllNodes(true, true, $this->addSQL);
  91          // Walk trough all nodes and compare it's relations whith the one provided
  92          // by the relation tree
  93          foreach($allnodes AS $nid => $node) {
  94              $parents = $this->_NeSe->getParents($nid, true, true, $this->addSQL);
  95              $exp_parents = array_reverse($this->_traverseParentRelations($relationTree, $nid, true), true);
  96              $this->assertEquals($exp_parents, $parents, 'Differs from relation traversal result.');
  97          }
  98          return true;
  99      }
 100  
 101      /**
 102       * tests_NestedSet_common::test_getParent()
 103       *
 104       * Build a simple tree run getParent() and compare it with the relation tree
 105       *
 106       * @access public
 107       * @return bool True on completion
 108       */
 109      function test_getParent() {
 110          $rnc = 3;
 111          $depth = 2;
 112          $npl = 3;
 113          // Create a new tree
 114          $relationTree = $this->_createSubNode($rnc, $depth, $npl);
 115          $allnodes = $this->_NeSe->getAllNodes(true);
 116          // Walk trough all nodes and compare it's relations whith the one provided
 117          // by the relation tree
 118          foreach($allnodes AS $nid => $node) {
 119              $parent = $this->_NeSe->getParent($nid, true, true, $this->addSQL);
 120              if (!isset($relationTree[$nid]['parent'])) {
 121                  $this->assertFalse($parent, 'A rootnode returned a parent');
 122                  continue;
 123              }
 124              $this->assertEquals($relationTree[$nid]['parent'], $parent['id'], 'Relation tree parent doesn\'t match method return');
 125          }
 126          return true;
 127      }
 128  
 129      function test_getSiblings() {
 130          $rnc = 3;
 131          $depth = 2;
 132          $npl = 3;
 133          // Create a new tree
 134          $relationTree = $this->_createSubNode($rnc, $depth, $npl);
 135          $allnodes = $this->_NeSe->getAllNodes(true, true, $this->addSQL);
 136          // Walk trough all nodes and compare it's relations whith the one provided
 137          // by the relation tree
 138          foreach($allnodes AS $nid => $node) {
 139              if (!$children = $this->_NeSe->getChildren($nid, true, true, false, $this->addSQL)) {
 140                  continue;
 141              }
 142              foreach($children AS $cid => $child) {
 143                  $siblings = $this->_NeSe->getSiblings($cid, true, true, $this->addSQL);
 144                  $this->assertEquals($children, $siblings, 'Children don\'t match getSiblings()');
 145              }
 146          }
 147          return true;
 148      }
 149  
 150      /**
 151       * tests_NestedSet_common::isParent()
 152       *
 153       * Create a tree, go trogh each node, fetch all children
 154       * and see if isParent() returns true
 155       *
 156       * @access public
 157       * @return bool True on completion
 158       */
 159      function test_isParent() {
 160          $rnc = 3;
 161          $depth = 2;
 162          $npl = 3;
 163          $relationTree = $this->_createSubNode($rnc, $depth, $npl);
 164          $allnodes = $this->_NeSe->getAllNodes(true);
 165          foreach($allnodes AS $nid => $node) {
 166              $children = $this->_NeSe->getChildren($nid, true);
 167  
 168              if (empty($children)) {
 169                  continue;
 170              }
 171              foreach($children AS $cid => $child) {
 172                  $isParent = $this->_NeSe->isParent($node, $child);
 173                  $this->assertEquals($relationTree[$cid]['parent'] , $nid, 'Parent from relation tree differs.');
 174                  $this->assertTrue($isParent, 'isParent was false.');
 175              }
 176          }
 177          return true;
 178      }
 179  
 180      /**
 181       * tests_NestedSet_common::test_getChildren()
 182       *
 183       * Create some children
 184       * The dirty work is done in _traverseChildren()
 185       * Here we only calc if the expected number of children returned matches
 186       * the count of getChildren()
 187       *
 188       * @access public
 189       * @see _createSubNode
 190       * @see _traverseChildren
 191       * @return bool True on completion
 192       */
 193      function test_getChildren() {
 194          $rnc = 2;
 195          $depth = 2;
 196          $npl = 3;
 197          // Just see if empty nodes are recognized
 198          $nids = $this->_setupRootnodes(3);
 199          foreach($nids AS $rix => $nid) {
 200              $this->assertFalse($this->_NeSe->getChildren($nid, true, true, false, $this->addSQL), 'getChildren returned value for empty rootnode');
 201          }
 202          // Now build a little tree to test
 203          $relationTree = $this->_createSubNode($rnc, $depth, $npl);
 204  
 205          $rootnodes = $this->_NeSe->getRootNodes(true);
 206          $exp_cct = 0;
 207          $cct = 0;
 208          foreach($rootnodes AS $rid => $rootnode) {
 209              // Traverse the tree and verify it against the relationTree
 210              $cct = $cct + $this->_traverseChildren($rootnode, $relationTree, true);
 211              // Calc the expected number of children from lft-rgt
 212              $exp_cct = $exp_cct + floor(($rootnode['r'] - $rootnode['l']) / 2);
 213          }
 214          // Test if all created nodes got returned
 215          $this->assertEquals($exp_cct, $cct, 'Total node count returned is wrong');
 216          return true;
 217      }
 218  
 219      /**
 220       * tests_NestedSet_common::test_getBranch()
 221       *
 222       * If we only have one branch getAllNodes() has to eual getBranch()
 223       *
 224       * @access public
 225       * @return bool True on completion
 226       */
 227      function test_getBranch() {
 228          $rnc = 1;
 229          $depth = 2;
 230          $npl = 3;
 231          // Create a new tree
 232          $this->_createSubNode($rnc, $depth, $npl);
 233          $allnodes = $this->_NeSe->getAllNodes(true);
 234          $branch = $this->_NeSe->getBranch($npl, true, true, $this->addSQL);
 235          $this->assertEquals($allnodes, $branch, 'Result differs from getAllNodes()');
 236      }
 237  
 238      /**
 239       * tests_NestedSet_common::test_getSubBranch()
 240       *
 241       * Handcraft a sub branch using the relation tree from _createSubNode()
 242       * and compare it against getSubBranch()
 243       *
 244       * @access public
 245       * @see _traverseChildRelations
 246       * @return bool True on completion
 247       */
 248      function test_getSubBranch() {
 249          $rnc = 3;
 250          $depth = 2;
 251          $npl = 3;
 252          // Create a new tree
 253          $relationTree = $this->_createSubNode($rnc, $depth, $npl);
 254          $allnodes = $this->_NeSe->getAllNodes(true);
 255          foreach($relationTree AS $nid => $relations) {
 256              $subbranch = $this->_NeSe->getSubBranch($nid, true, true, $this->addSQL);
 257              $exp_subbranch = $this->_traverseChildRelations($relationTree, $nid, true, true);
 258              $this->assertEquals($subbranch, $exp_subbranch, 'Differs from relation traversal result.');
 259          }
 260          return true;
 261      }
 262  
 263      /**
 264       * tests_NestedSet_common::test_pickNode()
 265       *
 266       * Create some rootnodes and run pickNode() on it.
 267       *
 268       * @access public
 269       * @return bool True on completion
 270       */
 271      function test_pickNode() {
 272          // Set some rootnodes
 273          $nids = $this->_setupRootnodes(5);
 274          // Loop trough the node id's of the newly created rootnodes
 275          for($i = 0; $i < count($nids); $i++) {
 276              $nid = $nids[$i];
 277  
 278              $nname = 'Node ' . $nid;
 279              $norder = $nid;
 280              // Pick the current node and do the tests
 281              $nnode = $this->_NeSe->pickNode($nid, true, true, 'id', $this->addSQL);
 282              // Test Array
 283              $this->assertEquals(is_array($nnode), "Node $nname: No array given.");
 284              // Test lft/rgt
 285              $this->assertEquals(1, $nnode['l'], "Node $nname: Wrong LFT");
 286              $this->assertEquals(2, $nnode['r'], "Node $nname: Wrong RGT");
 287              // Test order
 288              $this->assertEquals($norder, $nnode['norder'], "Node $nname: Wrong order.");
 289              // Test Level
 290              $this->assertEquals(1, $nnode['level'], "Node $nname: Wrong level.");
 291              // Test Name
 292              $this->assertEquals($nname, $nnode['name'], "Node $nname: Wrong name.");
 293          }
 294          return true;
 295      }
 296  }
 297  
 298  ?>


Généré le : Sat Feb 24 14:40:03 2007 par Balluche grâce à PHPXref 0.7