$timer = new sfTimer(); $menu = $rt->createMenu(); $timer->addTime(); $t->info(sprintf('### Menu created from db in %s sec (%s nodes/min)', round($timer->getElapsedTime(), 4), floor(8 * 60 / $timer->getElapsedTime()))); $t->info(' 3.3 - Running tests on the created menu object'); $t->is(get_class($menu), 'ioMenuItem', 'The menu rt has the correct class'); $t->is(count($menu->getChildren()), 2, 'The menu rt has 2 children'); $t->is(array_keys($menu->getChildren()), array('Parent 1', 'Parent 2')); $t->is($menu->getAttributes(), array('class' => 'root'), 'The menu rt has the correct attributes array'); $t->is($menu->getCredentials(), array('c1', 'c2'), 'The menu rt has the correct credentials array'); $t->is(count($menu['Parent 1']->getChildren()), 3, 'pt1 has 3 children.'); $t->is(array_keys($menu['Parent 1']->getChildren()), array('Child 1', 'Child 2', 'Child 3'), 'pt1\'s children are array(Child 1, Child 2, Child 3)'); $t->is(count($menu['Parent 2']->getChildren()), 1, 'pt2 has 1 child.'); $t->is(array_keys($menu['Parent 2']->getChildren()), array('Child 4'), 'pt2\'s children are array(Child 4)'); $t->info(' 3.4 - Compare the created meno to that of the menu from create_test_tree(). They should be identical.'); $arr = create_test_tree($t); $matchingMenu = $arr['menu']; // update its credentials to match $matchingMenu->setCredentials(array('c1', 'c2')); // just doing in stages so its more obvious when something fails $t->is($menu->toArray(false), $matchingMenu->toArray(false), 'The menus match non-recursively.'); $t->is($menu->toArray(), $matchingMenu->toArray(), 'The full menus match recursively.'); $t->info('4 - Test generateNestedSortableArray()'); Doctrine_Query::create()->from('ioDoctrineMenuItem')->delete()->execute(); $arr = create_doctrine_test_tree($t); $rt = $arr['rt']; $expected = array('requestFirstIndex' => 0, 'firstIndex' => 0, 'count' => 7, 'columns' => array('“Root li”'), 'items' => array(array('id' => $arr['pt1']->id, 'level' => '1', 'info' => array('<strong>Parent 1</strong>'), 'children' => array(array('id' => $arr['ch1']->id, 'level' => '2', 'info' => array('<strong>Child 1</strong>')), array('id' => $arr['ch2']->id, 'level' => '2', 'info' => array('<strong>Child 2</strong>')), array('id' => $arr['ch3']->id, 'level' => '2', 'info' => array('<strong>Child 3</strong>')))), array('id' => $arr['pt2']->id, 'level' => '1', 'info' => array('<strong>Parent 2</strong>'), 'children' => array(array('id' => $arr['ch4']->id, 'level' => '2', 'info' => array('<strong>Child 4</strong>'), 'children' => array(array('id' => $arr['gc1']->id, 'level' => '3', 'info' => array('<strong>Grandchild 1</strong>')))))))); $result = $rt->generateNestedSortableArray(); /** * If the final test doesn't pass, these could be helpful for debugging why: $t->is($result['count'], 7, '$result[count] = 7 for the 7 children under root.');
$menu->addChild('c4'); $menu['c3']->moveToFirstPosition(); $arr = array_keys($menu->getChildren()); $t->is($arr, array('c3', 'c1', 'c2', 'c4'), 'c3 moved to first position'); $menu['c2']->moveToLastPosition(); $arr = array_keys($menu->getChildren()); $t->is($arr, array('c3', 'c1', 'c4', 'c2'), 'c2 moved to last position'); $menu['c1']->moveToPosition(2); $arr = array_keys($menu->getChildren()); $t->is($arr, array('c3', 'c4', 'c1', 'c2'), 'c1 moved to 3rd position'); $menu->reorderChildren(array('c4', 'c3', 'c2', 'c1')); $arr = array_keys($menu->getChildren()); $t->is($arr, array('c4', 'c3', 'c2', 'c1'), 'reorder children'); $t->is($menu->render(), '<ul class="menu"><li class="first">c4</li><li>c3</li><li>c2</li><li class="last">c1</li></ul>', 'proper rendering after reorder'); // create the tree and make the variables available extract(create_test_tree($t, 'ioMenuItemTest')); $t->info('10 - Test copy'); check_test_tree($t, $menu); print_test_tree($t); // print the test tree $menu2 = $menu->copy(); $t->ok($menu2 !== $menu, 'menu2 is another instance then menu'); $t->ok($menu2['Parent 1'] !== $menu['Parent 1'], 'menu2->pt1 is another instance than menu->pt1'); $t->ok($menu2['Parent 1']['Child 2'] !== $menu['Parent 1']['Child 2'], 'menu2->pt1->ch2 is another instance than menu->pt1->ch2'); $t->ok($menu2['Parent 1']['Child 2']->getParent() === $menu2['Parent 1'], 'menu2->pt1->ch2->parent is same instance as menu2->pt1'); $t->ok($menu2['Parent 1']->getParent() === $menu2, 'menu2->pt1->parent is same instance as menu2'); //$t->ok($menu2['Parent 1']->getParent() !== $menu['Parent 1'], 'menu2->pt1->ch2->parent is same instance as menu->pt1'); $t->info('11 - Test slice'); $menu = new ioMenuItem('root'); $menu->addChild('c1'); $menu['c1']->addChild('gc1');