alltube/tests/VideoDownloadTest.php

519 lines
13 KiB
PHP
Raw Normal View History

2015-08-29 19:46:57 +00:00
<?php
/**
2016-09-07 22:28:28 +00:00
* VideoDownloadTest class.
2016-08-01 11:29:13 +00:00
*/
2016-12-05 12:12:27 +00:00
2016-03-29 23:49:08 +00:00
namespace Alltube\Test;
use Alltube\Config;
2016-10-18 08:45:54 +00:00
use Alltube\VideoDownload;
2017-10-26 08:46:22 +00:00
use PHPUnit\Framework\TestCase;
2015-08-29 19:46:57 +00:00
/**
2016-09-07 22:28:28 +00:00
* Unit tests for the VideoDownload class.
2016-08-01 11:29:13 +00:00
*/
2017-10-26 08:46:22 +00:00
class VideoDownloadTest extends TestCase
2015-08-29 19:46:57 +00:00
{
2016-08-01 11:29:13 +00:00
/**
2016-09-07 22:28:28 +00:00
* VideoDownload instance.
*
2016-08-01 11:29:13 +00:00
* @var VideoDownload
*/
private $download;
/**
* Config class instance.
*
* @var Config
*/
private $config;
2016-08-01 11:29:13 +00:00
/**
2016-09-07 22:28:28 +00:00
* Initialize properties used by test.
2016-08-01 11:29:13 +00:00
*/
2016-04-08 17:06:41 +00:00
protected function setUp()
{
if (PHP_OS == 'WINNT') {
$configFile = 'config_test_windows.yml';
} else {
$configFile = 'config_test.yml';
}
$this->config = Config::getInstance('config/'.$configFile);
$this->download = new VideoDownload($this->config);
2016-04-08 17:06:41 +00:00
}
2016-08-01 11:29:13 +00:00
/**
2016-09-07 22:28:28 +00:00
* Destroy properties after test.
2016-08-01 11:29:13 +00:00
*/
protected function tearDown()
{
Config::destroyInstance();
}
/**
* Test VideoDownload constructor with wrong youtube-dl path.
*
* @return void
* @expectedException Exception
*/
public function testConstructorWithMissingYoutubedl()
{
$this->config->youtubedl = 'foo';
new VideoDownload($this->config);
}
/**
* Test VideoDownload constructor with wrong Python path.
*
* @return void
* @expectedException Exception
*/
public function testConstructorWithMissingPython()
{
$this->config->python = 'foo';
new VideoDownload($this->config);
}
2015-08-29 19:46:57 +00:00
/**
2016-09-07 22:28:28 +00:00
* Test listExtractors function.
2015-10-29 17:40:22 +00:00
*
2015-08-29 19:46:57 +00:00
* @return void
*/
public function testListExtractors()
{
2016-04-08 17:06:41 +00:00
$extractors = $this->download->listExtractors();
$this->assertContains('youtube', $extractors);
2015-08-29 19:46:57 +00:00
}
/**
2016-09-07 22:28:28 +00:00
* Test getURL function.
2015-10-29 17:40:22 +00:00
*
2017-01-02 22:08:51 +00:00
* @param string $url URL
* @param string $format Format
* @param string $filename Filename
* @param string $extension File extension
* @param string $domain Domain
2015-10-29 17:40:22 +00:00
*
2016-09-07 22:28:28 +00:00
* @return void
2015-10-29 17:40:22 +00:00
* @dataProvider urlProvider
* @dataProvider m3uUrlProvider
* @dataProvider rtmpUrlProvider
* @dataProvider remuxUrlProvider
*/
2017-12-05 13:52:20 +00:00
public function testGetURL(
$url,
$format,
/* @scrutinizer ignore-unused */ $filename,
/* @scrutinizer ignore-unused */ $extension,
$domain
) {
2016-04-08 17:06:41 +00:00
$videoURL = $this->download->getURL($url, $format);
2017-04-24 22:40:24 +00:00
$this->assertContains($domain, $videoURL[0]);
}
/**
2016-10-20 21:03:13 +00:00
* Test getURL function with a protected video.
*
* @return void
*/
public function testGetURLWithPassword()
{
2017-04-24 22:40:24 +00:00
$videoURL = $this->download->getURL('http://vimeo.com/68375962', null, 'youtube-dl');
$this->assertContains('vimeocdn.com', $videoURL[0]);
}
/**
* Test getURL function with a protected video and no password.
*
* @return void
* @expectedException \Alltube\PasswordException
*/
public function testGetURLWithMissingPassword()
{
$this->download->getURL('http://vimeo.com/68375962');
}
/**
* Test getURL function with a protected video and a wrong password.
*
* @return void
* @expectedException Exception
*/
public function testGetURLWithWrongPassword()
{
$this->download->getURL('http://vimeo.com/68375962', null, 'foo');
}
/**
2016-09-07 22:28:28 +00:00
* Test getURL function errors.
2015-10-29 17:40:22 +00:00
*
* @param string $url URL
2015-10-29 17:40:22 +00:00
*
2016-09-07 22:28:28 +00:00
* @return void
* @expectedException Exception
2015-10-29 20:23:02 +00:00
* @dataProvider ErrorUrlProvider
*/
public function testGetURLError($url)
{
2016-04-08 18:08:04 +00:00
$this->download->getURL($url);
}
/**
2016-09-07 22:28:28 +00:00
* Provides URLs for tests.
2015-10-29 17:40:22 +00:00
*
2016-08-01 11:29:13 +00:00
* @return array[]
*/
2015-10-29 17:40:22 +00:00
public function urlProvider()
{
2016-09-07 22:28:28 +00:00
return [
[
'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'best[protocol^=http]',
2017-05-30 22:57:39 +00:00
'It_s_Not_Me_It_s_You_-_Hearts_Under_Fire-M7IpKCZ47pU',
2017-01-02 22:08:51 +00:00
'mp4',
2016-07-29 22:47:46 +00:00
'googlevideo.com',
2016-09-07 22:28:28 +00:00
],
[
'https://www.youtube.com/watch?v=RJJ6FCAXvKg', 22,
2017-05-30 22:57:39 +00:00
'Heart_Attack_-_Demi_Lovato_'.
'Sam_Tsui_Against_The_Current-RJJ6FCAXvKg',
2017-01-02 22:08:51 +00:00
'mp4',
2016-07-29 22:47:46 +00:00
'googlevideo.com',
2016-09-07 22:28:28 +00:00
],
[
'https://vimeo.com/24195442', 'best[protocol^=http]',
'Carving_the_Mountains-24195442',
2017-01-02 22:08:51 +00:00
'mp4',
2016-07-29 22:47:46 +00:00
'vimeocdn.com',
2016-09-07 22:28:28 +00:00
],
[
2016-07-30 12:01:00 +00:00
'http://www.bbc.co.uk/programmes/b039g8p7', 'bestaudio/best',
'Leonard_Cohen_Kaleidoscope_-_BBC_Radio_4-b039d07m',
2017-01-02 22:08:51 +00:00
'flv',
2016-07-30 12:01:00 +00:00
'bbcodspdns.fcod.llnwd.net',
2016-09-07 22:28:28 +00:00
],
[
2016-07-30 12:01:00 +00:00
'http://www.rtl2.de/sendung/grip-das-motormagazin/folge/folge-203-0', 'bestaudio/best',
'GRIP_sucht_den_Sommerkonig-folge-203-0',
2017-01-02 22:08:51 +00:00
'f4v',
2016-07-30 12:01:00 +00:00
'edgefcs.net',
2016-09-07 22:28:28 +00:00
],
2016-12-26 23:10:18 +00:00
];
}
/**
* Provides M3U8 URLs for tests.
*
* @return array[]
*/
2017-04-24 22:40:24 +00:00
public function remuxUrlProvider()
{
return [
[
'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'bestvideo+bestaudio',
2017-05-30 22:57:39 +00:00
'It_s_Not_Me_It_s_You_-_Hearts_Under_Fire-M7IpKCZ47pU',
2017-04-24 22:40:24 +00:00
'mp4',
'googlevideo.com',
],
];
}
/**
* Provides URLs for remux tests.
*
* @return array[]
*/
2017-01-16 12:47:29 +00:00
public function m3uUrlProvider()
2016-12-26 23:10:18 +00:00
{
return [
2016-12-26 14:50:26 +00:00
[
'https://twitter.com/verge/status/813055465324056576/video/1', 'best',
'The_Verge_-_This_tiny_origami_robot_can_self-fold_and_complete_tasks-813055465324056576',
2017-01-16 10:23:47 +00:00
'mp4',
2016-12-26 14:50:26 +00:00
'video.twimg.com',
2016-12-26 14:53:03 +00:00
],
2016-09-07 22:28:28 +00:00
];
}
/**
* Provides RTMP URLs for tests.
*
* @return array[]
*/
public function rtmpUrlProvider()
{
return [
[
2017-05-04 21:28:29 +00:00
'http://www.canalc2.tv/video/12163', 'rtmp',
'Terrasses_du_Numerique-12163',
2017-05-04 21:28:29 +00:00
'flv',
'vod-flash.u-strasbg.fr',
],
];
}
/**
2016-09-07 22:28:28 +00:00
* Provides incorrect URLs for tests.
2015-10-29 17:40:22 +00:00
*
2016-08-01 11:29:13 +00:00
* @return array[]
*/
2015-10-29 17:40:22 +00:00
public function errorUrlProvider()
{
2016-09-07 22:28:28 +00:00
return [
['http://example.com/video'],
];
}
/**
2016-09-07 22:28:28 +00:00
* Test getJSON function.
2015-10-29 17:40:22 +00:00
*
* @param string $url URL
* @param string $format Format
2015-10-29 17:40:22 +00:00
*
2016-09-07 22:28:28 +00:00
* @return void
* @dataProvider urlProvider
2017-01-16 12:47:29 +00:00
* @dataProvider m3uUrlProvider
* @dataProvider rtmpUrlProvider
*/
public function testGetJSON($url, $format)
{
2016-04-08 17:06:41 +00:00
$info = $this->download->getJSON($url, $format);
$this->assertObjectHasAttribute('webpage_url', $info);
$this->assertObjectHasAttribute('url', $info);
$this->assertObjectHasAttribute('ext', $info);
$this->assertObjectHasAttribute('title', $info);
$this->assertObjectHasAttribute('extractor_key', $info);
$this->assertObjectHasAttribute('formats', $info);
}
/**
2016-09-07 22:28:28 +00:00
* Test getJSON function errors.
2015-10-29 17:40:22 +00:00
*
* @param string $url URL
2015-10-29 17:40:22 +00:00
*
2016-09-07 22:28:28 +00:00
* @return void
* @expectedException Exception
2015-10-29 20:23:02 +00:00
* @dataProvider ErrorURLProvider
2015-08-29 19:46:57 +00:00
*/
public function testGetJSONError($url)
2015-08-29 19:46:57 +00:00
{
2017-01-16 17:51:42 +00:00
$this->download->getJSON($url);
2015-08-29 19:46:57 +00:00
}
2016-07-29 22:47:46 +00:00
/**
2016-09-07 22:28:28 +00:00
* Test getFilename function.
2016-07-29 22:47:46 +00:00
*
2017-01-02 22:08:51 +00:00
* @param string $url URL
* @param string $format Format
* @param string $filename Filename
* @param string $extension File extension
2016-07-29 22:47:46 +00:00
*
2016-09-07 22:28:28 +00:00
* @return void
2016-07-29 22:47:46 +00:00
* @dataProvider urlProvider
2017-01-16 12:47:29 +00:00
* @dataProvider m3uUrlProvider
* @dataProvider rtmpUrlProvider
* @dataProvider remuxUrlProvider
2016-07-29 22:47:46 +00:00
*/
2017-01-02 22:08:51 +00:00
public function testGetFilename($url, $format, $filename, $extension)
2016-07-29 22:47:46 +00:00
{
$videoFilename = $this->download->getFilename($url, $format);
2017-01-02 22:08:51 +00:00
$this->assertEquals($videoFilename, $filename.'.'.$extension);
2016-07-29 22:47:46 +00:00
}
/**
2016-09-07 22:28:28 +00:00
* Test getFilename function errors.
2016-07-29 22:47:46 +00:00
*
* @param string $url URL
*
2016-09-07 22:28:28 +00:00
* @return void
2016-07-29 22:47:46 +00:00
* @expectedException Exception
* @dataProvider ErrorUrlProvider
*/
public function testGetFilenameError($url)
{
$this->download->getFilename($url);
}
/**
2016-09-07 22:28:28 +00:00
* Test getAudioFilename function.
2016-07-29 22:47:46 +00:00
*
2017-01-16 17:54:03 +00:00
* @param string $url URL
* @param string $format Format
* @param string $filename Filename
2016-07-29 22:47:46 +00:00
*
2016-09-07 22:28:28 +00:00
* @return void
2016-07-29 22:47:46 +00:00
* @dataProvider urlProvider
2017-01-16 12:47:29 +00:00
* @dataProvider m3uUrlProvider
* @dataProvider rtmpUrlProvider
* @dataProvider remuxUrlProvider
2016-07-29 22:47:46 +00:00
*/
2017-01-02 22:08:51 +00:00
public function testGetAudioFilename($url, $format, $filename)
2016-07-29 22:47:46 +00:00
{
$videoFilename = $this->download->getAudioFilename($url, $format);
2017-01-02 22:08:51 +00:00
$this->assertEquals($videoFilename, $filename.'.mp3');
2016-07-29 22:47:46 +00:00
}
/**
2016-09-07 22:28:28 +00:00
* Test getAudioStream function.
2016-07-29 22:47:46 +00:00
*
* @param string $url URL
* @param string $format Format
*
2016-09-07 22:28:28 +00:00
* @return void
2016-07-29 22:47:46 +00:00
* @dataProvider urlProvider
*/
public function testGetAudioStream($url, $format)
{
2016-07-30 12:01:00 +00:00
$stream = $this->download->getAudioStream($url, $format);
$this->assertInternalType('resource', $stream);
2016-08-01 16:42:08 +00:00
$this->assertFalse(feof($stream));
2016-07-29 22:47:46 +00:00
}
/**
2016-09-07 22:28:28 +00:00
* Test getAudioStream function without avconv.
2016-07-29 22:47:46 +00:00
*
* @param string $url URL
* @param string $format Format
*
2016-09-07 22:28:28 +00:00
* @return void
2016-07-29 22:47:46 +00:00
* @expectedException Exception
* @dataProvider urlProvider
*/
public function testGetAudioStreamAvconvError($url, $format)
{
$this->config->avconv = 'foobar';
$download = new VideoDownload($this->config);
$download->getAudioStream($url, $format);
2016-07-29 22:47:46 +00:00
}
2016-07-30 12:01:00 +00:00
/**
2016-09-07 22:28:28 +00:00
* Test getAudioStream function without curl or rtmpdump.
2016-07-30 12:01:00 +00:00
*
* @param string $url URL
* @param string $format Format
*
2016-09-07 22:28:28 +00:00
* @return void
2016-07-30 12:01:00 +00:00
* @expectedException Exception
* @dataProvider rtmpUrlProvider
2016-07-30 12:01:00 +00:00
*/
public function testGetAudioStreamRtmpError($url, $format)
2016-07-30 12:01:00 +00:00
{
$this->config->rtmpdump = 'foobar';
$download = new VideoDownload($this->config);
$download->getAudioStream($url, $format);
2016-07-30 12:01:00 +00:00
}
/**
* Test getAudioStream function with a M3U8 file.
*
2016-12-26 23:36:30 +00:00
* @param string $url URL
* @param string $format Format
*
* @return void
* @expectedException Exception
2017-01-16 12:47:29 +00:00
* @dataProvider m3uUrlProvider
2016-12-26 23:36:30 +00:00
*/
public function testGetAudioStreamM3uError($url, $format)
{
$this->download->getAudioStream($url, $format);
}
2017-04-25 22:08:35 +00:00
/**
2017-04-25 22:10:00 +00:00
* Assert that a stream is valid.
2017-04-25 22:08:35 +00:00
*
* @param resource $stream Stream
*
* @return void
*/
private function assertStream($stream)
{
$this->assertInternalType('resource', $stream);
$this->assertFalse(feof($stream));
}
2016-12-26 23:36:30 +00:00
/**
* Test getM3uStream function.
*
* @param string $url URL
* @param string $format Format
*
* @return void
2017-01-16 12:47:29 +00:00
* @dataProvider m3uUrlProvider
2016-12-26 23:36:30 +00:00
*/
public function testGetM3uStream($url, $format)
{
2017-04-25 22:08:35 +00:00
$this->assertStream(
$this->download->getM3uStream(
$this->download->getJSON($url, $format)
)
);
2016-12-26 23:36:30 +00:00
}
2017-04-24 22:40:24 +00:00
/**
* Test getRemuxStream function.
*
* @param string $url URL
* @param string $format Format
*
* @return void
* @dataProvider remuxUrlProvider
*/
public function testGetRemuxStream($url, $format)
{
$urls = $this->download->getURL($url, $format);
if (count($urls) > 1) {
2017-04-25 22:08:35 +00:00
$this->assertStream($this->download->getRemuxStream($urls));
2017-04-24 22:40:24 +00:00
}
}
/**
* Test getRtmpStream function.
*
* @param string $url URL
* @param string $format Format
*
* @return void
* @dataProvider rtmpUrlProvider
*/
public function testGetRtmpStream($url, $format)
{
2017-04-25 22:08:35 +00:00
$this->assertStream(
$this->download->getRtmpStream(
$this->download->getJSON($url, $format)
)
);
}
2016-12-26 23:36:30 +00:00
/**
* Test getM3uStream function without avconv.
*
* @param string $url URL
* @param string $format Format
*
* @return void
* @expectedException Exception
2017-01-16 12:47:29 +00:00
* @dataProvider m3uUrlProvider
*/
2016-12-26 23:36:30 +00:00
public function testGetM3uStreamAvconvError($url, $format)
{
$this->config->avconv = 'foobar';
$download = new VideoDownload($this->config);
$video = $download->getJSON($url, $format);
$download->getM3uStream($video);
}
2017-05-02 15:04:55 +00:00
/**
* Test getPlaylistArchiveStream function without avconv.
*
* @return void
* @requires OS Linux
2017-05-02 15:04:55 +00:00
*/
public function testGetPlaylistArchiveStream()
{
2017-05-19 12:30:59 +00:00
$video = $this->download->getJSON(
'https://www.youtube.com/playlist?list=PLgdySZU6KUXL_8Jq5aUkyNV7wCa-4wZsC',
'best'
);
2017-05-02 15:04:55 +00:00
$this->assertStream($this->download->getPlaylistArchiveStream($video, 'best'));
}
2015-08-29 19:46:57 +00:00
}