From 3b1dd82603381c946e2a4e1a50a2bcd882c81bbb Mon Sep 17 00:00:00 2001 From: AD7six Date: Tue, 10 Jan 2012 10:18:59 +0100 Subject: [PATCH] add tests for set nest method --- lib/Cake/Test/Case/Utility/SetTest.php | 244 +++++++++++++++++++++++++ lib/Cake/Utility/Set.php | 4 +- 2 files changed, 246 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/SetTest.php b/lib/Cake/Test/Case/Utility/SetTest.php index b460da55b..faa08601c 100644 --- a/lib/Cake/Test/Case/Utility/SetTest.php +++ b/lib/Cake/Test/Case/Utility/SetTest.php @@ -3137,4 +3137,248 @@ class SetTest extends CakeTestCase { $expected = array('one' => array('a', 'b', 'c' => 'cee'), 'two' => 2, 'three' => null); $this->assertEquals($expected, $result); } + + public function testNestModel() { + $input = array( + array( + 'ModelName' => array( + 'id' => 1, + 'parent_id' => null + ), + ), + array( + 'ModelName' => array( + 'id' => 2, + 'parent_id' => 1 + ), + ), + array( + 'ModelName' => array( + 'id' => 3, + 'parent_id' => 1 + ), + ), + array( + 'ModelName' => array( + 'id' => 4, + 'parent_id' => 1 + ), + ), + array( + 'ModelName' => array( + 'id' => 5, + 'parent_id' => 1 + ), + ), + array( + 'ModelName' => array( + 'id' => 6, + 'parent_id' => null + ), + ), + array( + 'ModelName' => array( + 'id' => 7, + 'parent_id' => 6 + ), + ), + array( + 'ModelName' => array( + 'id' => 8, + 'parent_id' => 6 + ), + ), + array( + 'ModelName' => array( + 'id' => 9, + 'parent_id' => 6 + ), + ), + array( + 'ModelName' => array( + 'id' => 10, + 'parent_id' => 6 + ) + ) + ); + $expected = array( + array( + 'ModelName' => array( + 'id' => 1, + 'parent_id' => null + ), + 'children' => array( + array( + 'ModelName' => array( + 'id' => 2, + 'parent_id' => 1 + ), + 'children' => array() + ), + array( + 'ModelName' => array( + 'id' => 3, + 'parent_id' => 1 + ), + 'children' => array() + ), + array( + 'ModelName' => array( + 'id' => 4, + 'parent_id' => 1 + ), + 'children' => array() + ), + array( + 'ModelName' => array( + 'id' => 5, + 'parent_id' => 1 + ), + 'children' => array() + ), + + ) + ), + array( + 'ModelName' => array( + 'id' => 6, + 'parent_id' => null + ), + 'children' => array( + array( + 'ModelName' => array( + 'id' => 7, + 'parent_id' => 6 + ), + 'children' => array() + ), + array( + 'ModelName' => array( + 'id' => 8, + 'parent_id' => 6 + ), + 'children' => array() + ), + array( + 'ModelName' => array( + 'id' => 9, + 'parent_id' => 6 + ), + 'children' => array() + ), + array( + 'ModelName' => array( + 'id' => 10, + 'parent_id' => 6 + ), + 'children' => array() + ) + ) + ) + ); + $result = Set::nest($input); + $this->assertEquals($expected, $result); + } + + public function testNest1Dimensional() { + $input = array( + array( + 'id' => 1, + 'parent_id' => null + ), + array( + 'id' => 2, + 'parent_id' => 1 + ), + array( + 'id' => 3, + 'parent_id' => 1 + ), + array( + 'id' => 4, + 'parent_id' => 1 + ), + array( + 'id' => 5, + 'parent_id' => 1 + ), + array( + 'id' => 6, + 'parent_id' => null + ), + array( + 'id' => 7, + 'parent_id' => 6 + ), + array( + 'id' => 8, + 'parent_id' => 6 + ), + array( + 'id' => 9, + 'parent_id' => 6 + ), + array( + 'id' => 10, + 'parent_id' => 6 + ) + ); + $expected = array( + array( + 'id' => 1, + 'parent_id' => null, + 'children' => array( + array( + 'id' => 2, + 'parent_id' => 1, + 'children' => array() + ), + array( + 'id' => 3, + 'parent_id' => 1, + 'children' => array() + ), + array( + 'id' => 4, + 'parent_id' => 1, + 'children' => array() + ), + array( + 'id' => 5, + 'parent_id' => 1, + 'children' => array() + ), + + ) + ), + array( + 'id' => 6, + 'parent_id' => null, + 'children' => array( + array( + 'id' => 7, + 'parent_id' => 6, + 'children' => array() + ), + array( + 'id' => 8, + 'parent_id' => 6, + 'children' => array() + ), + array( + 'id' => 9, + 'parent_id' => 6, + 'children' => array() + ), + array( + 'id' => 10, + 'parent_id' => 6, + 'children' => array() + ) + ) + ) + ); + $result = Set::nest($input, array('idPath' => '/id', 'parentPath' => '/parent_id')); + $this->assertEquals($expected, $result); + } } diff --git a/lib/Cake/Utility/Set.php b/lib/Cake/Utility/Set.php index 48b399ea2..1e38ee776 100644 --- a/lib/Cake/Utility/Set.php +++ b/lib/Cake/Utility/Set.php @@ -1132,11 +1132,11 @@ class Set { } $alias = key(current($data)); - $options = array( + $options += array( 'idPath' => "/$alias/id", 'parentPath' => "/$alias/parent_id", 'children' => 'children' - ) + $options; + ); $return = $idMap = array(); $ids = Set::extract($data, $options['idPath']);