Skip to content

Commit 384cc21

Browse files
committed
checked #239 by writing another test
1 parent 257cd50 commit 384cc21

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/Models/Entity.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Franzose\ClosureTable\Extensions\Collection;
88
use Illuminate\Database\Eloquent\Relations\BelongsTo;
99
use Illuminate\Database\Eloquent\Relations\HasMany;
10-
use Illuminate\Support\Arr;
1110
use InvalidArgumentException;
1211

1312
/**
@@ -1763,7 +1762,7 @@ public static function createFromArray(array $tree, EntityInterface $parent = nu
17631762
$entities = [];
17641763

17651764
foreach ($tree as $item) {
1766-
$children = Arr::pull($item, static::CHILDREN_RELATION_NAME);
1765+
$children = $item[static::CHILDREN_RELATION_NAME] ?? [];
17671766

17681767
/**
17691768
* @var Entity $entity

tests/Models/Entity/TreeTests.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Franzose\ClosureTable\Tests\Models\Entity;
44

5+
use Franzose\ClosureTable\Extensions\Collection;
56
use Franzose\ClosureTable\Models\ClosureTable;
67
use Franzose\ClosureTable\Models\Entity;
78
use Franzose\ClosureTable\Tests\BaseTestCase;
@@ -162,4 +163,47 @@ public function testCreateFromArrayBug81()
162163
static::assertEquals(0, $child2->countChildren());
163164
static::assertEquals(19, $child2->getKey());
164165
}
166+
167+
/**
168+
* @link https://github.com/franzose/ClosureTable/issues/239
169+
*/
170+
public function testCreateFromArrayIssue239()
171+
{
172+
Page::createFromArray([
173+
[
174+
'id' => 100,
175+
'children' => [
176+
['id' => 200],
177+
['id' => 300],
178+
['id' => 400],
179+
['id' => 500],
180+
[
181+
'id' => 600,
182+
'children' => [
183+
['id' => 700],
184+
['id' => 800],
185+
]
186+
],
187+
]
188+
]
189+
]);
190+
191+
/** @var Page $page */
192+
$page = Page::find(100);
193+
194+
/** @var Collection|Page[] $children */
195+
$children = $page->getChildren();
196+
197+
static::assertCount(5, $children);
198+
static::assertEquals(200, $children->get(0)->id);
199+
static::assertEquals(300, $children->get(1)->id);
200+
static::assertEquals(400, $children->get(2)->id);
201+
static::assertEquals(500, $children->get(3)->id);
202+
static::assertEquals(600, $children->get(4)->id);
203+
204+
$childrenOf600 = $children->get(4)->getChildren();
205+
static::assertCount(2, $childrenOf600);
206+
static::assertEquals(700, $childrenOf600->get(0)->id);
207+
static::assertEquals(800, $childrenOf600->get(1)->id);
208+
}
165209
}

0 commit comments

Comments
 (0)