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
|
|
View Code Duplication |
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_() |
|
|
|
|
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
|
|
|
'message' => 'This action is unauthorized.', |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @test |
70
|
|
|
*/ |
71
|
|
|
public function testSearchUsersByName() |
72
|
|
|
{ |
73
|
|
|
$user = $this->getTestingUser([ |
74
|
|
|
'name' => 'mahmoudzzz' |
75
|
|
|
]); |
76
|
|
|
|
77
|
|
|
// 3 random users |
78
|
|
|
factory(User::class, 3)->create(); |
79
|
|
|
|
80
|
|
|
// send the HTTP request |
81
|
|
|
$response = $this->endpoint($this->endpoint. '?search=name:mahmoudzzz')->makeCall(); |
82
|
|
|
|
83
|
|
|
// assert response status is correct |
84
|
|
|
$response->assertStatus(200); |
85
|
|
|
|
86
|
|
|
$responseArray = $response->decodeResponseJson(); |
87
|
|
|
|
88
|
|
|
$this->assertEquals($user->name, $responseArray['data'][0]['name']); |
89
|
|
|
|
90
|
|
|
// assert only single user was returned |
91
|
|
|
$this->assertCount(1, $responseArray['data']); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
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.