|
|
@@ -872,4 +872,70 @@ class CollectionTest extends TestCase {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * Provider for testing each of the direcations for listNested
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function nestedListProvider() {
|
|
|
+ return [
|
|
|
+ ['desc', [1, 2, 3, 5, 7, 4, 8, 6, 9, 10]],
|
|
|
+ ['asc', [5, 7, 3, 8, 4, 2, 1, 9, 10, 6]],
|
|
|
+ ['leaves', [5, 7, 8, 9, 10]]
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+/**
|
|
|
+ * Tests the listNested method with the default 'children' nesting key
|
|
|
+ *
|
|
|
+ * @dataProvider nestedListProvider
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testListNested($dir, $expected) {
|
|
|
+ $items = [
|
|
|
+ ['id' => 1, 'parent_id' => null],
|
|
|
+ ['id' => 2, 'parent_id' => 1],
|
|
|
+ ['id' => 3, 'parent_id' => 2],
|
|
|
+ ['id' => 4, 'parent_id' => 2],
|
|
|
+ ['id' => 5, 'parent_id' => 3],
|
|
|
+ ['id' => 6, 'parent_id' => null],
|
|
|
+ ['id' => 7, 'parent_id' => 3],
|
|
|
+ ['id' => 8, 'parent_id' => 4],
|
|
|
+ ['id' => 9, 'parent_id' => 6],
|
|
|
+ ['id' => 10, 'parent_id' => 6]
|
|
|
+ ];
|
|
|
+ $collection = (new Collection($items))->nest('id', 'parent_id')->listNested($dir);
|
|
|
+ $this->assertEquals($expected, $collection->extract('id')->toArray(false));
|
|
|
+ }
|
|
|
+
|
|
|
+/**
|
|
|
+ * Tests using listNested with a different nesting key
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testListNestedCustomKey() {
|
|
|
+ $items = [
|
|
|
+ ['id' => 1, 'stuff' => [['id' => 2, 'stuff' => [['id' => 3]]]]],
|
|
|
+ ['id' => 4, 'stuff' => [['id' => 5]]]
|
|
|
+ ];
|
|
|
+ $collection = (new Collection($items))->listNested('desc', 'stuff');
|
|
|
+ $this->assertEquals(range(1, 5), $collection->extract('id')->toArray(false));
|
|
|
+ }
|
|
|
+
|
|
|
+/**
|
|
|
+ * Tests flattening the collection using a custom callable function
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testListNestedWithCallable() {
|
|
|
+ $items = [
|
|
|
+ ['id' => 1, 'stuff' => [['id' => 2, 'stuff' => [['id' => 3]]]]],
|
|
|
+ ['id' => 4, 'stuff' => [['id' => 5]]]
|
|
|
+ ];
|
|
|
+ $collection = (new Collection($items))->listNested('desc', function($item) {
|
|
|
+ return isset($item['stuff']) ? $item['stuff'] : [];
|
|
|
+ });
|
|
|
+ $this->assertEquals(range(1, 5), $collection->extract('id')->toArray(false));
|
|
|
+ }
|
|
|
+
|
|
|
}
|