File tree Expand file tree Collapse file tree 3 files changed +57
-0
lines changed
tests/integration/Tree/Eloquent Expand file tree Collapse file tree 3 files changed +57
-0
lines changed Original file line number Diff line number Diff line change @@ -112,4 +112,11 @@ public function parent(CanHaveParent $child);
112
112
* @return Node[]
113
113
*/
114
114
public function ancestors (CanHaveParent $ child );
115
+
116
+ /**
117
+ * Return all known root nodes.
118
+ *
119
+ * @return Node[]
120
+ */
121
+ public function rootNodes ();
115
122
}
Original file line number Diff line number Diff line change @@ -308,6 +308,25 @@ public function ancestors(CanHaveParent $child)
308
308
309
309
}
310
310
311
+ /**
312
+ * {@inheritdoc}
313
+ *
314
+ * @return Node[]
315
+ */
316
+ public function rootNodes ()
317
+ {
318
+ $ query = $ this ->model ->newQuery ()->whereNull ($ this ->parentIdKey );
319
+
320
+ $ this ->callBeforeListeners ('rootNodes ' , [$ query ]);
321
+
322
+ $ nodes = $ query ->get ()->all ();
323
+
324
+ $ this ->callAfterListeners ('rootNodes ' , [$ nodes ]);
325
+
326
+ return $ nodes ;
327
+ }
328
+
329
+
311
330
/**
312
331
* @inheritDoc
313
332
*/
Original file line number Diff line number Diff line change @@ -296,6 +296,37 @@ public function parent_returns_null_on_no_parent()
296
296
297
297
}
298
298
299
+ /**
300
+ * @test
301
+ */
302
+ public function rootNodes_returns_one_rootNode ()
303
+ {
304
+ $ repo = $ this ->newRepository ();
305
+
306
+ $ rootNodes = $ repo ->rootNodes ();
307
+
308
+ $ this ->assertCount (1 , $ rootNodes );
309
+
310
+ }
311
+
312
+ /**
313
+ * @test
314
+ */
315
+ public function rootNodes_returns_many_rootNodes ()
316
+ {
317
+ $ repo = $ this ->newRepository ();
318
+
319
+ $ rootNode2 = $ repo ->store (['name ' => 'root-2 ' ]);
320
+
321
+ $ rootNodes = $ repo ->rootNodes ();
322
+
323
+ $ this ->assertCount (2 , $ rootNodes );
324
+
325
+ $ this ->assertEquals ('root-1 ' , $ rootNodes [0 ]->getPathSegment ());
326
+ $ this ->assertEquals ('root-2 ' , $ rootNodes [1 ]->getPathSegment ());
327
+
328
+ }
329
+
299
330
/**
300
331
* @test
301
332
*/
You can’t perform that action at this time.
0 commit comments