1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use App\LikeDislike; |
4
|
|
|
use App\User; |
5
|
|
|
use App\Video; |
6
|
|
|
use Chrisbjr\ApiGuard\Models\ApiKey; |
7
|
|
|
use Illuminate\Foundation\Testing\DatabaseMigrations; |
8
|
|
|
|
9
|
|
|
class LikeDislikeAPITest extends TestCase |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
use DatabaseMigrations; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Create fake user. |
15
|
|
|
* |
16
|
|
|
* @return mixed |
17
|
|
|
*/ |
18
|
|
|
public function createUser() |
19
|
|
|
{ |
20
|
|
|
$user = factory(User::class)->create(); |
21
|
|
|
$this->createUserApiKey($user); |
|
|
|
|
22
|
|
|
|
23
|
|
|
return $user; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param User $user |
28
|
|
|
* |
29
|
|
|
* @return mixed |
30
|
|
|
*/ |
31
|
|
|
private function createUserApiKey(User $user) |
32
|
|
|
{ |
33
|
|
|
$apiKey = ApiKey::make($user->id); |
34
|
|
|
$user->apiKey()->save($apiKey); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create fake video. |
39
|
|
|
* |
40
|
|
|
* @return \App\Video |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
private function createFakeVideo($user) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$faker = Faker\Factory::create(); |
45
|
|
|
$video = new Video(); |
46
|
|
|
$video->name = $faker->sentence; |
|
|
|
|
47
|
|
|
$video->category = $faker->word; |
|
|
|
|
48
|
|
|
$video->path = $faker->url; |
|
|
|
|
49
|
|
|
$user->getVideos()->save($video); |
50
|
|
|
|
51
|
|
|
$this->createFakeLikes($video->id); |
|
|
|
|
52
|
|
|
|
53
|
|
|
return $video; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
private function createFakeLikes($video_id) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$video = Video::find($video_id); |
59
|
|
|
$type = [ |
60
|
|
|
'like', |
61
|
|
|
'dislike', |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
for ($i = 0; $i < 10; $i++) { |
65
|
|
|
$user = $this->createUser(); |
66
|
|
|
$key = array_rand($type); |
67
|
|
|
|
68
|
|
|
$data = [ |
69
|
|
|
'user_id' => $user->id, |
70
|
|
|
'video_id' => $video->id, |
71
|
|
|
'type' => $type[$key], |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
LikeDislike::create($data); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Test likes in database are listed by API. |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public function testLikesInDatabaseAreListedByAPI() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$user = $this->createUser(); |
86
|
|
|
$video = $this->createFakeVideo($user); |
87
|
|
|
|
88
|
|
|
$this->get('/api/videos/'.$video->id.'/likes') |
|
|
|
|
89
|
|
|
->seeJsonStructure([ |
90
|
|
|
'*' => [ |
91
|
|
|
'*' => [ |
92
|
|
|
'user_id', 'video_id', 'type', |
93
|
|
|
], |
94
|
|
|
], |
95
|
|
|
])->seeStatusCode(200); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Test dislikes in database are listed by API. |
100
|
|
|
* |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function testDislikesInDatabaseAreListedByAPI() |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$user = $this->createUser(); |
106
|
|
|
$video = $this->createFakeVideo($user); |
107
|
|
|
$this->get('/api/videos/'.$video->id.'/dislikes') |
|
|
|
|
108
|
|
|
->seeJsonStructure([ |
109
|
|
|
'*' => [ |
110
|
|
|
'*' => [ |
111
|
|
|
'user_id', 'video_id', 'type', |
112
|
|
|
], |
113
|
|
|
], |
114
|
|
|
])->seeStatusCode(200); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Test likes count return integer. |
119
|
|
|
* |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
View Code Duplication |
public function testLikesCountReturnInteger() |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$user = $this->createUser(); |
125
|
|
|
$video = $this->createFakeVideo($user); |
126
|
|
|
$count = $this->get('/api/videos/'.$video->id.'/likes/count'); |
|
|
|
|
127
|
|
|
|
128
|
|
|
$this->assertTrue(is_int($count->response->original)); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Test dislikes count return integer. |
133
|
|
|
* |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
|
View Code Duplication |
public function testDisikesCountReturnInteger() |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
$user = $this->createUser(); |
139
|
|
|
$video = $this->createFakeVideo($user); |
140
|
|
|
$count = $this->get('/api/videos/'.$video->id.'/dislikes/count'); |
|
|
|
|
141
|
|
|
|
142
|
|
|
$this->assertTrue(is_int($count->response->original)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Test store like and see in DB. |
147
|
|
|
* |
148
|
|
|
* @return void |
149
|
|
|
*/ |
150
|
|
View Code Duplication |
public function testCanBePostLikeAndSeeInDB() |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$user = $this->createUser(); |
153
|
|
|
$video = $this->createFakeVideo($user); |
154
|
|
|
|
155
|
|
|
$data = [ |
156
|
|
|
'user_id' => $user->id, |
157
|
|
|
'video_id' => $video->id, |
|
|
|
|
158
|
|
|
'type' => 'like', |
159
|
|
|
]; |
160
|
|
|
|
161
|
|
|
$this->post('/api/videos/'.$video->id.'/like-dislike', $data, ['X-Authorization' => $user->apiKey->key])->seeInDatabase('likes_dislikes', $data); |
|
|
|
|
162
|
|
|
$this->get('/api/videos/'.$video->id.'/likes')->seeJsonContains($data)->seeStatusCode(200); |
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Test delete likeDislike and not see in DB. |
167
|
|
|
* |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
|
View Code Duplication |
public function testCanBeDeleteLikeAndSeeInDB() |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
$user = $this->createUser(); |
173
|
|
|
$video = $this->createFakeVideo($user); |
174
|
|
|
|
175
|
|
|
$data = [ |
176
|
|
|
'user_id' => $user->id, |
177
|
|
|
'video_id' => $video->id, |
|
|
|
|
178
|
|
|
'type' => 'like', |
179
|
|
|
]; |
180
|
|
|
|
181
|
|
|
LikeDislike::create($data); |
182
|
|
|
|
183
|
|
|
$this->post('/api/videos/'.$video->id.'/like-dislike', $data, ['X-Authorization' => $user->apiKey->key])->notSeeInDatabase('likes_dislikes', $data); |
|
|
|
|
184
|
|
|
$this->get('/api/videos/'.$video->id.'/likes')->dontSeeJson([$data])->seeStatusCode(200); |
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Test update type likeDislike and not see in DB. |
189
|
|
|
* |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
|
public function testCanBeUpdateTypeLikeAndSeeInDB() |
193
|
|
|
{ |
194
|
|
|
$user = $this->createUser(); |
195
|
|
|
$video = $this->createFakeVideo($user); |
196
|
|
|
|
197
|
|
|
$data = [ |
198
|
|
|
'user_id' => $user->id, |
199
|
|
|
'video_id' => $video->id, |
|
|
|
|
200
|
|
|
'type' => 'like', |
201
|
|
|
]; |
202
|
|
|
|
203
|
|
|
LikeDislike::create($data); |
204
|
|
|
|
205
|
|
|
$dataUpdate = [ |
206
|
|
|
'user_id' => $user->id, |
207
|
|
|
'video_id' => $video->id, |
|
|
|
|
208
|
|
|
'type' => 'dislike', |
209
|
|
|
]; |
210
|
|
|
|
211
|
|
|
$this->post('/api/videos/'.$video->id.'/like-dislike', $dataUpdate, ['X-Authorization' => $user->apiKey->key])->seeInDatabase('likes_dislikes', $dataUpdate); |
|
|
|
|
212
|
|
|
$this->get('/api/videos/'.$video->id.'/dislikes')->seeJsonContains($dataUpdate)->seeStatusCode(200); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.