alltube/tests/FrontControllerTest.php

276 lines
6.8 KiB
PHP
Raw Permalink Normal View History

2017-01-16 16:19:19 +00:00
<?php
2017-01-16 16:19:19 +00:00
/**
* FrontControllerTest class.
*/
namespace Alltube\Test;
2017-01-16 16:31:20 +00:00
use Alltube\Controller\FrontController;
use Alltube\Exception\ConfigException;
2020-10-21 21:07:12 +00:00
use Alltube\Exception\DependencyException;
use Alltube\Library\Exception\AlltubeLibraryException;
use Exception;
use Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidURLException;
2017-01-16 16:19:19 +00:00
use Slim\Http\Environment;
use Slim\Http\Request;
use SmartyException;
2017-01-16 16:19:19 +00:00
/**
* Unit tests for the FrontController class.
*/
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-10-20 21:29:50 +00:00
* @throws ConfigException|SmartyException
2020-10-21 21:07:12 +00:00
* @throws 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
{
parent::setUp();
$this->controller = new FrontController($this->container);
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 = $this->container->get('config');
$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->container->get('response')
2017-01-16 16:19:19 +00:00
);
$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()
{
$this->assertRequestIsClientError('password');
2017-01-16 16:19:19 +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
*/
public function testInfoWithoutUrl()
2017-01-16 16:19:19 +00:00
{
$this->expectException(InvalidURLException::class);
$this->getRequestResult('info', []);
2017-01-16 16:19:19 +00:00
}
/**
* Test the info() function.
2017-01-16 16:31:20 +00:00
*
2017-01-16 16:19:19 +00:00
* @return void
* @requires download
2017-01-16 16:19:19 +00:00
*/
public function testInfo()
2017-01-16 16:19:19 +00:00
{
$this->assertRequestIsOk('info', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU']);
2017-01-16 16:19:19 +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
* @requires download
2017-01-16 16:19:19 +00:00
*/
public function testInfoWithAudio()
2017-01-16 16:19:19 +00:00
{
$config = $this->container->get('config');
$config->setOptions(['convert' => true]);
$this->assertRequestIsRedirect(
'info',
['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'audio' => true]
);
2017-01-16 16:19:19 +00:00
}
/**
* Test the info() function with audio conversion from a Vimeo video.
*
* @return void
* @requires download
*/
public function testInfoWithVimeoAudio()
{
$config = $this->container->get('config');
$config->setOptions(['convert' => true]);
// So we can test the fallback to default format
$this->assertRequestIsRedirect('info', ['url' => 'https://vimeo.com/251997032', 'audio' => true]);
}
2017-01-16 16:19:19 +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
* @requires download
2017-01-16 16:19:19 +00:00
*/
public function testInfoWithUnconvertedAudio()
2017-01-16 16:19:19 +00:00
{
$config = $this->container->get('config');
$config->setOptions(['convert' => true]);
2017-04-25 21:49:13 +00:00
$this->assertRequestIsRedirect(
'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
);
}
/**
* 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
* @requires download
2022-02-27 22:45:59 +00:00
* @throws AlltubeLibraryException|InvalidURLException
2017-01-16 16:19:19 +00:00
*/
public function testInfoWithPassword()
2017-01-16 16:19:19 +00:00
{
$result = $this->controller->info(
2022-02-27 22:44:36 +00:00
$this->container->get('request')->withQueryParams(['url' => 'https://vimeo.com/68375962'])
2017-10-29 22:21:13 +00:00
->withParsedBody(['password' => 'youtube-dl']),
$this->container->get('response')
2017-01-16 16:19:19 +00:00
);
$this->assertTrue($result->isOk());
}
/**
* 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
* @requires download
2017-01-16 16:19:19 +00:00
*/
public function testInfoWithMissingPassword()
2017-01-16 16:19:19 +00:00
{
2022-02-27 22:44:36 +00:00
$this->assertRequestIsClientError('info', ['url' => 'https://vimeo.com/68375962']);
$this->assertRequestIsClientError('info', ['url' => 'https://vimeo.com/68375962', 'audio' => true]);
2017-01-16 16:19:19 +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
* @requires download
2017-01-16 16:19:19 +00:00
*/
public function testInfoWithStream()
2017-01-16 16:19:19 +00:00
{
$config = $this->container->get('config');
$config->setOptions(['stream' => true]);
$this->assertRequestIsOk('info', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU']);
2017-04-25 21:49:13 +00:00
$this->assertRequestIsOk(
'info',
['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 info() function with a playlist.
2017-04-24 23:53:38 +00:00
*
* @return void
* @requires download
2017-04-24 23:53:38 +00:00
*/
public function testInfoWithPlaylist()
2017-04-24 23:53:38 +00:00
{
2017-04-25 21:49:13 +00:00
$this->assertRequestIsOk(
'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()
{
$result = $this->controller->error(
$this->container->get('request'),
$this->container->get('response'),
new Exception('foo')
);
2017-01-16 16:19:19 +00:00
$this->assertTrue($result->isServerError());
}
/**
* Test the locale() function.
*
* @return void
*/
public function testLocale()
{
$this->assertTrue(
$this->controller->locale(
$this->container->get('request'),
$this->container->get('response'),
2017-10-29 22:21:13 +00:00
['locale' => 'fr_FR']
)->isRedirect()
);
}
2017-01-16 16:19:19 +00:00
}