alltube/tests/VideoDownloadTest.php

266 lines
7.0 KiB
PHP
Raw Normal View History

2015-08-29 21:46:57 +02:00
<?php
/**
* VideoDownloadTest class
2016-08-01 13:29:13 +02:00
*/
2016-03-30 01:49:08 +02:00
namespace Alltube\Test;
2015-10-29 20:43:43 +01:00
use Alltube\VideoDownload;
2015-08-29 21:46:57 +02:00
/**
* Unit tests for the VideoDownload class
2016-08-01 13:29:13 +02:00
*/
2016-03-30 01:49:08 +02:00
class VideoDownloadTest extends \PHPUnit_Framework_TestCase
2015-08-29 21:46:57 +02:00
{
2016-08-01 13:29:13 +02:00
/**
* VideoDownload instance
* @var VideoDownload
*/
private $download;
/**
* Initialize properties used by test
*/
2016-04-08 19:06:41 +02:00
protected function setUp()
{
$this->download = new VideoDownload();
}
2016-08-01 13:29:13 +02:00
/**
* Destroy properties after test
*/
protected function tearDown()
{
\Alltube\Config::destroyInstance();
}
2015-08-29 21:46:57 +02:00
/**
2016-04-10 23:57:19 +02:00
* Test listExtractors function
2015-10-29 18:40:22 +01:00
*
2015-08-29 21:46:57 +02:00
* @return void
*/
public function testListExtractors()
{
2016-04-08 19:06:41 +02:00
$extractors = $this->download->listExtractors();
$this->assertContains('youtube', $extractors);
2015-08-29 21:46:57 +02:00
}
/**
* Test getURL function
2015-10-29 18:40:22 +01:00
*
* @param string $url URL
* @param string $format Format
2016-08-01 13:29:13 +02:00
* @param string $filename Filename
* @param string $domain Domain
2015-10-29 18:40:22 +01:00
*
2015-10-29 21:23:02 +01:00
* @return void
2015-10-29 18:40:22 +01:00
* @dataProvider urlProvider
*/
2016-04-08 20:08:04 +02:00
public function testGetURL($url, $format, $filename, $domain)
{
2016-04-08 19:06:41 +02:00
$videoURL = $this->download->getURL($url, $format);
2016-04-08 20:08:04 +02:00
$this->assertContains($domain, $videoURL);
}
/**
* Test getURL function errors
2015-10-29 18:40:22 +01:00
*
* @param string $url URL
2015-10-29 18:40:22 +01:00
*
2015-10-29 21:23:02 +01:00
* @return void
* @expectedException Exception
2015-10-29 21:23:02 +01:00
* @dataProvider ErrorUrlProvider
*/
public function testGetURLError($url)
{
2016-04-08 20:08:04 +02:00
$this->download->getURL($url);
}
/**
* Provides URLs for tests
2015-10-29 18:40:22 +01:00
*
2016-08-01 13:29:13 +02:00
* @return array[]
*/
2015-10-29 18:40:22 +01:00
public function urlProvider()
{
return array(
array(
'https://www.youtube.com/watch?v=M7IpKCZ47pU', null,
2016-04-08 20:08:04 +02:00
"It's Not Me, It's You - Hearts Under Fire-M7IpKCZ47pU.mp4",
2016-07-30 00:47:46 +02:00
'googlevideo.com',
"It's Not Me, It's You - Hearts Under Fire-M7IpKCZ47pU.mp3"
),
array(
'https://www.youtube.com/watch?v=RJJ6FCAXvKg', 22,
"'Heart Attack' - Demi Lovato ".
2016-04-08 20:08:04 +02:00
"(Sam Tsui & Against The Current)-RJJ6FCAXvKg.mp4",
2016-07-30 00:47:46 +02:00
'googlevideo.com',
"'Heart Attack' - Demi Lovato ".
"(Sam Tsui & Against The Current)-RJJ6FCAXvKg.mp3"
),
array(
'https://vimeo.com/24195442', null,
2016-04-08 20:08:04 +02:00
"Carving the Mountains-24195442.mp4",
2016-07-30 00:47:46 +02:00
'vimeocdn.com',
"Carving the Mountains-24195442.mp3"
),
2016-07-30 14:01:00 +02:00
array(
'http://www.bbc.co.uk/programmes/b039g8p7', 'bestaudio/best',
"Leonard Cohen, Kaleidoscope - BBC Radio 4-b039d07m.flv",
'bbcodspdns.fcod.llnwd.net',
"Leonard Cohen, Kaleidoscope - BBC Radio 4-b039d07m.mp3"
),
array(
'http://www.rtl2.de/sendung/grip-das-motormagazin/folge/folge-203-0', 'bestaudio/best',
"GRIP sucht den Sommerkönig-folge-203-0.f4v",
'edgefcs.net',
"GRIP sucht den Sommerkönig-folge-203-0.mp3"
)
);
}
/**
* Provides incorrect URLs for tests
2015-10-29 18:40:22 +01:00
*
2016-08-01 13:29:13 +02:00
* @return array[]
*/
2015-10-29 18:40:22 +01:00
public function errorUrlProvider()
{
return array(
array('http://example.com/video')
);
}
/**
* Test getJSON function
2015-10-29 18:40:22 +01:00
*
* @param string $url URL
* @param string $format Format
2015-10-29 18:40:22 +01:00
*
2015-10-29 21:23:02 +01:00
* @return void
* @dataProvider URLProvider
*/
public function testGetJSON($url, $format)
{
2016-04-08 19:06:41 +02: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('formats', $info);
$this->assertObjectHasAttribute('_filename', $info);
}
/**
* Test getJSON function errors
2015-10-29 18:40:22 +01:00
*
* @param string $url URL
2015-10-29 18:40:22 +01:00
*
2015-10-29 21:23:02 +01:00
* @return void
* @expectedException Exception
2015-10-29 21:23:02 +01:00
* @dataProvider ErrorURLProvider
2015-08-29 21:46:57 +02:00
*/
public function testGetJSONError($url)
2015-08-29 21:46:57 +02:00
{
2016-04-08 19:06:41 +02:00
$videoURL = $this->download->getJSON($url);
2015-08-29 21:46:57 +02:00
}
2016-07-30 00:47:46 +02:00
/**
* Test getFilename function
*
* @param string $url URL
* @param string $format Format
2016-08-01 13:29:13 +02:00
* @param string $filename Filename
2016-07-30 00:47:46 +02:00
*
* @return void
* @dataProvider urlProvider
*/
public function testGetFilename($url, $format, $filename)
{
$videoFilename = $this->download->getFilename($url, $format);
$this->assertEquals($videoFilename, $filename);
}
/**
* Test getFilename function errors
*
* @param string $url URL
*
* @return void
* @expectedException Exception
* @dataProvider ErrorUrlProvider
*/
public function testGetFilenameError($url)
{
$this->download->getFilename($url);
}
/**
* Test getAudioFilename function
*
* @param string $url URL
* @param string $format Format
2016-08-01 13:29:13 +02:00
* @param string $filename Filename
* @param string $domain Domain
* @param string $audioFilename MP3 audio file name
2016-07-30 00:47:46 +02:00
*
* @return void
* @dataProvider urlProvider
*/
public function testGetAudioFilename($url, $format, $filename, $domain, $audioFilename)
{
$videoFilename = $this->download->getAudioFilename($url, $format);
$this->assertEquals($videoFilename, $audioFilename);
}
/**
* Test getAudioStream function
*
* @param string $url URL
* @param string $format Format
*
* @return void
* @dataProvider urlProvider
*/
public function testGetAudioStream($url, $format)
{
2016-07-30 14:01:00 +02:00
$stream = $this->download->getAudioStream($url, $format);
$this->assertInternalType('resource', $stream);
2016-08-01 18:42:08 +02:00
$this->assertFalse(feof($stream));
2016-07-30 00:47:46 +02:00
}
/**
2016-07-30 14:01:00 +02:00
* Test getAudioStream function without avconv
2016-07-30 00:47:46 +02:00
*
* @param string $url URL
* @param string $format Format
*
* @return void
* @expectedException Exception
* @dataProvider urlProvider
*/
public function testGetAudioStreamAvconvError($url, $format)
{
$config = \Alltube\Config::getInstance();
$config->avconv = 'foobar';
$this->download->getAudioStream($url, $format);
}
2016-07-30 14:01:00 +02:00
/**
* Test getAudioStream function without curl or rtmpdump
*
* @param string $url URL
* @param string $format Format
*
* @return void
* @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 21:46:57 +02:00
}