2017-01-16 16:19:19 +00:00
|
|
|
<?php
|
2019-10-03 19:24:12 +00:00
|
|
|
|
2017-01-16 16:19:19 +00:00
|
|
|
/**
|
|
|
|
* FrontControllerTest class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Alltube\Test;
|
|
|
|
|
|
|
|
use Alltube\Config;
|
2017-01-16 16:31:20 +00:00
|
|
|
use Alltube\Controller\FrontController;
|
2020-06-20 23:44:20 +00:00
|
|
|
use Alltube\Exception\ConfigException;
|
2020-07-15 21:23:14 +00:00
|
|
|
use Alltube\Exception\DependencyException;
|
2020-06-20 23:44:20 +00:00
|
|
|
use Alltube\Library\Exception\AlltubeLibraryException;
|
2018-02-05 15:48:58 +00:00
|
|
|
use Exception;
|
2017-01-16 16:19:19 +00:00
|
|
|
use Slim\Http\Environment;
|
|
|
|
use Slim\Http\Request;
|
2020-06-20 23:44:20 +00:00
|
|
|
use SmartyException;
|
2017-01-16 16:19:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for the FrontController class.
|
|
|
|
*/
|
2019-04-22 14:05:58 +00:00
|
|
|
class FrontControllerTest extends ControllerTest
|
2017-01-16 16:19:19 +00:00
|
|
|
{
|
2020-05-13 20:28:05 +00:00
|
|
|
/**
|
|
|
|
* Controller instance used in tests.
|
|
|
|
* @var FrontController
|
|
|
|
*/
|
|
|
|
protected $controller;
|
|
|
|
|
2017-01-16 16:19:19 +00:00
|
|
|
/**
|
|
|
|
* Prepare tests.
|
2020-07-15 21:23:14 +00:00
|
|
|
* @throws ConfigException|SmartyException|DependencyException
|
2017-01-16 16:19:19 +00:00
|
|
|
*/
|
2019-11-30 13:08:18 +00:00
|
|
|
protected function setUp(): void
|
2017-01-16 16:19:19 +00:00
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->controller = new FrontController($this->container);
|
2018-03-20 11:02:21 +00:00
|
|
|
}
|
|
|
|
|
2017-04-24 16:07:59 +00:00
|
|
|
/**
|
|
|
|
* Test the constructor.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testConstructor()
|
2017-11-11 12:58:55 +00:00
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
$this->assertInstanceOf(FrontController::class, new FrontController($this->container));
|
2017-04-24 16:07:59 +00:00
|
|
|
}
|
|
|
|
|
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
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws ConfigException
|
2017-01-16 16:19:19 +00:00
|
|
|
*/
|
|
|
|
public function testConstructorWithStream()
|
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
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()
|
|
|
|
{
|
2020-06-20 23:44:20 +00:00
|
|
|
$this->assertRequestIsClientError('password');
|
2017-01-16 16:19:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-22 13:20:05 +00:00
|
|
|
* Test the info() function without the url parameter.
|
2017-01-16 16:31:20 +00:00
|
|
|
*
|
2017-01-16 16:19:19 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2019-04-22 13:20:05 +00:00
|
|
|
public function testInfoWithoutUrl()
|
2017-01-16 16:19:19 +00:00
|
|
|
{
|
2019-04-22 13:20:05 +00:00
|
|
|
$this->assertRequestIsRedirect('info');
|
2017-01-16 16:19:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-22 13:20:05 +00:00
|
|
|
* Test the info() function.
|
2017-01-16 16:31:20 +00:00
|
|
|
*
|
2017-01-16 16:19:19 +00:00
|
|
|
* @return void
|
2019-10-26 14:13:08 +00:00
|
|
|
* @requires download
|
2017-01-16 16:19:19 +00:00
|
|
|
*/
|
2019-04-22 13:20:05 +00:00
|
|
|
public function testInfo()
|
2017-01-16 16:19:19 +00:00
|
|
|
{
|
2019-04-22 13:20:05 +00:00
|
|
|
$this->assertRequestIsOk('info', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU']);
|
2017-01-16 16:19:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-22 13:20:05 +00:00
|
|
|
* Test the info() function with audio conversion.
|
2017-01-16 16:31:20 +00:00
|
|
|
*
|
2017-01-16 16:19:19 +00:00
|
|
|
* @return void
|
2019-10-26 14:13:08 +00:00
|
|
|
* @requires download
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws ConfigException
|
2017-01-16 16:19:19 +00:00
|
|
|
*/
|
2019-04-22 13:20:05 +00:00
|
|
|
public function testInfoWithAudio()
|
2017-01-16 16:19:19 +00:00
|
|
|
{
|
2019-04-21 21:26:11 +00:00
|
|
|
Config::setOptions(['convert' => true]);
|
|
|
|
|
2019-04-22 14:05:58 +00:00
|
|
|
$this->assertRequestIsRedirect(
|
|
|
|
'info',
|
|
|
|
['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'audio' => true]
|
|
|
|
);
|
2017-01-16 16:19:19 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 14:43:22 +00:00
|
|
|
/**
|
2019-04-22 13:20:05 +00:00
|
|
|
* Test the info() function with audio conversion from a Vimeo video.
|
2018-05-01 14:43:22 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2019-10-26 14:13:08 +00:00
|
|
|
* @requires download
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws ConfigException
|
2018-05-01 14:43:22 +00:00
|
|
|
*/
|
2019-04-22 13:20:05 +00:00
|
|
|
public function testInfoWithVimeoAudio()
|
2018-05-01 14:43:22 +00:00
|
|
|
{
|
2019-04-21 21:26:11 +00:00
|
|
|
Config::setOptions(['convert' => true]);
|
|
|
|
|
2018-05-01 14:43:22 +00:00
|
|
|
// So we can test the fallback to default format
|
2019-04-22 13:20:05 +00:00
|
|
|
$this->assertRequestIsRedirect('info', ['url' => 'https://vimeo.com/251997032', 'audio' => true]);
|
2018-05-01 14:43:22 +00:00
|
|
|
}
|
|
|
|
|
2017-01-16 16:19:19 +00:00
|
|
|
/**
|
2019-04-22 13:20:05 +00:00
|
|
|
* Test the info() function with audio enabled and an URL that doesn't need to be converted.
|
2017-01-16 16:31:20 +00:00
|
|
|
*
|
2017-01-16 16:19:19 +00:00
|
|
|
* @return void
|
2019-10-26 14:13:08 +00:00
|
|
|
* @requires download
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws ConfigException
|
2017-01-16 16:19:19 +00:00
|
|
|
*/
|
2019-04-22 13:20:05 +00:00
|
|
|
public function testInfoWithUnconvertedAudio()
|
2017-01-16 16:19:19 +00:00
|
|
|
{
|
2019-04-21 21:26:11 +00:00
|
|
|
Config::setOptions(['convert' => true]);
|
|
|
|
|
2017-04-25 21:49:13 +00:00
|
|
|
$this->assertRequestIsRedirect(
|
2019-04-22 13:20:05 +00:00
|
|
|
'info',
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-22 13:20:05 +00:00
|
|
|
* Test the info() function with a password.
|
2017-01-16 16:31:20 +00:00
|
|
|
*
|
2017-01-16 16:19:19 +00:00
|
|
|
* @return void
|
2019-10-26 14:13:08 +00:00
|
|
|
* @requires download
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws AlltubeLibraryException
|
2017-01-16 16:19:19 +00:00
|
|
|
*/
|
2019-04-22 13:20:05 +00:00
|
|
|
public function testInfoWithPassword()
|
2017-01-16 16:19:19 +00:00
|
|
|
{
|
2019-04-22 13:20:05 +00:00
|
|
|
$result = $this->controller->info(
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-22 13:20:05 +00:00
|
|
|
* Test the info() function with a missing password.
|
2017-01-16 16:31:20 +00:00
|
|
|
*
|
2017-01-16 16:19:19 +00:00
|
|
|
* @return void
|
2019-10-26 14:13:08 +00:00
|
|
|
* @requires download
|
2017-01-16 16:19:19 +00:00
|
|
|
*/
|
2019-04-22 13:20:05 +00:00
|
|
|
public function testInfoWithMissingPassword()
|
2017-01-16 16:19:19 +00:00
|
|
|
{
|
2020-06-20 23:44:20 +00:00
|
|
|
$this->assertRequestIsClientError('info', ['url' => 'http://vimeo.com/68375962']);
|
|
|
|
$this->assertRequestIsClientError('info', ['url' => 'http://vimeo.com/68375962', 'audio' => true]);
|
2017-01-16 16:19:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-22 13:20:05 +00:00
|
|
|
* Test the info() function with streams enabled.
|
2017-01-16 16:31:20 +00:00
|
|
|
*
|
2017-01-16 16:19:19 +00:00
|
|
|
* @return void
|
2019-10-26 14:13:08 +00:00
|
|
|
* @requires download
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws ConfigException
|
2017-01-16 16:19:19 +00:00
|
|
|
*/
|
2019-04-22 13:20:05 +00:00
|
|
|
public function testInfoWithStream()
|
2017-01-16 16:19:19 +00:00
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
Config::setOptions(['stream' => true]);
|
|
|
|
|
2019-04-22 13:20:05 +00:00
|
|
|
$this->assertRequestIsOk('info', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU']);
|
2017-04-25 21:49:13 +00:00
|
|
|
$this->assertRequestIsOk(
|
2019-04-22 13:20:05 +00:00
|
|
|
'info',
|
2019-04-21 16:30:02 +00:00
|
|
|
['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
|
|
|
/**
|
2019-04-22 13:20:05 +00:00
|
|
|
* Test the info() function with a playlist.
|
2017-04-24 23:53:38 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2019-10-26 14:13:08 +00:00
|
|
|
* @requires download
|
2017-04-24 23:53:38 +00:00
|
|
|
*/
|
2019-04-22 13:20:05 +00:00
|
|
|
public function testInfoWithPlaylist()
|
2017-04-24 23:53:38 +00:00
|
|
|
{
|
2017-04-25 21:49:13 +00:00
|
|
|
$this->assertRequestIsOk(
|
2019-04-22 13:20:05 +00:00
|
|
|
'info',
|
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()
|
|
|
|
{
|
2018-02-05 15:48:58 +00:00
|
|
|
$result = $this->controller->error($this->request, $this->response, new Exception('foo'));
|
2017-01-16 16:19:19 +00:00
|
|
|
$this->assertTrue($result->isServerError());
|
|
|
|
}
|
|
|
|
|
2017-05-30 22:48:50 +00:00
|
|
|
/**
|
|
|
|
* 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']
|
2017-05-30 22:48:50 +00:00
|
|
|
)->isRedirect()
|
|
|
|
);
|
|
|
|
}
|
2017-01-16 16:19:19 +00:00
|
|
|
}
|