From 2432a06c1bdce7cf05d778f5b114278ce1bb1e61 Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Sat, 11 Nov 2017 13:58:55 +0100 Subject: [PATCH] Make tests run on Windows (fixes #137) Use AppVeyor to run tests --- .appveyor.yml | 20 +++++++++++ classes/PlaylistArchiveStream.php | 4 +-- classes/VideoDownload.php | 4 +-- config/config.example.yml | 6 ++-- config/config_test_windows.yml | 12 +++++++ controllers/FrontController.php | 2 +- tests/FrontControllerTest.php | 54 +++++++++++++++++++++++------ tests/PlaylistArchiveStreamTest.php | 8 ++++- tests/VideoDownloadTest.php | 35 +++++++++++++------ 9 files changed, 116 insertions(+), 29 deletions(-) create mode 100644 .appveyor.yml create mode 100644 config/config_test_windows.yml diff --git a/.appveyor.yml b/.appveyor.yml new file mode 100644 index 0000000..12ea72f --- /dev/null +++ b/.appveyor.yml @@ -0,0 +1,20 @@ +--- +install: + - sc config wuauserv start= auto + - net start wuauserv + - cinst php composer ffmpeg rtmpdump + - refreshenv + - copy C:\tools\php71\php.ini-development C:\tools\php71\php.ini + - echo extension=C:\tools\php71\ext\php_gmp.dll >> C:\tools\php71\php.ini + - echo extension=C:\tools\php71\ext\php_gettext.dll >> C:\tools\php71\php.ini + - echo extension=C:\tools\php71\ext\php_intl.dll >> C:\tools\php71\php.ini + - echo extension=C:\tools\php71\ext\php_openssl.dll >> C:\tools\php71\php.ini + - echo extension=C:\tools\php71\ext\php_mbstring.dll >> C:\tools\php71\php.ini + - composer install --no-dev + - composer global require phpunit/phpunit + - C:\Python36\python.exe -m pip install youtube-dl + +test_script: + - phpunit + +build: "off" diff --git a/classes/PlaylistArchiveStream.php b/classes/PlaylistArchiveStream.php index c97f6b8..d9b37d5 100644 --- a/classes/PlaylistArchiveStream.php +++ b/classes/PlaylistArchiveStream.php @@ -61,10 +61,10 @@ class PlaylistArchiveStream extends TarArchive /** * PlaylistArchiveStream constructor. */ - public function __construct() + public function __construct(Config $config = null) { $this->client = new \GuzzleHttp\Client(); - $this->download = new VideoDownload(); + $this->download = new VideoDownload($config); } /** diff --git a/classes/VideoDownload.php b/classes/VideoDownload.php index 9005c85..12cb4fd 100644 --- a/classes/VideoDownload.php +++ b/classes/VideoDownload.php @@ -58,7 +58,7 @@ class VideoDownload * */ public function listExtractors() { - return explode(PHP_EOL, trim($this->getProp(null, null, 'list-extractors'))); + return explode("\n", trim($this->getProp(null, null, 'list-extractors'))); } /** @@ -135,7 +135,7 @@ class VideoDownload * */ public function getURL($url, $format = null, $password = null) { - return explode(PHP_EOL, $this->getProp($url, $format, 'get-url', $password)); + return explode("\n", $this->getProp($url, $format, 'get-url', $password)); } /** diff --git a/config/config.example.yml b/config/config.example.yml index 3479f61..2740303 100644 --- a/config/config.example.yml +++ b/config/config.example.yml @@ -7,10 +7,10 @@ python: /usr/bin/python # An array of parameters to pass to youtube-dl params: - - --no-playlist - --no-warnings - - --playlist-end - - 1 + - --ignore-errors + - --flat-playlist + - --restrict-filenames # True to enable audio conversion convert: false diff --git a/config/config_test_windows.yml b/config/config_test_windows.yml new file mode 100644 index 0000000..29a7740 --- /dev/null +++ b/config/config_test_windows.yml @@ -0,0 +1,12 @@ +--- +convert: false +python: C:\Python36\python.exe +avconv: C:\ProgramData\chocolatey\bin\ffmpeg.exe +rtmpdump: C:\ProgramData\chocolatey\bin\rtmpdump +youtubedl: C:\Python36\Lib\site-packages\youtube_dl\__main__.py +params: + - --no-warnings + - --ignore-errors + - --flat-playlist + - --restrict-filenames + - --no-check-certificate diff --git a/controllers/FrontController.php b/controllers/FrontController.php index 39c68a8..5ac9459 100644 --- a/controllers/FrontController.php +++ b/controllers/FrontController.php @@ -83,7 +83,7 @@ class FrontController } else { $this->config = Config::getInstance(); } - $this->download = new VideoDownload(); + $this->download = new VideoDownload($this->config); $this->container = $container; $this->view = $this->container->get('view'); $this->localeManager = $this->container->get('locale'); diff --git a/tests/FrontControllerTest.php b/tests/FrontControllerTest.php index 69ce74c..40e8d2e 100644 --- a/tests/FrontControllerTest.php +++ b/tests/FrontControllerTest.php @@ -48,6 +48,13 @@ class FrontControllerTest extends TestCase */ private $controller; + /** + * Config class instance. + * + * @var Config + */ + private $config; + /** * Prepare tests. */ @@ -58,7 +65,15 @@ class FrontControllerTest extends TestCase $this->response = new Response(); $this->container['view'] = ViewFactory::create($this->container, $this->request); $this->container['locale'] = new LocaleManager(); - $this->controller = new FrontController($this->container, Config::getInstance('config/config_test.yml')); + + if (PHP_OS == 'WINNT') { + $configFile = 'config_test_windows.yml'; + } else { + $configFile = 'config_test.yml'; + } + $this->config = Config::getInstance('config/'.$configFile); + $this->controller = new FrontController($this->container, $this->config); + $this->container['router']->map(['GET'], '/', [$this->controller, 'index']) ->setName('index'); $this->container['router']->map(['GET'], '/video', [$this->controller, 'video']) @@ -150,6 +165,18 @@ class FrontControllerTest extends TestCase * @return void */ public function testConstructor() + { + $controller = new FrontController($this->container, $this->config); + $this->assertInstanceOf(FrontController::class, $controller); + } + + /** + * Test the constructor with a default config. + * + * @return void + * @requires OS Linux + */ + public function testConstructorWithDefaultConfig() { $controller = new FrontController($this->container); $this->assertInstanceOf(FrontController::class, $controller); @@ -162,7 +189,8 @@ class FrontControllerTest extends TestCase */ public function testConstructorWithStream() { - $controller = new FrontController($this->container, new Config(['stream' => true])); + $this->config->stream = true; + $controller = new FrontController($this->container, $this->config); $this->assertInstanceOf(FrontController::class, $controller); } @@ -302,12 +330,12 @@ class FrontControllerTest extends TestCase */ public function testVideoWithStream() { - $config = new Config(['stream' => true]); - $this->assertRequestIsOk('video', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU'], $config); + $this->config->stream = true; + $this->assertRequestIsOk('video', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU'], $this->config); $this->assertRequestIsOk( 'video', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'audio' => true], - $config + $this->config ); } @@ -375,10 +403,11 @@ class FrontControllerTest extends TestCase */ public function testRedirectWithStream() { + $this->config->stream = true; $this->assertRequestIsOk( 'redirect', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU'], - new Config(['stream' => true]) + $this->config ); } @@ -389,10 +418,11 @@ class FrontControllerTest extends TestCase */ public function testRedirectWithM3uStream() { + $this->config->stream = true; $this->assertRequestIsOk( 'redirect', ['url' => 'https://twitter.com/verge/status/813055465324056576/video/1'], - new Config(['stream' => true]) + $this->config ); } @@ -403,10 +433,11 @@ class FrontControllerTest extends TestCase */ public function testRedirectWithRtmpStream() { + $this->config->stream = true; $this->assertRequestIsOk( 'redirect', ['url' => 'http://www.canalc2.tv/video/12163', 'format' => 'rtmp'], - new Config(['stream' => true]) + $this->config ); } @@ -417,13 +448,14 @@ class FrontControllerTest extends TestCase */ public function testRedirectWithRemux() { + $this->config->remux = true; $this->assertRequestIsOk( 'redirect', [ 'url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'format' => 'bestvideo+bestaudio', ], - new Config(['remux' => true]) + $this->config ); } @@ -481,13 +513,15 @@ class FrontControllerTest extends TestCase * Test the redirect() function with a playlist stream. * * @return void + * @requires OS Linux */ public function testRedirectWithPlaylist() { + $this->config->stream = true; $this->assertRequestIsOk( 'redirect', ['url' => 'https://www.youtube.com/playlist?list=PLgdySZU6KUXL_8Jq5aUkyNV7wCa-4wZsC'], - new Config(['stream' => true]) + $this->config ); } diff --git a/tests/PlaylistArchiveStreamTest.php b/tests/PlaylistArchiveStreamTest.php index 2e3e4a7..35ac628 100644 --- a/tests/PlaylistArchiveStreamTest.php +++ b/tests/PlaylistArchiveStreamTest.php @@ -5,6 +5,7 @@ namespace Alltube\Test; +use Alltube\Config; use Alltube\PlaylistArchiveStream; use PHPUnit\Framework\TestCase; @@ -25,7 +26,12 @@ class PlaylistArchiveStreamTest extends TestCase */ protected function setUp() { - $this->stream = new PlaylistArchiveStream(); + if (PHP_OS == 'WINNT') { + $configFile = 'config_test_windows.yml'; + } else { + $configFile = 'config_test.yml'; + } + $this->stream = new PlaylistArchiveStream(Config::getInstance('config/'.$configFile)); } /** diff --git a/tests/VideoDownloadTest.php b/tests/VideoDownloadTest.php index ed5d75c..4cad529 100644 --- a/tests/VideoDownloadTest.php +++ b/tests/VideoDownloadTest.php @@ -21,12 +21,25 @@ class VideoDownloadTest extends TestCase */ private $download; + /** + * Config class instance. + * + * @var Config + */ + private $config; + /** * Initialize properties used by test. */ protected function setUp() { - $this->download = new VideoDownload(Config::getInstance('config/config_test.yml')); + 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); } /** @@ -45,9 +58,8 @@ class VideoDownloadTest extends TestCase */ public function testConstructorWithMissingYoutubedl() { - new VideoDownload( - new Config(['youtubedl' => 'foo']) - ); + $this->config->youtubedl = 'foo'; + new VideoDownload($this->config); } /** @@ -58,9 +70,8 @@ class VideoDownloadTest extends TestCase */ public function testConstructorWithMissingPython() { - new VideoDownload( - new Config(['python' => 'foo']) - ); + $this->config->python = 'foo'; + new VideoDownload($this->config); } /** @@ -364,7 +375,8 @@ class VideoDownloadTest extends TestCase */ public function testGetAudioStreamAvconvError($url, $format) { - $download = new VideoDownload(new Config(['avconv' => 'foobar'])); + $this->config->avconv = 'foobar'; + $download = new VideoDownload($this->config); $download->getAudioStream($url, $format); } @@ -380,7 +392,8 @@ class VideoDownloadTest extends TestCase */ public function testGetAudioStreamRtmpError($url, $format) { - $download = new VideoDownload(new Config(['rtmpdump' => 'foobar'])); + $this->config->rtmpdump = 'foobar'; + $download = new VideoDownload($this->config); $download->getAudioStream($url, $format); } @@ -477,7 +490,8 @@ class VideoDownloadTest extends TestCase */ public function testGetM3uStreamAvconvError($url, $format) { - $download = new VideoDownload(new Config(['avconv' => 'foobar'])); + $this->config->avconv = 'foobar'; + $download = new VideoDownload($this->config); $video = $download->getJSON($url, $format); $download->getM3uStream($video); } @@ -486,6 +500,7 @@ class VideoDownloadTest extends TestCase * Test getPlaylistArchiveStream function without avconv. * * @return void + * @requires OS Linux */ public function testGetPlaylistArchiveStream() {