public function testImportExport() { /* ### test empty tree ### */ $this->assertEquals(array(), json_decode(Baobab::export(self::$db, self::$forest_name), TRUE)); Baobab::import(self::$db, self::$forest_name, '[]'); $this->assertEquals(NULL, $this->baobab->getTree()); $empty_json_tree = '[{"fields":["id","lft","rgt","label"],"values":null}]'; Baobab::import(self::$db, self::$forest_name, $empty_json_tree); $this->assertEquals(NULL, $this->baobab->getTree()); /* ### test import/export not empty tree with non numeric values ### */ $json_tree = '[{ "fields":["id","lft","label","rgt"], "values": [5,1,"AAA",14,[ [3,2,"BBB",7,[ [4,3,"CCC",4,[]], [6,5,"DDD",6,[]] ]], [1,8,"EEE",13,[ [2,9,"FFF",10,[]], [7,11,"GGG",12,[]] ]] ]] }]'; Baobab::import(self::$db, self::$forest_name, $json_tree); $this->assertEquals(array(array("tree_id" => 1, "fields" => array("id", "label", "rgt"), "values" => array(5, "AAA", 14, array(array(3, "BBB", 7, array(array(4, "CCC", 4, array()), array(6, "DDD", 6, array()))), array(1, "EEE", 13, array(array(2, "FFF", 10, array()), array(7, "GGG", 12, array()))))))), json_decode(Baobab::export(self::$db, self::$forest_name, array("id", "label", "rgt")), TRUE)); }
/** * .. method:: _useTreeTestData($whatToTest) * Apply a method to a starting tree and check if the resulted tree is * equal to the tree expected. * * :param $whatToTest: one of the values returned by _getJsonTestData * :type $whatToTest: array */ function _useTreeTestData(&$whatToTest) { if ($whatToTest["random_tree"]) { // add a fake tree $random_tree = $whatToTest["random_tree"]; $this->_fillFakeTree($random_tree["tree_id"], $random_tree["from_node_id"], $random_tree["num_children"]); } // save the state of the trees before modifications $preTrees = array(); if ($whatToTest["existing_trees"]) { // add as much trees as requested $existing_trees = $whatToTest["existing_trees"]; foreach ($existing_trees as $treeToBuild) { $preTrees[$treeToBuild["tree_id"]] = $treeToBuild["tree"]; Baobab::import(self::$db, self::$forest_name, '[{ "tree_id":' . $treeToBuild["tree_id"] . ', "fields":["id","lft","rgt"], "values":' . json_encode($treeToBuild["tree"]) . ' }]'); } } if (isset($whatToTest["from"])) { // load the data Baobab::import(self::$db, self::$forest_name, array(array("tree_id" => $this->base_tree, "fields" => $whatToTest["fields"], "values" => $whatToTest["from"]))); } // call the func to test try { call_user_func_array(array($this->baobab, $whatToTest["methodName"]), $whatToTest["params"]); if (isset($whatToTest["error"])) { $this->fail("Expecting exception " . $whatToTest["error"]); } // get the current tree state (pop because this function work always on a single array) $treesOnDb = json_decode(Baobab::export(self::$db, self::$forest_name, NULL, $whatToTest["random_tree"] ? $this->base_tree : NULL), TRUE); // $whatToTest has either a 'to' or 'toTrees' keyword if (array_key_exists("to", $whatToTest)) { $treeState = current($treesOnDb); // check that the tree state is what we expected $this->assertEquals($whatToTest["to"], $treeState["values"]); } else { $idToTree = array(); foreach ($treesOnDb as $treeData) { $idToTree[$treeData["tree_id"]] = $treeData; } foreach ($whatToTest["toTrees"] as $toTreeData) { $tree_id = $toTreeData["tree_id"]; $this->assertEquals($toTreeData["tree"] !== NULL ? $toTreeData["tree"] : $preTrees[$tree_id], $idToTree[$tree_id]["values"]); } } } catch (Exception $e) { if (isset($whatToTest["error"])) { $this->assertTrue($whatToTest["error"] === $e->getCode()); } else { throw $e; } } }