Skip to content
Snippets Groups Projects
Commit 07d03dad authored by Pegane Nestor's avatar Pegane Nestor
Browse files

Parameters controllers and models

parent 104b118b
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,7 @@
},
"require": {
"slim/slim": "^3",
"respect/validation": "^1.1"
"respect/validation": "^1.1",
"guzzlehttp/guzzle": "~6.0"
}
}
<?php
namespace Core\Controllers;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Respect\Validation\Validator;
use Core\Models\ParametersModel;
require_once 'core/class/class_db_pdo.php';
require_once 'modules/notes/Models/NotesModel.php';
class ParametersController
{
public function getList(RequestInterface $request, ResponseInterface $response)
{
$obj = ParametersModel::getList();
return $response->withJson($obj);
}
public function getById(RequestInterface $request, ResponseInterface $response, $aArgs)
{
$obj = ParametersModel::getById(['id' => $aArgs['id']]);
return $response->withJson($obj);
}
public function create(RequestInterface $request, ResponseInterface $response)
{
$datas = $request->getParams();
$errors = $this->control($request, 'create',$datas);
if (!empty($errors)) {
return $response
->withJson(['errors' => $errors]);
}
$return = ParametersModel::create($datas);
if ($return) {
$obj = ParametersModel::getById([
'id' => $datas['id']
]);
} else {
return $response
->withStatus(500)
->withJson(['errors' => _NOT_CREATE]);
}
return $response->withJson($obj);
}
public function update(RequestInterface $request, ResponseInterface $response, $aArgs)
{
$datas = $request->getParams();
$errors = $this->control($request, 'update',$datas);
if (!empty($errors)) {
return $response
->withJson(['errors' => $errors]);
}
$return = ParametersModel::update($datas);
if ($return) {
$obj = ParametersModel::getById([
'id' => $aArgs['id']
]);
} else {
return $response
->withStatus(500)
->withJson(['errors' => _NOT_UPDATE]);
}
return $response->withJson($obj);
}
public function delete(RequestInterface $request, ResponseInterface $response, $aArgs)
{
$obj = ParametersModel::delete(['id' => $aArgs['id']]);
return $response->withJson($obj);
}
protected function control($request, $mode, $aArgs)
{
$errors = [];
if ($mode == 'update') {
$obj = ParametersModel::getById([
'id' => $aArgs['id'],
'param_value_int' => $aArgs['param_value_int']
]);
if (empty($obj)) {
array_push(
$errors,
_ID . ' '. _NOT_EXISTS
);
}
}
if (!Validator::notEmpty()->validate($aArgs['id'])) {
array_push($errors, '_ID_IS_EMPTY_CONTROLLER');
} elseif ($mode == 'create') {
if(!Validator::regex('/^[\w.-]*$/')->validate($request->getParam('id'))){
array_push($errors,'ID INVALIDE');
}
if(!Validator::regex('/^[\w.-]*$/')->validate($request->getParam('description'))&&$request->getParam('description')!=null){
array_push($errors,'DESCRIPTION INVALIDE');
}
if (!Validator::regex('/^[\w.-]*$/')->validate($request->getParam('param_value_string'))&&$request->getParam('param_value_string')!=null) {
array_push($errors,'PARAM STRING INVALIDE');
}
$obj = ParametersModel::getById([
'id' => $aArgs['id']
]);
if (!empty($obj)) {
array_push(
$errors,
_ID . ' ' . $obj[0]['id'] . ' ' . _ALREADY_EXISTS
);
}
}
if ($aArgs['param_value_date']!=null) {
if (date('Y-m-d H:i:s', strtotime($aArgs['param_value_date'])) != $aArgs['param_value_date']) {
array_push(
$errors,
'PARAMETRE DATE INVALIDE.'
);
}
}
if ($mode=='create'&&!Validator::notEmpty()->validate($aArgs['param_value_int'])&&
!Validator::notEmpty()->validate($aArgs['param_value_string'])&&
!Validator::notEmpty()->validate($aArgs['param_value_date'])
) {
array_push($errors, '_PARAM_VALUE_IS_EMPTY');
}
return $errors;
}
}
?>
\ No newline at end of file
<?php
namespace Core\Models;
/*
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Respect\Validation\Validator;
use Core\Models\DocserverModel;
use Core\Models\DocserverTypeModel;
use Core\Models\UserModel;
use Core\Models\ParameterModel;
use Entities\Models\EntitiesModel;
use Core\Controllers\DocserverController;
use Core\Controllers\DocserverToolsController;
*/
use Core\Models\ParametersModelAbstract;
require_once 'core/class/class_db_pdo.php';
require_once 'modules/notes/Models/NotesModel.php';
class ParametersModel extends ParametersModelAbstract
{
}
?>
\ No newline at end of file
<?php
namespace Core\Models;
require_once 'apps/maarch_entreprise/services/Table.php';
class ParametersModelAbstract extends \Apps_Table_Service
{
public static function getList()
{
$aReturn = static::select([
'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
'table' => ['parameters'],
]);
return $aReturn;
}
public static function getById(array $aArgs = [])
{
static::checkRequired($aArgs, ['id']);
static::checkString($aArgs,['id']);
$aReturn = static::select([
'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
'table' =>['parameters'],
'where' => ['id = ?'],
'data' => [$aArgs['id']]
]);
return $aReturn;
}
public static function create(array $aArgs = [])
{
static::checkRequired($aArgs, ['id']);
static::checkString($aArgs, ['id']);
$aReturn = static::insertInto($aArgs, 'parameters');
return $aReturn;
}
public static function update(array $aArgs = [])
{
static::checkRequired($aArgs, ['id']);
static::checkString($aArgs, ['id']);
$where['id'] = $aArgs['id'];
$aReturn = static::updateTable(
$aArgs,
'parameters',
$where
);
return $aReturn;
}
public static function delete(array $aArgs = [])
{
static::checkRequired($aArgs, ['id']);
static::checkString($aArgs, ['id']);
$aReturn = static::deleteFrom([
'table' => 'parameters',
'where' => ['id = ?'],
'data' => [$aArgs['id']]
]);
return $aReturn;
}
}
?>
\ No newline at end of file
<?php
namespace MaarchTest;
require_once __DIR__.'/define.php';
class ParametersControllerTest extends \PHPUnit_Framework_TestCase
{
public function testCreate()
{
$aArgs = [
'id'=> 'TEST',
'description' => null,
'param_value_string' => 'abcd',
'param_value_int' => null,
'param_value_date' => null
];
$client = new \GuzzleHttp\Client([
'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters',
// You can set any number of default request options.
'timeout' => 2.0,
]);
$response = $client->request('POST', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', [
'auth'=> ['superadmin','superadmin'],
'form_params' => $aArgs
]);
$compare = '[{"id":"TEST","description":null,'
. '"param_value_string":"abcd","param_value_int":null,"param_value_date":null}]';
$this->assertSame($compare,(string)$response->getBody());
//TEST EXISTE DEJA
$aArgs = [
'id'=> 'TEST',
'description' => null,
'param_value_string' => null,
'param_value_int' => 1234,
'param_value_date' => null
];
$client = new \GuzzleHttp\Client([
'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters',
// You can set any number of default request options.
'timeout' => 2.0,
]);
$response = $client->request('POST', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', [
'auth'=> ['superadmin','superadmin'],
'form_params' => $aArgs
]);
$compare = '{"errors":["Identifiant TEST existe d\u00e9j\u00e0 !"]}';
$this->assertSame($compare,(string)$response->getBody());
//AUCUN PARAMETRE
$aArgs = [
'id'=> 'TEST3',
'description' => null,
'param_value_string' => null,
'param_value_int' => null,
'param_value_date' => null
];
$client = new \GuzzleHttp\Client([
'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters',
// You can set any number of default request options.
'timeout' => 2.0,
]);
$response = $client->request('POST', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', [
'auth'=> ['superadmin','superadmin'],
'form_params' => $aArgs
]);
$compare = '{"errors":["_PARAM_VALUE_IS_EMPTY"]}';
$this->assertSame($compare,(string)$response->getBody());
//AUCUN ARGUMENTS
$aArgs = [ ];
$client = new \GuzzleHttp\Client([
'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters',
// You can set any number of default request options.
'timeout' => 2.0,
]);
$response = $client->request('POST', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', [
'auth'=> ['superadmin','superadmin'],
'form_params' => $aArgs
]);
$compare = '{"errors":["_ID_IS_EMPTY_CONTROLLER","_PARAM_VALUE_IS_EMPTY"]}';
$this->assertSame($compare,(string)$response->getBody());
//DATE MAUVAIS FORMAT
$aArgs = [
'id'=> 'TEST4',
'description' => null,
'param_value_string' => null,
'param_value_int' => null,
'param_value_date' => '123456'
];
$client = new \GuzzleHttp\Client([
'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters',
// You can set any number of default request options.
'timeout' => 2.0,
]);
$response = $client->request('POST', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', [
'auth'=> ['superadmin','superadmin'],
'form_params' => $aArgs
]);
$compare ='{"errors":["PARAMETRE DATE INVALIDE."]}';
$this->assertSame($compare,(string)$response->getBody());
//TEST ID MAUVAIS FORMAT (REGEX)
$aArgs = [
'id'=> 'A*-#==',
'description' => "*///*//",
'param_value_string' => "//-//**",
'param_value_int' => null,
'param_value_date' => null
];
$response = $client->request('POST', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', [
'auth'=> ['superadmin','superadmin'],
'form_params' => $aArgs
]);
$compare ='{"errors":["ID INVALIDE","DESCRIPTION INVALIDE","PARAM STRING INVALIDE"]}';
$this->assertSame($compare,(string)$response->getBody());
}
public function testGetList()
{
$client = new \GuzzleHttp\Client([
'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters',
// You can set any number of default request options.
'timeout' => 2.0,
]);
$response = $client->request('GET', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', [
'auth'=> ['superadmin','superadmin']
]);
$this->assertNotNull($response->getBody());
}
public function testGetById()
{
$aArgs = [
'id'=> 'TEST'
];
$client = new \GuzzleHttp\Client([
'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters',
// You can set any number of default request options.
'timeout' => 2.0,
]);
$response = $client->request('GET', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters/'.$aArgs['id'], [
'auth'=> ['superadmin','superadmin']
]);
$compare = '[[{"id":"TEST",'
.'"description":null,'
.'"param_value_string":"abcd",'
.'"param_value_int":null,'
.'"param_value_date":null}]]';
$this->assertNotNull((string)$response->getBody());
}
public function testUpdate()
{
$aArgs = [
'id'=> 'TEST',
'description' =>'TEST AFTER UP'
];
$client = new \GuzzleHttp\Client([
'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters',
// You can set any number of default request options.
'timeout' => 2.0,
]);
$response = $client->request('PUT', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters/'.$aArgs['id'], [
'auth'=> ['superadmin','superadmin'],
'form_params' => $aArgs
]);
$compare = '[{"id":"TEST",'
.'"description":"TEST AFTER UP",'
.'"param_value_string":"abcd",'
.'"param_value_int":null,'
.'"param_value_date":null}]';
$this->assertSame($compare,(string)$response->getBody());
//TEST ID NULL
$aArgs = [
'id'=> 'NEW'
];
$client = new \GuzzleHttp\Client([
'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters',
// You can set any number of default request options.
'timeout' => 2.0,
]);
$response = $client->request('PUT', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters/'.$aArgs['id'], [
'auth'=> ['superadmin','superadmin'],
'form_params' => $aArgs
]);
$compare = '{"errors":["Identifiant n\'existe pas"]}';
$this->assertSame($compare,(string)$response->getBody());
}
public function testDelete()
{
$aArgs = [
'id'=> 'TEST'
];
$client = new \GuzzleHttp\Client([
'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters',
// You can set any number of default request options.
'timeout' => 2.0,
]);
$response = $client->request('DELETE', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters/'.$aArgs['id'], [
'auth'=> ['superadmin','superadmin'],
'form_params' => $aArgs
]);
$compare='true';
$this->assertSame($compare,(string)$response->getBody());
}
}
?>
\ No newline at end of file
......@@ -134,4 +134,11 @@ $app->post('/resExt', \Core\Controllers\ResExtController::class . ':create');
//Users
$app->get('/user/profile', \Core\Controllers\UserController::class . ':getCurrentUserInfos');
//parameters
$app->get('/parameters', \Core\Controllers\ParametersController::class . ':getList');
$app->get('/parameters/{id}', \Core\Controllers\ParametersController::class . ':getById');
$app->post('/parameters', \Core\Controllers\ParametersController::class . ':create');
$app->put('/parameters/{id}', \Core\Controllers\ParametersController::class . ':update');
$app->delete('/parameters/{id}', \Core\Controllers\ParametersController::class . ':delete');
$app->run();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment