diff --git a/classes/VideoDownload.php b/classes/VideoDownload.php index 67ebd9b..c252697 100644 --- a/classes/VideoDownload.php +++ b/classes/VideoDownload.php @@ -131,7 +131,7 @@ class VideoDownload if (!$process->isSuccessful()) { throw new \Exception($process->getErrorOutput()); } else { - return $process->getOutput(); + return trim($process->getOutput()); } } @@ -147,7 +147,7 @@ class VideoDownload ); } - public function getConversionProcess($url, $format) + public function getAudioStream($url, $format) { if (!shell_exec('which '.$this->config->avconv)) { throw(new \Exception('Can\'t find avconv or ffmpeg')); diff --git a/controllers/FrontController.php b/controllers/FrontController.php index ea9dbd5..f4098b8 100644 --- a/controllers/FrontController.php +++ b/controllers/FrontController.php @@ -129,7 +129,7 @@ class FrontController $response = $response->withHeader('Content-Type', 'audio/mpeg'); if ($request->isGet()) { - $process = $this->download->getConversionProcess($params["url"], 'bestaudio/best'); + $process = $this->download->getAudioStream($params["url"], 'bestaudio/best'); $response = $response->withBody(new Stream($process)); } return $response; diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index c4fde67..a103010 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -38,5 +38,11 @@ class ConfigTest extends \PHPUnit_Framework_TestCase putenv('CONVERT=1'); $config = Config::getInstance(); $this->assertEquals($config->convert, true); + $this->assertInternalType('array', $config->curl_params); + $this->assertInternalType('array', $config->params); + $this->assertInternalType('string', $config->youtubedl); + $this->assertInternalType('string', $config->python); + $this->assertInternalType('string', $config->avconv); + $this->assertInternalType('string', $config->rtmpdump); } } diff --git a/tests/VideoDownloadTest.php b/tests/VideoDownloadTest.php index ce9e9b2..4855d74 100644 --- a/tests/VideoDownloadTest.php +++ b/tests/VideoDownloadTest.php @@ -83,18 +83,22 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase array( 'https://www.youtube.com/watch?v=M7IpKCZ47pU', null, "It's Not Me, It's You - Hearts Under Fire-M7IpKCZ47pU.mp4", - 'googlevideo.com' + '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 ". "(Sam Tsui & Against The Current)-RJJ6FCAXvKg.mp4", - 'googlevideo.com' + 'googlevideo.com', + "'Heart Attack' - Demi Lovato ". + "(Sam Tsui & Against The Current)-RJJ6FCAXvKg.mp3" ), array( 'https://vimeo.com/24195442', null, "Carving the Mountains-24195442.mp4", - 'vimeocdn.com' + 'vimeocdn.com', + "Carving the Mountains-24195442.mp3" ), ); } @@ -144,4 +148,80 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase { $videoURL = $this->download->getJSON($url); } + + /** + * Test getFilename function + * + * @param string $url URL + * @param string $format Format + * + * @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 + * + * @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) + { + $process = $this->download->getAudioStream($url, $format); + $this->assertInternalType('resource', $process); + } + + /** + * Test getAudioStream function + * + * @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); + } }