From a5252393deff26e8ed1543e217389177696bf6fa Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Tue, 25 Apr 2017 23:49:13 +0200 Subject: [PATCH] Refactor FrontControllerTest --- tests/FrontControllerTest.php | 230 +++++++++++++++++----------------- 1 file changed, 117 insertions(+), 113 deletions(-) diff --git a/tests/FrontControllerTest.php b/tests/FrontControllerTest.php index 3ba8884..d0f05f6 100644 --- a/tests/FrontControllerTest.php +++ b/tests/FrontControllerTest.php @@ -83,6 +83,70 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase Config::destroyInstance(); } + /** + * Run controller function with custom query parameters and return the result + * + * @param string $request Controller function to call + * @param array $params Query parameters + * @param Config $config Custom config + * + * @return Response HTTP response + */ + private function getRequestResult($request, array $params, Config $config = null) + { + if (isset($config)) { + $controller = new FrontController($this->container, $config); + } else { + $controller= $this->controller; + } + return $controller->$request( + $this->request->withQueryParams($params), + $this->response + ); + } + + /** + * Assert that calling controller function with these parameters returns a 200 HTTP response + * + * @param string $request Controller function to call + * @param array $params Query parameters + * @param Config $config Custom config + * + * @return void + */ + private function assertRequestIsOk($request, array $params = [], Config $config = null) + { + $this->assertTrue($this->getRequestResult($request, $params, $config)->isOk()); + } + + /** + * Assert that calling controller function with these parameters returns an HTTP redirect + * + * @param string $request Controller function to call + * @param array $params Query parameters + * @param Config $config Custom config + * + * @return void + */ + private function assertRequestIsRedirect($request, array $params = [], Config $config = null) + { + $this->assertTrue($this->getRequestResult($request, $params, $config)->isRedirect()); + } + + /** + * Assert that calling controller function with these parameters returns an HTTP redirect + * + * @param string $request Controller function to call + * @param array $params Query parameters + * @param Config $config Custom config + * + * @return void + */ + private function assertRequestIsServerError($request, array $params = [], Config $config = null) + { + $this->assertTrue($this->getRequestResult($request, $params, $config)->isServerError()); + } + /** * Test the constructor. * @@ -112,8 +176,7 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase */ public function testIndex() { - $result = $this->controller->index($this->request, $this->response); - $this->assertTrue($result->isOk()); + $this->assertRequestIsOk('index'); } /** @@ -139,8 +202,7 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase */ public function testExtractors() { - $result = $this->controller->extractors($this->request, $this->response); - $this->assertTrue($result->isOk()); + $this->assertRequestIsOk('extractors'); } /** @@ -150,8 +212,7 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase */ public function testPassword() { - $result = $this->controller->password($this->request, $this->response); - $this->assertTrue($result->isOk()); + $this->assertRequestIsOk('password'); } /** @@ -161,8 +222,7 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase */ public function testVideoWithoutUrl() { - $result = $this->controller->video($this->request, $this->response); - $this->assertTrue($result->isRedirect()); + $this->assertRequestIsRedirect('video'); } /** @@ -172,11 +232,7 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase */ public function testVideo() { - $result = $this->controller->video( - $this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU']), - $this->response - ); - $this->assertTrue($result->isOk()); + $this->assertRequestIsOk('video', ['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU']); } /** @@ -186,11 +242,7 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase */ public function testVideoWithoutTitle() { - $result = $this->controller->video( - $this->request->withQueryParams(['url'=>'http://html5demos.com/video']), - $this->response - ); - $this->assertTrue($result->isOk()); + $this->assertRequestIsOk('video', ['url'=>'http://html5demos.com/video']); } /** @@ -200,11 +252,7 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase */ 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()); + $this->assertRequestIsOk('video', ['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'audio'=>true]); } /** @@ -214,14 +262,10 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase */ public function testVideoWithUnconvertedAudio() { - $result = $this->controller->video( - $this->request->withQueryParams( - ['url' => 'https://2080.bandcamp.com/track/cygnus-x-the-orange-theme-2080-faulty-chip-cover', - 'audio'=> true, ] - ), - $this->response + $this->assertRequestIsRedirect( + 'video', + ['url'=>'https://2080.bandcamp.com/track/cygnus-x-the-orange-theme-2080-faulty-chip-cover', 'audio'=>true] ); - $this->assertTrue($result->isRedirect()); } /** @@ -246,16 +290,8 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase */ 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()); + $this->assertRequestIsOk('video', ['url'=>'http://vimeo.com/68375962']); + $this->assertRequestIsOk('video', ['url'=>'http://vimeo.com/68375962', 'audio'=>true]); } /** @@ -265,17 +301,13 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase */ public function testVideoWithStream() { - $controller = new FrontController($this->container, new Config(['stream'=>true])); - $result = $controller->video( - $this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU']), - $this->response + $config = new Config(['stream'=>true]); + $this->assertRequestIsOk('video', ['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU'], $config); + $this->assertRequestIsOk( + 'video', + ['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'audio'=>true], + $config ); - $this->assertTrue($result->isOk()); - $result = $controller->video( - $this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'audio'=>true]), - $this->response - ); - $this->assertTrue($result->isOk()); } /** @@ -285,13 +317,10 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase */ public function testVideoWithPlaylist() { - $result = $this->controller->video( - $this->request->withQueryParams( - ['url'=>'https://www.youtube.com/playlist?list=PLgdySZU6KUXL_8Jq5aUkyNV7wCa-4wZsC'] - ), - $this->response + $this->assertRequestIsOk( + 'video', + ['url'=>'https://www.youtube.com/playlist?list=PLgdySZU6KUXL_8Jq5aUkyNV7wCa-4wZsC'] ); - $this->assertTrue($result->isOk()); } /** @@ -312,8 +341,7 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase */ public function testRedirectWithoutUrl() { - $result = $this->controller->redirect($this->request, $this->response); - $this->assertTrue($result->isRedirect()); + $this->assertRequestIsRedirect('redirect'); } /** @@ -323,11 +351,7 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase */ public function testRedirect() { - $result = $this->controller->redirect( - $this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU']), - $this->response - ); - $this->assertTrue($result->isRedirect()); + $this->assertRequestIsRedirect('redirect', ['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU']); } /** @@ -337,11 +361,10 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase */ public function testRedirectWithFormat() { - $result = $this->controller->redirect( - $this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'format'=>'worst']), - $this->response + $this->assertRequestIsRedirect( + 'redirect', + ['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'format'=>'worst'] ); - $this->assertTrue($result->isRedirect()); } /** @@ -351,12 +374,11 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase */ public function testRedirectWithStream() { - $controller = new FrontController($this->container, new Config(['stream'=>true])); - $result = $controller->redirect( - $this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU']), - $this->response + $this->assertRequestIsOk( + 'redirect', + ['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU'], + new Config(['stream'=>true]) ); - $this->assertTrue($result->isOk()); } /** @@ -366,12 +388,11 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase */ public function testRedirectWithM3uStream() { - $controller = new FrontController($this->container, new Config(['stream'=>true])); - $result = $controller->redirect( - $this->request->withQueryParams(['url'=>'https://twitter.com/verge/status/813055465324056576/video/1']), - $this->response + $this->assertRequestIsOk( + 'redirect', + ['url'=>'https://twitter.com/verge/status/813055465324056576/video/1'], + new Config(['stream'=>true]) ); - $this->assertTrue($result->isOk()); } /** @@ -381,14 +402,11 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase */ public function testRedirectWithRtmpStream() { - $controller = new FrontController($this->container, new Config(['stream'=>true])); - $result = $controller->redirect( - $this->request->withQueryParams( - ['url'=>'http://www.rtl2.de/sendung/grip-das-motormagazin/folge/folge-203-0'] - ), - $this->response + $this->assertRequestIsOk( + 'redirect', + ['url'=>'http://www.rtl2.de/sendung/grip-das-motormagazin/folge/folge-203-0'], + new Config(['stream'=>true]) ); - $this->assertTrue($result->isOk()); } /** @@ -398,17 +416,14 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase */ public function testRedirectWithRemux() { - $controller = new FrontController($this->container, new Config(['remux'=>true])); - $result = $controller->redirect( - $this->request->withQueryParams( - [ - 'url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU', - 'format'=> 'bestvideo+bestaudio', - ] - ), - $this->response + $this->assertRequestIsOk( + 'redirect', + [ + 'url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU', + 'format'=> 'bestvideo+bestaudio', + ], + new Config(['remux'=>true]) ); - $this->assertTrue($result->isOk()); } /** @@ -418,16 +433,13 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase */ public function testRedirectWithRemuxDisabled() { - $result = $this->controller->redirect( - $this->request->withQueryParams( - [ - 'url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU', - 'format'=> 'bestvideo+bestaudio', - ] - ), - $this->response + $this->assertRequestIsServerError( + 'redirect', + [ + 'url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU', + 'format'=> 'bestvideo+bestaudio', + ] ); - $this->assertTrue($result->isServerError()); } /** @@ -437,11 +449,7 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase */ public function testRedirectWithMissingPassword() { - $result = $this->controller->redirect( - $this->request->withQueryParams(['url'=>'http://vimeo.com/68375962']), - $this->response - ); - $this->assertTrue($result->isRedirect()); + $this->assertRequestIsRedirect('redirect', ['url'=>'http://vimeo.com/68375962']); } /** @@ -451,10 +459,6 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase */ public function testRedirectWithError() { - $result = $this->controller->redirect( - $this->request->withQueryParams(['url'=>'http://example.com/foo']), - $this->response - ); - $this->assertTrue($result->isServerError()); + $this->assertRequestIsServerError('redirect', ['url'=>'http://example.com/foo']); } }