Skip to content

Commit 9398174

Browse files
committed
1 parent fb0ccd3 commit 9398174

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

src/Node.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ protected function deleteDescendants()
750750

751751
// In case if user wants to re-create the node
752752
$this->makeRoot();
753+
754+
static::$actionsPerformed++;
753755
}
754756
}
755757

tests/NodeTest.php

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public function setUp()
2525
$data = include __DIR__.'/data/categories.php';
2626

2727
Capsule::table('categories')->insert($data);
28+
29+
Category::resetActionsPerformed();
2830
}
2931

3032
public function tearDown()
@@ -250,7 +252,7 @@ public function testDescendants()
250252
{
251253
$node = $this->findCategory('mobile');
252254
$descendants = $node->descendants()->lists('name');
253-
$expected = array('nokia', 'samsung', 'galaxy', 'sony');
255+
$expected = array('nokia', 'samsung', 'galaxy', 'sony', 'lenovo');
254256

255257
$this->assertEquals($expected, $descendants);
256258

@@ -358,8 +360,6 @@ public function testSoftDeletedNodeisDeletedWhenParentIsDeleted()
358360

359361
$this->assertTreeNotBroken();
360362

361-
$this->dumpTree();
362-
363363
$this->assertNull($this->findCategory('samsung', true));
364364
$this->assertNull($this->findCategory('sony'));
365365
}
@@ -382,16 +382,16 @@ public function testSiblings()
382382
$next = $node->nextSiblings()->lists('id');
383383
$prev = $node->prevSiblings()->lists('id');
384384

385-
$this->assertEquals(array(6, 9), $siblings);
386-
$this->assertEquals(array(9), $next);
385+
$this->assertEquals(array(6, 9, 10), $siblings);
386+
$this->assertEquals(array(9, 10), $next);
387387
$this->assertEquals(array(6), $prev);
388388

389389
$siblings = $node->getSiblings()->lists('id');
390390
$next = $node->getNextSiblings()->lists('id');
391391
$prev = $node->getPrevSiblings()->lists('id');
392392

393-
$this->assertEquals(array(6, 9), $siblings);
394-
$this->assertEquals(array(9), $next);
393+
$this->assertEquals(array(6, 9, 10), $siblings);
394+
$this->assertEquals(array(9, 10), $next);
395395
$this->assertEquals(array(6), $prev);
396396

397397
$next = $node->getNextSibling();
@@ -417,7 +417,7 @@ public function testToTreeBuildsWithDefaultOrder()
417417

418418
$root = $tree->first();
419419
$this->assertEquals('mobile', $root->name);
420-
$this->assertEquals(3, count($root->children));
420+
$this->assertEquals(4, count($root->children));
421421
}
422422

423423
public function testToTreeBuildsWithCustomOrder()
@@ -431,7 +431,7 @@ public function testToTreeBuildsWithCustomOrder()
431431

432432
$root = $tree->first();
433433
$this->assertEquals('mobile', $root->name);
434-
$this->assertEquals(3, count($root->children));
434+
$this->assertEquals(4, count($root->children));
435435
$this->assertEquals($root, $root->children->first()->parent);
436436
}
437437

@@ -443,8 +443,8 @@ public function testToTreeWithSpecifiedRoot()
443443
$tree1 = \Kalnoy\Nestedset\Collection::make($nodes)->toTree(5);
444444
$tree2 = \Kalnoy\Nestedset\Collection::make($nodes)->toTree($node);
445445

446-
$this->assertEquals(3, $tree1->count());
447-
$this->assertEquals(3, $tree2->count());
446+
$this->assertEquals(4, $tree1->count());
447+
$this->assertEquals(4, $tree2->count());
448448
}
449449

450450
public function testToTreeBuildsWithDefaultOrderAndMultipleRootNodes()
@@ -458,7 +458,7 @@ public function testToTreeBuildsWithRootItemIdProvided()
458458
{
459459
$tree = Category::whereBetween('_lft', array(8, 17))->get()->toTree(5);
460460

461-
$this->assertEquals(3, count($tree));
461+
$this->assertEquals(4, count($tree));
462462

463463
$root = $tree[1];
464464
$this->assertEquals('samsung', $root->name);
@@ -501,7 +501,7 @@ public function testDefaultCategoryIsSavedAsRoot()
501501
$node = new Category([ 'name' => 'test' ]);
502502
$node->save();
503503

504-
$this->assertEquals(19, $node->_lft);
504+
$this->assertEquals(21, $node->_lft);
505505
$this->assertTreeNotBroken();
506506

507507
$this->assertTrue($node->isRoot());
@@ -545,7 +545,7 @@ public function testCreatesNode()
545545
{
546546
$node = Category::create([ 'name' => 'test' ]);
547547

548-
$this->assertEquals(19, $node->getLft());
548+
$this->assertEquals(21, $node->getLft());
549549
}
550550

551551
public function testCreatesViaRelationship()
@@ -604,4 +604,16 @@ public function testDescendantsByNode()
604604

605605
$this->assertEquals([ 3, 4 ], $res);
606606
}
607+
608+
public function testMultipleDeletionsDoNotBrakeTree()
609+
{
610+
$category = $this->findCategory('mobile');
611+
612+
foreach ($category->children()->take(2)->get() as $child)
613+
{
614+
$child->forceDelete();
615+
}
616+
617+
$this->assertTreeNotBroken();
618+
}
607619
}

tests/data/categories.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?php
22

33
return array(
4-
array('id' => 1, 'name' => 'store', '_lft' => 1, '_rgt' => 18, 'parent_id' => null),
4+
array('id' => 1, 'name' => 'store', '_lft' => 1, '_rgt' => 20, 'parent_id' => null),
55
array('id' => 2, 'name' => 'notebooks', '_lft' => 2, '_rgt' => 7, 'parent_id' => 1),
66
array('id' => 3, 'name' => 'apple', '_lft' => 3, '_rgt' => 4, 'parent_id' => 2),
77
array('id' => 4, 'name' => 'lenovo', '_lft' => 5, '_rgt' => 6, 'parent_id' => 2),
8-
array('id' => 5, 'name' => 'mobile', '_lft' => 8, '_rgt' => 17, 'parent_id' => 1),
8+
array('id' => 5, 'name' => 'mobile', '_lft' => 8, '_rgt' => 19, 'parent_id' => 1),
99
array('id' => 6, 'name' => 'nokia', '_lft' => 9, '_rgt' => 10, 'parent_id' => 5),
1010
array('id' => 7, 'name' => 'samsung', '_lft' => 11, '_rgt' => 14, 'parent_id' => 5),
1111
array('id' => 8, 'name' => 'galaxy', '_lft' => 12, '_rgt' => 13, 'parent_id' => 7),
1212
array('id' => 9, 'name' => 'sony', '_lft' => 15, '_rgt' => 16, 'parent_id' => 5),
13+
array('id' => 10, 'name' => 'lenovo', '_lft' => 17, '_rgt' => 18, 'parent_id' => 5),
1314
);

tests/models/Category.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@ class Category extends Kalnoy\Nestedset\Node {
77
protected $fillable = array('name', 'parent_id');
88

99
public $timestamps = false;
10+
11+
public static function resetActionsPerformed()
12+
{
13+
static::$actionsPerformed = 0;
14+
}
1015
}

0 commit comments

Comments
 (0)