alltube/tests/FrontControllerTest.php

276 lines
6.8 KiB
PHP
Raw Normal View History

2017-01-16 17:19:19 +01:00
<?php
2017-01-16 17:19:19 +01:00
/**
* FrontControllerTest class.
*/
namespace Alltube\Test;
2017-01-16 17:31:20 +01:00
use Alltube\Controller\FrontController;
use Alltube\Exception\ConfigException;
2020-10-21 23:07:12 +02:00
use Alltube\Exception\DependencyException;
use Alltube\Library\Exception\AlltubeLibraryException;
use Exception;
use Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidURLException;
2017-01-16 17:19:19 +01:00
use Slim\Http\Environment;
use Slim\Http\Request;
use SmartyException;
2017-01-16 17:19:19 +01:00
/**
* Unit tests for the FrontController class.
*/
class FrontControllerTest extends ControllerTest
2017-01-16 17:19:19 +01:00
{
2020-05-13 22:28:05 +02:00
/**
* Controller instance used in tests.
* @var FrontController
*/
protected $controller;
2017-01-16 17:19:19 +01:00
/**
* Prepare tests.
2020-10-20 23:29:50 +02:00
* @throws ConfigException|SmartyException
2020-10-21 23:07:12 +02:00
* @throws DependencyException
2017-01-16 17:19:19 +01:00
*/
2019-11-30 14:08:18 +01:00
protected function setUp(): void
2017-01-16 17:19:19 +01:00
{
parent::setUp();
$this->controller = new FrontController($this->container);
2018-03-20 12:02:21 +01:00
}
/**
* Test the constructor.
*
* @return void
*/
public function testConstructor()
{
$this->assertInstanceOf(FrontController::class, new FrontController($this->container));
}
2017-01-16 17:19:19 +01:00
/**
2017-01-16 17:31:20 +01:00
* Test the constructor with streams enabled.
*
2017-01-16 17:19:19 +01: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 17:19:19 +01:00
}
/**
2017-01-16 17:31:20 +01:00
* Test the index() function.
*
2017-01-16 17:19:19 +01:00
* @return void
*/
public function testIndex()
{
2017-04-25 23:49:13 +02:00
$this->assertRequestIsOk('index');
2017-01-16 17:19:19 +01:00
}
/**
2017-01-16 17:31:20 +01:00
* Test the index() function with a custom URI.
*
2017-01-16 17:19:19 +01:00
* @return void
*/
public function testIndexWithCustomUri()
{
$result = $this->controller->index(
Request::createFromEnvironment(
2017-10-29 23:21:13 +01:00
Environment::mock(['REQUEST_URI' => '/foo', 'QUERY_STRING' => 'foo=bar'])
2017-01-16 17:19:19 +01:00
),
$this->container->get('response')
2017-01-16 17:19:19 +01:00
);
$this->assertTrue($result->isOk());
}
/**
2017-01-16 17:31:20 +01:00
* Test the extractors() function.
*
2017-01-16 17:19:19 +01:00
* @return void
*/
public function testExtractors()
{
2017-04-25 23:49:13 +02:00
$this->assertRequestIsOk('extractors');
2017-01-16 17:19:19 +01:00
}
/**
2017-01-16 17:31:20 +01:00
* Test the password() function.
*
2017-01-16 17:19:19 +01:00
* @return void
*/
public function testPassword()
{
$this->assertRequestIsClientError('password');
2017-01-16 17:19:19 +01:00
}
/**
* Test the info() function without the url parameter.
2017-01-16 17:31:20 +01:00
*
2017-01-16 17:19:19 +01:00
* @return void
*/
public function testInfoWithoutUrl()
2017-01-16 17:19:19 +01:00
{
$this->expectException(InvalidURLException::class);
$this->getRequestResult('info', []);
2017-01-16 17:19:19 +01:00
}
/**
* Test the info() function.
2017-01-16 17:31:20 +01:00
*
2017-01-16 17:19:19 +01:00
* @return void
* @requires download
2017-01-16 17:19:19 +01:00
*/
public function testInfo()
2017-01-16 17:19:19 +01:00
{
$this->assertRequestIsOk('info', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU']);
2017-01-16 17:19:19 +01:00
}
/**
* Test the info() function with audio conversion.
2017-01-16 17:31:20 +01:00
*
2017-01-16 17:19:19 +01:00
* @return void
* @requires download
2017-01-16 17:19:19 +01:00
*/
public function testInfoWithAudio()
2017-01-16 17:19:19 +01: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 17:19:19 +01: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 17:19:19 +01:00
/**
* Test the info() function with audio enabled and an URL that doesn't need to be converted.
2017-01-16 17:31:20 +01:00
*
2017-01-16 17:19:19 +01:00
* @return void
* @requires download
2017-01-16 17:19:19 +01:00
*/
public function testInfoWithUnconvertedAudio()
2017-01-16 17:19:19 +01:00
{
$config = $this->container->get('config');
$config->setOptions(['convert' => true]);
2017-04-25 23:49:13 +02:00
$this->assertRequestIsRedirect(
'info',
2017-11-10 12:19:04 +01:00
[
'url' => 'https://2080.bandcamp.com/track/cygnus-x-the-orange-theme-2080-faulty-chip-cover',
'audio' => true,
]
2017-01-16 17:19:19 +01:00
);
}
/**
* Test the info() function with a password.
2017-01-16 17:31:20 +01:00
*
2017-01-16 17:19:19 +01:00
* @return void
* @requires download
2022-02-27 23:45:59 +01:00
* @throws AlltubeLibraryException|InvalidURLException
2017-01-16 17:19:19 +01:00
*/
public function testInfoWithPassword()
2017-01-16 17:19:19 +01:00
{
$result = $this->controller->info(
2022-02-27 23:44:36 +01:00
$this->container->get('request')->withQueryParams(['url' => 'https://vimeo.com/68375962'])
2017-10-29 23:21:13 +01:00
->withParsedBody(['password' => 'youtube-dl']),
$this->container->get('response')
2017-01-16 17:19:19 +01:00
);
$this->assertTrue($result->isOk());
}
/**
* Test the info() function with a missing password.
2017-01-16 17:31:20 +01:00
*
2017-01-16 17:19:19 +01:00
* @return void
* @requires download
2017-01-16 17:19:19 +01:00
*/
public function testInfoWithMissingPassword()
2017-01-16 17:19:19 +01:00
{
2022-02-27 23:44:36 +01:00
$this->assertRequestIsClientError('info', ['url' => 'https://vimeo.com/68375962']);
$this->assertRequestIsClientError('info', ['url' => 'https://vimeo.com/68375962', 'audio' => true]);
2017-01-16 17:19:19 +01:00
}
/**
* Test the info() function with streams enabled.
2017-01-16 17:31:20 +01:00
*
2017-01-16 17:19:19 +01:00
* @return void
* @requires download
2017-01-16 17:19:19 +01:00
*/
public function testInfoWithStream()
2017-01-16 17:19:19 +01:00
{
$config = $this->container->get('config');
$config->setOptions(['stream' => true]);
$this->assertRequestIsOk('info', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU']);
2017-04-25 23:49:13 +02:00
$this->assertRequestIsOk(
'info',
['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU', 'audio' => true]
2017-01-16 17:19:19 +01:00
);
}
2017-04-25 01:53:38 +02:00
/**
* Test the info() function with a playlist.
2017-04-25 01:53:38 +02:00
*
* @return void
* @requires download
2017-04-25 01:53:38 +02:00
*/
public function testInfoWithPlaylist()
2017-04-25 01:53:38 +02:00
{
2017-04-25 23:49:13 +02:00
$this->assertRequestIsOk(
'info',
2017-10-29 23:21:13 +01:00
['url' => 'https://www.youtube.com/playlist?list=PLgdySZU6KUXL_8Jq5aUkyNV7wCa-4wZsC']
2017-04-25 01:53:38 +02:00
);
}
2017-01-16 17:19:19 +01:00
/**
2017-01-16 17:31:20 +01:00
* Test the error() function.
*
2017-01-16 17:19:19 +01:00
* @return void
*/
public function testError()
{
$result = $this->controller->error(
$this->container->get('request'),
$this->container->get('response'),
new Exception('foo')
);
2017-01-16 17:19:19 +01: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 23:21:13 +01:00
['locale' => 'fr_FR']
)->isRedirect()
);
}
2017-01-16 17:19:19 +01:00
}