GetAllUsersTest::testSearchUsersByName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
c 0
b 0
f 0
rs 9.568
cc 1
nc 1
nop 0
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