Refactor FrontControllerTest

This commit is contained in:
Pierre Rudloff 2017-04-25 23:49:13 +02:00
parent 8aee9054a8
commit a5252393de

View file

@ -83,6 +83,70 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
Config::destroyInstance(); 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. * Test the constructor.
* *
@ -112,8 +176,7 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testIndex() public function testIndex()
{ {
$result = $this->controller->index($this->request, $this->response); $this->assertRequestIsOk('index');
$this->assertTrue($result->isOk());
} }
/** /**
@ -139,8 +202,7 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testExtractors() public function testExtractors()
{ {
$result = $this->controller->extractors($this->request, $this->response); $this->assertRequestIsOk('extractors');
$this->assertTrue($result->isOk());
} }
/** /**
@ -150,8 +212,7 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testPassword() public function testPassword()
{ {
$result = $this->controller->password($this->request, $this->response); $this->assertRequestIsOk('password');
$this->assertTrue($result->isOk());
} }
/** /**
@ -161,8 +222,7 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testVideoWithoutUrl() public function testVideoWithoutUrl()
{ {
$result = $this->controller->video($this->request, $this->response); $this->assertRequestIsRedirect('video');
$this->assertTrue($result->isRedirect());
} }
/** /**
@ -172,11 +232,7 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testVideo() public function testVideo()
{ {
$result = $this->controller->video( $this->assertRequestIsOk('video', ['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU']);
$this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU']),
$this->response
);
$this->assertTrue($result->isOk());
} }
/** /**
@ -186,11 +242,7 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testVideoWithoutTitle() public function testVideoWithoutTitle()
{ {
$result = $this->controller->video( $this->assertRequestIsOk('video', ['url'=>'http://html5demos.com/video']);
$this->request->withQueryParams(['url'=>'http://html5demos.com/video']),
$this->response
);
$this->assertTrue($result->isOk());
} }
/** /**
@ -200,11 +252,7 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testVideoWithAudio() public function testVideoWithAudio()
{ {
$result = $this->controller->video( $this->assertRequestIsOk('video', ['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'audio'=>true]);
$this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'audio'=>true]),
$this->response
);
$this->assertTrue($result->isOk());
} }
/** /**
@ -214,14 +262,10 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testVideoWithUnconvertedAudio() public function testVideoWithUnconvertedAudio()
{ {
$result = $this->controller->video( $this->assertRequestIsRedirect(
$this->request->withQueryParams( 'video',
['url' => 'https://2080.bandcamp.com/track/cygnus-x-the-orange-theme-2080-faulty-chip-cover', ['url'=>'https://2080.bandcamp.com/track/cygnus-x-the-orange-theme-2080-faulty-chip-cover', 'audio'=>true]
'audio'=> true, ]
),
$this->response
); );
$this->assertTrue($result->isRedirect());
} }
/** /**
@ -246,16 +290,8 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testVideoWithMissingPassword() public function testVideoWithMissingPassword()
{ {
$result = $this->controller->video( $this->assertRequestIsOk('video', ['url'=>'http://vimeo.com/68375962']);
$this->request->withQueryParams(['url'=>'http://vimeo.com/68375962']), $this->assertRequestIsOk('video', ['url'=>'http://vimeo.com/68375962', 'audio'=>true]);
$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());
} }
/** /**
@ -265,17 +301,13 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testVideoWithStream() public function testVideoWithStream()
{ {
$controller = new FrontController($this->container, new Config(['stream'=>true])); $config = new Config(['stream'=>true]);
$result = $controller->video( $this->assertRequestIsOk('video', ['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU'], $config);
$this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU']), $this->assertRequestIsOk(
$this->response '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() public function testVideoWithPlaylist()
{ {
$result = $this->controller->video( $this->assertRequestIsOk(
$this->request->withQueryParams( 'video',
['url'=>'https://www.youtube.com/playlist?list=PLgdySZU6KUXL_8Jq5aUkyNV7wCa-4wZsC'] ['url'=>'https://www.youtube.com/playlist?list=PLgdySZU6KUXL_8Jq5aUkyNV7wCa-4wZsC']
),
$this->response
); );
$this->assertTrue($result->isOk());
} }
/** /**
@ -312,8 +341,7 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testRedirectWithoutUrl() public function testRedirectWithoutUrl()
{ {
$result = $this->controller->redirect($this->request, $this->response); $this->assertRequestIsRedirect('redirect');
$this->assertTrue($result->isRedirect());
} }
/** /**
@ -323,11 +351,7 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testRedirect() public function testRedirect()
{ {
$result = $this->controller->redirect( $this->assertRequestIsRedirect('redirect', ['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU']);
$this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU']),
$this->response
);
$this->assertTrue($result->isRedirect());
} }
/** /**
@ -337,11 +361,10 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testRedirectWithFormat() public function testRedirectWithFormat()
{ {
$result = $this->controller->redirect( $this->assertRequestIsRedirect(
$this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'format'=>'worst']), 'redirect',
$this->response ['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() public function testRedirectWithStream()
{ {
$controller = new FrontController($this->container, new Config(['stream'=>true])); $this->assertRequestIsOk(
$result = $controller->redirect( 'redirect',
$this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU']), ['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU'],
$this->response new Config(['stream'=>true])
); );
$this->assertTrue($result->isOk());
} }
/** /**
@ -366,12 +388,11 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testRedirectWithM3uStream() public function testRedirectWithM3uStream()
{ {
$controller = new FrontController($this->container, new Config(['stream'=>true])); $this->assertRequestIsOk(
$result = $controller->redirect( 'redirect',
$this->request->withQueryParams(['url'=>'https://twitter.com/verge/status/813055465324056576/video/1']), ['url'=>'https://twitter.com/verge/status/813055465324056576/video/1'],
$this->response new Config(['stream'=>true])
); );
$this->assertTrue($result->isOk());
} }
/** /**
@ -381,14 +402,11 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testRedirectWithRtmpStream() public function testRedirectWithRtmpStream()
{ {
$controller = new FrontController($this->container, new Config(['stream'=>true])); $this->assertRequestIsOk(
$result = $controller->redirect( 'redirect',
$this->request->withQueryParams( ['url'=>'http://www.rtl2.de/sendung/grip-das-motormagazin/folge/folge-203-0'],
['url'=>'http://www.rtl2.de/sendung/grip-das-motormagazin/folge/folge-203-0'] new Config(['stream'=>true])
),
$this->response
); );
$this->assertTrue($result->isOk());
} }
/** /**
@ -398,17 +416,14 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testRedirectWithRemux() public function testRedirectWithRemux()
{ {
$controller = new FrontController($this->container, new Config(['remux'=>true])); $this->assertRequestIsOk(
$result = $controller->redirect( 'redirect',
$this->request->withQueryParams( [
[ 'url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU',
'url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'format'=> 'bestvideo+bestaudio',
'format'=> 'bestvideo+bestaudio', ],
] new Config(['remux'=>true])
),
$this->response
); );
$this->assertTrue($result->isOk());
} }
/** /**
@ -418,16 +433,13 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testRedirectWithRemuxDisabled() public function testRedirectWithRemuxDisabled()
{ {
$result = $this->controller->redirect( $this->assertRequestIsServerError(
$this->request->withQueryParams( 'redirect',
[ [
'url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU',
'format'=> 'bestvideo+bestaudio', 'format'=> 'bestvideo+bestaudio',
] ]
),
$this->response
); );
$this->assertTrue($result->isServerError());
} }
/** /**
@ -437,11 +449,7 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testRedirectWithMissingPassword() public function testRedirectWithMissingPassword()
{ {
$result = $this->controller->redirect( $this->assertRequestIsRedirect('redirect', ['url'=>'http://vimeo.com/68375962']);
$this->request->withQueryParams(['url'=>'http://vimeo.com/68375962']),
$this->response
);
$this->assertTrue($result->isRedirect());
} }
/** /**
@ -451,10 +459,6 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testRedirectWithError() public function testRedirectWithError()
{ {
$result = $this->controller->redirect( $this->assertRequestIsServerError('redirect', ['url'=>'http://example.com/foo']);
$this->request->withQueryParams(['url'=>'http://example.com/foo']),
$this->response
);
$this->assertTrue($result->isServerError());
} }
} }