Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion phpunit.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
$capsule->bootEloquent();
$capsule->setAsGlobal();

include __DIR__.'/tests/models/Category.php';
include __DIR__.'/tests/models/Category.php';
include __DIR__.'/tests/models/CategoryWithCustomParent.php';
34 changes: 34 additions & 0 deletions tests/NodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ public static function setUpBeforeClass()
NestedSet::columns($table);
});

$schema->create('categories_with_custom_parent', function (\Illuminate\Database\Schema\Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->unsignedInteger('parent_category_id')->nullable();
$table->unsignedInteger('_lft')->default(0);
$table->unsignedInteger('_rgt')->default(0);
$table->softDeletes();
});

Capsule::enableQueryLog();
}

Expand All @@ -29,6 +38,10 @@ public function setUp()

Capsule::table('categories')->insert($data);

$data = include __DIR__.'/data/categories_with_custom_parent.php';

Capsule::table('categories_with_custom_parent')->insert($data);

Capsule::flushQueryLog();

Category::resetActionsPerformed();
Expand All @@ -39,6 +52,7 @@ public function setUp()
public function tearDown()
{
Capsule::table('categories')->truncate();
Capsule::table('categories_with_custom_parent')->truncate();
}

// public static function tearDownAfterClass()
Expand Down Expand Up @@ -338,6 +352,26 @@ public function testParentIdAttributeAccessorAppendsNode()
$this->assertTrue($node->isRoot());
}

public function testParentIdCustomAttributeAccessorAppendsNode()
{
$node = new CategoryWithCustomParent([
'name' => 'lg',
'parent_category_id' => 5,
]);
$node->save();

$this->assertEquals(5, $node->parent_category_id);
$this->assertEquals(5, $node->getParentId());

$node->parent_category_id = null;
$node->save();

$node->refreshNode();

$this->assertEquals(null, $node->parent_category_id);
$this->assertTrue($node->isRoot());
}

/**
* @expectedException Exception
*/
Expand Down
15 changes: 15 additions & 0 deletions tests/data/categories_with_custom_parent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

return [
['id' => 1, 'name' => 'store', '_lft' => 1, '_rgt' => 20, 'parent_category_id' => null],
['id' => 2, 'name' => 'notebooks', '_lft' => 2, '_rgt' => 7, 'parent_category_id' => 1],
['id' => 3, 'name' => 'apple', '_lft' => 3, '_rgt' => 4, 'parent_category_id' => 2],
['id' => 4, 'name' => 'lenovo', '_lft' => 5, '_rgt' => 6, 'parent_category_id' => 2],
['id' => 5, 'name' => 'mobile', '_lft' => 8, '_rgt' => 19, 'parent_category_id' => 1],
['id' => 6, 'name' => 'nokia', '_lft' => 9, '_rgt' => 10, 'parent_category_id' => 5],
['id' => 7, 'name' => 'samsung', '_lft' => 11, '_rgt' => 14, 'parent_category_id' => 5],
['id' => 8, 'name' => 'galaxy', '_lft' => 12, '_rgt' => 13, 'parent_category_id' => 7],
['id' => 9, 'name' => 'sony', '_lft' => 15, '_rgt' => 16, 'parent_category_id' => 5],
['id' => 10, 'name' => 'lenovo', '_lft' => 17, '_rgt' => 18, 'parent_category_id' => 5],
['id' => 11, 'name' => 'store_2', '_lft' => 21, '_rgt' => 22, 'parent_category_id' => null],
];
30 changes: 30 additions & 0 deletions tests/models/CategoryWithCustomParent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use \Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Kalnoy\Nestedset\NodeTrait;

class CategoryWithCustomParent extends Model
{
use SoftDeletes;
use NodeTrait;

protected $table = 'categories_with_custom_parent';

protected $fillable = [
'name',
'parent_category_id',
];

public $timestamps = false;

public static function resetActionsPerformed()
{
static::$actionsPerformed = 0;
}

public function getParentIdName()
{
return 'parent_category_id';
}
}