GetAllUsersTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 80
Duplicated Lines 22.5 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 18
loc 80
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetAllUsersByAdmin_() 0 17 1
A testGetAllUsersByNonAdmin_() 18 18 1
A testSearchUsersByName() 0 22 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\User\UI\API\Tests\Functional;
4
5
use App\Containers\User\Models\User;
6
use App\Containers\User\Tests\ApiTestCase;
7
8
/**
9
 * Class GetAllUsersTest.
10
 *
11
 * @group user
12
 * @group api
13
 *
14
 * @author  Mahmoud Zalt <[email protected]>
15
 */
16
class GetAllUsersTest extends ApiTestCase
17
{
18
19
    protected $endpoint = '[email protected]/users';
20
21
    protected $access = [
22
        'roles'       => 'admin',
23
        'permissions' => 'list-users',
24
    ];
25
26
    /**
27
     * @test
28
     */
29
    public function testGetAllUsersByAdmin_()
30
    {
31
        // create some non-admin users who are clients
32
        factory(User::class, 2)->create();
33
34
        // send the HTTP request
35
        $response = $this->makeCall();
36
37
        // assert response status is correct
38
        $response->assertStatus(200);
39
40
        // convert JSON response string to Object
41
        $responseContent = $this->getResponseContentObject();
42
43
        // assert the returned data size is correct
44
        $this->assertCount(4, $responseContent->data);
45
    }
46
47
    /**
48
     * @test
49
     */
50 View Code Duplication
    public function testGetAllUsersByNonAdmin_()
0 ignored issues
show
Duplication introduced by Mustafa Elkhalifa
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...
51
    {
52
        $this->getTestingUserWithoutAccess();
53
54
        // create some fake users
55
        factory(User::class, 2)->create();
56
57
        // send the HTTP request
58
        $response = $this->makeCall();
59
60
        // assert response status is correct
61
        $response->assertStatus(403);
62
63
        $this->assertResponseContainKeyValue([
64
            'errors' => 'You have no access to this resource!',
65
            'message' => 'This action is unauthorized.',
66
        ]);
67
    }
68
69
    /**
70
     * @test
71
     */
72
    public function testSearchUsersByName()
73
    {
74
        $user = $this->getTestingUser([
75
            'name' => 'mahmoudzzz'
76
        ]);
77
78
        // 3 random users
79
        factory(User::class, 3)->create();
80
81
        // send the HTTP request
82
        $response = $this->endpoint($this->endpoint. '?search=name:mahmoudzzz')->makeCall();
83
84
        // assert response status is correct
85
        $response->assertStatus(200);
86
87
        $responseArray = $response->decodeResponseJson();
88
89
        $this->assertEquals($user->name, $responseArray['data'][0]['name']);
90
91
        // assert only single user was returned
92
        $this->assertCount(1, $responseArray['data']);
93
    }
94
95
}
96