alltube/tests/FrontControllerTest.php

602 lines
15 KiB
PHP
Raw Normal View History

2017-01-16 16:19:19 +00:00
<?php
/**
* FrontControllerTest class.
*/
namespace Alltube\Test;
use Alltube\Config;
2017-01-16 16:31:20 +00:00
use Alltube\Controller\FrontController;
2017-05-30 22:07:34 +00:00
use Alltube\LocaleManager;
use Alltube\ViewFactory;
use Exception;
2017-10-26 08:46:22 +00:00
use PHPUnit\Framework\TestCase;
2017-01-16 16:19:19 +00:00
use Slim\Container;
use Slim\Http\Environment;
use Slim\Http\Request;
use Slim\Http\Response;
/**
* Unit tests for the FrontController class.
*/
2017-10-26 08:46:22 +00:00
class FrontControllerTest extends TestCase
2017-01-16 16:19:19 +00:00
{
2017-01-16 17:47:49 +00:00
/**
2017-01-16 17:54:03 +00:00
* Slim dependency container.
*
2017-01-16 17:47:49 +00:00
* @var Container
*/
private $container;
/**
2017-01-16 17:54:03 +00:00
* Mock HTTP request.
*
2017-01-16 17:47:49 +00:00
* @var Request
*/
private $request;
/**
2017-01-16 17:54:03 +00:00
* Mock HTTP response.
*
2017-01-16 17:47:49 +00:00
* @var Response
*/
private $response;
/**
2017-01-16 17:54:03 +00:00
* FrontController instance used in tests.
*
2017-01-16 17:47:49 +00:00
* @var FrontController
*/
private $controller;
/**
* Config class instance.
*
* @var Config
*/
private $config;
2017-01-16 16:19:19 +00:00
/**
* Prepare tests.
*/
protected function setUp()
{
$this->container = new Container();
$this->request = Request::createFromEnvironment(Environment::mock());
$this->response = new Response();
$this->container['view'] = ViewFactory::create($this->container, $this->request);
2017-05-30 22:07:34 +00:00
$this->container['locale'] = new LocaleManager();
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);
2017-01-16 16:19:19 +00:00
$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');
2017-05-30 22:07:34 +00:00
$this->container['router']->map(['GET'], '/locale', [$this->controller, 'locale'])
->setName('locale');
2017-01-16 16:19:19 +00:00
}
/**
* Destroy properties after test.
*/
protected function tearDown()
{
Config::destroyInstance();
}
2017-04-25 21:49:13 +00:00
/**
2017-04-25 22:10:00 +00:00
* Run controller function with custom query parameters and return the result.
2017-04-25 21:49:13 +00:00
*
* @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 {
2017-04-25 22:10:00 +00:00
$controller = $this->controller;
2017-04-25 21:49:13 +00:00
}
2017-04-25 22:10:00 +00:00
2017-04-25 21:49:13 +00:00
return $controller->$request(
$this->request->withQueryParams($params),
$this->response
);
}
/**
2017-04-25 22:10:00 +00:00
* Assert that calling controller function with these parameters returns a 200 HTTP response.
2017-04-25 21:49:13 +00:00
*
* @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());
}
/**
2017-04-25 22:10:00 +00:00
* Assert that calling controller function with these parameters returns an HTTP redirect.
2017-04-25 21:49:13 +00:00
*
* @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());
}
/**
2018-03-20 11:02:21 +00:00
* Assert that calling controller function with these parameters returns an HTTP 500 error.
2017-04-25 21:49:13 +00:00
*
* @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());
}
2018-03-20 11:02:21 +00:00
/**
* Assert that calling controller function with these parameters returns an HTTP 400 error.
*
* @param string $request Controller function to call
* @param array $params Query parameters
* @param Config $config Custom config
*
* @return void
*/
private function assertRequestIsClientError($request, array $params = [], Config $config = null)
{
$this->assertTrue($this->getRequestResult($request, $params, $config)->isClientError());
}
/**
* Test the constructor.
*
* @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);
}
2017-01-16 16:19:19 +00:00
/**
2017-01-16 16:31:20 +00:00
* Test the constructor with streams enabled.
*
2017-01-16 16:19:19 +00:00
* @return void
*/
public function testConstructorWithStream()
{
$this->config->stream = true;
$controller = new FrontController($this->container, $this->config);
2017-01-16 16:19:19 +00:00
$this->assertInstanceOf(FrontController::class, $controller);
}
/**
2017-01-16 16:31:20 +00:00
* Test the index() function.
*
2017-01-16 16:19:19 +00:00
* @return void
*/
public function testIndex()
{
2017-04-25 21:49:13 +00:00
$this->assertRequestIsOk('index');
2017-01-16 16:19:19 +00:00
}
/**
2017-01-16 16:31:20 +00:00
* Test the index() function with a custom URI.
*
2017-01-16 16:19:19 +00:00
* @return void
*/
public function testIndexWithCustomUri()
{
$result = $this->controller->index(
Request::createFromEnvironment(
2017-10-29 22:21:13 +00:00
Environment::mock(['REQUEST_URI' => '/foo', 'QUERY_STRING' => 'foo=bar'])
2017-01-16 16:19:19 +00:00
),
$this->response
);
$this->assertTrue($result->isOk());
}
/**
2017-01-16 16:31:20 +00:00
* Test the extractors() function.
*
2017-01-16 16:19:19 +00:00
* @return void
*/
public function testExtractors()
{
2017-04-25 21:49:13 +00:00
$this->assertRequestIsOk('extractors');
2017-01-16 16:19:19 +00:00
}
/**
2017-01-16 16:31:20 +00:00
* Test the password() function.
*
2017-01-16 16:19:19 +00:00
* @return void
*/
public function testPassword()
{
2017-04-25 21:49:13 +00:00
$this->assertRequestIsOk('password');
2017-01-16 16:19:19 +00:00
}
/**
2017-01-16 16:31:20 +00:00
* Test the video() function without the url parameter.
*
2017-01-16 16:19:19 +00:00
* @return void
*/
public function testVideoWithoutUrl()
{
2017-04-25 21:49:13 +00:00
$this->assertRequestIsRedirect('video');
2017-01-16 16:19:19 +00:00
}
/**
2017-01-16 16:31:20 +00:00
* Test the video() function.
*
2017-01-16 16:19:19 +00:00
* @return void
*/
public function testVideo()
{
2017-10-29 22:21:13 +00:00
$this->assertRequestIsOk('video', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU']);
2017-01-16 16:19:19 +00:00
}
/**
2017-01-16 16:31:20 +00:00
* Test the video() function with audio conversion.
*
2017-01-16 16:19:19 +00:00
* @return void
*/
public function testVideoWithAudio()
{
2017-10-29 22:21:13 +00:00
$this->assertRequestIsOk('video', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'audio' => true]);
2017-01-16 16:19:19 +00:00
}
/**
2017-01-16 16:31:20 +00:00
* Test the video() function with audio enabled and an URL that doesn't need to be converted.
*
2017-01-16 16:19:19 +00:00
* @return void
*/
public function testVideoWithUnconvertedAudio()
{
2017-04-25 21:49:13 +00:00
$this->assertRequestIsRedirect(
'video',
2017-11-10 11:19:04 +00:00
[
'url' => 'https://2080.bandcamp.com/track/cygnus-x-the-orange-theme-2080-faulty-chip-cover',
'audio' => true,
]
2017-01-16 16:19:19 +00:00
);
}
/**
2017-01-16 16:31:20 +00:00
* Test the video() function with a password.
*
2017-01-16 16:19:19 +00:00
* @return void
*/
public function testVideoWithPassword()
{
$result = $this->controller->video(
2017-10-29 22:21:13 +00:00
$this->request->withQueryParams(['url' => 'http://vimeo.com/68375962'])
->withParsedBody(['password' => 'youtube-dl']),
2017-01-16 16:19:19 +00:00
$this->response
);
$this->assertTrue($result->isOk());
}
/**
2017-01-16 16:31:20 +00:00
* Test the video() function with a missing password.
*
2017-01-16 16:19:19 +00:00
* @return void
*/
public function testVideoWithMissingPassword()
{
2017-10-29 22:21:13 +00:00
$this->assertRequestIsOk('video', ['url' => 'http://vimeo.com/68375962']);
$this->assertRequestIsOk('video', ['url' => 'http://vimeo.com/68375962', 'audio' => true]);
2017-01-16 16:19:19 +00:00
}
/**
2017-01-16 16:31:20 +00:00
* Test the video() function with streams enabled.
*
2017-01-16 16:19:19 +00:00
* @return void
*/
public function testVideoWithStream()
{
$this->config->stream = true;
$this->assertRequestIsOk('video', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU'], $this->config);
2017-04-25 21:49:13 +00:00
$this->assertRequestIsOk(
'video',
2017-10-29 22:21:13 +00:00
['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'audio' => true],
$this->config
2017-01-16 16:19:19 +00:00
);
}
2017-04-24 23:53:38 +00:00
/**
* Test the video() function with a playlist.
*
* @return void
*/
public function testVideoWithPlaylist()
{
2017-04-25 21:49:13 +00:00
$this->assertRequestIsOk(
'video',
2017-10-29 22:21:13 +00:00
['url' => 'https://www.youtube.com/playlist?list=PLgdySZU6KUXL_8Jq5aUkyNV7wCa-4wZsC']
2017-04-24 23:53:38 +00:00
);
}
2017-01-16 16:19:19 +00:00
/**
2017-01-16 16:31:20 +00:00
* Test the error() function.
*
2017-01-16 16:19:19 +00:00
* @return void
*/
public function testError()
{
$result = $this->controller->error($this->request, $this->response, new Exception('foo'));
2017-01-16 16:19:19 +00:00
$this->assertTrue($result->isServerError());
}
/**
2017-01-16 16:31:20 +00:00
* Test the redirect() function without the URL parameter.
*
2017-01-16 16:19:19 +00:00
* @return void
*/
public function testRedirectWithoutUrl()
{
2017-04-25 21:49:13 +00:00
$this->assertRequestIsRedirect('redirect');
2017-01-16 16:19:19 +00:00
}
/**
2017-01-16 16:31:20 +00:00
* Test the redirect() function.
*
2017-01-16 16:19:19 +00:00
* @return void
*/
public function testRedirect()
{
2017-10-29 22:21:13 +00:00
$this->assertRequestIsRedirect('redirect', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU']);
2017-01-16 16:19:19 +00:00
}
/**
* Test the redirect() function with a specific format.
2017-03-19 11:28:54 +00:00
*
* @return void
*/
public function testRedirectWithFormat()
{
2017-04-25 21:49:13 +00:00
$this->assertRequestIsRedirect(
'redirect',
2017-10-29 22:21:13 +00:00
['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'format' => 'worst']
);
}
2017-01-16 16:19:19 +00:00
/**
2017-01-16 16:31:20 +00:00
* Test the redirect() function with streams enabled.
*
2017-01-16 16:19:19 +00:00
* @return void
*/
public function testRedirectWithStream()
{
$this->config->stream = true;
2017-04-25 21:49:13 +00:00
$this->assertRequestIsOk(
'redirect',
2017-10-29 22:21:13 +00:00
['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU'],
$this->config
2017-01-16 16:19:19 +00:00
);
}
/**
2017-01-16 16:31:20 +00:00
* Test the redirect() function with an M3U stream.
*
2017-01-16 16:19:19 +00:00
* @return void
*/
public function testRedirectWithM3uStream()
{
$this->config->stream = true;
2017-04-25 21:49:13 +00:00
$this->assertRequestIsOk(
'redirect',
[
'url' => 'https://twitter.com/verge/status/813055465324056576/video/1',
'format' => 'hls-2176',
],
$this->config
2017-01-16 16:19:19 +00:00
);
}
/**
* Test the redirect() function with an RTMP stream.
*
* @return void
*/
public function testRedirectWithRtmpStream()
{
$this->config->stream = true;
2017-04-25 21:49:13 +00:00
$this->assertRequestIsOk(
'redirect',
['url' => 'http://www.rtvnh.nl/video/131946', 'format' => 'rtmp-264'],
$this->config
);
}
2017-04-24 22:40:24 +00:00
/**
* Test the redirect() function with a remuxed video.
*
* @return void
*/
public function testRedirectWithRemux()
{
$this->config->remux = true;
2017-04-25 21:49:13 +00:00
$this->assertRequestIsOk(
'redirect',
[
2017-10-29 22:21:13 +00:00
'url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU',
'format' => 'bestvideo+bestaudio',
2017-04-25 21:49:13 +00:00
],
$this->config
2017-04-24 22:40:24 +00:00
);
}
/**
* Test the redirect() function with a remuxed video but remux disabled.
*
* @return void
*/
public function testRedirectWithRemuxDisabled()
{
2017-04-25 21:49:13 +00:00
$this->assertRequestIsServerError(
'redirect',
[
2017-10-29 22:21:13 +00:00
'url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU',
'format' => 'bestvideo+bestaudio',
2017-04-25 21:49:13 +00:00
]
2017-04-24 22:40:24 +00:00
);
}
2017-01-16 16:19:19 +00:00
/**
2017-01-16 16:31:20 +00:00
* Test the redirect() function with a missing password.
*
2017-01-16 16:19:19 +00:00
* @return void
*/
public function testRedirectWithMissingPassword()
{
2017-10-29 22:21:13 +00:00
$this->assertRequestIsRedirect('redirect', ['url' => 'http://vimeo.com/68375962']);
2017-01-16 16:19:19 +00:00
}
/**
2017-01-16 16:31:20 +00:00
* Test the redirect() function with an error.
*
2017-01-16 16:19:19 +00:00
* @return void
*/
public function testRedirectWithError()
{
2017-10-29 22:21:13 +00:00
$this->assertRequestIsServerError('redirect', ['url' => 'http://example.com/foo']);
2017-01-16 16:19:19 +00:00
}
2017-05-02 15:04:55 +00:00
/**
* Test the redirect() function with an video that returns an empty URL.
* This can be caused by trying to redirect to a playlist.
*
* @return void
*/
public function testRedirectWithEmptyUrl()
{
2017-05-19 12:30:59 +00:00
$this->assertRequestIsServerError(
'redirect',
2017-10-29 22:21:13 +00:00
['url' => 'https://www.youtube.com/playlist?list=PLgdySZU6KUXL_8Jq5aUkyNV7wCa-4wZsC']
2017-05-19 12:30:59 +00:00
);
2017-05-02 15:04:55 +00:00
}
/**
* Test the redirect() function with a playlist stream.
*
* @return void
* @requires OS Linux
2017-05-02 15:04:55 +00:00
*/
public function testRedirectWithPlaylist()
{
$this->config->stream = true;
2017-05-02 15:04:55 +00:00
$this->assertRequestIsOk(
'redirect',
2017-10-29 22:21:13 +00:00
['url' => 'https://www.youtube.com/playlist?list=PLgdySZU6KUXL_8Jq5aUkyNV7wCa-4wZsC'],
$this->config
2017-05-02 15:04:55 +00:00
);
}
/**
* Test the redirect() function with an advanced conversion.
*
* @return void
*/
public function testRedirectWithAdvancedConversion()
{
$this->config->convertAdvanced = true;
$this->assertRequestIsOk(
'redirect',
[
'url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU',
'format' => 'best',
'customConvert' => 'on',
'customBitrate' => 32,
'customFormat' => 'flv',
],
$this->config
);
}
2018-03-20 11:02:21 +00:00
/**
* Test the json() function without the URL parameter.
*
* @return void
*/
public function testJsonWithoutUrl()
{
$this->assertRequestIsClientError('json');
}
/**
* Test the json() function.
*
* @return void
*/
public function testJson()
{
$this->assertRequestIsOk('json', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU']);
}
/**
* Test the json() function with an error.
*
* @return void
*/
public function testJsonWithError()
{
$this->assertRequestIsServerError('json', ['url' => 'http://example.com/foo']);
}
/**
* Test the locale() function.
*
* @return void
*/
public function testLocale()
{
$this->assertTrue(
$this->controller->locale(
$this->request,
$this->response,
2017-10-29 22:21:13 +00:00
['locale' => 'fr_FR']
)->isRedirect()
);
}
2017-01-16 16:19:19 +00:00
}