Completed
Push — master ( 730998...f24d78 )
by Mahmoud
03:24
created

testDetachMultiplePermissionFromRole_()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 48
rs 9.125
cc 1
eloc 28
nc 1
nop 0
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 DetachPermissionsFromRoleTest.
11
 *
12
 * @author  Mahmoud Zalt <[email protected]>
13
 */
14
class DetachPermissionsFromRoleTest extends TestCase
15
{
16
17
    protected $endpoint = '/permissions/detach';
18
19
    protected $access = [
20
        'roles'       => 'admin',
21
        'permissions' => '',
22
    ];
23
24 View Code Duplication
    public function testDetachSinglePermissionFromRole_()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
25
    {
26
        $this->getTestingAdmin();
27
28
        $permissionA = Permission::create([
29
            'name'         => 'permission-A',
30
            'description'  => 'AA',
31
            'display_name' => 'A',
32
        ]);
33
34
        $roleA = Role::create([
35
            'name'         => 'role-A',
36
            'description'  => 'AA',
37
            'display_name' => 'A',
38
        ]);
39
40
        $roleA->givePermissionTo($permissionA);
41
42
        $data = [
43
            'role_name'       => $roleA['name'],
44
            'permission_name' => $permissionA['name'],
45
        ];
46
47
        // send the HTTP request
48
        $response = $this->apiCall($this->endpoint, 'post', $data, true);
49
50
        // assert response status is correct
51
        $this->assertEquals('200', $response->getStatusCode());
52
53
        $responseObject = $this->getResponseObject($response);
54
55
        $this->assertEquals($roleA['name'], $responseObject->data->name);
56
57
        $this->missingFromDatabase('role_has_permissions', [
58
            'permission_id' => $permissionA->id,
59
            'role_id'       => $roleA->id
60
        ]);
61
    }
62
63
    public function testDetachMultiplePermissionFromRole_()
64
    {
65
        $this->getTestingAdmin();
66
67
        $permissionA = Permission::create([
68
            'name'         => 'permission-A',
69
            'description'  => 'AA',
70
            'display_name' => 'A',
71
        ]);
72
73
        $permissionB = Permission::create([
74
            'name'         => 'permission-B',
75
            'description'  => 'BB',
76
            'display_name' => 'B',
77
        ]);
78
79
        $roleA = Role::create([
80
            'name'         => 'role-A',
81
            'description'  => 'AA',
82
            'display_name' => 'A',
83
        ]);
84
85
        $roleA->givePermissionTo($permissionA);
86
        $roleA->givePermissionTo($permissionB);
87
88
        $data = [
89
            'role_name'       => $roleA['name'],
90
            'permission_name' => [$permissionA['name'], $permissionB['name']]
91
        ];
92
93
        // send the HTTP request
94
        $response = $this->apiCall($this->endpoint, 'post', $data, true);
95
96
        // assert response status is correct
97
        $this->assertEquals('200', $response->getStatusCode());
98
99
        $responseObject = $this->getResponseObject($response);
0 ignored issues
show
Unused Code introduced by
$responseObject is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
100
101
        $responseObject = $this->getResponseObject($response);
102
103
        $this->assertEquals($roleA['name'], $responseObject->data->name);
104
105
        $this->missingFromDatabase('role_has_permissions', [
106
            'permission_id' => $permissionA->id,
107
            'permission_id' => $permissionB->id,
108
            'role_id'       => $roleA->id
109
        ]);
110
    }
111
112
113
}
114