Skip to content

Commit

Permalink
checked #239 by writing another test
Browse files Browse the repository at this point in the history
  • Loading branch information
franzose committed Oct 5, 2020
1 parent 257cd50 commit 384cc21
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/Models/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Franzose\ClosureTable\Extensions\Collection;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Arr;
use InvalidArgumentException;

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

foreach ($tree as $item) {
$children = Arr::pull($item, static::CHILDREN_RELATION_NAME);
$children = $item[static::CHILDREN_RELATION_NAME] ?? [];

/**
* @var Entity $entity
Expand Down
44 changes: 44 additions & 0 deletions tests/Models/Entity/TreeTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Franzose\ClosureTable\Tests\Models\Entity;

use Franzose\ClosureTable\Extensions\Collection;
use Franzose\ClosureTable\Models\ClosureTable;
use Franzose\ClosureTable\Models\Entity;
use Franzose\ClosureTable\Tests\BaseTestCase;
Expand Down Expand Up @@ -162,4 +163,47 @@ public function testCreateFromArrayBug81()
static::assertEquals(0, $child2->countChildren());
static::assertEquals(19, $child2->getKey());
}

/**
* @link https://github.com/franzose/ClosureTable/issues/239
*/
public function testCreateFromArrayIssue239()
{
Page::createFromArray([
[
'id' => 100,
'children' => [
['id' => 200],
['id' => 300],
['id' => 400],
['id' => 500],
[
'id' => 600,
'children' => [
['id' => 700],
['id' => 800],
]
],
]
]
]);

/** @var Page $page */
$page = Page::find(100);

/** @var Collection|Page[] $children */
$children = $page->getChildren();

static::assertCount(5, $children);
static::assertEquals(200, $children->get(0)->id);
static::assertEquals(300, $children->get(1)->id);
static::assertEquals(400, $children->get(2)->id);
static::assertEquals(500, $children->get(3)->id);
static::assertEquals(600, $children->get(4)->id);

$childrenOf600 = $children->get(4)->getChildren();
static::assertCount(2, $childrenOf600);
static::assertEquals(700, $childrenOf600->get(0)->id);
static::assertEquals(800, $childrenOf600->get(1)->id);
}
}

0 comments on commit 384cc21

Please sign in to comment.