|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use App\User; |
|
4
|
|
|
use App\Video; |
|
5
|
|
|
use Chrisbjr\ApiGuard\Models\ApiKey; |
|
6
|
|
|
use Illuminate\Foundation\Testing\DatabaseMigrations; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Created by PhpStorm. |
|
10
|
|
|
* User: adam |
|
11
|
|
|
* Date: 22/04/16 |
|
12
|
|
|
* Time: 09:18. |
|
13
|
|
|
*/ |
|
14
|
|
|
class VideoAPITest extends TestCase |
|
|
|
|
|
|
15
|
|
|
{ |
|
16
|
|
|
use DatabaseMigrations; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Create fake user. |
|
20
|
|
|
* |
|
21
|
|
|
* @return mixed |
|
22
|
|
|
*/ |
|
23
|
|
|
public function createUser() |
|
24
|
|
|
{ |
|
25
|
|
|
$user = factory(User::class)->create(); |
|
26
|
|
|
$this->createUserApiKey($user); |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
return $user; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param User $user |
|
33
|
|
|
* |
|
34
|
|
|
* @return mixed |
|
35
|
|
|
*/ |
|
36
|
|
|
private function createUserApiKey(User $user) |
|
37
|
|
|
{ |
|
38
|
|
|
$apiKey = ApiKey::make($user->id); |
|
39
|
|
|
$user->apiKey()->save($apiKey); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Create fake video. |
|
44
|
|
|
* |
|
45
|
|
|
* @return \App\Video |
|
46
|
|
|
*/ |
|
47
|
|
View Code Duplication |
private function createFakeVideo($user) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
$faker = Faker\Factory::create(); |
|
50
|
|
|
$video = new Video(); |
|
51
|
|
|
$video->name = $faker->sentence; |
|
|
|
|
|
|
52
|
|
|
$video->category = $faker->word; |
|
|
|
|
|
|
53
|
|
|
$video->path = $faker->url; |
|
|
|
|
|
|
54
|
|
|
$user->getVideos()->save($video); |
|
55
|
|
|
|
|
56
|
|
|
return $video; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Create fake videos. |
|
61
|
|
|
* |
|
62
|
|
|
* @param int $count |
|
63
|
|
|
* |
|
64
|
|
|
* @return \App\Video |
|
65
|
|
|
*/ |
|
66
|
|
|
private function createFakeVideos($count = 10) |
|
67
|
|
|
{ |
|
68
|
|
|
$user = $this->createUser(); |
|
69
|
|
|
foreach (range(0, $count) as $number) { |
|
70
|
|
|
$this->createFakeVideo($user); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Test video is an api then returns JSON. |
|
76
|
|
|
* |
|
77
|
|
|
* @return void |
|
78
|
|
|
*/ |
|
79
|
|
|
public function testVideoUseJson() |
|
80
|
|
|
{ |
|
81
|
|
|
$this->get('/api/videos')->seeJson()->seeStatusCode(200); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Test videos in database are listed by API. |
|
86
|
|
|
* |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
|
|
public function testVideosInDatabaseAreListedByAPI() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->createFakeVideos(); |
|
92
|
|
|
$this->get('/api/videos') |
|
93
|
|
|
->seeJsonStructure([ |
|
94
|
|
|
'*' => [ |
|
95
|
|
|
'*' => [ |
|
96
|
|
|
'category', 'id', 'name', 'path', |
|
97
|
|
|
], |
|
98
|
|
|
], |
|
99
|
|
|
])->seeStatusCode(200); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Test Video Return 404 on video not exists. |
|
104
|
|
|
* |
|
105
|
|
|
* @return void |
|
106
|
|
|
*/ |
|
107
|
|
|
public function testVideoReturn404OnVideoNotExists() |
|
108
|
|
|
{ |
|
109
|
|
|
$this->get('/api/videos/50000')->seeJson()->seeStatusCode(404); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Test best videos is an api then returns JSON. |
|
114
|
|
|
* |
|
115
|
|
|
* @return void |
|
116
|
|
|
*/ |
|
117
|
|
|
public function testBestVideosUseJson() |
|
118
|
|
|
{ |
|
119
|
|
|
$user = $this->createUser(); |
|
120
|
|
|
$this->createFakeVideo($user); |
|
121
|
|
|
$this->get('/api/videos/best')->seeJson()->seeStatusCode(200); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Test videos user is an api then returns JSON. |
|
126
|
|
|
* |
|
127
|
|
|
* @return void |
|
128
|
|
|
*/ |
|
129
|
|
View Code Duplication |
public function testVideosUserUseJson() |
|
|
|
|
|
|
130
|
|
|
{ |
|
131
|
|
|
$user = $this->createUser(); |
|
132
|
|
|
$video = $this->createFakeVideo($user); |
|
133
|
|
|
$this->get('/api/videos/user/'.$user->id)->seeJsonContains(['name' => $video->name, 'category' => $video->category, 'path' => $video->path]) |
|
|
|
|
|
|
134
|
|
|
->seeStatusCode(200); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Test videos for category is an api then returns JSON. |
|
139
|
|
|
* |
|
140
|
|
|
* @return void |
|
141
|
|
|
*/ |
|
142
|
|
View Code Duplication |
public function testVideosForCategory() |
|
|
|
|
|
|
143
|
|
|
{ |
|
144
|
|
|
$user = $this->createUser(); |
|
145
|
|
|
$video = $this->createFakeVideo($user); |
|
146
|
|
|
$this->get('/api/videos/category/'.$video->category)->seeJsonContains(['name' => $video->name, 'category' => $video->category, 'path' => $video->path]) |
|
|
|
|
|
|
147
|
|
|
->seeStatusCode(200); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Test video in database is shown by API. |
|
152
|
|
|
* |
|
153
|
|
|
* @return void |
|
154
|
|
|
*/ |
|
155
|
|
View Code Duplication |
public function testVideoInDatabaseAreShownByAPI() |
|
|
|
|
|
|
156
|
|
|
{ |
|
157
|
|
|
$user = $this->createUser(); |
|
158
|
|
|
$video = $this->createFakeVideo($user); |
|
159
|
|
|
$this->get('/api/videos/'.$video->id)->seeJsonContains(['name' => $video->name, 'category' => $video->category, 'path' => $video->path]) |
|
|
|
|
|
|
160
|
|
|
->seeStatusCode(200); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Test videos Unauthorized posted without apikey. |
|
165
|
|
|
* |
|
166
|
|
|
* @return void |
|
167
|
|
|
*/ |
|
168
|
|
|
public function testVideosUnauthorizedPostedWithoutApiKey() |
|
169
|
|
|
{ |
|
170
|
|
|
$data = ['name' => 'Foobar', 'category' => 'Movie', 'path' => '/videos/foobar.mp4']; |
|
171
|
|
|
$this->post('/api/videos', $data)->seeStatusCode(401)->seeJsonContains(['message' => 'Unauthorized']); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Test videos can be posted and saved to database. |
|
176
|
|
|
* |
|
177
|
|
|
* @return void |
|
178
|
|
|
*/ |
|
179
|
|
|
public function testVideosCanBePostedAndSavedIntoDatabase() |
|
180
|
|
|
{ |
|
181
|
|
|
$user = $this->createUser(); |
|
182
|
|
|
$this->createUserApiKey($user); |
|
|
|
|
|
|
183
|
|
|
$file = storage_path('app/public/videos/demo.mp4'); |
|
184
|
|
|
$video = new Symfony\Component\HttpFoundation\File\UploadedFile( |
|
185
|
|
|
$file, |
|
186
|
|
|
'demo.mp4', |
|
187
|
|
|
'video/mp4', |
|
188
|
|
|
null, |
|
189
|
|
|
null, |
|
190
|
|
|
true |
|
191
|
|
|
); |
|
192
|
|
|
|
|
193
|
|
|
$data = ['name' => 'demo', 'category' => 'Movie', 'video' => $video]; |
|
194
|
|
|
$this->post('/api/videos', $data, ['X-Authorization' => $user->apiKey->key])->seeInDatabase('videos', ['name' => 'demo', 'category' => 'Movie', 'path' => '/storage/videos/demo'.$user->id]); |
|
195
|
|
|
$this->get('/api/videos')->seeJsonContains(['name' => 'demo', 'category' => 'Movie', 'path' => '/storage/videos/demo'.$user->id])->seeStatusCode(200); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Test videos can be update and see changes on database. |
|
200
|
|
|
* |
|
201
|
|
|
* @return void |
|
202
|
|
|
*/ |
|
203
|
|
|
public function testVideosCanBeUpdatedAndSeeChangesInDatabase() |
|
204
|
|
|
{ |
|
205
|
|
|
$user = $this->createUser(); |
|
206
|
|
|
$video = $this->createFakeVideo($user); |
|
207
|
|
|
$data = ['name' => 'V for Vendetta', 'category' => 'Movie']; |
|
208
|
|
|
$this->put('/api/videos/'.$video->id, $data, ['X-Authorization' => $user->apiKey->key])->seeInDatabase('videos', $data); |
|
|
|
|
|
|
209
|
|
|
$this->get('/api/videos')->seeJsonContains([$data = ['id' => $video->id, 'name' => 'V for Vendetta', 'category' => 'Movie', 'path' => $video->path, 'likes' => $video->likes()->count(), 'dislikes' => $video->dislikes()->count(), 'comments' => $video->getComments]])->seeStatusCode(200); |
|
|
|
|
|
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Test videos can be deleted and not see on database. |
|
214
|
|
|
* |
|
215
|
|
|
* @return void |
|
216
|
|
|
*/ |
|
217
|
|
View Code Duplication |
public function testVideosCanBeDeletedAndNotSeenOnDatabase() |
|
|
|
|
|
|
218
|
|
|
{ |
|
219
|
|
|
$user = $this->createUser(); |
|
220
|
|
|
$video = $this->createFakeVideo($user); |
|
221
|
|
|
$data = ['name' => $video->name, 'category' => $video->category, 'path' => $video->path]; |
|
|
|
|
|
|
222
|
|
|
$this->delete('/api/videos/'.$video->id, ['X-Authorization' => $user->apiKey->key])->notSeeInDatabase('videos', $data); |
|
|
|
|
|
|
223
|
|
|
$this->get('/api/videos')->dontSeeJson($data)->seeStatusCode(200); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Test videos can be search and see result. |
|
228
|
|
|
* |
|
229
|
|
|
* @return void |
|
230
|
|
|
*/ |
|
231
|
|
View Code Duplication |
public function testVideosCanBeSearchAndSeenResult() |
|
|
|
|
|
|
232
|
|
|
{ |
|
233
|
|
|
$user = $this->createUser(); |
|
234
|
|
|
$video = $this->createFakeVideo($user); |
|
235
|
|
|
$data = ['name' => $video->name, 'category' => $video->category, 'path' => $video->path]; |
|
|
|
|
|
|
236
|
|
|
$this->get('/api/videos/search/'.$video->name)->seeJson($data)->seeStatusCode(200); |
|
|
|
|
|
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
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.