alltube/tests/FrontControllerTest.php

593 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-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.
*/
class FrontControllerTest extends BaseTest
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()
{
parent::setUp();
2017-01-16 16:19:19 +00:00
$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();
$this->controller = new FrontController($this->container);
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
}
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
*
* @return Response HTTP response
*/
private function getRequestResult($request, array $params)
2017-04-25 21:49:13 +00:00
{
return $this->controller->$request(
2017-04-25 21:49:13 +00:00
$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
*
* @return void
*/
private function assertRequestIsOk($request, array $params = [])
2017-04-25 21:49:13 +00:00
{
$this->assertTrue($this->getRequestResult($request, $params)->isOk());
2017-04-25 21:49:13 +00:00
}
/**
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
*
* @return void
*/
private function assertRequestIsRedirect($request, array $params = [])
2017-04-25 21:49:13 +00:00
{
$this->assertTrue($this->getRequestResult($request, $params)->isRedirect());
2017-04-25 21:49:13 +00:00
}
/**
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
*
* @return void
*/
private function assertRequestIsServerError($request, array $params = [])
2017-04-25 21:49:13 +00:00
{
$this->assertTrue($this->getRequestResult($request, $params)->isServerError());
2017-04-25 21:49:13 +00:00
}
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
*
* @return void
*/
private function assertRequestIsClientError($request, array $params = [])
2018-03-20 11:02:21 +00:00
{
$this->assertTrue($this->getRequestResult($request, $params)->isClientError());
2018-03-20 11:02:21 +00:00
}
/**
* Test the constructor.
*
* @return void
*/
public function testConstructor()
{
$this->assertInstanceOf(FrontController::class, new FrontController($this->container));
}
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()
{
Config::setOptions(['stream' => true]);
$this->assertInstanceOf(FrontController::class, new FrontController($this->container));
2017-01-16 16:19:19 +00:00
}
/**
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
}
/**
* Test the video() function with audio conversion from a Vimeo video.
*
* @return void
*/
public function testVideoWithVimeoAudio()
{
if (getenv('CI')) {
$this->markTestSkipped('Travis is blacklisted by Vimeo.');
}
// So we can test the fallback to default format
$this->assertRequestIsOk('video', ['url' => 'https://vimeo.com/251997032', '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()
{
if (getenv('CI')) {
$this->markTestSkipped('Travis is blacklisted by Vimeo.');
}
2017-01-16 16:19:19 +00:00
$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()
{
if (getenv('CI')) {
$this->markTestSkipped('Travis is blacklisted by Vimeo.');
}
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()
{
Config::setOptions(['stream' => true]);
$this->assertRequestIsOk('video', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU']);
2017-04-25 21:49: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-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()
{
Config::setOptions(['stream' => true]);
2017-04-25 21:49:13 +00:00
$this->assertRequestIsOk(
'redirect',
['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 redirect() function with an M3U stream.
*
2017-01-16 16:19:19 +00:00
* @return void
*/
public function testRedirectWithM3uStream()
{
if (getenv('CI')) {
$this->markTestSkipped('Twitter returns a 429 error when the test is ran too many times.');
}
Config::setOptions(['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',
]
2017-01-16 16:19:19 +00:00
);
}
/**
* Test the redirect() function with an RTMP stream.
*
* @return void
*/
public function testRedirectWithRtmpStream()
{
$this->markTestIncomplete('We need to find another RTMP video.');
Config::setOptions(['stream' => true]);
2017-04-25 21:49:13 +00:00
$this->assertRequestIsOk(
'redirect',
['url' => 'http://www.rtvnh.nl/video/131946', 'format' => 'rtmp-264']
);
}
2017-04-24 22:40:24 +00:00
/**
* Test the redirect() function with a remuxed video.
*
* @return void
*/
public function testRedirectWithRemux()
{
Config::setOptions(['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-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()
{
if (getenv('CI')) {
$this->markTestSkipped('Travis is blacklisted by Vimeo.');
}
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()
{
Config::setOptions(['stream' => true]);
2017-05-02 15:04:55 +00:00
$this->assertRequestIsOk(
'redirect',
['url' => 'https://www.youtube.com/playlist?list=PLgdySZU6KUXL_8Jq5aUkyNV7wCa-4wZsC']
2017-05-02 15:04:55 +00:00
);
}
/**
* Test the redirect() function with an advanced conversion.
*
* @return void
*/
public function testRedirectWithAdvancedConversion()
{
Config::setOptions(['convertAdvanced' => true]);
$this->assertRequestIsOk(
'redirect',
[
'url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU',
'format' => 'best',
'customConvert' => 'on',
'customBitrate' => 32,
'customFormat' => 'flv',
]
);
}
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
}