Merge branch 'release-3.0.0-beta3'

This commit is contained in:
Pierre Rudloff 2020-10-28 21:56:24 +01:00
commit ac6cd1dbb7
25 changed files with 344 additions and 266 deletions

105
classes/App.php Normal file
View file

@ -0,0 +1,105 @@
<?php
namespace Alltube;
use Alltube\Controller\DownloadController;
use Alltube\Controller\FrontController;
use Alltube\Controller\JsonController;
use Alltube\Exception\ConfigException;
use Alltube\Exception\DependencyException;
use Alltube\Factory\ConfigFactory;
use Alltube\Factory\LocaleManagerFactory;
use Alltube\Factory\LoggerFactory;
use Alltube\Factory\SessionFactory;
use Alltube\Factory\ViewFactory;
use Alltube\Middleware\CspMiddleware;
use Alltube\Middleware\LinkHeaderMiddleware;
use Alltube\Middleware\LocaleMiddleware;
use Alltube\Middleware\RouterPathMiddleware;
use Slim\Container;
use SmartyException;
class App extends \Slim\App
{
/**
* App constructor.
* @throws ConfigException
* @throws DependencyException
* @throws SmartyException
*/
public function __construct()
{
parent::__construct();
/** @var Container $container */
$container = $this->getContainer();
// Config.
$container['config'] = ConfigFactory::create($container);
// Session.
$container['session'] = SessionFactory::create($container);
// Locales.
$container['locale'] = LocaleManagerFactory::create($container);
// Smarty.
$container['view'] = ViewFactory::create($container);
// Logger.
$container['logger'] = LoggerFactory::create($container);
// Middlewares.
$this->add(new LocaleMiddleware($container));
$this->add(new CspMiddleware($container));
$this->add(new LinkHeaderMiddleware($container));
$this->add(new RouterPathMiddleware($container));
// Controllers.
$frontController = new FrontController($container);
$jsonController = new JsonController($container);
$downloadController = new DownloadController($container);
// Error handling.
$container['errorHandler'] = [$frontController, 'error'];
$container['phpErrorHandler'] = [$frontController, 'error'];
$container['notFoundHandler'] = [$frontController, 'notFound'];
$container['notAllowedHandler'] = [$frontController, 'notAllowed'];
// Routes.
$this->get(
'/',
[$frontController, 'index']
)->setName('index');
$this->get(
'/extractors',
[$frontController, 'extractors']
)->setName('extractors');
$this->any(
'/info',
[$frontController, 'info']
)->setName('info');
$this->any(
'/watch',
[$frontController, 'info']
);
$this->any(
'/download',
[$downloadController, 'download']
)->setName('download');
$this->get(
'/locale/{locale}',
[$frontController, 'locale']
)->setName('locale');
$this->get(
'/json',
[$jsonController, 'json']
)->setName('json');
}
}

View file

@ -10,12 +10,12 @@ use Alltube\Config;
use Alltube\Library\Downloader;
use Alltube\Library\Video;
use Alltube\LocaleManager;
use Alltube\SessionFactory;
use Aura\Session\Segment;
use Consolidation\Log\Logger;
use Psr\Container\ContainerInterface;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Router;
/**
* Abstract class used by every controller.
@ -76,6 +76,11 @@ abstract class BaseController
*/
protected $logger;
/**
* @var Router
*/
protected $router;
/**
* BaseController constructor.
*
@ -89,6 +94,7 @@ abstract class BaseController
$this->sessionSegment = $session->getSegment(self::class);
$this->localeManager = $this->container->get('locale');
$this->downloader = $this->config->getDownloader();
$this->router = $this->container->get('router');
$this->logger = $this->container->get('logger');
$this->downloader->setLogger($this->logger);

View file

@ -86,7 +86,7 @@ class DownloadController extends BaseController
}
}
} else {
return $response->withRedirect($this->container->get('router')->pathFor('index'));
return $response->withRedirect($this->router->pathFor('index'));
}
}

View file

@ -54,20 +54,15 @@ class FrontController extends BaseController
*/
public function index(Request $request, Response $response)
{
$uri = $request->getUri()->withUserInfo('');
$this->view->render(
$response,
'index.tpl',
[
'config' => $this->config,
'class' => 'index',
'description' => $this->localeManager->t(
'Easily download videos from Youtube, Dailymotion, Vimeo and other websites.'
),
'domain' => $uri->getScheme() . '://' . $uri->getAuthority(),
'canonical' => $this->getCanonicalUrl($request),
'supportedLocales' => $this->localeManager->getSupportedLocales(),
'locale' => $this->localeManager->getLocale(),
]
);
@ -87,7 +82,7 @@ class FrontController extends BaseController
{
$this->localeManager->setLocale(new Locale($data['locale']));
return $response->withRedirect($this->container->get('router')->pathFor('index'));
return $response->withRedirect($this->router->pathFor('index'));
}
/**
@ -105,14 +100,11 @@ class FrontController extends BaseController
$response,
'extractors.tpl',
[
'config' => $this->config,
'extractors' => $this->downloader->getExtractors(),
'class' => 'extractors',
'title' => $this->localeManager->t('Supported websites'),
'description' => $this->localeManager->t('List of all supported websites from which Alltube Download ' .
'can extract video or audio files'),
'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->localeManager->getLocale(),
]
);
@ -133,14 +125,11 @@ class FrontController extends BaseController
$response,
'password.tpl',
[
'config' => $this->config,
'class' => 'password',
'title' => $this->localeManager->t('Password prompt'),
'description' => $this->localeManager->t(
'You need a password in order to download this video with Alltube Download'
),
'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->localeManager->getLocale(),
]
);
@ -194,9 +183,6 @@ class FrontController extends BaseController
'class' => 'info',
'title' => $title,
'description' => $description,
'config' => $this->config,
'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->localeManager->getLocale(),
'defaultFormat' => $this->defaultFormat,
]
);
@ -223,14 +209,13 @@ class FrontController extends BaseController
if ($this->config->convert && $request->getQueryParam('audio')) {
// We skip the info page and get directly to the download.
return $response->withRedirect(
$this->container->get('router')->pathFor('download') .
'?' . http_build_query($request->getQueryParams())
$this->router->pathFor('download', [], $request->getQueryParams())
);
} else {
return $this->getInfoResponse($request, $response);
}
} else {
return $response->withRedirect($this->container->get('router')->pathFor('index'));
return $response->withRedirect($this->router->pathFor('index'));
}
}
@ -249,12 +234,9 @@ class FrontController extends BaseController
$response,
'error.tpl',
[
'config' => $this->config,
'error' => $message,
'class' => 'video',
'title' => $this->localeManager->t('Error'),
'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->localeManager->getLocale(),
]
);
@ -322,29 +304,4 @@ class FrontController extends BaseController
return $this->displayError($request, $response, $message);
}
}
/**
* Generate the canonical URL of the current page.
*
* @param Request $request PSR-7 Request
*
* @return string URL
*/
private function getCanonicalUrl(Request $request)
{
$uri = $request->getUri();
$return = 'https://alltubedownload.net/';
$path = $uri->getPath();
if ($path != '/') {
$return .= $path;
}
$query = $uri->getQuery();
if (!empty($query)) {
$return .= '?' . $query;
}
return $return;
}
}

View file

@ -19,6 +19,23 @@ use SmartyException;
*/
class ViewFactory
{
/**
* Generate the canonical URL of the current page.
*
* @param Request $request PSR-7 Request
*
* @return string URL
*/
private static function getCanonicalUrl(Request $request)
{
/** @var Uri $uri */
$uri = $request->getUri();
return $uri->withBasePath('')
->withHost('alltubedownload.net')
->withScheme('https');
}
/**
* Create Smarty view object.
*
@ -63,6 +80,11 @@ class ViewFactory
$view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']);
$view->registerPlugin('block', 't', [$localeManager, 'smartyTranslate']);
$view->offsetSet('canonical', self::getCanonicalUrl($request));
$view->offsetSet('locale', $container->get('locale')->getLocale());
$view->offsetSet('config', $container->get('config'));
$view->offsetSet('domain', $uri->withBasePath('')->getBaseUrl());
return $view;
}
}

View file

@ -9,6 +9,7 @@ namespace Alltube;
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface;
use RuntimeException;
use Slim\Http\Uri;
use Slim\Router;
/**
@ -27,15 +28,17 @@ class UglyRouter extends Router
*/
public function dispatch(ServerRequestInterface $request)
{
parse_str($request->getUri()->getQuery(), $args);
$uri = '/';
if (isset($args['page'])) {
$uri .= $args['page'];
$params = $request->getQueryParams();
$uri = new Uri('', '');
if (isset($params['page'])) {
// Build an URI that the router can understand.
$uri = $uri->withPath($params['page']);
}
return $this->createDispatcher()->dispatch(
$request->getMethod(),
$uri
(string) $uri
);
}
@ -52,10 +55,11 @@ class UglyRouter extends Router
*/
public function pathFor($name, array $data = [], array $queryParams = [])
{
$url = str_replace('/', '/?page=', $this->relativePathFor($name, $data, $queryParams));
$queryParams['page'] = $name;
$url = Uri::createFromString($this->relativePathFor($name, $data, $queryParams))->withPath('');
if ($this->basePath) {
$url = $this->basePath . $url;
$url = $url->withBasePath($this->basePath);
}
return $url;

View file

@ -30,7 +30,7 @@
"oomphinc/composer-installers-extender": "^2.0",
"paragonie/csp-builder": "^2.5",
"rinvex/countries": "^6.1",
"rudloff/alltube-library": "dev-develop",
"rudloff/alltube-library": "^0.1.1",
"symfony/finder": "^5.0",
"symfony/translation": "^4.0",
"symfony/yaml": "^4.0",
@ -88,8 +88,8 @@
"name": "ytdl-org/youtube-dl",
"version": "2020.09.20",
"dist": {
"type": "zip",
"url": "https://github.com/ytdl-org/youtube-dl/archive/2020.09.20.zip"
"type": "tar",
"url": "https://files.pythonhosted.org/packages/12/8b/51cae2929739d637fdfbc706b2d5f8925b5710d8f408b5319a07ea45fe99/youtube_dl-2020.9.20.tar.gz"
}
}
}

23
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "3c2fabffdbf381b138354a986accc302",
"content-hash": "7db8acd0f18d73da93880f015265e1f6",
"packages": [
{
"name": "aura/session",
@ -1232,16 +1232,16 @@
},
{
"name": "rudloff/alltube-library",
"version": "dev-develop",
"version": "0.1.1",
"source": {
"type": "git",
"url": "https://github.com/Rudloff/alltube-library.git",
"reference": "69d09b780e01ec0f3e6e3d123be14fa3b981b64e"
"reference": "f0bbd14b06bb4df9e3fbae62a6f09b455fdcc703"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Rudloff/alltube-library/zipball/69d09b780e01ec0f3e6e3d123be14fa3b981b64e",
"reference": "69d09b780e01ec0f3e6e3d123be14fa3b981b64e",
"url": "https://api.github.com/repos/Rudloff/alltube-library/zipball/f0bbd14b06bb4df9e3fbae62a6f09b455fdcc703",
"reference": "f0bbd14b06bb4df9e3fbae62a6f09b455fdcc703",
"shasum": ""
},
"require": {
@ -1269,7 +1269,7 @@
],
"description": "PHP wrapper for youtube-dl",
"homepage": "http://alltubedownload.net/",
"time": "2020-10-17T20:47:16+00:00"
"time": "2020-10-27T22:25:49+00:00"
},
{
"name": "slim/slim",
@ -2428,8 +2428,8 @@
"name": "ytdl-org/youtube-dl",
"version": "2020.09.20",
"dist": {
"type": "zip",
"url": "https://github.com/ytdl-org/youtube-dl/archive/2020.09.20.zip"
"type": "tar",
"url": "https://files.pythonhosted.org/packages/12/8b/51cae2929739d637fdfbc706b2d5f8925b5710d8f408b5319a07ea45fe99/youtube_dl-2020.9.20.tar.gz"
},
"type": "library"
},
@ -7106,9 +7106,7 @@
],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": {
"rudloff/alltube-library": 20
},
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
@ -7118,5 +7116,6 @@
"platform-dev": [],
"platform-overrides": {
"php": "7.3.11"
}
},
"plugin-api-version": "1.1.0"
}

View file

@ -2,21 +2,8 @@
require_once __DIR__ . '/vendor/autoload.php';
use Alltube\Controller\DownloadController;
use Alltube\Controller\FrontController;
use Alltube\Controller\JsonController;
use Alltube\App;
use Alltube\ErrorHandler;
use Alltube\Factory\ConfigFactory;
use Alltube\Factory\LocaleManagerFactory;
use Alltube\Factory\LoggerFactory;
use Alltube\Factory\SessionFactory;
use Alltube\Factory\ViewFactory;
use Alltube\Middleware\CspMiddleware;
use Alltube\Middleware\LinkHeaderMiddleware;
use Alltube\Middleware\LocaleMiddleware;
use Alltube\Middleware\RouterPathMiddleware;
use Slim\App;
use Slim\Container;
if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], '/index.php') !== false) {
header('Location: ' . str_ireplace('/index.php', '/', $_SERVER['REQUEST_URI']));
@ -27,77 +14,6 @@ try {
// Create app.
$app = new App();
/** @var Container $container */
$container = $app->getContainer();
// Config.
$container['config'] = ConfigFactory::create($container);
// Session.
$container['session'] = SessionFactory::create($container);
// Locales.
$container['locale'] = LocaleManagerFactory::create($container);
// Smarty.
$container['view'] = ViewFactory::create($container);
// Logger.
$container['logger'] = LoggerFactory::create($container);
// Middlewares.
$app->add(new LocaleMiddleware($container));
$app->add(new CspMiddleware($container));
$app->add(new LinkHeaderMiddleware($container));
$app->add(new RouterPathMiddleware($container));
// Controllers.
$frontController = new FrontController($container);
$jsonController = new JsonController($container);
$downloadController = new DownloadController($container);
// Error handling.
$container['errorHandler'] = [$frontController, 'error'];
$container['phpErrorHandler'] = [$frontController, 'error'];
$container['notFoundHandler'] = [$frontController, 'notFound'];
$container['notAllowedHandler'] = [$frontController, 'notAllowed'];
// Routes.
$app->get(
'/',
[$frontController, 'index']
)->setName('index');
$app->get(
'/extractors',
[$frontController, 'extractors']
)->setName('extractors');
$app->any(
'/info',
[$frontController, 'info']
)->setName('info');
$app->any(
'/watch',
[$frontController, 'info']
);
$app->any(
'/download',
[$downloadController, 'download']
)->setName('download');
$app->get(
'/locale/{locale}',
[$frontController, 'locale']
)->setName('locale');
$app->get(
'/json',
[$jsonController, 'json']
)->setName('json');
$app->run();
} catch (Throwable $e) {
ErrorHandler::handle($e);

View file

@ -42,7 +42,7 @@
<div id="bookmarklet" class="bookmarklet_wrapper">
<p> {t}Drag this to your bookmarks bar:{/t} </p>
<a class="bookmarklet small-font"
href="javascript:window.location='{$domain}{path_for name='info'}?url='+encodeURIComponent(location.href);">{t}Bookmarklet{/t}</a>
href="javascript:window.location='{$domain}{path_for name='info' queryParams=['url' => '%url%']}'.replace('%url%', encodeURIComponent(location.href));">{t}Bookmarklet{/t}</a>
</div>
</main>

57
tests/ContainerTest.php Normal file
View file

@ -0,0 +1,57 @@
<?php
/**
* PlaylistArchiveStreamTest class.
*/
namespace Alltube\Test;
use Alltube\Config;
use Alltube\Exception\ConfigException;
use Alltube\Exception\DependencyException;
use Alltube\Factory\LocaleManagerFactory;
use Alltube\Factory\SessionFactory;
use Alltube\Factory\ViewFactory;
use Psr\Log\NullLogger;
use Slim\Container;
use Slim\Http\Environment;
use SmartyException;
/**
* Base class for tests that require a container.
*/
abstract class ContainerTest extends BaseTest
{
/**
* Slim dependency container.
*
* @var Container
*/
protected $container;
/**
* Prepare tests.
* @throws ConfigException
* @throws DependencyException
* @throws SmartyException
*/
protected function setUp(): void
{
$this->checkRequirements();
$this->container = new Container(['environment' => Environment::mock()]);
$this->container['config'] = Config::fromFile($this->getConfigFile());
$this->container['session'] = SessionFactory::create($this->container);
$this->container['locale'] = LocaleManagerFactory::create($this->container);
$this->container['view'] = ViewFactory::create($this->container);
$this->container['logger'] = new NullLogger();
}
/**
* Cleanup after each test.
*/
protected function tearDown(): void
{
$this->container->get('session')->clear();
}
}

View file

@ -6,48 +6,19 @@
namespace Alltube\Test;
use Alltube\Config;
use Alltube\Controller\BaseController;
use Alltube\Controller\DownloadController;
use Alltube\Controller\FrontController;
use Alltube\Exception\ConfigException;
use Alltube\Exception\DependencyException;
use Alltube\Factory\LocaleManagerFactory;
use Alltube\Factory\SessionFactory;
use Alltube\Factory\ViewFactory;
use Psr\Log\NullLogger;
use Slim\Container;
use Slim\Http\Environment;
use Slim\Http\Request;
use Slim\Http\Response;
use SmartyException;
/**
* Abstract class used by the controller tests.
*/
abstract class ControllerTest extends BaseTest
abstract class ControllerTest extends ContainerTest
{
/**
* Slim dependency container.
*
* @var Container
*/
protected $container;
/**
* Mock HTTP request.
*
* @var Request
*/
protected $request;
/**
* Mock HTTP response.
*
* @var Response
*/
protected $response;
/**
* Controller instance used in tests.
* @var BaseController
@ -63,27 +34,20 @@ abstract class ControllerTest extends BaseTest
{
parent::setUp();
$this->container = new Container();
$this->request = Request::createFromEnvironment(Environment::mock());
$this->response = new Response();
$this->container['config'] = Config::fromFile($this->getConfigFile());
$this->container['session'] = SessionFactory::create($this->container);
$this->container['locale'] = LocaleManagerFactory::create($this->container);
$this->container['view'] = ViewFactory::create($this->container, $this->request);
$this->container['logger'] = new NullLogger();
$frontController = new FrontController($this->container);
$downloadController = new DownloadController($this->container);
$this->container['router']->map(['GET'], '/', [$frontController, 'index'])
$router = $this->container->get('router');
$router->map(['GET'], '/', [$frontController, 'index'])
->setName('index');
$this->container['router']->map(['GET'], '/video', [$frontController, 'info'])
$router->map(['GET'], '/video', [$frontController, 'info'])
->setName('info');
$this->container['router']->map(['GET'], '/extractors', [$frontController, 'extractors'])
$router->map(['GET'], '/extractors', [$frontController, 'extractors'])
->setName('extractors');
$this->container['router']->map(['GET'], '/locale', [$frontController, 'locale'])
$router->map(['GET'], '/locale', [$frontController, 'locale'])
->setName('locale');
$this->container['router']->map(['GET'], '/redirect', [$downloadController, 'download'])
$router->map(['GET'], '/redirect', [$downloadController, 'download'])
->setName('download');
}
@ -98,8 +62,8 @@ abstract class ControllerTest extends BaseTest
protected function getRequestResult(string $request, array $params)
{
return $this->controller->$request(
$this->request->withQueryParams($params),
$this->response
$this->container->get('request')->withQueryParams($params),
$this->container->get('response')
);
}

View file

@ -7,7 +7,9 @@
namespace Alltube\Test;
use Alltube\Exception\ConfigException;
use Alltube\Exception\DependencyException;
use Alltube\Stream\ConvertedPlaylistArchiveStream;
use SmartyException;
/**
* Unit tests for the ConvertedPlaylistArchiveStream class.
@ -15,9 +17,13 @@ use Alltube\Stream\ConvertedPlaylistArchiveStream;
*/
class ConvertedPlaylistArchiveStreamTest extends StreamTest
{
/**
* Prepare tests.
*
* @throws ConfigException
* @throws DependencyException
* @throws SmartyException
*/
protected function setUp(): void
{

View file

@ -81,7 +81,7 @@ class FrontControllerTest extends ControllerTest
Request::createFromEnvironment(
Environment::mock(['REQUEST_URI' => '/foo', 'QUERY_STRING' => 'foo=bar'])
),
$this->response
$this->container->get('response')
);
$this->assertTrue($result->isOk());
}
@ -189,9 +189,9 @@ class FrontControllerTest extends ControllerTest
public function testInfoWithPassword()
{
$result = $this->controller->info(
$this->request->withQueryParams(['url' => 'http://vimeo.com/68375962'])
$this->container->get('request')->withQueryParams(['url' => 'http://vimeo.com/68375962'])
->withParsedBody(['password' => 'youtube-dl']),
$this->response
$this->container->get('response')
);
$this->assertTrue($result->isOk());
}
@ -247,7 +247,11 @@ class FrontControllerTest extends ControllerTest
*/
public function testError()
{
$result = $this->controller->error($this->request, $this->response, new Exception('foo'));
$result = $this->controller->error(
$this->container->get('request'),
$this->container->get('response'),
new Exception('foo')
);
$this->assertTrue($result->isServerError());
}
@ -260,8 +264,8 @@ class FrontControllerTest extends ControllerTest
{
$this->assertTrue(
$this->controller->locale(
$this->request,
$this->response,
$this->container->get('request'),
$this->container->get('response'),
['locale' => 'fr_FR']
)->isRedirect()
);

View file

@ -6,15 +6,17 @@
namespace Alltube\Test;
use Alltube\Exception\ConfigException;
use Alltube\Exception\DependencyException;
use Alltube\Factory\SessionFactory;
use Alltube\Locale;
use Alltube\LocaleManager;
use Slim\Container;
use SmartyException;
/**
* Unit tests for the LocaleManagerTest class.
*/
class LocaleManagerTest extends BaseTest
class LocaleManagerTest extends ContainerTest
{
/**
* LocaleManager class instance.
@ -25,11 +27,17 @@ class LocaleManagerTest extends BaseTest
/**
* Prepare tests.
*
* @throws ConfigException
* @throws DependencyException
* @throws SmartyException
*/
protected function setUp(): void
{
parent::setUp();
$_SESSION[LocaleManager::class]['locale'] = 'foo_BAR';
$this->localeManager = new LocaleManager(SessionFactory::create(new Container()));
$this->localeManager = new LocaleManager(SessionFactory::create($this->container));
}
/**
@ -39,6 +47,8 @@ class LocaleManagerTest extends BaseTest
*/
protected function tearDown(): void
{
parent::tearDown();
$this->localeManager->unsetLocale();
}

View file

@ -6,19 +6,17 @@
namespace Alltube\Test;
use Alltube\Exception\ConfigException;
use Alltube\Exception\DependencyException;
use Alltube\Factory\LocaleManagerFactory;
use Alltube\Factory\SessionFactory;
use Alltube\Middleware\LocaleMiddleware;
use Slim\Container;
use Slim\Http\Environment;
use Slim\Http\Request;
use Slim\Http\Response;
use SmartyException;
/**
* Unit tests for the FrontController class.
*/
class LocaleMiddlewareTest extends BaseTest
class LocaleMiddlewareTest extends ContainerTest
{
/**
* LocaleMiddleware instance.
@ -27,22 +25,17 @@ class LocaleMiddlewareTest extends BaseTest
*/
private $middleware;
/**
* Slim dependency container.
*
* @var Container
*/
private $container;
/**
* Prepare tests.
*
* @throws DependencyException
* @throws ConfigException
* @throws SmartyException
*/
protected function setUp(): void
{
$this->container = new Container();
$this->container['session'] = SessionFactory::create($this->container);
$this->container['locale'] = LocaleManagerFactory::create($this->container);
parent::setUp();
$this->middleware = new LocaleMiddleware($this->container);
}
@ -53,7 +46,9 @@ class LocaleMiddlewareTest extends BaseTest
*/
protected function tearDown(): void
{
$this->container['locale']->unsetLocale();
parent::tearDown();
$this->container->get('locale')->unsetLocale();
}
/**
@ -119,9 +114,8 @@ class LocaleMiddlewareTest extends BaseTest
*/
public function testInvoke()
{
$request = Request::createFromEnvironment(Environment::mock());
$this->middleware->__invoke(
$request->withHeader('Accept-Language', 'foo-BAR'),
$this->container->get('request')->withHeader('Accept-Language', 'foo-BAR'),
new Response(),
[$this, 'assertHeader']
);
@ -134,9 +128,8 @@ class LocaleMiddlewareTest extends BaseTest
*/
public function testInvokeWithoutHeader()
{
$request = Request::createFromEnvironment(Environment::mock());
$this->middleware->__invoke(
$request->withoutHeader('Accept-Language'),
$this->container->get('request')->withoutHeader('Accept-Language'),
new Response(),
[$this, 'assertNoHeader']
);

View file

@ -6,12 +6,15 @@
namespace Alltube\Test;
use Alltube\Exception\ConfigException;
use Alltube\Exception\DependencyException;
use Alltube\Locale;
use SmartyException;
/**
* Unit tests for the LocaleTest class.
*/
class LocaleTest extends BaseTest
class LocaleTest extends ContainerTest
{
/**
* Locale class instance.
@ -22,9 +25,15 @@ class LocaleTest extends BaseTest
/**
* Prepare tests.
*
* @throws DependencyException
* @throws ConfigException
* @throws SmartyException
*/
protected function setUp(): void
{
parent::setUp();
$this->localeObject = new Locale('fr_FR');
}

View file

@ -7,7 +7,9 @@
namespace Alltube\Test;
use Alltube\Exception\ConfigException;
use Alltube\Exception\DependencyException;
use Alltube\Stream\PlaylistArchiveStream;
use SmartyException;
/**
* Unit tests for the PlaylistArchiveStream class.
@ -17,7 +19,10 @@ class PlaylistArchiveStreamTest extends StreamTest
{
/**
* Prepare tests.
*
* @throws ConfigException
* @throws DependencyException
* @throws SmartyException
*/
protected function setUp(): void
{

View file

@ -6,16 +6,17 @@
namespace Alltube\Test;
use Alltube\Config;
use Alltube\Exception\ConfigException;
use Alltube\Exception\DependencyException;
use Alltube\Library\Downloader;
use Psr\Http\Message\StreamInterface;
use RuntimeException;
use SmartyException;
/**
* Abstract class used by the stream tests.
*/
abstract class StreamTest extends BaseTest
abstract class StreamTest extends ContainerTest
{
/**
* Stream instance.
@ -31,15 +32,16 @@ abstract class StreamTest extends BaseTest
/**
* Prepare tests.
*
* @throws DependencyException
* @throws ConfigException
* @throws SmartyException
*/
protected function setUp(): void
{
parent::setUp();
// So ffmpeg does not spam the output with broken pipe errors.
$config = new Config(['ffmpegVerbosity' => 'fatal']);
$this->downloader = $config->getDownloader();
$this->downloader = $this->container->get('config')->getDownloader();
}
/**
@ -49,6 +51,8 @@ abstract class StreamTest extends BaseTest
*/
protected function tearDown(): void
{
parent::tearDown();
$this->stream->close();
}

View file

@ -6,14 +6,17 @@
namespace Alltube\Test;
use Alltube\Exception\ConfigException;
use Alltube\Exception\DependencyException;
use Alltube\UglyRouter;
use Slim\Http\Environment;
use Slim\Http\Request;
use SmartyException;
/**
* Unit tests for the UglyRouter class.
*/
class UglyRouterTest extends BaseTest
class UglyRouterTest extends ContainerTest
{
/**
* UglyRouter instance.
@ -24,9 +27,15 @@ class UglyRouterTest extends BaseTest
/**
* Prepare tests.
*
* @throws ConfigException
* @throws DependencyException
* @throws SmartyException
*/
protected function setUp(): void
{
parent::setUp();
$this->router = new UglyRouter();
$this->router->map(['GET'], '/foo', 'print')->setName('foo');
}

View file

@ -6,13 +6,15 @@
namespace Alltube\Test;
use Alltube\Config;
use Alltube\Exception\ConfigException;
use Alltube\Exception\DependencyException;
use Alltube\Library\Downloader;
use Alltube\Library\Exception\AlltubeLibraryException;
use Alltube\Library\Exception\PopenStreamException;
use Alltube\Library\Video;
use Mockery;
use phpmock\mockery\PHPMockery;
use SmartyException;
/**
* Unit tests for the Video class.
@ -20,7 +22,7 @@ use phpmock\mockery\PHPMockery;
*
* @requires download
*/
class VideoStubsTest extends BaseTest
class VideoStubsTest extends ContainerTest
{
/**
* Video used in many tests.
@ -38,6 +40,10 @@ class VideoStubsTest extends BaseTest
/**
* Initialize properties used by test.
*
* @throws ConfigException
* @throws DependencyException
* @throws SmartyException
*/
protected function setUp(): void
{
@ -46,8 +52,7 @@ class VideoStubsTest extends BaseTest
PHPMockery::mock('Alltube\Library', 'popen');
PHPMockery::mock('Alltube\Library', 'fopen');
$config = new Config();
$this->downloader = $config->getDownloader();
$this->downloader = $this->container->get('config')->getDownloader();
$this->video = $this->downloader->getVideo('https://www.youtube.com/watch?v=XJC9_JkzugE');
}
@ -58,6 +63,8 @@ class VideoStubsTest extends BaseTest
*/
protected function tearDown(): void
{
parent::tearDown();
Mockery::close();
}

View file

@ -8,6 +8,7 @@ namespace Alltube\Test;
use Alltube\Config;
use Alltube\Exception\ConfigException;
use Alltube\Exception\DependencyException;
use Alltube\Library\Downloader;
use Alltube\Library\Exception\AlltubeLibraryException;
use Alltube\Library\Exception\AvconvException;
@ -18,13 +19,14 @@ use Alltube\Library\Exception\RemuxException;
use Alltube\Library\Exception\WrongPasswordException;
use Alltube\Library\Exception\YoutubedlException;
use Alltube\Library\Video;
use SmartyException;
/**
* Unit tests for the Video class.
* @requires download
* @todo Split Downloader and Video tests.
*/
class VideoTest extends BaseTest
class VideoTest extends ContainerTest
{
/**
* Downloader instance used in tests.
@ -42,15 +44,16 @@ class VideoTest extends BaseTest
/**
* Prepare tests.
*
* @throws ConfigException
* @throws DependencyException
* @throws SmartyException
*/
protected function setUp(): void
{
parent::setUp();
// So ffmpeg does not spam the output with broken pipe errors.
$config = new Config(['ffmpegVerbosity' => 'fatal']);
$this->downloader = $config->getDownloader();
$this->downloader = $this->container->get('config')->getDownloader();
$this->format = 'best';
}

View file

@ -6,34 +6,24 @@
namespace Alltube\Test;
use Alltube\Exception\DependencyException;
use Alltube\Factory\LocaleManagerFactory;
use Alltube\Factory\SessionFactory;
use Alltube\Factory\ViewFactory;
use Slim\Container;
use Slim\Http\Environment;
use Slim\Http\Request;
use Slim\Views\Smarty;
use SmartyException;
/**
* Unit tests for the ViewFactory class.
*/
class ViewFactoryTest extends BaseTest
class ViewFactoryTest extends ContainerTest
{
/**
* Test the create() function.
*
* @return void
* @throws SmartyException
* @throws DependencyException
*/
public function testCreate()
{
$container = new Container();
$container['session'] = SessionFactory::create($container);
$container['locale'] = LocaleManagerFactory::create($container);
$view = ViewFactory::create($container);
$view = ViewFactory::create($this->container);
$this->assertInstanceOf(Smarty::class, $view);
}
@ -42,15 +32,13 @@ class ViewFactoryTest extends BaseTest
*
* @return void
* @throws SmartyException
* @throws DependencyException
*/
public function testCreateWithXForwardedProto()
{
$container = new Container();
$container['session'] = SessionFactory::create($container);
$container['locale'] = LocaleManagerFactory::create($container);
$request = Request::createFromEnvironment(Environment::mock());
$view = ViewFactory::create($container, $request->withHeader('X-Forwarded-Proto', 'https'));
$view = ViewFactory::create(
$this->container,
$this->container->get('request')->withHeader('X-Forwarded-Proto', 'https')
);
$this->assertInstanceOf(Smarty::class, $view);
}
}

View file

@ -7,8 +7,10 @@
namespace Alltube\Test;
use Alltube\Exception\ConfigException;
use Alltube\Exception\DependencyException;
use Alltube\Library\Exception\AlltubeLibraryException;
use Alltube\Stream\YoutubeChunkStream;
use SmartyException;
/**
* Unit tests for the YoutubeChunkStream class.
@ -18,8 +20,11 @@ class YoutubeChunkStreamTest extends StreamTest
{
/**
* Prepare tests.
*
* @throws AlltubeLibraryException
* @throws ConfigException
* @throws DependencyException
* @throws SmartyException
*/
protected function setUp(): void
{

View file

@ -7,8 +7,10 @@
namespace Alltube\Test;
use Alltube\Exception\ConfigException;
use Alltube\Exception\DependencyException;
use Alltube\Library\Exception\AlltubeLibraryException;
use Alltube\Stream\YoutubeStream;
use SmartyException;
/**
* Unit tests for the YoutubeStream class.
@ -18,8 +20,11 @@ class YoutubeStreamTest extends StreamTest
{
/**
* Prepare tests.
*
* @throws AlltubeLibraryException
* @throws ConfigException
* @throws DependencyException
* @throws SmartyException
*/
protected function setUp(): void
{