alltube/tests/FrontControllerTest.php

371 lines
10 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-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 \PHPUnit_Framework_TestCase
{
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;
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'] = 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);
$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();
}
/**
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 = Config::getInstance();
$config->stream = true;
$controller = new FrontController($this->container);
$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()
{
$result = $this->controller->index($this->request, $this->response);
$this->assertTrue($result->isOk());
}
/**
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(
Environment::mock(['REQUEST_URI'=>'/foo', 'QUERY_STRING'=>'foo=bar'])
),
$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()
{
$result = $this->controller->extractors($this->request, $this->response);
$this->assertTrue($result->isOk());
}
/**
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()
{
$result = $this->controller->password($this->request, $this->response);
$this->assertTrue($result->isOk());
}
/**
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()
{
$result = $this->controller->video($this->request, $this->response);
$this->assertTrue($result->isRedirect());
}
/**
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()
{
$result = $this->controller->video(
$this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU']),
$this->response
);
$this->assertTrue($result->isOk());
}
/**
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()
{
$result = $this->controller->video(
$this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'audio'=>true]),
$this->response
);
$this->assertTrue($result->isOk());
}
/**
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()
{
$result = $this->controller->video(
$this->request->withQueryParams(
['url' => 'https://2080.bandcamp.com/track/cygnus-x-the-orange-theme-2080-faulty-chip-cover',
2017-01-16 16:31:20 +00:00
'audio'=> true, ]
2017-01-16 16:19:19 +00:00
),
$this->response
);
$this->assertTrue($result->isRedirect());
}
/**
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(
$this->request->withQueryParams(['url'=>'http://vimeo.com/68375962'])
->withParsedBody(['password'=>'youtube-dl']),
$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()
{
$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());
}
/**
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 = Config::getInstance();
$config->stream = true;
$result = $this->controller->video(
$this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU']),
$this->response
);
$this->assertTrue($result->isOk());
$result = $this->controller->video(
$this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'audio'=>true]),
$this->response
);
$this->assertTrue($result->isOk());
}
/**
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'));
$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()
{
$result = $this->controller->redirect($this->request, $this->response);
$this->assertTrue($result->isRedirect());
}
/**
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()
{
$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.
2017-03-19 11:28:54 +00:00
*
* @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());
}
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 = Config::getInstance();
$config->stream = true;
$result = $this->controller->redirect(
$this->request->withQueryParams(['url'=>'https://www.youtube.com/watch?v=M7IpKCZ47pU']),
$this->response
);
$this->assertTrue($result->isOk());
}
/**
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()
{
$config = Config::getInstance();
$config->stream = true;
//We need to create a new controller instance in order to apply the custom config
$controller = new FrontController($this->container);
$result = $controller->redirect(
2017-01-16 16:19:19 +00:00
$this->request->withQueryParams(['url'=>'https://twitter.com/verge/status/813055465324056576/video/1']),
$this->response
);
$this->assertTrue($result->isOk());
}
/**
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()
{
$result = $this->controller->redirect(
$this->request->withQueryParams(['url'=>'http://vimeo.com/68375962']),
$this->response
);
$this->assertTrue($result->isRedirect());
}
/**
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()
{
$result = $this->controller->redirect(
$this->request->withQueryParams(['url'=>'http://example.com/foo']),
$this->response
);
$this->assertTrue($result->isServerError());
}
}