2019-04-22 14:05:58 +00:00
|
|
|
<?php
|
2019-10-03 19:24:12 +00:00
|
|
|
|
2019-04-22 14:05:58 +00:00
|
|
|
/**
|
|
|
|
* ControllerTest class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Alltube\Test;
|
|
|
|
|
2020-05-13 20:28:05 +00:00
|
|
|
use Alltube\Controller\BaseController;
|
2019-04-22 14:05:58 +00:00
|
|
|
use Alltube\Controller\DownloadController;
|
|
|
|
use Alltube\Controller\FrontController;
|
2020-06-20 23:44:20 +00:00
|
|
|
use Alltube\Exception\ConfigException;
|
2019-04-22 14:05:58 +00:00
|
|
|
use Alltube\LocaleManager;
|
|
|
|
use Alltube\ViewFactory;
|
|
|
|
use Slim\Container;
|
|
|
|
use Slim\Http\Environment;
|
|
|
|
use Slim\Http\Request;
|
|
|
|
use Slim\Http\Response;
|
2020-06-20 23:44:20 +00:00
|
|
|
use SmartyException;
|
2019-04-22 14:05:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 20:28:05 +00:00
|
|
|
* @var BaseController
|
2019-04-22 14:05:58 +00:00
|
|
|
*/
|
|
|
|
protected $controller;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare tests.
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws ConfigException|SmartyException
|
2019-04-22 14:05:58 +00:00
|
|
|
*/
|
2019-11-30 13:08:18 +00:00
|
|
|
protected function setUp(): void
|
2019-04-22 14:05:58 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->container = new Container();
|
|
|
|
$this->request = Request::createFromEnvironment(Environment::mock());
|
|
|
|
$this->response = new Response();
|
2019-11-27 23:04:05 +00:00
|
|
|
$this->container['locale'] = LocaleManager::getInstance();
|
2019-04-22 14:05:58 +00:00
|
|
|
$this->container['view'] = ViewFactory::create($this->container, $this->request);
|
|
|
|
|
|
|
|
$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 20:28:05 +00:00
|
|
|
* @param mixed[] $params Query parameters
|
2019-04-22 14:05:58 +00:00
|
|
|
*
|
|
|
|
* @return Response HTTP response
|
|
|
|
*/
|
|
|
|
protected function getRequestResult($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 20:28:05 +00:00
|
|
|
* @param mixed[] $params Query parameters
|
2019-04-22 14:05:58 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function assertRequestIsOk($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 20:28:05 +00:00
|
|
|
* @param mixed[] $params Query parameters
|
2019-04-22 14:05:58 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function assertRequestIsRedirect($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 20:28:05 +00:00
|
|
|
* @param mixed[] $params Query parameters
|
2019-04-22 14:05:58 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function assertRequestIsServerError($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 20:28:05 +00:00
|
|
|
* @param mixed[] $params Query parameters
|
2019-04-22 14:05:58 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function assertRequestIsClientError($request, array $params = [])
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->getRequestResult($request, $params)->isClientError());
|
|
|
|
}
|
|
|
|
}
|