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;
|
|
|
|
|
2016-10-18 08:45:16 +00:00
|
|
|
use Alltube\Config;
|
2016-10-18 08:45:54 +00:00
|
|
|
use Alltube\VideoDownload;
|
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
|
|
|
*/
|
2016-03-29 23:49:08 +00:00
|
|
|
class VideoDownloadTest extends \PHPUnit_Framework_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;
|
|
|
|
|
|
|
|
/**
|
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()
|
|
|
|
{
|
|
|
|
$this->download = new VideoDownload();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2016-07-30 10:40:49 +00:00
|
|
|
protected function tearDown()
|
|
|
|
{
|
2016-10-18 08:45:16 +00:00
|
|
|
Config::destroyInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test VideoDownload constructor with wrong youtube-dl path.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @expectedException Exception
|
|
|
|
*/
|
|
|
|
public function testConstructorWithMissingYoutubedl()
|
|
|
|
{
|
|
|
|
new VideoDownload(
|
2016-10-18 08:45:54 +00:00
|
|
|
new Config(['youtubedl' => 'foo'])
|
2016-10-18 08:45:16 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test VideoDownload constructor with wrong Python path.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @expectedException Exception
|
|
|
|
*/
|
|
|
|
public function testConstructorWithMissingPython()
|
|
|
|
{
|
|
|
|
new VideoDownload(
|
2016-10-18 08:45:54 +00:00
|
|
|
new Config(['python' => 'foo'])
|
2016-10-18 08:45:16 +00:00
|
|
|
);
|
2016-07-30 10:40:49 +00:00
|
|
|
}
|
|
|
|
|
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();
|
2015-09-04 20:45:55 +00:00
|
|
|
$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
|
|
|
*
|
2016-09-07 22:28:28 +00:00
|
|
|
* @param string $url URL
|
|
|
|
* @param string $format Format
|
2016-08-01 11:29:13 +00:00
|
|
|
* @param string $filename Filename
|
2016-09-07 22:28:28 +00:00
|
|
|
* @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
|
2015-09-04 20:45:55 +00:00
|
|
|
*/
|
2016-04-08 18:08:04 +00:00
|
|
|
public function testGetURL($url, $format, $filename, $domain)
|
2015-09-04 20:45:55 +00:00
|
|
|
{
|
2016-04-08 17:06:41 +00:00
|
|
|
$videoURL = $this->download->getURL($url, $format);
|
2016-04-08 18:08:04 +00:00
|
|
|
$this->assertContains($domain, $videoURL);
|
2015-09-04 20:45:55 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 21:01:31 +00:00
|
|
|
/**
|
2016-10-20 21:03:13 +00:00
|
|
|
* Test getURL function with a protected video.
|
2016-10-20 21:01:31 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testGetURLWithPassword()
|
|
|
|
{
|
|
|
|
$this->assertContains('vimeocdn.com', $this->download->getURL('http://vimeo.com/68375962', null, 'youtube-dl'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
}
|
|
|
|
|
2015-09-04 20:45:55 +00:00
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Test getURL function errors.
|
2015-10-29 17:40:22 +00:00
|
|
|
*
|
2015-09-04 20:45:55 +00:00
|
|
|
* @param string $url URL
|
2015-10-29 17:40:22 +00:00
|
|
|
*
|
2016-09-07 22:28:28 +00:00
|
|
|
* @return void
|
2015-09-04 20:45:55 +00:00
|
|
|
* @expectedException Exception
|
2015-10-29 20:23:02 +00:00
|
|
|
* @dataProvider ErrorUrlProvider
|
2015-09-04 20:45:55 +00:00
|
|
|
*/
|
|
|
|
public function testGetURLError($url)
|
|
|
|
{
|
2016-04-08 18:08:04 +00:00
|
|
|
$this->download->getURL($url);
|
2015-09-04 20:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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-09-04 20:45:55 +00:00
|
|
|
*/
|
2015-10-29 17:40:22 +00:00
|
|
|
public function urlProvider()
|
2015-09-04 20:45:55 +00:00
|
|
|
{
|
2016-09-07 22:28:28 +00:00
|
|
|
return [
|
|
|
|
[
|
2015-09-04 20:45:55 +00:00
|
|
|
'https://www.youtube.com/watch?v=M7IpKCZ47pU', null,
|
2016-04-08 18:08:04 +00:00
|
|
|
"It's Not Me, It's You - Hearts Under Fire-M7IpKCZ47pU.mp4",
|
2016-07-29 22:47:46 +00:00
|
|
|
'googlevideo.com',
|
2016-09-07 22:28:28 +00:00
|
|
|
"It's Not Me, It's You - Hearts Under Fire-M7IpKCZ47pU.mp3",
|
|
|
|
],
|
|
|
|
[
|
2015-09-04 20:45:55 +00:00
|
|
|
'https://www.youtube.com/watch?v=RJJ6FCAXvKg', 22,
|
|
|
|
"'Heart Attack' - Demi Lovato ".
|
2016-09-07 22:28:28 +00:00
|
|
|
'(Sam Tsui & Against The Current)-RJJ6FCAXvKg.mp4',
|
2016-07-29 22:47:46 +00:00
|
|
|
'googlevideo.com',
|
|
|
|
"'Heart Attack' - Demi Lovato ".
|
2016-09-07 22:28:28 +00:00
|
|
|
'(Sam Tsui & Against The Current)-RJJ6FCAXvKg.mp3',
|
|
|
|
],
|
|
|
|
[
|
2015-09-04 20:45:55 +00:00
|
|
|
'https://vimeo.com/24195442', null,
|
2016-09-07 22:28:28 +00:00
|
|
|
'Carving the Mountains-24195442.mp4',
|
2016-07-29 22:47:46 +00:00
|
|
|
'vimeocdn.com',
|
2016-09-07 22:28:28 +00:00
|
|
|
'Carving the Mountains-24195442.mp3',
|
|
|
|
],
|
|
|
|
[
|
2016-07-30 12:01:00 +00:00
|
|
|
'http://www.bbc.co.uk/programmes/b039g8p7', 'bestaudio/best',
|
2016-09-07 22:28:28 +00:00
|
|
|
'Leonard Cohen, Kaleidoscope - BBC Radio 4-b039d07m.flv',
|
2016-07-30 12:01:00 +00:00
|
|
|
'bbcodspdns.fcod.llnwd.net',
|
2016-09-07 22:28:28 +00:00
|
|
|
'Leonard Cohen, Kaleidoscope - BBC Radio 4-b039d07m.mp3',
|
|
|
|
],
|
|
|
|
[
|
2016-07-30 12:01:00 +00:00
|
|
|
'http://www.rtl2.de/sendung/grip-das-motormagazin/folge/folge-203-0', 'bestaudio/best',
|
2016-09-07 22:28:28 +00:00
|
|
|
'GRIP sucht den Sommerkönig-folge-203-0.f4v',
|
2016-07-30 12:01:00 +00:00
|
|
|
'edgefcs.net',
|
2016-09-07 22:28:28 +00:00
|
|
|
'GRIP sucht den Sommerkönig-folge-203-0.mp3',
|
|
|
|
],
|
2016-12-26 14:50:26 +00:00
|
|
|
[
|
|
|
|
//Only works on the feature/stream branch
|
|
|
|
'https://twitter.com/verge/status/813055465324056576/video/1', null,
|
|
|
|
'The Verge - This tiny origami robot can self-fold and complete tasks-813055465324056576.mp4',
|
|
|
|
'video.twimg.com',
|
|
|
|
'The Verge - This tiny origami robot can self-fold and complete tasks-813055465324056576.mp3',
|
|
|
|
]
|
2016-09-07 22:28:28 +00:00
|
|
|
];
|
2015-09-04 20:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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-09-04 20:45:55 +00:00
|
|
|
*/
|
2015-10-29 17:40:22 +00:00
|
|
|
public function errorUrlProvider()
|
2015-09-04 20:45:55 +00:00
|
|
|
{
|
2016-09-07 22:28:28 +00:00
|
|
|
return [
|
|
|
|
['http://example.com/video'],
|
|
|
|
];
|
2015-09-04 20:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Test getJSON function.
|
2015-10-29 17:40:22 +00:00
|
|
|
*
|
2015-09-04 20:45:55 +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
|
2015-09-04 20:45:55 +00:00
|
|
|
* @dataProvider URLProvider
|
|
|
|
*/
|
|
|
|
public function testGetJSON($url, $format)
|
|
|
|
{
|
2016-04-08 17:06:41 +00:00
|
|
|
$info = $this->download->getJSON($url, $format);
|
2015-09-04 20:45:55 +00:00
|
|
|
$this->assertObjectHasAttribute('webpage_url', $info);
|
|
|
|
$this->assertObjectHasAttribute('url', $info);
|
|
|
|
$this->assertObjectHasAttribute('ext', $info);
|
|
|
|
$this->assertObjectHasAttribute('title', $info);
|
|
|
|
$this->assertObjectHasAttribute('formats', $info);
|
|
|
|
$this->assertObjectHasAttribute('_filename', $info);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-07 22:28:28 +00:00
|
|
|
* Test getJSON function errors.
|
2015-10-29 17:40:22 +00:00
|
|
|
*
|
2015-09-04 20:45:55 +00:00
|
|
|
* @param string $url URL
|
2015-10-29 17:40:22 +00:00
|
|
|
*
|
2016-09-07 22:28:28 +00:00
|
|
|
* @return void
|
2015-09-04 20:45:55 +00:00
|
|
|
* @expectedException Exception
|
2015-10-29 20:23:02 +00:00
|
|
|
* @dataProvider ErrorURLProvider
|
2015-08-29 19:46:57 +00:00
|
|
|
*/
|
2015-09-04 20:45:55 +00:00
|
|
|
public function testGetJSONError($url)
|
2015-08-29 19:46:57 +00:00
|
|
|
{
|
2016-04-08 17:06:41 +00:00
|
|
|
$videoURL = $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
|
|
|
*
|
2016-09-07 22:28:28 +00:00
|
|
|
* @param string $url URL
|
|
|
|
* @param string $format Format
|
2016-08-01 11:29:13 +00:00
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
public function testGetFilename($url, $format, $filename)
|
|
|
|
{
|
|
|
|
$videoFilename = $this->download->getFilename($url, $format);
|
|
|
|
$this->assertEquals($videoFilename, $filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*
|
2016-09-07 22:28:28 +00:00
|
|
|
* @param string $url URL
|
|
|
|
* @param string $format Format
|
|
|
|
* @param string $filename Filename
|
|
|
|
* @param string $domain Domain
|
2016-08-01 11:29:13 +00:00
|
|
|
* @param string $audioFilename MP3 audio file name
|
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
|
|
|
|
*/
|
|
|
|
public function testGetAudioFilename($url, $format, $filename, $domain, $audioFilename)
|
|
|
|
{
|
|
|
|
$videoFilename = $this->download->getAudioFilename($url, $format);
|
|
|
|
$this->assertEquals($videoFilename, $audioFilename);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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)
|
|
|
|
{
|
|
|
|
$config = \Alltube\Config::getInstance();
|
|
|
|
$config->avconv = 'foobar';
|
|
|
|
$this->download->getAudioStream($url, $format);
|
|
|
|
}
|
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 urlProvider
|
|
|
|
*/
|
|
|
|
public function testGetAudioStreamCurlError($url, $format)
|
|
|
|
{
|
|
|
|
$config = \Alltube\Config::getInstance();
|
|
|
|
$config->curl = 'foobar';
|
|
|
|
$config->rtmpdump = 'foobar';
|
|
|
|
$this->download->getAudioStream($url, $format);
|
|
|
|
}
|
2015-08-29 19:46:57 +00:00
|
|
|
}
|