Create a test container that we can use in any test

This commit is contained in:
Pierre Rudloff 2020-10-22 22:39:09 +02:00
parent d83774ae7d
commit 3d2b518cb4
16 changed files with 179 additions and 117 deletions

View file

@ -13,7 +13,6 @@ use Alltube\Locale;
use Alltube\Middleware\CspMiddleware;
use Exception;
use Slim\Http\StatusCode;
use Slim\Http\Uri;
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Throwable;
use Psr\Container\ContainerInterface;

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();
}
/**
@ -66,7 +61,7 @@ class LocaleMiddlewareTest extends BaseTest
{
$locale = [
'language' => 'en',
'region' => 'US',
'region' => 'US',
];
$this->assertEquals('en_US', $this->middleware->testLocale($locale));
}
@ -80,7 +75,7 @@ class LocaleMiddlewareTest extends BaseTest
{
$locale = [
'language' => 'foo',
'region' => 'BAR',
'region' => 'BAR',
];
$this->assertNull($this->middleware->testLocale($locale));
$this->assertNull($this->middleware->testLocale([]));
@ -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,38 +6,24 @@
namespace Alltube\Test;
use Alltube\Exception\ConfigException;
use Alltube\Exception\DependencyException;
use Alltube\Factory\ConfigFactory;
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
* @throws ConfigException
*/
public function testCreate()
{
$container = new Container();
$container['session'] = SessionFactory::create($container);
$container['locale'] = LocaleManagerFactory::create($container);
$container['config'] = ConfigFactory::create($container);
$view = ViewFactory::create($container);
$view = ViewFactory::create($this->container);
$this->assertInstanceOf(Smarty::class, $view);
}
@ -46,17 +32,13 @@ class ViewFactoryTest extends BaseTest
*
* @return void
* @throws SmartyException
* @throws DependencyException
* @throws ConfigException
*/
public function testCreateWithXForwardedProto()
{
$container = new Container();
$container['session'] = SessionFactory::create($container);
$container['locale'] = LocaleManagerFactory::create($container);
$container['config'] = ConfigFactory::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
{