2
2
3
3
namespace Spatie \Permission \Test ;
4
4
5
+ use Illuminate \Database \Schema \Blueprint ;
6
+
5
7
class RoleWithNestingTest extends TestCase
6
8
{
7
- private static $ old_migration ;
9
+ /** @var bool */
10
+ protected $ useCustomModels = true ;
8
11
9
- /**
10
- * @var RoleWithNesting[]
11
- */
12
+ /** @var Role[] */
12
13
protected $ parent_roles = [];
13
14
14
- /**
15
- * @var RoleWithNesting[]
16
- */
15
+ /** @var Role[] */
17
16
protected $ child_roles = [];
18
17
19
- public static function setUpBeforeClass (): void
20
- {
21
- parent ::setUpBeforeClass ();
22
- self ::$ old_migration = self ::$ migration ;
23
- self ::$ migration = self ::getMigration ();
24
- }
25
18
26
19
public function setUp (): void
27
20
{
28
21
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 ' ]);
34
22
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
+ ];
39
34
40
35
$ 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
+ ]);
50
41
}
51
42
52
- protected function getEnvironmentSetUp ($ app )
43
+ /**
44
+ * Set up the database.
45
+ *
46
+ * @param \Illuminate\Foundation\Application $app
47
+ */
48
+ protected function setUpDatabase ($ app )
53
49
{
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 );
58
51
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 ' );
62
53
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
+ });
64
61
}
65
62
66
63
/** @test
@@ -71,7 +68,7 @@ public function it_returns_correct_withCount_of_nested_roles($role_group, $index
71
68
$ role = $ this ->$ role_group [$ index ];
72
69
$ count_field_name = sprintf ('%s_count ' , $ relation );
73
70
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 );
75
72
76
73
$ this ->assertSame (
77
74
$ expectedCount ,
0 commit comments