Skip to content

Commit 5ba548b

Browse files
committed
Fix nesting tests
1 parent a769221 commit 5ba548b

File tree

6 files changed

+66
-123
lines changed

6 files changed

+66
-123
lines changed

src/Models/Permission.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ public function __construct(array $attributes = [])
3434
parent::__construct($attributes);
3535

3636
$this->guarded[] = $this->primaryKey;
37-
}
38-
39-
public function getTable()
40-
{
41-
return config('permission.table_names.permissions', parent::getTable());
37+
$this->table = config('permission.table_names.permissions') ?: parent::getTable();
4238
}
4339

4440
public static function create(array $attributes = [])

src/Models/Role.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct(array $attributes = [])
3434
parent::__construct($attributes);
3535

3636
$this->guarded[] = $this->primaryKey;
37-
$this->table = config('permission.table_names.roles', parent::getTable());
37+
$this->table = config('permission.table_names.roles') ?: parent::getTable();
3838
}
3939

4040
public static function create(array $attributes = [])

tests/Role.php

+26
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,32 @@ class Role extends \Spatie\Permission\Models\Role
1111
'name',
1212
];
1313

14+
const HIERARCHY_TABLE = 'roles_hierarchy';
15+
16+
/**
17+
* @return BelongsToMany
18+
*/
19+
public function parents()
20+
{
21+
return $this->belongsToMany(
22+
static::class,
23+
static::HIERARCHY_TABLE,
24+
'child_id',
25+
'parent_id');
26+
}
27+
28+
/**
29+
* @return BelongsToMany
30+
*/
31+
public function children()
32+
{
33+
return $this->belongsToMany(
34+
static::class,
35+
static::HIERARCHY_TABLE,
36+
'parent_id',
37+
'child_id');
38+
}
39+
1440
protected static function boot()
1541
{
1642
parent::boot();

tests/RoleWithNesting.php

-36
This file was deleted.

tests/RoleWithNestingTest.php

+38-41
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,62 @@
22

33
namespace Spatie\Permission\Test;
44

5+
use Illuminate\Database\Schema\Blueprint;
6+
57
class RoleWithNestingTest extends TestCase
68
{
7-
private static $old_migration;
9+
/** @var bool */
10+
protected $useCustomModels = true;
811

9-
/**
10-
* @var RoleWithNesting[]
11-
*/
12+
/** @var Role[] */
1213
protected $parent_roles = [];
1314

14-
/**
15-
* @var RoleWithNesting[]
16-
*/
15+
/** @var Role[] */
1716
protected $child_roles = [];
1817

19-
public static function setUpBeforeClass(): void
20-
{
21-
parent::setUpBeforeClass();
22-
self::$old_migration = self::$migration;
23-
self::$migration = self::getMigration();
24-
}
2518

2619
public function setUp(): void
2720
{
2821
parent::setUp();
29-
$this->parent_roles = [];
30-
$this->child_roles = [];
31-
$this->parent_roles['has_no_children'] = RoleWithNesting::create(['name' => 'has_no_children']);
32-
$this->parent_roles['has_1_child'] = RoleWithNesting::create(['name' => 'has_1_child']);
33-
$this->parent_roles['has_3_children'] = RoleWithNesting::create(['name' => 'has_3_children']);
3422

35-
$this->child_roles['has_no_parents'] = RoleWithNesting::create(['name' => 'has_no_parents']);
36-
$this->child_roles['has_1_parent'] = RoleWithNesting::create(['name' => 'has_1_parent']);
37-
$this->child_roles['has_2_parents'] = RoleWithNesting::create(['name' => 'has_2_parents']);
38-
$this->child_roles['third_child'] = RoleWithNesting::create(['name' => 'third_child']);
23+
$this->parent_roles = [
24+
'has_no_children' => Role::create(['name' => 'has_no_children']),
25+
'has_1_child' => Role::create(['name' => 'has_1_child']),
26+
'has_3_children' => Role::create(['name' => 'has_3_children']),
27+
];
28+
$this->child_roles = [
29+
'has_no_parents' => Role::create(['name' => 'has_no_parents']),
30+
'has_1_parent' => Role::create(['name' => 'has_1_parent']),
31+
'has_2_parents' => Role::create(['name' => 'has_2_parents']),
32+
'third_child' => Role::create(['name' => 'third_child']),
33+
];
3934

4035
$this->parent_roles['has_1_child']->children()->attach($this->child_roles['has_2_parents']);
41-
$this->parent_roles['has_3_children']->children()->attach($this->child_roles['has_2_parents']);
42-
$this->parent_roles['has_3_children']->children()->attach($this->child_roles['has_1_parent']);
43-
$this->parent_roles['has_3_children']->children()->attach($this->child_roles['third_child']);
44-
}
45-
46-
public static function tearDownAfterClass(): void
47-
{
48-
parent::tearDownAfterClass();
49-
self::$migration = self::$old_migration;
36+
$this->parent_roles['has_3_children']->children()->attach([
37+
$this->child_roles['has_2_parents']->getKey(),
38+
$this->child_roles['has_1_parent']->getKey(),
39+
$this->child_roles['third_child']->getKey()
40+
]);
5041
}
5142

52-
protected function getEnvironmentSetUp($app)
43+
/**
44+
* Set up the database.
45+
*
46+
* @param \Illuminate\Foundation\Application $app
47+
*/
48+
protected function setUpDatabase($app)
5349
{
54-
parent::getEnvironmentSetUp($app);
55-
$app['config']->set('permission.models.role', RoleWithNesting::class);
56-
$app['config']->set('permission.table_names.roles', 'nesting_role');
57-
}
50+
parent::setUpDatabase($app);
5851

59-
protected static function getMigration()
60-
{
61-
require_once __DIR__.'/customMigrations/roles_with_nesting_migration.php.stub';
52+
$tableRoles = $app['config']->get('permission.table_names.roles');
6253

63-
return new \CreatePermissionTablesWithNested();
54+
$app['db']->connection()->getSchemaBuilder()->create(Role::HIERARCHY_TABLE, function ($table) use ($tableRoles) {
55+
$table->id();
56+
$table->uuid("parent_id");
57+
$table->uuid("child_id");
58+
$table->foreign("parent_id")->references("role_test_id")->on($tableRoles);
59+
$table->foreign("child_id")->references("role_test_id")->on($tableRoles);
60+
});
6461
}
6562

6663
/** @test
@@ -71,7 +68,7 @@ public function it_returns_correct_withCount_of_nested_roles($role_group, $index
7168
$role = $this->$role_group[$index];
7269
$count_field_name = sprintf('%s_count', $relation);
7370

74-
$actualCount = intval(RoleWithNesting::query()->withCount($relation)->find($role->id)->$count_field_name);
71+
$actualCount = intval(Role::withCount($relation)->find($role->getKey())->$count_field_name);
7572

7673
$this->assertSame(
7774
$expectedCount,

tests/customMigrations/roles_with_nesting_migration.php.stub

-40
This file was deleted.

0 commit comments

Comments
 (0)