From 0a66dce2b89525c869499d9702c99d617f089e9e Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Wed, 31 May 2017 00:48:50 +0200 Subject: [PATCH] More test coverage Run youtube-dl with --restrict-filenames in order to avoid issues when testing against different locales --- classes/Config.php | 2 +- tests/FrontControllerTest.php | 16 +++++++ tests/LocaleManagerTest.php | 65 ++++++++++++++++++++++++++++ tests/LocaleMiddlewareTest.php | 24 +++-------- tests/LocaleTest.php | 79 ++++++++++++++++++++++++++++++++++ tests/VideoDownloadTest.php | 18 ++++---- tests/ViewFactoryTest.php | 15 +++++++ 7 files changed, 190 insertions(+), 29 deletions(-) create mode 100644 tests/LocaleManagerTest.php create mode 100644 tests/LocaleTest.php diff --git a/classes/Config.php b/classes/Config.php index 306c28e..6226f26 100644 --- a/classes/Config.php +++ b/classes/Config.php @@ -38,7 +38,7 @@ class Config * * @var array */ - public $params = ['--no-warnings', '--ignore-errors', '--flat-playlist']; + public $params = ['--no-warnings', '--ignore-errors', '--flat-playlist', '--restrict-filenames']; /** * Enable audio conversion. diff --git a/tests/FrontControllerTest.php b/tests/FrontControllerTest.php index 84c4660..c61273a 100644 --- a/tests/FrontControllerTest.php +++ b/tests/FrontControllerTest.php @@ -485,4 +485,20 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase new Config(['stream'=>true]) ); } + + /** + * Test the locale() function. + * + * @return void + */ + public function testLocale() + { + $this->assertTrue( + $this->controller->locale( + $this->request, + $this->response, + ['locale'=>'fr_FR'] + )->isRedirect() + ); + } } diff --git a/tests/LocaleManagerTest.php b/tests/LocaleManagerTest.php new file mode 100644 index 0000000..073b9a5 --- /dev/null +++ b/tests/LocaleManagerTest.php @@ -0,0 +1,65 @@ +localeManager = new LocaleManager(); + } + + /** + * Test the getSupportedLocales function. + * + * @return void + */ + public function testGetSupportedLocales() + { + foreach ($this->localeManager->getSupportedLocales() as $locale) { + $this->assertInstanceOf(Locale::class, $locale); + } + } + + /** + * Test the getLocale function. + * + * @return void + */ + public function testGetLocale() + { + $this->assertNull($this->localeManager->getLocale()); + } + + /** + * Test the setLocale function. + * + * @return void + */ + public function testSetLocale() + { + $this->localeManager->setLocale(new Locale('foo_BAR')); + $locale = $this->localeManager->getLocale(); + $this->assertInstanceOf(Locale::class, $locale); + $this->assertEquals('foo_BAR', (string) $locale); + } +} diff --git a/tests/LocaleMiddlewareTest.php b/tests/LocaleMiddlewareTest.php index 89d3b35..9aeb3e2 100644 --- a/tests/LocaleMiddlewareTest.php +++ b/tests/LocaleMiddlewareTest.php @@ -17,13 +17,6 @@ use Slim\Http\Response; */ class LocaleMiddlewareTest extends \PHPUnit_Framework_TestCase { - /** - * Original locale. - * - * @var string - */ - private $origlocale; - /** * LocaleMiddleware instance. * @@ -36,23 +29,11 @@ class LocaleMiddlewareTest extends \PHPUnit_Framework_TestCase */ protected function setUp() { - $this->origlocale = getenv('LANG'); $container = new Container(); $container['locale'] = new LocaleManager(); $this->middleware = new LocaleMiddleware($container); } - /** - * Restore environment after the tests. - * - * @return void - */ - protected function tearDown() - { - putenv('LANG='.$this->origlocale); - setlocale(LC_ALL, $this->origlocale); - } - /** * Test the testLocale() function. * @@ -114,6 +95,11 @@ class LocaleMiddlewareTest extends \PHPUnit_Framework_TestCase ); } + /** + * Test that the environment is correctly set up. + * + * @return void + */ public function testEnv() { $this->markTestIncomplete('We need to find a way to reliably test LC_ALL and LANG values'); diff --git a/tests/LocaleTest.php b/tests/LocaleTest.php new file mode 100644 index 0000000..9581248 --- /dev/null +++ b/tests/LocaleTest.php @@ -0,0 +1,79 @@ +locale = new Locale('fr_FR'); + } + + /** + * Test the __toString function. + * + * @return void + */ + public function testGetToString() + { + $this->assertEquals('fr_FR', $this->locale->__toString()); + } + + /** + * Test the getFullName function. + * + * @return void + */ + public function testGetFullName() + { + $this->assertEquals('français (France)', $this->locale->getFullName()); + } + + /** + * Test the getIso15897 function. + * + * @return void + */ + public function testGetIso15897() + { + $this->assertEquals('fr_FR', $this->locale->getIso15897()); + } + + /** + * Test the getBcp47 function. + * + * @return void + */ + public function testGetBcp47() + { + $this->assertEquals('fr-FR', $this->locale->getBcp47()); + } + + /** + * Test the getIso3166 function. + * + * @return void + */ + public function testGetIso3166() + { + $this->assertEquals('fr', $this->locale->getIso3166()); + } +} diff --git a/tests/VideoDownloadTest.php b/tests/VideoDownloadTest.php index 8167b89..8ad006d 100644 --- a/tests/VideoDownloadTest.php +++ b/tests/VideoDownloadTest.php @@ -151,32 +151,32 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase return [ [ 'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'best[protocol^=http]', - "It's Not Me, It's You - Hearts Under Fire-M7IpKCZ47pU", + "It_s_Not_Me_It_s_You_-_Hearts_Under_Fire-M7IpKCZ47pU", 'mp4', 'googlevideo.com', ], [ 'https://www.youtube.com/watch?v=RJJ6FCAXvKg', 22, - "'Heart Attack' - Demi Lovato ". - '(Sam Tsui & Against The Current)-RJJ6FCAXvKg', + "Heart_Attack_-_Demi_Lovato_". + 'Sam_Tsui_Against_The_Current-RJJ6FCAXvKg', 'mp4', 'googlevideo.com', ], [ 'https://vimeo.com/24195442', 'best[protocol^=http]', - 'Carving the Mountains-24195442', + 'Carving_the_Mountains-24195442', 'mp4', 'vimeocdn.com', ], [ 'http://www.bbc.co.uk/programmes/b039g8p7', 'bestaudio/best', - 'Leonard Cohen, Kaleidoscope - BBC Radio 4-b039d07m', + 'Leonard_Cohen_Kaleidoscope_-_BBC_Radio_4-b039d07m', 'flv', 'bbcodspdns.fcod.llnwd.net', ], [ 'http://www.rtl2.de/sendung/grip-das-motormagazin/folge/folge-203-0', 'bestaudio/best', - 'GRIP sucht den Sommerkönig-folge-203-0', + 'GRIP_sucht_den_Sommerkonig-folge-203-0', 'f4v', 'edgefcs.net', ], @@ -193,7 +193,7 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase return [ [ 'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'bestvideo+bestaudio', - "It's Not Me, It's You - Hearts Under Fire-M7IpKCZ47pU", + "It_s_Not_Me_It_s_You_-_Hearts_Under_Fire-M7IpKCZ47pU", 'mp4', 'googlevideo.com', ], @@ -210,7 +210,7 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase return [ [ 'https://twitter.com/verge/status/813055465324056576/video/1', 'best', - 'The Verge - This tiny origami robot can self-fold and complete tasks-813055465324056576', + 'The_Verge_-_This_tiny_origami_robot_can_self-fold_and_complete_tasks-813055465324056576', 'mp4', 'video.twimg.com', ], @@ -227,7 +227,7 @@ class VideoDownloadTest extends \PHPUnit_Framework_TestCase return [ [ 'http://www.canalc2.tv/video/12163', 'rtmp', - 'Terrasses du Numérique-12163', + 'Terrasses_du_Numerique-12163', 'flv', 'vod-flash.u-strasbg.fr', ], diff --git a/tests/ViewFactoryTest.php b/tests/ViewFactoryTest.php index 9155faa..b173b97 100644 --- a/tests/ViewFactoryTest.php +++ b/tests/ViewFactoryTest.php @@ -7,6 +7,8 @@ namespace Alltube\Test; use Alltube\ViewFactory; use Slim\Container; +use Slim\Http\Environment; +use Slim\Http\Request; use Slim\Views\Smarty; /** @@ -24,4 +26,17 @@ class ViewFactoryTest extends \PHPUnit_Framework_TestCase $view = ViewFactory::create(new Container()); $this->assertInstanceOf(Smarty::class, $view); } + + + /** + * Test the create() function with a X-Forwarded-Proto header. + * + * @return void + */ + public function testCreateWithXForwardedProto() + { + $request = Request::createFromEnvironment(Environment::mock()); + $view = ViewFactory::create(new Container(), $request->withHeader('X-Forwarded-Proto', 'https')); + $this->assertInstanceOf(Smarty::class, $view); + } }