Skip to content
Permalink
Browse files

add tests for set nest method

  • Loading branch information...
AD7six committed Jan 10, 2012
1 parent d534013 commit 3b1dd82603381c946e2a4e1a50a2bcd882c81bbb
Showing with 246 additions and 2 deletions.
  1. +244 −0 lib/Cake/Test/Case/Utility/SetTest.php
  2. +2 −2 lib/Cake/Utility/Set.php
@@ -3137,4 +3137,248 @@ public function testNormalizeArrays() {
$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);
}
}
@@ -1132,11 +1132,11 @@ public static function nest($data, $options = array()) {
}
$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']);

0 comments on commit 3b1dd82

Please sign in to comment.
You can’t perform that action at this time.