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

@ -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')
);
}