public function testMakeTreePrunesAndInserts() { with(new CategorySeeder())->run(); $parent = Category::find(3); $subtree = array(array('id' => 4, 'name' => 'Child 2.1'), array('name' => 'Child 2.2'), array('name' => 'Child 2.3', 'children' => array(array('name' => 'Child 2.3.1', 'children' => array(array('name' => 'Child 2.3.1.1'), array('name' => 'Child 2.3.1.1'))), array('name' => 'Child 2.3.2'), array('name' => 'Child 2.3.3'))), array('name' => 'Child 2.4')); $this->assertTrue($parent->makeTree($subtree)); $this->assertTrue(Category::isValidNestedSet()); $expected = array(array('id' => 4, 'name' => 'Child 2.1'), array('id' => 7, 'name' => 'Child 2.2'), array('id' => 8, 'name' => 'Child 2.3', 'children' => array(array('id' => 9, 'name' => 'Child 2.3.1', 'children' => array(array('id' => 10, 'name' => 'Child 2.3.1.1'), array('id' => 11, 'name' => 'Child 2.3.1.1'))), array('id' => 12, 'name' => 'Child 2.3.2'), array('id' => 13, 'name' => 'Child 2.3.3'))), array('id' => 14, 'name' => 'Child 2.4')); $hierarchy = $parent->reload()->getDescendants()->toHierarchy()->toArray(); $this->assertArraysAreEqual($expected, array_ints_keys(hmap($hierarchy, array('id', 'name')))); $modified = array(array('id' => 7, 'name' => 'Child 2.2'), array('id' => 8, 'name' => 'Child 2.3'), array('id' => 14, 'name' => 'Child 2.4'), array('name' => 'Child 2.5', 'children' => array(array('name' => 'Child 2.5.1', 'children' => array(array('name' => 'Child 2.5.1.1'), array('name' => 'Child 2.5.1.1'))), array('name' => 'Child 2.5.2'), array('name' => 'Child 2.5.3')))); $this->assertTrue($parent->makeTree($modified)); $this->assertTrue(Category::isValidNestedSet()); $expected = array(array('id' => 7, 'name' => 'Child 2.2'), array('id' => 8, 'name' => 'Child 2.3'), array('id' => 14, 'name' => 'Child 2.4'), array('id' => 15, 'name' => 'Child 2.5', 'children' => array(array('id' => 16, 'name' => 'Child 2.5.1', 'children' => array(array('id' => 17, 'name' => 'Child 2.5.1.1'), array('id' => 18, 'name' => 'Child 2.5.1.1'))), array('id' => 19, 'name' => 'Child 2.5.2'), array('id' => 20, 'name' => 'Child 2.5.3')))); $hierarchy = $parent->reload()->getDescendants()->toHierarchy()->toArray(); $this->assertArraysAreEqual($expected, array_ints_keys(hmap($hierarchy, array('id', 'name')))); }
/** * Simple function which aids in converting the tree hierarchy into something * more easily testable... * * @param array $nodes * @return array */ function hmap(array $nodes, $preserve = null) { $output = array(); foreach ($nodes as $node) { if (is_null($preserve)) { $output[$node['name']] = empty($node['children']) ? null : hmap($node['children']); } else { $preserve = is_string($preserve) ? array($preserve) : $preserve; $current = array_only($node, $preserve); if (array_key_exists('children', $node)) { $children = $node['children']; if (count($children) > 0) { $current['children'] = hmap($children, $preserve); } } $output[] = $current; } } return $output; }
public function testToHierarchyNestsCorrectlyWithOrder() { with(new OrderedCategorySeeder())->run(); $expectedWhole = array('Root A' => null, 'Root Z' => array('Child A' => null, 'Child C' => null, 'Child G' => array('Child G.1' => null))); $this->assertArraysAreEqual($expectedWhole, hmap(OrderedCategory::all()->toHierarchy()->toArray())); $expectedSubtreeZ = array('Root Z' => array('Child A' => null, 'Child C' => null, 'Child G' => array('Child G.1' => null))); $this->assertArraysAreEqual($expectedSubtreeZ, hmap($this->categories('Root Z', 'OrderedCategory')->getDescendantsAndSelf()->toHierarchy()->toArray())); }
public function testToHierarchyNestsCorrectlyWithScopedOrder() { with(new OrderedScopedCategorySeeder())->run(); $expectedWhole1 = array('Root 1' => array('Child 1' => null, 'Child 2' => array('Child 2.1' => null), 'Child 3' => null)); $expectedWhole2 = array('Root 2' => array('Child 4' => null, 'Child 5' => array('Child 5.1' => null), 'Child 6' => null)); $this->assertArraysAreEqual($expectedWhole1, hmap(OrderedScopedCategory::where('company_id', 1)->get()->toHierarchy()->toArray())); $this->assertArraysAreEqual($expectedWhole2, hmap(OrderedScopedCategory::where('company_id', 2)->get()->toHierarchy()->toArray())); }