query.php
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 326 行
PHP
326 行
<?php/*** UnitTest* Query method tests** @author Daniel Khan <dk@webcluster.at>* @package DB_NestedSetTest* @version $Revision: 1.3 $* @access public*/class tests_NestedSet_query extends DB_NestedSetTest { // +----------------------------------------------+ // | Testing query methods | // |----------------------------------------------+ // | [PUBLIC] | // +----------------------------------------------+ /** * tests_NestedSet_common::test_getAllNodes() * * Creates some nodes and verifies the result * * @access public * @return bool True on completion */ function test_getAllNodes() { $rnc = 3; $depth = 2; $npl = 3; $this->_createSubNode($rnc, $depth, $npl); $allnodes = $this->_NeSe->getAllNodes(true); $rootnodes = $this->_NeSe->getRootNodes(true); $exp_cct = 0; foreach($rootnodes AS $rid=>$rootnode) { $exp_cct = $exp_cct + floor(($rootnode['r'] - $rootnode['l'])/2); } // Does it really return all nodes? $cct = count($allnodes); $exp_cct = $exp_cct + count($rootnodes); $this->assertEquals($exp_cct, $cct, 'Total node count returned is wrong'); // Verify the result agains pickNode() foreach($allnodes AS $nid=>$node) { $this->assertEquals($this->_NeSe->pickNode($nid, true), $node, 'Result differs from pickNode()'); } return true; } /** * tests_NestedSet_common::test_getRootNodes() * * Create 2 sets of rootnodes (ordered and mixed) and see if the result matches * getRootNodes() * * @access public * @see _createRootNodes() * @return bool True on completion */ function test_getRootNodes() { // Create a simple set of rootnodes $rootnodes_exp = $this->_createRootNodes(15); $rootnodes = $this->_NeSe->getRootNodes(true); $this->assertEquals($rootnodes_exp, $rootnodes, 'getRootNodes() failed'); // Create a mixed order set of rootnodes $rootnodes_exp = $this->_createRootNodes(15, true); $rootnodes = $this->_NeSe->getRootNodes(true); $this->assertEquals($rootnodes_exp, $rootnodes, 'getRootNodes() failed on mixed set'); return true; } /** * tests_NestedSet_common::test_getParents() * * Handcraft the parent tree using the relation tree from _createSubNode() * and compare it against getParents() * * @access public * @see _traverseParentRelations() * @return bool True on completion */ function test_getParents() { $rnc = 3; $depth = 2; $npl = 3; // Create a new tree $relationTree = $this->_createSubNode($rnc, $depth, $npl); $allnodes = $this->_NeSe->getAllNodes(true); // Walk trough all nodes and compare it's relations whith the one provided // by the relation tree foreach($allnodes AS $nid=>$node) { $parents = $this->_NeSe->getParents($nid,true); $exp_parents = array_reverse($this->_traverseParentRelations($relationTree, $nid, true), true); $this->assertEquals($exp_parents, $parents, 'Differs from relation traversal result.'); } return true; } /** * tests_NestedSet_common::test_getParent() * * Build a simple tree run getParent() and compare it with the relation tree * * @access public * @return bool True on completion */ function test_getParent() { $rnc = 3; $depth = 2; $npl = 3; // Create a new tree $relationTree = $this->_createSubNode($rnc, $depth, $npl); $allnodes = $this->_NeSe->getAllNodes(true); // Walk trough all nodes and compare it's relations whith the one provided // by the relation tree foreach($allnodes AS $nid=>$node) { $parent = $this->_NeSe->getParent($nid, true); if(!isset($relationTree[$nid]['parent'])) { $this->assertFalse($parent, 'A rootnode returned a parent'); continue; } $this->assertEquals($relationTree[$nid]['parent'], $parent['id'], 'Relation tree parent doesn\'t match method return'); } return true; } function test_getSiblings() { $rnc = 3; $depth = 2; $npl = 3; // Create a new tree $relationTree = $this->_createSubNode($rnc, $depth, $npl); $allnodes = $this->_NeSe->getAllNodes(true); // Walk trough all nodes and compare it's relations whith the one provided // by the relation tree foreach($allnodes AS $nid=>$node) { if(!$children = $this->_NeSe->getChildren($nid, true)) { continue; } foreach($children AS $cid=>$child) { $siblings = $this->_NeSe->getSiblings($cid, true); $this->assertEquals($children, $siblings, 'Children don\'t match getSiblings()'); } } return true; } /** * tests_NestedSet_common::isParent() * * Create a tree, go trogh each node, fetch all children * and see if isParent() returns true * * @access public * @return bool True on completion */ function test_isParent() { $rnc = 3; $depth = 2; $npl = 3; $relationTree = $this->_createSubNode($rnc, $depth, $npl); $allnodes = $this->_NeSe->getAllNodes(true); foreach($allnodes AS $nid=>$node) { $children = $this->_NeSe->getChildren($nid, true); if(empty($children)) { continue; } foreach($children AS $cid=>$child) { $isParent = $this->_NeSe->isParent($node, $child); $this->assertEquals($relationTree[$cid]['parent'] , $nid, 'Parent from relation tree differs.'); $this->assertTrue($isParent, 'isParent was false.'); } } return true; } /** * tests_NestedSet_common::test_getChildren() * * Create some children * The dirty work is done in _traverseChildren() * Here we only calc if the expected number of children returned matches * the count of getChildren() * * @access public * @see _createSubNode() * @see _traverseChildren() * @return bool True on completion */ function test_getChildren() { $rnc = 2; $depth = 2; $npl = 3; // Just see if empty nodes are recognized $nids = $this->_setupRootnodes(3); foreach($nids AS $rix=>$nid) { $this->assertFalse($this->_NeSe->getChildren($nid, true), 'getChildren returned value for empty rootnode'); } // Now build a little tree to test $relationTree = $this->_createSubNode($rnc, $depth, $npl); $rootnodes = $this->_NeSe->getRootNodes(true); $exp_cct = 0; $cct = 0; foreach($rootnodes AS $rid=>$rootnode) { // Traverse the tree and verify it against the relationTree $cct = $cct + $this->_traverseChildren($rootnode, $relationTree, true); // Calc the expected number of children from lft-rgt $exp_cct = $exp_cct + floor(($rootnode['r'] - $rootnode['l'])/2); } // Test if all created nodes got returned $this->assertEquals($exp_cct, $cct, 'Total node count returned is wrong'); return true; } /** * tests_NestedSet_common::test_getBranch() * * If we only have one branch getAllNodes() has to eual getBranch() * * @access public * @return bool True on completion */ function test_getBranch() { $rnc = 1; $depth = 2; $npl = 3; // Create a new tree $this->_createSubNode($rnc, $depth, $npl); $allnodes = $this->_NeSe->getAllNodes(true); $branch = $this->_NeSe->getBranch($npl,true); $this->assertEquals($allnodes, $branch, 'Result differs from getAllNodes()'); } /** * tests_NestedSet_common::test_getSubBranch() * * Handcraft a sub branch using the relation tree from _createSubNode() * and compare it against getSubBranch() * * @access public * @see _traverseChildRelations() * @return bool True on completion */ function test_getSubBranch() { $rnc = 3; $depth = 2; $npl = 3; // Create a new tree $relationTree = $this->_createSubNode($rnc, $depth, $npl); $allnodes = $this->_NeSe->getAllNodes(true); foreach($relationTree AS $nid=>$relations) { $subbranch = $this->_NeSe->getSubBranch($nid,true); $exp_subbranch = $this->_traverseChildRelations($relationTree, $nid, true, true); $this->assertEquals($subbranch, $exp_subbranch, 'Differs from relation traversal result.'); } return true; } /** * tests_NestedSet_common::test_pickNode() * * Create some rootnodes and run pickNode() on it. * * @access public * @return bool True on completion */ function test_pickNode() { // Set some rootnodes $nids = $this->_setupRootnodes(5); // Loop trough the node id's of the newly created rootnodes for($i=0; $i<count($nids); $i++) { $nid = $nids[$i]; $nname = 'Node '.$nid; $norder = $nid; // Pick the current node and do the tests $nnode = $this->_NeSe->pickNode($nid, true); // Test Array $this->assertEquals(is_array($nnode), "Node $nname: No array given."); // Test lft/rgt $this->assertEquals(1, $nnode['l'], "Node $nname: Wrong LFT"); $this->assertEquals(2, $nnode['r'], "Node $nname: Wrong RGT"); // Test order $this->assertEquals($norder, $nnode['norder'], "Node $nname: Wrong order."); // Test Level $this->assertEquals(1, $nnode['level'], "Node $nname: Wrong level."); // Test Name $this->assertEquals($nname, $nnode['name'], "Node $nname: Wrong name."); } return true; }}?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?