From 11ff0fa9c4ae37e72fc673a8ec1615745b80b6ca Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Fri, 4 Sep 2015 22:45:55 +0200 Subject: [PATCH] Remove unused code Unit tests for every function Error handling --- .gitignore | 1 + api.php | 18 ++--- download.php | 131 +++++++----------------------------- json.php | 8 ++- phpunit.xml | 15 +++++ redirect.php | 9 +-- tests/VideoDownloadTest.php | 115 ++++++++++++++++++++++++++----- 7 files changed, 159 insertions(+), 138 deletions(-) create mode 100644 phpunit.xml diff --git a/.gitignore b/.gitignore index 65a8d60..7b3d7a5 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ templates_c/ ffmpeg.tar.xz ffmpeg-*/ alltube-release.zip +coverage/ diff --git a/api.php b/api.php index cf18c14..6066621 100644 --- a/api.php +++ b/api.php @@ -15,9 +15,9 @@ $smarty->assign('class', 'video'); require_once 'download.php'; if (isset($_GET["url"])) { if (isset($_GET['audio'])) { - $video = VideoDownload::getJSON($_GET["url"]); + try { + $video = VideoDownload::getJSON($_GET["url"]); - if (isset($video->url)) { //Vimeo needs a correct user-agent $UA = VideoDownload::getUA(); ini_set( @@ -61,24 +61,24 @@ if (isset($_GET["url"])) { ); exit; } - } else { - $error=true; + } catch (Exception $e) { + $error = $e->getMessage(); } } else { - $video = VideoDownload::getJSON($_GET["url"]); - if (isset($video->webpage_url)) { + try { + $video = VideoDownload::getJSON($_GET["url"]); $smarty->display('head.tpl'); $smarty->assign('video', $video); $smarty->display('video.tpl'); $smarty->display('footer.tpl'); - } else { - $error=true; + } catch (Exception $e) { + $error = $e->getMessage(); } } } if (isset($error)) { $smarty->display('head.tpl'); - $smarty->assign('errors', $video['error']); + $smarty->assign('errors', $error); $smarty->display('error.tpl'); $smarty->display('footer.tpl'); } diff --git a/download.php b/download.php index 516fda1..8bcd6de 100644 --- a/download.php +++ b/download.php @@ -2,9 +2,9 @@ /** * PHP web interface for youtube-dl (http://rg3.github.com/youtube-dl/) * Main class - * + * * PHP Version 5.3.10 - * + * * @category Youtube-dl * @package Youtubedl * @author Pierre Rudloff @@ -15,9 +15,9 @@ /** * PHP web interface for youtube-dl (http://rg3.github.com/youtube-dl/) * Main class - * + * * PHP Version 5.3.10 - * + * * @category Youtube-dl * @package Youtubedl * @author Pierre Rudloff @@ -26,53 +26,40 @@ * */ Class VideoDownload { - /** - * Get version of youtube-dl - * - * @return string Version - * */ - static function getVersion() - { - exec( - PYTHON.' '.YOUTUBE_DL.' --version', - $version, $code - ); - return $version[0]; - } /** * Get the user agent used youtube-dl - * + * * @return string UA * */ static function getUA() { exec( PYTHON.' '.YOUTUBE_DL.' --dump-user-agent', - $version, $code + $version ); return $version[0]; } - + /** * List all extractors - * + * * @return array Extractors * */ static function listExtractors() { exec( PYTHON.' '.YOUTUBE_DL.' --list-extractors', - $extractors, $code + $extractors ); return $extractors; } - + /** * Get filename of video - * + * * @param string $url URL of page * @param string $format Format to use for the video - * + * * @return string Filename * */ static function getFilename($url, $format=null) @@ -88,31 +75,13 @@ Class VideoDownload ); return end($filename); } - - /** - * Get title of video - * - * @param string $url URL of page - * - * @return string Title - * */ - static function getTitle($url) - { - exec( - PYTHON.' '.YOUTUBE_DL.' --get-title '. - escapeshellarg($url), - $title - ); - $title=$title[0]; - return $title; - } - + /** * Get all information about a video - * + * * @param string $url URL of page * @param string $format Format to use for the video - * + * * @return string JSON * */ static function getJSON($url, $format=null) @@ -123,88 +92,38 @@ Class VideoDownload } $cmd .=' --dump-json '.escapeshellarg($url)." 2>&1"; exec( - $cmd, - $json, $code + $cmd, $result, $code ); if ($code>0) { - return array('success'=>false, 'error'=>$json); + throw new Exception(implode(PHP_EOL, $result)); } else { - return json_decode($json[0]); + return json_decode($result[0]); } } - /** - * Get thumbnail of video - * - * @param string $url URL of page - * - * @return string URL of image - * */ - static function getThumbnail($url) - { - exec( - PYTHON.' '.YOUTUBE_DL.' --get-thumbnail '. - escapeshellarg($url), - $thumb - ); - if (isset($thumb[0])) { - return $thumb[0]; - } - } - - /** - * Get a list available formats for this video - * - * @param string $url URL of page - * - * @return string Title - * */ - static function getAvailableFormats($url) - { - exec( - PYTHON.' '.YOUTUBE_DL.' -F '. - escapeshellarg($url), - $formats - ); - $return=array(); - foreach ($formats as $i=>$format) { - if ($i > 4) { - $return[]=(preg_split('/(\s\s+)|(\s+:?\s+)|(\s+\[)|\]/', $format)); - } - } - if (empty($return)) { - foreach ($formats as $i=>$format) { - if ($i > 3) { - $return[]=preg_split('/(\s\s+)|(\s+:?\s+)|(\s+\[)|\]/', $format); - } - } - } - return $return; - } - /** * Get URL of video from URL of page - * + * * @param string $url URL of page * @param string $format Format to use for the video - * + * * @return string URL of video * */ static function getURL($url, $format=null) { - $cmd=PYTHON.' '.YOUTUBE_DL; + $cmd=PYTHON.' '.YOUTUBE_DL.' '.PARAMS; if (isset($format)) { $cmd .= ' -f '.escapeshellarg($format); } $cmd .=' -g '.escapeshellarg($url)." 2>&1"; exec( - $cmd, $url, $code + $cmd, $result, $code ); if ($code>0) { - return array('success'=>false, 'error'=>$url); + throw new Exception(implode(PHP_EOL, $result)); } else { - return array('success'=>true, 'url'=>end($url)); + return array('success'=>true, 'url'=>end($result)); } - + } } diff --git a/json.php b/json.php index 9131fde..0212e9d 100644 --- a/json.php +++ b/json.php @@ -15,6 +15,10 @@ require_once 'common.php'; require_once 'download.php'; if (isset($_GET["url"])) { header('Content-Type: application/json'); - $video = VideoDownload::getJSON($_GET["url"]); - echo json_encode($video); + try { + $video = VideoDownload::getJSON($_GET["url"]); + echo json_encode($video); + } catch (Exception $e) { + echo json_encode(array('success'=>false, 'error'=>$e->getMessage())); + } } diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..32420be --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,15 @@ + + + + download.php + + + + + tests/ + + + + + + diff --git a/redirect.php b/redirect.php index de9a40d..c43f141 100644 --- a/redirect.php +++ b/redirect.php @@ -14,10 +14,11 @@ require_once 'common.php'; require_once 'download.php'; if (isset($_GET["url"])) { - $video = VideoDownload::getURL($_GET["url"]); - if (isset($video['url'])) { + try { + $video = VideoDownload::getURL($_GET["url"]); header('Location: '.$video['url']); - } else { - echo "Can't find video"; + } catch (Exception $e) { + header('Content-Type: text/plain'); + echo $e->getMessage(); } } diff --git a/tests/VideoDownloadTest.php b/tests/VideoDownloadTest.php index 7e01706..430f25b 100644 --- a/tests/VideoDownloadTest.php +++ b/tests/VideoDownloadTest.php @@ -27,17 +27,6 @@ require_once __DIR__.'/../download.php'; * */ class VideoDownloadTest extends PHPUnit_Framework_TestCase { - static private $_testVideoURL = 'https://www.youtube.com/watch?v=RJJ6FCAXvKg'; - - /** - * Test getVersion function - * @return void - */ - public function testGetVersion() - { - $this->assertStringMatchesFormat('%i.%i.%i', VideoDownload::getVersion()); - } - /** * Test getUA function * @return void @@ -54,18 +43,110 @@ class VideoDownloadTest extends PHPUnit_Framework_TestCase public function testListExtractors() { $extractors = VideoDownload::listExtractors(); - $this->assertNotEmpty($extractors); - $this->assertInternalType('array', $extractors); + $this->assertContains('youtube', $extractors); } /** * Test getURL function + * @param string $url URL + * @param string $format Format + * @return void + * @dataProvider URLProvider + */ + public function testGetURL($url, $format) + { + $videoURL = VideoDownload::getURL($url, $format); + $this->assertArrayHasKey('success', $videoURL); + $this->assertArrayHasKey('url', $videoURL); + } + + /** + * Test getURL function errors + * @param string $url URL + * @return void + * @expectedException Exception + * @dataProvider ErrorURLProvider + */ + public function testGetURLError($url) + { + $videoURL = VideoDownload::getURL($url); + } + + /** + * Provides URLs for tests * @return void */ - public function testGetURL() + public function URLProvider() { - $url = VideoDownload::getURL(self::$_testVideoURL); - $this->assertArrayHasKey('success', $url); - $this->assertArrayHasKey('url', $url); + return array( + array( + 'https://www.youtube.com/watch?v=M7IpKCZ47pU', null, + "It's Not Me, It's You - Hearts Under Fire-M7IpKCZ47pU.mp4" + ), + array( + 'https://www.youtube.com/watch?v=RJJ6FCAXvKg', 22, + "'Heart Attack' - Demi Lovato ". + "(Sam Tsui & Against The Current)-RJJ6FCAXvKg.mp4" + ), + array( + 'https://vimeo.com/24195442', null, + "Carving the Mountains-24195442.mp4" + ), + ); + } + + /** + * Provides incorrect URLs for tests + * @return void + */ + public function errorURLProvider() + { + return array( + array('http://example.com/video') + ); + } + + /** + * Test getFilename function + * @param string $url URL + * @param string $format Format + * @param string $result Expected filename + * @return void + * @dataProvider URLProvider + */ + public function testGetFilename($url, $format, $result) + { + $filename = VideoDownload::getFilename($url, $format); + $this->assertEquals($filename, $result); + } + + /** + * Test getJSON function + * @param string $url URL + * @param string $format Format + * @return void + * @dataProvider URLProvider + */ + public function testGetJSON($url, $format) + { + $info = VideoDownload::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 + * @param string $url URL + * @return void + * @expectedException Exception + * @dataProvider ErrorURLProvider + */ + public function testGetJSONError($url) + { + $videoURL = VideoDownload::getJSON($url); } }