446 lines
12 KiB
PHP
446 lines
12 KiB
PHP
<?php
|
|
/**
|
|
* FrontControllerTest class.
|
|
*/
|
|
|
|
namespace Alltube\Test;
|
|
|
|
use Alltube\Config;
|
|
use Alltube\Controller\FrontController;
|
|
use Slim\Container;
|
|
use Slim\Http\Environment;
|
|
use Slim\Http\Request;
|
|
use Slim\Http\Response;
|
|
|
|
/**
|
|
* Unit tests for the FrontController class.
|
|
*/
|
|
class FrontControllerTest extends \PHPUnit_Framework_TestCase
|
|
{
|
|
/**
|
|
* Slim dependency container.
|
|
*
|
|
* @var Container
|
|
*/
|
|
private $container;
|
|
|
|
/**
|
|
* Mock HTTP request.
|
|
*
|
|
* @var Request
|
|
*/
|
|
private $request;
|
|
|
|
/**
|
|
* Mock HTTP response.
|
|
*
|
|
* @var Response
|
|
*/
|
|
private $response;
|
|
|
|
/**
|
|
* FrontController instance used in tests.
|
|
*
|
|
* @var FrontController
|
|
*/
|
|
private $controller;
|
|
|
|
/**
|
|
* Prepare tests.
|
|
*/
|
|
protected function setUp()
|
|
{
|
|
$this->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, Config::getInstance('config_test.yml'));
|
|
$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.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testConstructor()
|
|
{
|
|
$controller = new FrontController($this->container);
|
|
$this->assertInstanceOf(FrontController::class, $controller);
|
|
}
|
|
|
|
/**
|
|
* Test the constructor with streams enabled.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testConstructorWithStream()
|
|
{
|
|
$controller = new FrontController($this->container, new Config(['stream'=>true]));
|
|
$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://2080.bandcamp.com/track/cygnus-x-the-orange-theme-2080-faulty-chip-cover',
|
|
'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()
|
|
{
|
|
$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
|
|
);
|
|
$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());
|
|
}
|
|
|
|
/**
|
|
* Test the video() function with a playlist.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testVideoWithPlaylist()
|
|
{
|
|
$result = $this->controller->video(
|
|
$this->request->withQueryParams(
|
|
['url'=>'https://www.youtube.com/playlist?list=PLgdySZU6KUXL_8Jq5aUkyNV7wCa-4wZsC']
|
|
),
|
|
$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 a specific format.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testRedirectWithFormat()
|
|
{
|
|
$result = $this->controller->redirect(
|
|
$this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'format'=>'worst']),
|
|
$this->response
|
|
);
|
|
$this->assertTrue($result->isRedirect());
|
|
}
|
|
|
|
/**
|
|
* Test the redirect() function with streams enabled.
|
|
*
|
|
* @return void
|
|
*/
|
|
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->assertTrue($result->isOk());
|
|
}
|
|
|
|
/**
|
|
* Test the redirect() function with an M3U stream.
|
|
*
|
|
* @return void
|
|
*/
|
|
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->assertTrue($result->isOk());
|
|
}
|
|
|
|
/**
|
|
* Test the redirect() function with an RTMP stream.
|
|
*
|
|
* @return void
|
|
*/
|
|
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->assertTrue($result->isOk());
|
|
}
|
|
|
|
/**
|
|
* Test the redirect() function with a remuxed video.
|
|
*
|
|
* @return void
|
|
*/
|
|
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->assertTrue($result->isOk());
|
|
}
|
|
|
|
/**
|
|
* Test the redirect() function with a remuxed video but remux disabled.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testRedirectWithRemuxDisabled()
|
|
{
|
|
$result = $this->controller->redirect(
|
|
$this->request->withQueryParams(
|
|
[
|
|
'url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU',
|
|
'format'=> 'bestvideo+bestaudio',
|
|
]
|
|
),
|
|
$this->response
|
|
);
|
|
$this->assertTrue($result->isServerError());
|
|
}
|
|
|
|
/**
|
|
* 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());
|
|
}
|
|
}
|