alltube/tests/ControllerTest.php

158 lines
4.7 KiB
PHP
Raw Normal View History

<?php
/**
* ControllerTest class.
*/
namespace Alltube\Test;
2020-07-15 23:23:14 +02:00
use Alltube\Config;
2020-05-13 22:28:05 +02:00
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;
2020-10-20 23:29:50 +02:00
use Alltube\Factory\ViewFactory;
2020-07-15 23:23:14 +02:00
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
{
/**
* 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.
2020-05-13 22:28:05 +02:00
* @var BaseController
*/
protected $controller;
/**
* Prepare tests.
* @throws ConfigException|SmartyException
* @throws DependencyException
*/
2019-11-30 14:08:18 +01:00
protected function setUp(): void
{
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);
2020-07-15 23:23:14 +02:00
$this->container['logger'] = new NullLogger();
$frontController = new FrontController($this->container);
$downloadController = new DownloadController($this->container);
$this->container['router']->map(['GET'], '/', [$frontController, 'index'])
->setName('index');
$this->container['router']->map(['GET'], '/video', [$frontController, 'info'])
->setName('info');
$this->container['router']->map(['GET'], '/extractors', [$frontController, 'extractors'])
->setName('extractors');
$this->container['router']->map(['GET'], '/locale', [$frontController, 'locale'])
->setName('locale');
$this->container['router']->map(['GET'], '/redirect', [$downloadController, 'download'])
->setName('download');
}
/**
* Run controller function with custom query parameters and return the result.
*
* @param string $request Controller function to call
2020-05-13 22:28:05 +02:00
* @param mixed[] $params Query parameters
*
* @return Response HTTP response
*/
2020-09-27 19:56:19 +02:00
protected function getRequestResult(string $request, array $params)
{
return $this->controller->$request(
$this->request->withQueryParams($params),
$this->response
);
}
/**
* Assert that calling controller function with these parameters returns a 200 HTTP response.
*
* @param string $request Controller function to call
2020-05-13 22:28:05 +02:00
* @param mixed[] $params Query parameters
*
* @return void
*/
2020-09-27 19:56:19 +02:00
protected function assertRequestIsOk(string $request, array $params = [])
{
$this->assertTrue($this->getRequestResult($request, $params)->isOk());
}
/**
* Assert that calling controller function with these parameters returns an HTTP redirect.
*
* @param string $request Controller function to call
2020-05-13 22:28:05 +02:00
* @param mixed[] $params Query parameters
*
* @return void
*/
2020-09-27 19:56:19 +02:00
protected function assertRequestIsRedirect(string $request, array $params = [])
{
$this->assertTrue($this->getRequestResult($request, $params)->isRedirect());
}
/**
* Assert that calling controller function with these parameters returns an HTTP 500 error.
*
* @param string $request Controller function to call
2020-05-13 22:28:05 +02:00
* @param mixed[] $params Query parameters
*
* @return void
*/
2020-09-27 19:56:19 +02:00
protected function assertRequestIsServerError(string $request, array $params = [])
{
$this->assertTrue($this->getRequestResult($request, $params)->isServerError());
}
/**
* Assert that calling controller function with these parameters returns an HTTP 400 error.
*
* @param string $request Controller function to call
2020-05-13 22:28:05 +02:00
* @param mixed[] $params Query parameters
*
* @return void
*/
2020-09-27 19:56:19 +02:00
protected function assertRequestIsClientError(string $request, array $params = [])
{
$this->assertTrue($this->getRequestResult($request, $params)->isClientError());
}
}