* [ADD] Unit testing. Work in progress

* [ADD] Integration tests for API module. Work in progress
* [MOD] Code refactoring
This commit is contained in:
nuxsmin
2018-08-27 11:06:07 +02:00
parent 3d437d8348
commit 330e85fc07
33 changed files with 1533 additions and 235 deletions

View File

@@ -30,6 +30,7 @@ use Psr\Container\ContainerInterface;
use SP\Core\Context\StatelessContext;
use SP\Core\Events\EventDispatcher;
use SP\Core\Exceptions\SPException;
use SP\Http\Json;
use SP\Services\Api\ApiResponse;
use SP\Services\Api\ApiService;
use SP\Services\Api\JsonRpcResponse;
@@ -145,25 +146,30 @@ abstract class ControllerBase
throw new SPException(__u('Acceso no permitido'));
}
$this->router->response()->headers()->set('Content-type', 'application/json; charset=utf-8');
$this->router->response()->send(true);
echo JsonRpcResponse::getResponse($apiResponse, $this->apiService->getRequestId());
$this->sendJsonResponse(JsonRpcResponse::getResponse($apiResponse, $this->apiService->getRequestId()));
} catch (SPException $e) {
processException($e);
echo JsonRpcResponse::getResponseException($e, $this->apiService->getRequestId());
$this->returnResponseException($e);
}
}
/**
* Returns a JSON response back to the browser
*
* @param string $response
*/
final private function sendJsonResponse(string $response)
{
$json = Json::factory($this->router->response());
$json->returnRawJson($response);
}
/**
* @param \Exception $e
*/
final protected function returnResponseException(\Exception $e)
{
$this->router->response()->headers()->set('Content-type', 'application/json; charset=utf-8');
$this->router->response()->send(true);
echo JsonRpcResponse::getResponseException($e, $this->apiService->getRequestId());
$this->sendJsonResponse(JsonRpcResponse::getResponseException($e, $this->apiService->getRequestId()));
}
}