1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
<?php
namespace Madcoda\Youtube\Tests;
use Madcoda\Youtube\Youtube;
/**
* Class YoutubeTest
*
* @category Youtube
* @package Youtube
* @author Jason Leung <jason@madcoda.com>
*/
class YoutubeTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Youtube
* @var optionParams
*/
protected $youtube;
protected $optionParams;
public function setUp()
{
$TEST_API_KEY = getenv('YOUTUBE_API_KEY');
$params = array(
'key' => $TEST_API_KEY,
'referer' => 'fake-refer',
'apis' => array(
'videos.list' => 'https://www.googleapis.com/youtube/v3/videos',
'search.list'=> 'https://www.googleapis.com/youtube/v3/search',
'channels.list'=> 'https://www.googleapis.com/youtube/v3/channels',
'playlists.list'=> 'https://www.googleapis.com/youtube/v3/playlists',
'playlistItems.list'=> 'https://www.googleapis.com/youtube/v3/playlistItems',
'activities'=> 'https://www.googleapis.com/youtube/v3/activities',
)
);
$this->youtube = new Youtube($params);
$this->optionParams = array(
'order' => 'title'
);
}
public function tearDown()
{
$this->youtube = null;
$this->optionParams = null;
}
public function MalFormURLProvider()
{
return array(
array('https://'),
array('http://www.yuotube.com'),
);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testConstructorFail()
{
$this->youtube = new Youtube(array());
}
/**
* @expectedException \InvalidArgumentException
*/
public function testConstructorFail2()
{
$this->youtube = new Youtube('FAKE API KEY');
}
/**
* @expectedException \Exception
*/
public function testInvalidApiKey()
{
$this->youtube = new Youtube(array('key' => 'nonsense'));
$vID = 'rie-hPVJ7Sw';
$this->youtube->getVideoInfo($vID);
}
public function testGetVideoInfo()
{
$vID = 'rie-hPVJ7Sw';
$response = $this->youtube->getVideoInfo($vID);
$this->assertEquals($vID, $response->id);
$this->assertNotNull('response');
$this->assertEquals('youtube#video', $response->kind);
//add all these assertions here in case the api is changed,
//we can detect it instantly
$this->assertObjectHasAttribute('statistics', $response);
$this->assertObjectHasAttribute('status', $response);
$this->assertObjectHasAttribute('snippet', $response);
$this->assertObjectHasAttribute('contentDetails', $response);
}
public function testGetVideosInfo()
{
$vID = array('rie-hPVJ7Sw', 'lRRk97FYLJM');
$response = $this->youtube->getVideosInfo($vID);
$this->assertInternalType('array', $response);
foreach ($response as $value) {
$this->assertContains($value->id, $vID);
$this->assertEquals('youtube#video', $value->kind);
//add all these assertions here in case the api is changed,
//we can detect it instantly
$this->assertObjectHasAttribute('statistics', $value);
$this->assertObjectHasAttribute('status', $value);
$this->assertObjectHasAttribute('snippet', $value);
$this->assertObjectHasAttribute('contentDetails', $value);
}
}
public function testSearch()
{
$limit = rand(3, 10);
$response = $this->youtube->search('Android', $limit);
$this->assertEquals($limit, count($response));
$this->assertEquals('youtube#searchResult', $response[0]->kind);
}
public function testSearchVideos()
{
$limit = rand(3, 10);
$response = $this->youtube->searchVideos('Android', $limit, 'title');
$this->assertEquals($limit, count($response));
$this->assertEquals('youtube#searchResult', $response[0]->kind);
$this->assertEquals('youtube#video', $response[0]->id->kind);
}
public function testSearchChannelVideos()
{
$limit = rand(3, 10);
$response = $this->youtube->searchChannelVideos('Android', 'UCVHFbqXqoYvEWM1Ddxl0QDg', $limit, 'title');
$this->assertEquals($limit, count($response));
$this->assertEquals('youtube#searchResult', $response[0]->kind);
$this->assertEquals('youtube#video', $response[0]->id->kind);
}
public function testSearchChannelLiveStream()
{
$limit = rand(3, 10);
$expectCount = 1;
$response = $this->youtube->searchChannelLiveStream('東森', 'UCR3asjvr_WAaxwJYEDV_Bfw', $limit, 'title');
$this->assertEquals($expectCount, count($response));
$this->assertEquals('youtube#searchResult', $response[0]->kind);
$this->assertEquals('youtube#video', $response[0]->id->kind);
}
public function testSearchAdvanced()
{
$limit = rand(3, 10);
$params = array(
'q' => 'Android',
'type' => 'video',
'channelId' => 'UCVHFbqXqoYvEWM1Ddxl0QDg',
'part' => 'id, snippet',
'order' => 'title',
'maxResults' => $limit
);
$response = $this->youtube->searchAdvanced($params, true);
$this->assertEquals('youtube#searchResult', $response['results'][0]->kind);
$this->assertEquals('youtube#searchListResponse', $response['info']['kind']);
}
/**
*
* @expectedException \InvalidArgumentException
*/
public function testSearchAdvancedWithException()
{
$limit = rand(3, 10);
$params = array();
$response = $this->youtube->searchAdvanced($params, true);
}
public function testPaginateResults()
{
$limit = rand(3, 10);
$params = array(
'q' => 'Android',
'type' => 'video',
'channelId' => 'UCVHFbqXqoYvEWM1Ddxl0QDg',
'part' => 'id, snippet',
'order' => 'title',
'maxResults' => $limit
);
$response = $this->youtube->searchAdvanced($params, true);
$nextPageToken = $response['info']['nextPageToken'];
$response = $this->youtube->paginateResults($params, $nextPageToken);
$this->assertEquals('youtube#searchResult', $response['results'][0]->kind);
$this->assertEquals('youtube#searchListResponse', $response['info']['kind']);
}
public function testGetChannelByName()
{
$response = $this->youtube->getChannelByName('Google', $this->optionParams);
$this->assertEquals('youtube#channel', $response->kind);
//This is not a safe Assertion because the name can change, but include it anyway
$this->assertEquals('Google', $response->snippet->title);
//add all these assertions here in case the api is changed,
//we can detect it instantly
$this->assertObjectHasAttribute('snippet', $response);
$this->assertObjectHasAttribute('contentDetails', $response);
$this->assertObjectHasAttribute('statistics', $response);
}
public function testGetChannelById()
{
$channelId = 'UCk1SpWNzOs4MYmr0uICEntg';
$response = $this->youtube->getChannelById($channelId, $this->optionParams);
$this->assertEquals('youtube#channel', $response->kind);
$this->assertEquals($channelId, $response->id);
$this->assertObjectHasAttribute('snippet', $response);
$this->assertObjectHasAttribute('contentDetails', $response);
$this->assertObjectHasAttribute('statistics', $response);
}
public function testGetChannelsById()
{
$channels = array('UCk1SpWNzOs4MYmr0uICEntg', 'UCK8sQmJBp8GCxrOtXWBpyEA');
$response = $this->youtube->getChannelsById($channels, $this->optionParams);
$this->assertTrue(count($response) === 2);
$this->assertEquals('youtube#channel', $response[0]->kind);
$this->assertObjectHasAttribute('snippet', $response[0]);
$this->assertObjectHasAttribute('contentDetails', $response[0]);
$this->assertObjectHasAttribute('statistics', $response[0]);
}
public function testGetPlaylistsByChannelId()
{
$GOOGLE_CHANNELID = 'UCK8sQmJBp8GCxrOtXWBpyEA';
$response = $this->youtube->getPlaylistsByChannelId($GOOGLE_CHANNELID, $this->optionParams);
$this->assertTrue(count($response) > 0);
$this->assertEquals('youtube#playlist', $response[0]->kind);
$this->assertEquals('Google', $response[0]->snippet->channelTitle);
}
public function testGetPlaylistById()
{
//get one of the playlist
$GOOGLE_CHANNELID = 'UCK8sQmJBp8GCxrOtXWBpyEA';
$response = $this->youtube->getPlaylistsByChannelId($GOOGLE_CHANNELID);
$playlist = $response[0];
$response = $this->youtube->getPlaylistById($playlist->id);
$this->assertEquals('youtube#playlist', $response->kind);
}
public function testGetPlaylistItemsByPlaylistId()
{
$GOOGLE_ZEITGEIST_PLAYLIST = 'PL590L5WQmH8fJ54F369BLDSqIwcs-TCfs';
$response = $this->youtube->getPlaylistItemsByPlaylistId($GOOGLE_ZEITGEIST_PLAYLIST);
$this->assertTrue(count($response) > 0);
$this->assertEquals('youtube#playlistItem', $response[0]->kind);
}
public function testGetPlaylistItemsByPlaylistIdAdvanced()
{
$GOOGLE_ZEITGEIST_PLAYLIST = 'PL590L5WQmH8fJ54F369BLDSqIwcs-TCfs';
$params = array(
'playlistId' => $GOOGLE_ZEITGEIST_PLAYLIST,
'part' => 'id, snippet'
);
$response = $this->youtube->getPlaylistItemsByPlaylistIdAdvanced($params, true);
$this->assertEquals('youtube#playlistItem', $response['results'][0]->kind);
$this->assertEquals('youtube#playlistItemListResponse', $response['info']['kind']);
}
/**
*
* @expectedException \InvalidArgumentException
*/
public function testGetPlaylistItemsByPlaylistIdAdvancedWithException()
{
$response = $this->youtube->getPlaylistItemsByPlaylistIdAdvanced(null, true);
}
public function testParseVIdFromURLFull()
{
$vId = $this->youtube->parseVIdFromURL('http://www.youtube.com/watch?v=1FJHYqE0RDg');
$this->assertEquals('1FJHYqE0RDg', $vId);
}
public function testParseVIdFromURLFullWithEmbed()
{
$vId = $this->youtube->parseVIdFromURL('https://www.youtube.com/embed/_vA5PvDfNTs');
$this->assertEquals('_vA5PvDfNTs', $vId);
}
public function testParseVIdFromURLShort()
{
$vId = $this->youtube->parseVIdFromURL('http://youtu.be/1FJHYqE0RDg');
$this->assertEquals('1FJHYqE0RDg', $vId);
}
/**
*
* @dataProvider MalFormURLProvider
* @expectedException \Exception
*/
public function testParseVIdFromURLException($url)
{
$vId = $this->youtube->parseVIdFromURL($url);
}
/**
* @expectedException \Exception
*/
public function testParseVIdException()
{
$vId = $this->youtube->parseVIdFromURL('http://www.facebook.com');
}
public function testGetActivitiesByChannelId()
{
$GOOGLE_CHANNELID = 'UCK8sQmJBp8GCxrOtXWBpyEA';
$response = $this->youtube->getActivitiesByChannelId($GOOGLE_CHANNELID);
$this->assertTrue(count($response) > 0);
$this->assertEquals('youtube#activity', $response[0]->kind);
$this->assertEquals('Google', $response[0]->snippet->channelTitle);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testGetActivitiesByChannelIdException()
{
$channelId = '';
$response = $this->youtube->getActivitiesByChannelId($channelId);
}
public function testGetChannelFromURL()
{
$channel = $this->youtube->getChannelFromURL('http://www.youtube.com/user/Google');
$this->assertEquals('UCK8sQmJBp8GCxrOtXWBpyEA', $channel->id);
$this->assertEquals('Google', $channel->snippet->title);
}
public function testGetChannelFromURLWithChannel()
{
$channel = $this->youtube->getChannelFromURL('https://www.youtube.com/channel/UCyNF0DijeiZ8jhc_xlVa4Vg');
$this->assertEquals('UCyNF0DijeiZ8jhc_xlVa4Vg', $channel->id);
$this->assertEquals('PHPoC', $channel->snippet->title);
}
/**
* @expectedException \Exception
*/
public function testGetChannelFromURLWithNoYoutubeUrlException()
{
$channel = $this->youtube->getChannelFromURL('https://google.com');
}
/**
* @expectedException \Exception
*/
public function testGetChannelFromURLWithInvalidYoutubeUrlException()
{
$channel = $this->youtube->getChannelFromURL('https://www.youtube.com/invalid/UCyNF0DijeiZ8jhc_xlVa4Vg');
}
// /**
// * @expectedException \Exception
// */
// public function testDecodeListWithException()
// {
// $GOOGLE_ZEITGEIST_PLAYLIST = 'PL590L5WQmH8fJ54F369BLDSqIwcs-TCfs';
// $params = array(
// 'playlistId' => $GOOGLE_ZEITGEIST_PLAYLIST
// );
// $response = $this->youtube->getPlaylistItemsByPlaylistIdAdvanced($params, true);
// }
public function testDecodeListWithFalse()
{
$limit = rand(3, 10);
$params = array(
'q' => 'Android',
'type' => 'video',
'channelId' => 'UCsxfgk64cpT61c8KZuaqUMA',
'part' => 'id, snippet',
'order' => 'title',
'maxResults' => $limit
);
$response = $this->youtube->searchAdvanced($params);
$this->assertFalse($response);
}
/**
* @expectedException \Exception
*/
public function testApi_getWithException()
{
$limit = rand(3, 10);
$params = array(
'q' => 'Android',
'type' => 'video',
'channelId' => 'UCsxfgk64cpT61c8KZuaqUMA',
'order' => 'title',
'maxResults' => $limit
);
$response = $this->youtube->api_get('http://404.php.net/', $params);
}
public function test_parse_url_queryWithEmptyArray()
{
$url = 'http://404.php.net';
$youtube = $this->youtube;
$response = $youtube::_parse_url_query($url);
$this->assertCount(0, $response);
}
/**
* Test skipped for now, since the API returns Error 500
*/
public function testNotFoundAPICall()
{
$vID = 'Utn7NBtbHL4'; //an deleted video
$response = $this->youtube->getVideoInfo($vID);
$this->assertFalse($response);
}
}
|