From 31b5e0d627880ac17a7648396a0dff4277f6ccf0 Mon Sep 17 00:00:00 2001 From: Art4es Date: Mon, 16 Mar 2020 13:03:46 +0300 Subject: [PATCH] Fix calling getAncestors on empty collection (#20) * Fix executing getAncestors on empty collection --- src/Helpers/LTreeHelper.php | 5 ++++- tests/Functional/LtreeTest.php | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Helpers/LTreeHelper.php b/src/Helpers/LTreeHelper.php index aaaba5c..a73ddd1 100644 --- a/src/Helpers/LTreeHelper.php +++ b/src/Helpers/LTreeHelper.php @@ -93,8 +93,11 @@ public static function pathAsString($path): string public static function getAncestors(Collection $collection): Collection { + if ($collection->count() === 0) { + return new Collection(); + } $first = $collection->first(); - $paths = $collection->pluck('path')->map(function ($item) { + $paths = $collection->pluck($first->getLtreePathColumn())->map(function ($item) { return "'${item}'"; }); $paths = $paths->implode(', '); diff --git a/tests/Functional/LtreeTest.php b/tests/Functional/LtreeTest.php index 53814b0..73937bc 100644 --- a/tests/Functional/LtreeTest.php +++ b/tests/Functional/LtreeTest.php @@ -137,6 +137,7 @@ public function deleteViaServiceSubtree(): void /** @test */ public function ancestors(): void { + $this->assertCount(0, LTreeHelper::getAncestors(new CollectionBase())); $nodes = $this->createTreeNodes($this->getTreeNodes()); $this->assertSame(3, $nodes[1]::ancestorsOf($nodes[6])->get()->count()); $this->assertTrue($nodes[2]->isParentOf(5));