Completed
Push — master ( 3c952a...bf66da )
by Mahmoud
03:33
created

DetachPermissionsFromRoleTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 75
Duplicated Lines 38.67 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 1
cbo 2
dl 29
loc 75
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B testDetachSinglePermissionFromRole_() 29 29 1
B testDetachMultiplePermissionFromRole_() 0 32 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Containers\Authorization\Tests\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 = factory(Permission::class)->create();
29
30
        $roleA = factory(Role::class)->create();
31
        $roleA->givePermissionTo($permissionA);
32
33
        $data = [
34
            'role_id'         => $roleA->getHashedKey(),
35
            'permissions_ids' => $permissionA->getHashedKey(),
36
        ];
37
38
        // send the HTTP request
39
        $response = $this->apiCall($this->endpoint, 'post', $data, true);
40
41
        // assert response status is correct
42
        $this->assertEquals('200', $response->getStatusCode());
43
44
        $responseObject = $this->getResponseObject($response);
45
46
        $this->assertEquals($roleA->name, $responseObject->data->name);
47
48
        $this->missingFromDatabase('role_has_permissions', [
49
            'permission_id' => $permissionA->id,
50
            'role_id'       => $roleA->id
51
        ]);
52
    }
53
54
    public function testDetachMultiplePermissionFromRole_()
55
    {
56
        $this->getTestingAdmin();
57
58
        $permissionA = factory(Permission::class)->create();
59
        $permissionB = factory(Permission::class)->create();
60
61
        $roleA = factory(Role::class)->create();
62
        $roleA->givePermissionTo($permissionA);
63
        $roleA->givePermissionTo($permissionB);
64
65
        $data = [
66
            'role_id'         => $roleA->getHashedKey(),
67
            'permissions_ids' => [$permissionA->getHashedKey(), $permissionB->getHashedKey()],
68
        ];
69
70
        // send the HTTP request
71
        $response = $this->apiCall($this->endpoint, 'post', $data, true);
72
73
        // assert response status is correct
74
        $this->assertEquals('200', $response->getStatusCode());
75
76
        $responseObject = $this->getResponseObject($response);
77
78
        $this->assertEquals($roleA->name, $responseObject->data->name);
79
80
        $this->missingFromDatabase('role_has_permissions', [
81
            'permission_id' => $permissionA->id,
82
            'permission_id' => $permissionB->id,
83
            'role_id'       => $roleA->id
84
        ]);
85
    }
86
87
88
}
89