testbase.php

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 472 行 · 第 1/2 页

PHP
472
字号
<?PHP/*** UnitTest* Unit test helper methods** @author       Daniel Khan <dk@webcluster.at>* @package      DB_NestedSetTest* @version      $Revision: 1.4 $* @access       public*/Class TestBase extends PhpUnit_Testcase {    // +----------------------------------------------+    // | Internal helper methods                      |    // |----------------------------------------------+    // | [PRIVATE]                                    |    // +----------------------------------------------+    function _moveTree__Across($branches, $mvt, $nodecount) {        foreach($branches[0] AS $nodeid=>$node) {            foreach($branches[1] AS $tnodeid=>$tnode) {                $ret =  $this->_NeSe->moveTree($nodeid, $tnodeid, $mvt);                $this->assertEquals($nodecount, count($this->_NeSe->getAllNodes(true)), 'Node count changed');            }        }    }    function _deleteNodes($parentID, $keep=false) {        $children = $this->_NeSe->getChildren($parentID, true);        $dc = 0;        if(is_array($children)) {            $cct = count($children);            $randval = $randval = mt_rand(0, $cct-1);            foreach($children AS $cid=>$child) {                // Randomly delete some trees top down instead of deleting bottom up                // and see if the result is still O.K.                if($dc == $randval) {                    $this->_NeSe->deleteNode($cid);                    $this->assertFalse($this->_NeSe->pickNode($cid, true), 'pickNode didn not return false after node deletion.');                    continue;                }                if($child['r']-1 != $child['l']) {                    $this->_deleteNodes($cid);                }                $currchild = $this->_NeSe->pickNode($cid, true);                // The next remaining child in the tree should always have the order 1                $this->assertEquals(1, $currchild['norder'], 'Child has wrong order');                $this->assertEquals($currchild['l'], $currchild['r']-1, 'Wrong lft-rgt checksum after child deletion.');                $this->_NeSe->deleteNode($cid);                $this->assertFalse($this->_NeSe->pickNode($cid, true), 'pickNode didn not return false after node deletion.');                $dc++;            }        } elseif(!$keep) {            $parent = $this->_NeSe->pickNode($parentID, true);            $this->assertEquals($parent['l'], $parent['r']-1, 'Wrong lft-rgt checksum after child deletion.');            $this->_NeSe->deleteNode($parentID);            $this->assertTrue($this->_NeSe->pickNode($parentID, true), 'pickNode didn not return false after node deletion.');        }    }    function _setupRootnodes($nbr) {        $nodes = array();        $lnid = false;        // Create some rootnodes        for($i=0;$i<$nbr;$i++) {            $nodeIndex = $i+1;            $values = array();            $values['STRNA'] = 'Node '.$nodeIndex;            if($i==0) {                $nid[$i] = $this->_NeSe->createRootnode($values, false, true);            } else {                $nid[$i] = $this->_NeSe->createRootnode($values, $nid[$i-1]);            }            $this->assertEquals($nodeIndex, $nid[$i], 'Rootnode $nodeIndex: creation failed');        }        $this->assertEquals($nbr, count($nid), "RootNode creation went wrong.");        return $nid;    }    function _createRandomNodes($rnc, $nbr) {        $rootnodes = $this->_createRootNodes($rnc);        // Number of nodes to create        $available_parents = array();        $relationTree = array();        foreach($rootnodes AS $rid=>$rootnode) {            $available_parents[] = $rid;        }        for($i=0; $i < $nbr-1; $i++) {            $randval = mt_rand(0, count($available_parents)-1);            $choosemethod = mt_rand(1, 2);            $target = $this->_NeSe->pickNode($available_parents[$randval], true);            $nindex = $i;            $values = array();            $returnID = false;            if($choosemethod == 1) {                $method = 'createSubNode';                $exp_target_lft_after = $target['l'];                $exp_target_rgt_after = $target['r']+2;                $values['STRNA'] = $target['name'].'.'.$nindex;                $parentid = $target['id'];            } else {                $method = 'createRightNode';                $returnID = true;                if(isset($relationTree[$target['id']]['parent'])) {                    $parentid = $relationTree[$target['id']]['parent'];                    $parent =  $this->_NeSe->pickNode($parentid, true);                    $exp_target_lft_after = $parent['l'];                    $exp_target_rgt_after = $parent['r']+2;                } else {                    $parentid = false;                }                if(isset($relationTree[$parentid]['children'])) {                    $cct = count($relationTree[$parentid]['children']) +1 ;                } else {                    $cct = 1;                }                if(!empty($parent)) {                    $values['STRNA'] = $parent['name'].'.'.$cct;                } else {                    $rootnodes = $this->_NeSe->getRootNodes(true);                    $cct = count($rootnodes)+1;                    $values['STRNA'] = 'Node '.$cct;                }            }            $available_parents[] = $nid = $this->_NeSe->$method($target['id'], $values, $returnID);            $target_after = false;            if($method == 'createSubNode') {                $target_after = $this->_NeSe->pickNode($target['id'], true);            } elseif($parentid) {               $target_after = $this->_NeSe->pickNode($parent['id'], true);            }            if($target_after) {                $this->assertEquals($exp_target_lft_after,  $target_after['l'], "Wrong LFT after $method");                $this->assertEquals($exp_target_rgt_after,  $target_after['r'], "Wrong RGT after $method");            }            if($choosemethod == 1) {                // createSubNode()                $relationTree[$nid]['parent'] = $parentid;                $relationTree[$target['id']]['children'][] = $nid;                $exp_rootid = $target['rootid'];            } else {                // createRightNode()                if($parentid) {                    $exp_rootid = $parent['rootid'];                } else {                    $exp_rootid = $nid;                }                $relationTree[$parentid]['children'][] = $nid;                $relationTree[$nid]['parent'] = $parentid;            }            $cnode = $this->_NeSe->pickNode($nid, true);            // Test rootid            $this->assertEquals($exp_rootid,  $cnode['rootid'], "Node {$cnode['name']}: Wrong root id.");        }        $exp_cct = 0;        $cct = 0;        // Traverse the tree and verify it using getChildren        foreach($rootnodes AS $rid=>$rootnode) {            $rn = $this->_NeSe->pickNode($rid, true);            $cct = $cct + $this->_traverseChildren($rn, $relationTree);            // Calc the expected number of children from lft-rgt            $exp_cct = $exp_cct + floor(($rn['r'] - $rn['l'])/2);        }        // Test if all created nodes got returned        $this->assertEquals($exp_cct, $cct, 'Total node count returned is wrong');        return $relationTree;    }    function _createRootNodes($nbr, $dist=false) {        // Creates 10 rootnodes        $rplc = array();        $nodes = $this->_setupRootnodes($nbr);        $disturbidx = false;        $disturb = false;        $disturbSet = false;        // Disturb the order by adding a node in the middle of the set        if($dist) {            $values = array();            $values['STRNA'] = 'disturb';            // Try to overwrite the rootid which should be set inside the method            //$values['ROOTID'] = -100;            $disturbidx = count($nodes);            $disturb = 6;            $nodes[$disturbidx] = $this->_NeSe->createRootnode($values, $disturb);        }        for($i=0; $i<count($nodes); $i++) {            $node[$nodes[$i]] = $this->_NeSe->pickNode($nodes[$i], true);            $nodeIndex = $i+1;            if(!empty($disturb) && $nodeIndex - 1  == $disturb) {                $disturbSet = true;            }            if(!$disturbSet) {                $exp_order = $nodeIndex;                $exp_name  = 'Node '.$nodeIndex;            } elseif($i == $disturbidx) {                $exp_order = $disturb+1;                $exp_name  = 'disturb';            } else {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?