Skip to content
Browse files

Added test for Collection::nest when passing objects

  • Loading branch information...
1 parent 41ddf1a commit e671a530bf29f831fec6aae55ba6bc7450dbba4c @lorenzo lorenzo committed
Showing with 60 additions and 0 deletions.
  1. +60 −0 tests/TestCase/Collection/CollectionTest.php
View
60 tests/TestCase/Collection/CollectionTest.php
@@ -14,6 +14,7 @@
*/
namespace Cake\Test\TestCase\Collection;
+use ArrayObject;
use Cake\Collection\Collection;
use Cake\TestSuite\TestCase;
@@ -779,4 +780,63 @@ public function testNestMultiLevel() {
$this->assertEquals($expected, $collection->toArray());
}
+/**
+ * Tests the nest method with more than one level
+ *
+ * @return void
+ */
+ public function testNestObjects() {
+ $items = [
+ new ArrayObject(['id' => 1, 'parent_id' => null]),
+ new ArrayObject(['id' => 2, 'parent_id' => 1]),
+ new ArrayObject(['id' => 3, 'parent_id' => 2]),
+ new ArrayObject(['id' => 4, 'parent_id' => 2]),
+ new ArrayObject(['id' => 5, 'parent_id' => 3]),
+ new ArrayObject(['id' => 6, 'parent_id' => null]),
+ new ArrayObject(['id' => 7, 'parent_id' => 3]),
+ new ArrayObject(['id' => 8, 'parent_id' => 4]),
+ new ArrayObject(['id' => 9, 'parent_id' => 6]),
+ new ArrayObject(['id' => 10, 'parent_id' => 6])
+ ];
+ $collection = (new Collection($items))->nest('id', 'parent_id');
+ $expected = [
+ new ArrayObject([
+ 'id' => 1,
+ 'parent_id' => null,
+ 'children' => [
+ new ArrayObject([
+ 'id' => 2,
+ 'parent_id' => 1,
+ 'children' => [
+ new ArrayObject([
+ 'id' => 3,
+ 'parent_id' => 2,
+ 'children' => [
+ new ArrayObject(['id' => 5, 'parent_id' => 3, 'children' => []]),
+ new ArrayObject(['id' => 7, 'parent_id' => 3, 'children' => []])
+ ]
+ ]),
+ new ArrayObject([
+ 'id' => 4,
+ 'parent_id' => 2,
+ 'children' => [
+ new ArrayObject(['id' => 8, 'parent_id' => 4, 'children' => []])
+ ]
+ ])
+ ]
+ ])
+ ]
+ ]),
+ new ArrayObject([
+ 'id' => 6,
+ 'parent_id' => null,
+ 'children' => [
+ new ArrayObject(['id' => 9, 'parent_id' => 6, 'children' => []]),
+ new ArrayObject(['id' => 10, 'parent_id' => 6, 'children' => []])
+ ]
+ ])
+ ];
+ $this->assertEquals($expected, $collection->toArray());
+ }
+
}

0 comments on commit e671a53

Please sign in to comment.
Something went wrong with that request. Please try again.