From 16e1f9d6db29ac6e8c3d935f93f49b2466ee752c Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Mon, 16 Jan 2017 17:19:19 +0100 Subject: [PATCH] Test controller --- controllers/FrontController.php | 8 +- phpunit.xml | 3 +- tests/FrontControllerTest.php | 307 ++++++++++++++++++++++++++++++++ tests/bootstrap.php | 11 ++ 4 files changed, 327 insertions(+), 2 deletions(-) create mode 100644 tests/bootstrap.php diff --git a/controllers/FrontController.php b/controllers/FrontController.php index a9d9115..2126211 100644 --- a/controllers/FrontController.php +++ b/controllers/FrontController.php @@ -98,6 +98,7 @@ class FrontController 'canonical' => $this->getCanonicalUrl($request), ] ); + return $response; } /** @@ -122,6 +123,7 @@ class FrontController 'canonical' => $this->getCanonicalUrl($request), ] ); + return $response; } /** @@ -144,6 +146,7 @@ class FrontController 'canonical' => $this->getCanonicalUrl($request), ] ); + return $response; } /** @@ -216,6 +219,7 @@ class FrontController 'canonical' => $this->getCanonicalUrl($request), ] ); + return $response; } /** @@ -343,8 +347,10 @@ class FrontController } catch (\Exception $e) { $response->getBody()->write($e->getMessage()); - return $response->withHeader('Content-Type', 'text/plain'); + return $response->withHeader('Content-Type', 'text/plain')->withStatus(500); } + } else { + return $response->withRedirect($this->container->get('router')->pathFor('index')); } } diff --git a/phpunit.xml b/phpunit.xml index fe96cd9..f37d2a5 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,7 +1,8 @@ - + classes/ + controllers/ diff --git a/tests/FrontControllerTest.php b/tests/FrontControllerTest.php index e69de29..f04d9d5 100644 --- a/tests/FrontControllerTest.php +++ b/tests/FrontControllerTest.php @@ -0,0 +1,307 @@ +container = new Container(); + $this->request = Request::createFromEnvironment(Environment::mock()); + $this->response = new Response(); + $this->container['view'] = function ($c) { + $view = new \Slim\Views\Smarty(__DIR__.'/../templates/'); + + $smartyPlugins = new \Slim\Views\SmartyPlugins($c['router'], $this->request->getUri()); + $view->registerPlugin('function', 'path_for', [$smartyPlugins, 'pathFor']); + $view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']); + + $view->registerPlugin('modifier', 'noscheme', 'Smarty_Modifier_noscheme'); + + return $view; + }; + $this->controller = new FrontController($this->container); + $this->container['router']->map(['GET'], '/', [$this->controller, 'index']) + ->setName('index'); + $this->container['router']->map(['GET'], '/video', [$this->controller, 'video']) + ->setName('video'); + $this->container['router']->map(['GET'], '/extractors', [$this->controller, 'extractors']) + ->setName('extractors'); + $this->container['router']->map(['GET'], '/redirect', [$this->controller, 'redirect']) + ->setName('redirect'); + } + + /** + * Destroy properties after test. + */ + protected function tearDown() + { + Config::destroyInstance(); + } + + /** + * Test the constructor with streams enabled + * @return void + */ + public function testConstructorWithStream() + { + $config = Config::getInstance(); + $config->stream = true; + $controller = new FrontController($this->container); + $this->assertInstanceOf(FrontController::class, $controller); + } + + /** + * Test the index() function + * @return void + */ + public function testIndex() + { + $result = $this->controller->index($this->request, $this->response); + $this->assertTrue($result->isOk()); + } + + /** + * Test the index() function with a custom URI + * @return void + */ + public function testIndexWithCustomUri() + { + $result = $this->controller->index( + Request::createFromEnvironment( + Environment::mock(['REQUEST_URI'=>'/foo', 'QUERY_STRING'=>'foo=bar']) + ), + $this->response + ); + $this->assertTrue($result->isOk()); + } + + /** + * Test the extractors() function + * @return void + */ + public function testExtractors() + { + $result = $this->controller->extractors($this->request, $this->response); + $this->assertTrue($result->isOk()); + } + + /** + * Test the password() function + * @return void + */ + public function testPassword() + { + $result = $this->controller->password($this->request, $this->response); + $this->assertTrue($result->isOk()); + } + + /** + * Test the video() function without the url parameter + * @return void + */ + public function testVideoWithoutUrl() + { + $result = $this->controller->video($this->request, $this->response); + $this->assertTrue($result->isRedirect()); + } + + /** + * Test the video() function + * @return void + */ + public function testVideo() + { + $result = $this->controller->video( + $this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU']), + $this->response + ); + $this->assertTrue($result->isOk()); + } + + /** + * Test the video() function with audio conversion + * @return void + */ + public function testVideoWithAudio() + { + $result = $this->controller->video( + $this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'audio'=>true]), + $this->response + ); + $this->assertTrue($result->isOk()); + } + + /** + * Test the video() function with audio enabled and an URL that doesn't need to be converted + * @return void + */ + public function testVideoWithUnconvertedAudio() + { + $result = $this->controller->video( + $this->request->withQueryParams( + ['url'=>'https://soundcloud.com/verwandlungskuenstler/metamorphosis-by-franz-kafka-1', + 'audio'=>true] + ), + $this->response + ); + $this->assertTrue($result->isRedirect()); + } + + /** + * Test the video() function with a password + * @return void + */ + public function testVideoWithPassword() + { + $result = $this->controller->video( + $this->request->withQueryParams(['url'=>'http://vimeo.com/68375962']) + ->withParsedBody(['password'=>'youtube-dl']), + $this->response + ); + $this->assertTrue($result->isOk()); + } + + /** + * Test the video() function with a missing password + * @return void + */ + public function testVideoWithMissingPassword() + { + $result = $this->controller->video( + $this->request->withQueryParams(['url'=>'http://vimeo.com/68375962']), + $this->response + ); + $this->assertTrue($result->isOk()); + $result = $this->controller->video( + $this->request->withQueryParams(['url'=>'http://vimeo.com/68375962', 'audio'=>true]), + $this->response + ); + $this->assertTrue($result->isOk()); + } + + /** + * Test the video() function with streams enabled + * @return void + */ + public function testVideoWithStream() + { + $config = Config::getInstance(); + $config->stream = true; + $result = $this->controller->video( + $this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU']), + $this->response + ); + $this->assertTrue($result->isOk()); + $result = $this->controller->video( + $this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'audio'=>true]), + $this->response + ); + $this->assertTrue($result->isOk()); + } + + /** + * Test the error() function + * @return void + */ + public function testError() + { + $result = $this->controller->error($this->request, $this->response, new \Exception('foo')); + $this->assertTrue($result->isServerError()); + } + + /** + * Test the redirect() function without the URL parameter + * @return void + */ + public function testRedirectWithoutUrl() + { + $result = $this->controller->redirect($this->request, $this->response); + $this->assertTrue($result->isRedirect()); + } + + /** + * Test the redirect() function + * @return void + */ + public function testRedirect() + { + $result = $this->controller->redirect( + $this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU']), + $this->response + ); + $this->assertTrue($result->isRedirect()); + } + + /** + * Test the redirect() function with streams enabled + * @return void + */ + public function testRedirectWithStream() + { + $config = Config::getInstance(); + $config->stream = true; + $result = $this->controller->redirect( + $this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU']), + $this->response + ); + $this->assertTrue($result->isOk()); + } + + /** + * Test the redirect() function with an M3U stream + * @return void + */ + public function testRedirectWithM3uStream() + { + $config = Config::getInstance(); + $config->stream = true; + $result = $this->controller->redirect( + $this->request->withQueryParams(['url'=>'https://twitter.com/verge/status/813055465324056576/video/1']), + $this->response + ); + $this->assertTrue($result->isOk()); + } + + /** + * Test the redirect() function with a missing password + * @return void + */ + public function testRedirectWithMissingPassword() + { + $result = $this->controller->redirect( + $this->request->withQueryParams(['url'=>'http://vimeo.com/68375962']), + $this->response + ); + $this->assertTrue($result->isRedirect()); + } + + /** + * Test the redirect() function with an error + * @return void + */ + public function testRedirectWithError() + { + $result = $this->controller->redirect( + $this->request->withQueryParams(['url'=>'http://example.com/foo']), + $this->response + ); + $this->assertTrue($result->isServerError()); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..3913383 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,11 @@ +