Completed
Push — master ( 5798cc...56eac4 )
by Mahmoud
03:11
created

AssignUserToRoleTest::testAssignUserToManyRoles_()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace App\Containers\Order\UI\API\Tests\Functional;
4
5
use App\Containers\User\Models\User;
6
use App\Port\Test\PHPUnit\Abstracts\TestCase;
7
8
/**
9
 * Class AssignUserToRoleTest.
10
 *
11
 * @author  Mahmoud Zalt <[email protected]>
12
 */
13
class AssignUserToRoleTest extends TestCase
14
{
15
16
    protected $endpoint = '/roles/assign';
17
18
    protected $permissions = [
19
        'admin-access' // no need to set `admin-access` since it's given to the admins by default while seeding.
20
    ];
21
22
    public function testAssignUserToRole_()
23
    {
24
        $this->getLoggedInTestingAdmin();
25
26
        $randomUser = factory(User::class)->create();
27
28
        $data = [
29
            'roles_names' => 'admin',
30
            'user_id'     => $randomUser->getHashedKey(),
31
        ];
32
33
        // send the HTTP request
34
        $response = $this->apiCall($this->endpoint, 'post', $data, true);
35
36
        // assert response status is correct
37
        $this->assertEquals($response->getStatusCode(), '200');
38
39
        $responseObject = $this->getResponseObject($response);
40
41
        $this->assertEquals($data['user_id'], $responseObject->data->id);
42
43
        $this->assertEquals($data['roles_names'], $responseObject->data->roles->data[0]->name);
44
    }
45
46
    public function testAssignUserToRoleWithRealId_()
47
    {
48
        $this->getLoggedInTestingAdmin();
49
50
        $randomUser = factory(User::class)->create();
51
52
        $data = [
53
            'roles_names' => 'admin',
54
            'user_id'     => $randomUser->id,
55
        ];
56
57
        // send the HTTP request
58
        $response = $this->apiCall($this->endpoint, 'post', $data, true);
59
60
        // assert response status is correct
61
        $this->assertEquals($response->getStatusCode(), '400');
62
63
        $this->assertResponseContainKeyValue([
64
            'message' => 'Only Hashed ID\'s allowed to be passed.',
65
        ], $response);
66
    }
67
68
69
    public function testAssignUserToManyRoles_()
70
    {
71
        $admin = $this->getLoggedInTestingAdmin();
0 ignored issues
show
Unused Code introduced by
$admin 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...
72
73
        $randomUser = factory(User::class)->create();
74
75
        $data = [
76
            'roles_names' => ['admin', 'client'],
77
            'user_id'     => $randomUser->getHashedKey(),
78
        ];
79
80
        // send the HTTP request
81
        $response = $this->apiCall($this->endpoint, 'post', $data, true);
82
83
        // assert response status is correct
84
        $this->assertEquals($response->getStatusCode(), '200');
85
86
        $responseObject = $this->getResponseObject($response);
87
88
        $this->assertTrue(count($responseObject->data->roles->data) > 1);
89
90
        $this->assertEquals($data['roles_names'][0], $responseObject->data->roles->data[0]->name);
91
92
        $this->assertEquals($data['roles_names'][1], $responseObject->data->roles->data[1]->name);
93
    }
94
95
}
96