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-11-21 13:38:37 +00:00
|
|
|
use Alltube\Locale;
|
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;
|
2020-10-21 20:47:15 +00:00
|
|
|
use Alltube\Exception\DependencyException;
|
2019-04-22 14:05:58 +00:00
|
|
|
use Slim\Http\Response;
|
2020-12-17 21:30:19 +00:00
|
|
|
use Slim\Views\Smarty;
|
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.
|
|
|
|
*/
|
2020-10-22 20:39:09 +00:00
|
|
|
abstract class ControllerTest extends ContainerTest
|
2019-04-22 14:05:58 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 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
|
2020-10-21 20:47:15 +00:00
|
|
|
* @throws DependencyException
|
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();
|
|
|
|
|
2020-11-21 13:38:37 +00:00
|
|
|
$this->container->get('locale')->setLocale(new Locale('en_US'));
|
|
|
|
|
2019-04-22 14:05:58 +00:00
|
|
|
$frontController = new FrontController($this->container);
|
|
|
|
$downloadController = new DownloadController($this->container);
|
|
|
|
|
2020-10-22 20:39:09 +00:00
|
|
|
$router = $this->container->get('router');
|
|
|
|
|
|
|
|
$router->map(['GET'], '/', [$frontController, 'index'])
|
2019-04-22 14:05:58 +00:00
|
|
|
->setName('index');
|
2020-10-22 20:39:09 +00:00
|
|
|
$router->map(['GET'], '/video', [$frontController, 'info'])
|
2019-04-22 14:05:58 +00:00
|
|
|
->setName('info');
|
2020-10-22 20:39:09 +00:00
|
|
|
$router->map(['GET'], '/extractors', [$frontController, 'extractors'])
|
2019-04-22 14:05:58 +00:00
|
|
|
->setName('extractors');
|
2020-10-22 20:39:09 +00:00
|
|
|
$router->map(['GET'], '/locale', [$frontController, 'locale'])
|
2019-04-22 14:05:58 +00:00
|
|
|
->setName('locale');
|
2020-10-22 20:39:09 +00:00
|
|
|
$router->map(['GET'], '/redirect', [$downloadController, 'download'])
|
2019-04-22 14:05:58 +00:00
|
|
|
->setName('download');
|
2020-12-17 21:30:19 +00:00
|
|
|
|
|
|
|
/** @var Smarty $view */
|
|
|
|
$view = $this->container->get('view');
|
|
|
|
|
|
|
|
// Make sure we start the tests without compiled templates.
|
|
|
|
$view->getSmarty()->clearCompiledTemplate();
|
2019-04-22 14:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
protected function getRequestResult(string $request, array $params): Response
|
2019-04-22 14:05:58 +00:00
|
|
|
{
|
|
|
|
return $this->controller->$request(
|
2020-10-22 20:39:09 +00:00
|
|
|
$this->container->get('request')->withQueryParams($params),
|
|
|
|
$this->container->get('response')
|
2019-04-22 14:05:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2020-09-27 17:56:19 +00:00
|
|
|
protected function assertRequestIsOk(string $request, array $params = [])
|
2019-04-22 14:05:58 +00:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
2020-09-27 17:56:19 +00:00
|
|
|
protected function assertRequestIsRedirect(string $request, array $params = [])
|
2019-04-22 14:05:58 +00:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
2020-09-27 17:56:19 +00:00
|
|
|
protected function assertRequestIsServerError(string $request, array $params = [])
|
2019-04-22 14:05:58 +00:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
2020-09-27 17:56:19 +00:00
|
|
|
protected function assertRequestIsClientError(string $request, array $params = [])
|
2019-04-22 14:05:58 +00:00
|
|
|
{
|
|
|
|
$this->assertTrue($this->getRequestResult($request, $params)->isClientError());
|
|
|
|
}
|
|
|
|
}
|