From 3d2b518cb4b9a4a3dcbd568bc65f5bca2e387eba Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Thu, 22 Oct 2020 22:39:09 +0200 Subject: [PATCH] Create a test container that we can use in any test --- classes/Controller/FrontController.php | 1 - tests/ContainerTest.php | 57 ++++++++++++++++++++ tests/ControllerTest.php | 56 ++++--------------- tests/ConvertedPlaylistArchiveStreamTest.php | 6 +++ tests/FrontControllerTest.php | 16 +++--- tests/LocaleManagerTest.php | 16 ++++-- tests/LocaleMiddlewareTest.php | 37 ++++++------- tests/LocaleTest.php | 11 +++- tests/PlaylistArchiveStreamTest.php | 5 ++ tests/StreamTest.php | 14 +++-- tests/UglyRouterTest.php | 11 +++- tests/VideoStubsTest.php | 15 ++++-- tests/VideoTest.php | 11 ++-- tests/ViewFactoryTest.php | 30 +++-------- tests/YoutubeChunkStreamTest.php | 5 ++ tests/YoutubeStreamTest.php | 5 ++ 16 files changed, 179 insertions(+), 117 deletions(-) create mode 100644 tests/ContainerTest.php diff --git a/classes/Controller/FrontController.php b/classes/Controller/FrontController.php index 0cd8efa..18b1a11 100644 --- a/classes/Controller/FrontController.php +++ b/classes/Controller/FrontController.php @@ -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; diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php new file mode 100644 index 0000000..3be8ae3 --- /dev/null +++ b/tests/ContainerTest.php @@ -0,0 +1,57 @@ +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(); + } +} diff --git a/tests/ControllerTest.php b/tests/ControllerTest.php index 9eae71b..ec32398 100644 --- a/tests/ControllerTest.php +++ b/tests/ControllerTest.php @@ -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') ); } diff --git a/tests/ConvertedPlaylistArchiveStreamTest.php b/tests/ConvertedPlaylistArchiveStreamTest.php index 4301fa7..0e42d49 100644 --- a/tests/ConvertedPlaylistArchiveStreamTest.php +++ b/tests/ConvertedPlaylistArchiveStreamTest.php @@ -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 { diff --git a/tests/FrontControllerTest.php b/tests/FrontControllerTest.php index 51526b9..65a3d61 100644 --- a/tests/FrontControllerTest.php +++ b/tests/FrontControllerTest.php @@ -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() ); diff --git a/tests/LocaleManagerTest.php b/tests/LocaleManagerTest.php index d06af1f..e8fc7f0 100644 --- a/tests/LocaleManagerTest.php +++ b/tests/LocaleManagerTest.php @@ -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(); } diff --git a/tests/LocaleMiddlewareTest.php b/tests/LocaleMiddlewareTest.php index 48977d8..b9a45cd 100644 --- a/tests/LocaleMiddlewareTest.php +++ b/tests/LocaleMiddlewareTest.php @@ -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'] ); diff --git a/tests/LocaleTest.php b/tests/LocaleTest.php index 2bf2358..e5949d4 100644 --- a/tests/LocaleTest.php +++ b/tests/LocaleTest.php @@ -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'); } diff --git a/tests/PlaylistArchiveStreamTest.php b/tests/PlaylistArchiveStreamTest.php index ffae5ee..252bcd1 100644 --- a/tests/PlaylistArchiveStreamTest.php +++ b/tests/PlaylistArchiveStreamTest.php @@ -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 { diff --git a/tests/StreamTest.php b/tests/StreamTest.php index 5b3c92a..e7a5700 100644 --- a/tests/StreamTest.php +++ b/tests/StreamTest.php @@ -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(); } diff --git a/tests/UglyRouterTest.php b/tests/UglyRouterTest.php index f2c08ea..a70fc98 100644 --- a/tests/UglyRouterTest.php +++ b/tests/UglyRouterTest.php @@ -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'); } diff --git a/tests/VideoStubsTest.php b/tests/VideoStubsTest.php index ae6f55a..35292e7 100644 --- a/tests/VideoStubsTest.php +++ b/tests/VideoStubsTest.php @@ -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(); } diff --git a/tests/VideoTest.php b/tests/VideoTest.php index 05b9856..6a26801 100644 --- a/tests/VideoTest.php +++ b/tests/VideoTest.php @@ -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'; } diff --git a/tests/ViewFactoryTest.php b/tests/ViewFactoryTest.php index 868af18..5802d24 100644 --- a/tests/ViewFactoryTest.php +++ b/tests/ViewFactoryTest.php @@ -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); } } diff --git a/tests/YoutubeChunkStreamTest.php b/tests/YoutubeChunkStreamTest.php index 8064f60..b49bc1f 100644 --- a/tests/YoutubeChunkStreamTest.php +++ b/tests/YoutubeChunkStreamTest.php @@ -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 { diff --git a/tests/YoutubeStreamTest.php b/tests/YoutubeStreamTest.php index 838a270..1baed87 100644 --- a/tests/YoutubeStreamTest.php +++ b/tests/YoutubeStreamTest.php @@ -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 {