1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use App\User; |
4
|
|
|
use Chrisbjr\ApiGuard\Models\ApiKey; |
5
|
|
|
use Illuminate\Foundation\Testing\DatabaseMigrations; |
6
|
|
|
|
7
|
|
|
class UserAPITest extends TestCase |
8
|
|
|
{ |
9
|
|
|
use DatabaseMigrations; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Create fake user. |
13
|
|
|
* |
14
|
|
|
* @return mixed |
15
|
|
|
*/ |
16
|
|
|
public function createUser() |
17
|
|
|
{ |
18
|
|
|
$user = factory(User::class)->create(); |
19
|
|
|
$this->createUserApiKey($user); |
20
|
|
|
|
21
|
|
|
return $user; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param User $user |
26
|
|
|
* |
27
|
|
|
* @return mixed |
28
|
|
|
*/ |
29
|
|
|
private function createUserApiKey(User $user) |
30
|
|
|
{ |
31
|
|
|
$apiKey = ApiKey::make($user->id); |
32
|
|
|
$user->apiKey()->save($apiKey); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Test user in database are listed by API. |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function testUserInDatabaseAreListedByAPI() |
41
|
|
|
{ |
42
|
|
|
$user = $this->createUser(); |
43
|
|
|
$this->get('/api/users/') |
44
|
|
|
->seeJsonStructure([ |
45
|
|
|
'*' => [ |
46
|
|
|
'*' => [ |
47
|
|
|
'id', 'name', 'email', 'avatar', |
48
|
|
|
], |
49
|
|
|
], |
50
|
|
|
])->seeStatusCode(200); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Test user is an api then returns JSON. |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function testCreateUserAndShowUseJson() |
59
|
|
|
{ |
60
|
|
|
$user = $this->createUser(); |
61
|
|
|
$this->get('/api/users/'.$user->id)->seeJson()->seeStatusCode(200); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Test upate user and see in DB. |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
public function testCanBeUpdateAndSeeInDB() |
70
|
|
|
{ |
71
|
|
|
$user = $this->createUser(); |
72
|
|
|
$file = storage_path('app/public/images/avatars/demo.jpg'); |
73
|
|
|
$image = new Symfony\Component\HttpFoundation\File\UploadedFile( |
74
|
|
|
$file, |
75
|
|
|
'demo.jpg', |
76
|
|
|
'image/jpg', |
77
|
|
|
null, |
78
|
|
|
null, |
79
|
|
|
true |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$data = ['id' => $user->id, 'name' => 'Test User', 'email' => '[email protected]', 'password' => '123456', 'avatar' => $image]; |
83
|
|
|
$this->post('/api/users/'.$user->id, $data, ['X-Authorization' => $user->apiKey->key]); |
84
|
|
|
$user = User::find($user->id); |
85
|
|
|
$this->assertEquals('Test User', $user->name); |
86
|
|
|
$this->assertEquals('[email protected]', $user->email); |
87
|
|
|
$this->assertEquals('/storage/images/avatars/'.$user->id.'.jpg', $user->avatar); |
88
|
|
|
$this->get('/api/users')->seeJsonContains(['id' => $user->id, 'name' => 'Test User', 'email' => '[email protected]', 'avatar' => '/storage/images/avatars/'.$user->id.'.jpg'])->seeStatusCode(200); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Test upate user without avatar and see in DB. |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public function testCanBeUpdateWithoutAvatarAndSeeInDB() |
97
|
|
|
{ |
98
|
|
|
$user = $this->createUser(); |
99
|
|
|
|
100
|
|
|
$data = ['id' => $user->id, 'name' => 'Test User', 'email' => '[email protected]', 'password' => '123456']; |
101
|
|
|
$this->post('/api/users/'.$user->id, $data, ['X-Authorization' => $user->apiKey->key]); |
102
|
|
|
$user = User::find($user->id); |
103
|
|
|
$this->assertEquals('Test User', $user->name); |
104
|
|
|
$this->assertEquals('[email protected]', $user->email); |
105
|
|
|
$this->assertEquals('/img/user2-160x160.png', $user->avatar); |
106
|
|
|
$this->get('/api/users')->seeJsonContains(['id' => $user->id, 'name' => 'Test User', 'email' => '[email protected]', 'avatar' => '/img/user2-160x160.png'])->seeStatusCode(200); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Test delete user and not see in DB. |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
public function testCanBeDeleteAndNotSeeInDB() |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
$user = $this->createUser(); |
117
|
|
|
$data = ['name' => $user->name, 'email' => $user->email, 'avatar' => $user->avatar]; |
118
|
|
|
|
119
|
|
|
$this->delete('/api/users/'.$user->id, ['X-Authorization' => $user->apiKey->key])->notSeeInDatabase('users', $data); |
120
|
|
|
$this->get('/api/users')->dontSeeJson($data)->seeStatusCode(200); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
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.