1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Containers\Authorization\UI\API\Tests\Functional; |
4
|
|
|
|
5
|
|
|
use App\Containers\Authorization\Models\Permission; |
6
|
|
|
use App\Containers\Authorization\Models\Role; |
7
|
|
|
use App\Port\Test\PHPUnit\Abstracts\TestCase; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AttachPermissionsToRoleTest. |
11
|
|
|
* |
12
|
|
|
* @author Mahmoud Zalt <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class AttachPermissionsToRoleTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
protected $endpoint = '/permissions/attach'; |
18
|
|
|
|
19
|
|
|
protected $access = [ |
20
|
|
|
'roles' => 'admin', |
21
|
|
|
'permissions' => '', |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
View Code Duplication |
public function testAttachSinglePermissionToRole_() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$this->getTestingAdmin(); |
27
|
|
|
|
28
|
|
|
$roleA = Role::create([ |
29
|
|
|
'name' => 'role-A', |
30
|
|
|
'description' => 'AA', |
31
|
|
|
'display_name' => 'A', |
32
|
|
|
]); |
33
|
|
|
|
34
|
|
|
$permissionA = Permission::create([ |
35
|
|
|
'name' => 'permission-A', |
36
|
|
|
'description' => 'AA', |
37
|
|
|
'display_name' => 'A', |
38
|
|
|
]); |
39
|
|
|
|
40
|
|
|
$data = [ |
41
|
|
|
'role_name' => $roleA['name'], |
42
|
|
|
'permission_name' => $permissionA['name'], |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
// send the HTTP request |
46
|
|
|
$response = $this->apiCall($this->endpoint, 'post', $data, true); |
47
|
|
|
|
48
|
|
|
// assert response status is correct |
49
|
|
|
$this->assertEquals('200', $response->getStatusCode()); |
50
|
|
|
|
51
|
|
|
$responseObject = $this->getResponseObject($response); |
52
|
|
|
|
53
|
|
|
$this->assertEquals($roleA['name'], $responseObject->data->name); |
54
|
|
|
|
55
|
|
|
$this->seeInDatabase('role_has_permissions', [ |
56
|
|
|
'permission_id' => $permissionA->id, |
57
|
|
|
'role_id' => $roleA->id |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testAttachMultiplePermissionToRole_() |
62
|
|
|
{ |
63
|
|
|
$this->getTestingAdmin(); |
64
|
|
|
|
65
|
|
|
$roleA = Role::create([ |
66
|
|
|
'name' => 'role-A', |
67
|
|
|
'description' => 'AA', |
68
|
|
|
'display_name' => 'A', |
69
|
|
|
]); |
70
|
|
|
|
71
|
|
|
$permissionA = Permission::create([ |
72
|
|
|
'name' => 'permission-A', |
73
|
|
|
'description' => 'AA', |
74
|
|
|
'display_name' => 'A', |
75
|
|
|
]); |
76
|
|
|
|
77
|
|
|
$permissionB = Permission::create([ |
78
|
|
|
'name' => 'permission-B', |
79
|
|
|
'description' => 'BB', |
80
|
|
|
'display_name' => 'B', |
81
|
|
|
]); |
82
|
|
|
|
83
|
|
|
$data = [ |
84
|
|
|
'role_name' => $roleA['name'], |
85
|
|
|
'permission_name' => [$permissionA['name'], $permissionB['name']] |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
// send the HTTP request |
89
|
|
|
$response = $this->apiCall($this->endpoint, 'post', $data, true); |
90
|
|
|
|
91
|
|
|
// assert response status is correct |
92
|
|
|
$this->assertEquals('200', $response->getStatusCode()); |
93
|
|
|
|
94
|
|
|
$this->seeInDatabase('role_has_permissions', [ |
95
|
|
|
'permission_id' => $permissionA->id, |
96
|
|
|
'permission_id' => $permissionB->id, |
97
|
|
|
'role_id' => $roleA->id |
98
|
|
|
]); |
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.