Skip to content
Snippets Groups Projects
Commit 8c46310d authored by Florian Azizian's avatar Florian Azizian
Browse files

refactoring rookies + debut conf sonar

parent 4ac8fa0c
No related branches found
No related tags found
No related merge requests found
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
"require": { "require": {
"slim/slim": "^3", "slim/slim": "^3",
"respect/validation": "^1.1", "respect/validation": "^1.1",
"guzzlehttp/guzzle": "~6.0" "guzzlehttp/guzzle": "^6.2",
"m4tthumphrey/php-gitlab-api": "dev-master" "m4tthumphrey/php-gitlab-api": "dev-master"
} }
} }
<?php <?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/
/**
* @brief Parameters Controller
* @author dev@maarch.org
* @ingroup core
*/
namespace Core\Controllers; namespace Core\Controllers;
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\RequestInterface;
...@@ -28,15 +41,15 @@ use Core\Models\ParametersModel; ...@@ -28,15 +41,15 @@ use Core\Models\ParametersModel;
} }
public function create(RequestInterface $request, ResponseInterface $response) public function create(RequestInterface $request, ResponseInterface $response)
{ {
$datas = $request->getParams(); $errors = $this->control($request, 'create');
$errors = $this->control($request, 'create',$datas);
if (!empty($errors)) { if (!empty($errors)) {
return $response return $response
->withJson(['errors' => $errors]); ->withJson(['errors' => $errors]);
} }
$datas = $request->getQueryParams();
$return = ParametersModel::create($datas); $return = ParametersModel::create($datas);
if ($return) { if ($return) {
...@@ -52,18 +65,17 @@ use Core\Models\ParametersModel; ...@@ -52,18 +65,17 @@ use Core\Models\ParametersModel;
return $response->withJson($obj); return $response->withJson($obj);
} }
public function update(RequestInterface $request, ResponseInterface $response, $aArgs) public function update(RequestInterface $request, ResponseInterface $response)
{ {
$datas = $request->getParams(); $errors = $this->control($request, 'update');
$errors = $this->control($request, 'update',$datas);
if (!empty($errors)) { if (!empty($errors)) {
return $response return $response
->withJson(['errors' => $errors]); ->withJson(['errors' => $errors]);
} }
$return = ParametersModel::update($datas); $aArgs = $request->getQueryParams();
$return = ParametersModel::update($aArgs);
if ($return) { if ($return) {
$obj = ParametersModel::getById([ $obj = ParametersModel::getById([
...@@ -84,14 +96,14 @@ use Core\Models\ParametersModel; ...@@ -84,14 +96,14 @@ use Core\Models\ParametersModel;
return $response->withJson($obj); return $response->withJson($obj);
} }
protected function control($request, $mode, $aArgs) protected function control($request, $mode)
{ {
$errors = []; $errors = [];
if ($mode == 'update') { if ($mode == 'update') {
$obj = ParametersModel::getById([ $obj = ParametersModel::getById([
'id' => $aArgs['id'], 'id' => $request->getParam('id'),
'param_value_int' => $aArgs['param_value_int'] 'param_value_int' => $request->getParam('param_value_int')
]); ]);
if (empty($obj)) { if (empty($obj)) {
array_push( array_push(
...@@ -101,7 +113,7 @@ use Core\Models\ParametersModel; ...@@ -101,7 +113,7 @@ use Core\Models\ParametersModel;
} }
} }
if (!Validator::notEmpty()->validate($aArgs['id'])) { if (!Validator::notEmpty()->validate($request->getParam('id'))) {
array_push($errors, '_ID_IS_EMPTY_CONTROLLER'); array_push($errors, '_ID_IS_EMPTY_CONTROLLER');
} elseif ($mode == 'create') { } elseif ($mode == 'create') {
if(!Validator::regex('/^[\w.-]*$/')->validate($request->getParam('id'))){ if(!Validator::regex('/^[\w.-]*$/')->validate($request->getParam('id'))){
...@@ -114,7 +126,7 @@ use Core\Models\ParametersModel; ...@@ -114,7 +126,7 @@ use Core\Models\ParametersModel;
array_push($errors,'PARAM STRING INVALIDE'); array_push($errors,'PARAM STRING INVALIDE');
} }
$obj = ParametersModel::getById([ $obj = ParametersModel::getById([
'id' => $aArgs['id'] 'id' => $request->getParam('id')
]); ]);
if (!empty($obj)) { if (!empty($obj)) {
array_push( array_push(
...@@ -123,17 +135,17 @@ use Core\Models\ParametersModel; ...@@ -123,17 +135,17 @@ use Core\Models\ParametersModel;
); );
} }
} }
if ($aArgs['param_value_date']!=null) { if ($request->getParam('param_value_date')!=null) {
if (date('Y-m-d H:i:s', strtotime($aArgs['param_value_date'])) != $aArgs['param_value_date']) { if (date('Y-m-d H:i:s', strtotime($request->getParam('param_value_date'))) != $request->getParam('param_value_date')) {
array_push( array_push(
$errors, $errors,
'PARAMETRE DATE INVALIDE.' 'PARAMETRE DATE INVALIDE.'
); );
} }
} }
if ($mode=='create'&&!Validator::notEmpty()->validate($aArgs['param_value_int'])&& if ($mode=='create'&&!Validator::notEmpty()->validate($request->getParam('param_value_int'))&&
!Validator::notEmpty()->validate($aArgs['param_value_string'])&& !Validator::notEmpty()->validate($request->getParam('param_value_string'))&&
!Validator::notEmpty()->validate($aArgs['param_value_date']) !Validator::notEmpty()->validate($request->getParam('param_value_date'))
) { ) {
array_push($errors, '_PARAM_VALUE_IS_EMPTY'); array_push($errors, '_PARAM_VALUE_IS_EMPTY');
} }
...@@ -142,5 +154,3 @@ use Core\Models\ParametersModel; ...@@ -142,5 +154,3 @@ use Core\Models\ParametersModel;
} }
} }
?>
\ No newline at end of file
<?php <?php
namespace MaarchTest; /**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/
require_once __DIR__.'/define.php'; namespace MaarchTest;
class ParametersControllerTest extends \PHPUnit_Framework_TestCase require_once __DIR__.'/define.php';
{
public function testCreate() class ParametersControllerTest extends \PHPUnit_Framework_TestCase
{ {
$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,' public function testCreate()
. '"param_value_string":"abcd","param_value_int":null,"param_value_date":null}]'; {
$this->assertSame($compare,(string)$response->getBody()); $query = 'id=TEST&';
$query .= 'param_value_string=abcd&';
//TEST EXISTE DEJA $query .= 'description=papa';
$aArgs = [
'id'=> 'TEST', $environment = \Slim\Http\Environment::mock(
'description' => null, [
'param_value_string' => null, 'REQUEST_METHOD' => 'POST',
'param_value_int' => 1234, 'QUERY_STRING' => $query,
'param_value_date' => null ]
]; );
$client = new \GuzzleHttp\Client([ $parameter = new \Core\Controllers\ParametersController();
'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', $response = $parameter->create(\Slim\Http\Request::createFromEnvironment($environment), new \Slim\Http\Response());
// You can set any number of default request options.
'timeout' => 2.0, $compare = '[{"id":"TEST",'
]); .'"description":"papa",'
$response = $client->request('POST', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', [ .'"param_value_string":"abcd",'
'auth'=> ['superadmin','superadmin'], .'"param_value_int":null,'
'form_params' => $aArgs .'"param_value_date":null}]';
$this->assertSame($compare,(string)$response->getBody());
$client = new \GuzzleHttp\Client([
'base_uri' => $_SESSION['config']['coreurl'] . 'rest/parameters',
// You can set any number of default request options.
'timeout' => 2.0,
]); ]);
//TEST EXISTE DEJA
$aArgs = [
'id' => 'TEST',
'description' => null,
'param_value_string' => null,
'param_value_int' => 1234,
'param_value_date' => null
];
$response = $client->request('POST', $_SESSION['config']['coreurl'] . 'rest/parameters', [
'auth' => ['superadmin','superadmin'],
'form_params' => $aArgs
]);
$compare = '{"errors":["Identifiant TEST existe d\u00e9j\u00e0 !"]}';
$this->assertSame($compare,(string)$response->getBody());
$compare = '{"errors":["Identifiant TEST existe d\u00e9j\u00e0 !"]}'; //AUCUN PARAMETRE
$this->assertSame($compare,(string)$response->getBody()); $aArgs = [
'id' => 'TEST3',
//AUCUN PARAMETRE 'description' => null,
'param_value_string' => null,
$aArgs = [ 'param_value_int' => null,
'id'=> 'TEST3', 'param_value_date' => null
'description' => null, ];
'param_value_string' => null,
'param_value_int' => null, $response = $client->request('POST', $_SESSION['config']['coreurl'] . 'rest/parameters', [
'param_value_date' => null 'auth' => ['superadmin','superadmin'],
]; 'form_params' => $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":["_PARAM_VALUE_IS_EMPTY"]}'; $compare = '{"errors":["_PARAM_VALUE_IS_EMPTY"]}';
$this->assertSame($compare,(string)$response->getBody()); $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"]}'; //AUCUN ARGUMENTS
$aArgs = [ ];
$this->assertSame($compare,(string)$response->getBody()); $response = $client->request('POST', $_SESSION['config']['coreurl'] . 'rest/parameters', [
'auth' => ['superadmin','superadmin'],
'form_params' => $aArgs
]);
//DATE MAUVAIS FORMAT $compare = '{"errors":["_ID_IS_EMPTY_CONTROLLER","_PARAM_VALUE_IS_EMPTY"]}';
$aArgs = [ $this->assertSame($compare,(string)$response->getBody());
'id'=> 'TEST4',
'description' => null,
'param_value_string' => null,
'param_value_int' => null,
'param_value_date' => '123456'
];
$client = new \GuzzleHttp\Client([ //DATE MAUVAIS FORMAT
'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."]}'; $aArgs = [
$this->assertSame($compare,(string)$response->getBody()); 'id' => 'TEST4',
'description' => null,
'param_value_string' => null,
'param_value_int' => null,
'param_value_date' => '123456'
];
//TEST ID MAUVAIS FORMAT (REGEX) $response = $client->request('POST', $_SESSION['config']['coreurl'] . 'rest/parameters', [
'auth' => ['superadmin','superadmin'],
'form_params' => $aArgs
]);
$aArgs = [ $compare = '{"errors":["PARAMETRE DATE INVALIDE."]}';
'id'=> 'A*-#==', $this->assertSame($compare,(string)$response->getBody());
'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', [ //TEST ID MAUVAIS FORMAT (REGEX)
'auth'=> ['superadmin','superadmin'],
'form_params' => $aArgs
]);
$compare ='{"errors":["ID INVALIDE","DESCRIPTION INVALIDE","PARAM STRING INVALIDE"]}'; $aArgs = [
$this->assertSame($compare,(string)$response->getBody()); 'id' => 'A*-#==',
} 'description' => "*///*//",
'param_value_string' => "//-//**",
public function testGetList() '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('GET', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', [
'auth'=> ['superadmin','superadmin']
]);
$this->assertNotNull($response->getBody()); $response = $client->request('POST', $_SESSION['config']['coreurl'] . 'rest/parameters', [
} 'auth' => ['superadmin','superadmin'],
'form_params' => $aArgs
public function testGetById() ]);
{
$compare ='{"errors":["ID INVALIDE","DESCRIPTION INVALIDE","PARAM STRING INVALIDE"]}';
$aArgs = [ $this->assertSame($compare,(string)$response->getBody());
'id'=> 'TEST' }
];
public function testGetList()
$client = new \GuzzleHttp\Client([ {
'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', $parameters = new \Core\Controllers\ParametersController();
// You can set any number of default request options.
'timeout' => 2.0, $environment = \Slim\Http\Environment::mock(
]); [
$response = $client->request('GET', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters/'.$aArgs['id'], [ 'REQUEST_METHOD' => 'GET',
'auth'=> ['superadmin','superadmin'] ]
]); );
$compare = '[[{"id":"TEST",'
.'"description":null,' $request = \Slim\Http\Request::createFromEnvironment($environment);
.'"param_value_string":"abcd",' $response = new \Slim\Http\Response();
.'"param_value_int":null,' $response = $parameters->getList($request, $response);
.'"param_value_date":null}]]';
$this->assertNotNull((string)$response->getBody());
$this->assertNotNull((string)$response->getBody()); }
}
public function testGetById()
public function testUpdate() {
{
$aArgs = [
$aArgs = [ 'id' => 'TEST'
'id'=> 'TEST', ];
'description' =>'TEST AFTER UP'
]; $client = new \GuzzleHttp\Client([
'base_uri' => $_SESSION['config']['coreurl'] . 'rest/parameters',
$client = new \GuzzleHttp\Client([ // You can set any number of default request options.
'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', 'timeout' => 2.0,
// 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
]); ]);
$response = $client->request('GET', $_SESSION['config']['coreurl'] . '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()
{
$query = 'id=TEST&';
$query .= 'description=TEST AFTER UP';
$environment = \Slim\Http\Environment::mock(
[
'REQUEST_METHOD' => 'PUT',
'QUERY_STRING' => $query,
]
);
$parameter = new \Core\Controllers\ParametersController();
$response = $parameter->update(\Slim\Http\Request::createFromEnvironment($environment), new \Slim\Http\Response());
$compare = '[{"id":"TEST",' $compare = '[{"id":"TEST",'
.'"description":"TEST AFTER UP",' .'"description":"TEST AFTER UP",'
.'"param_value_string":"abcd",' .'"param_value_string":"abcd",'
.'"param_value_int":null,' .'"param_value_int":null,'
.'"param_value_date":null}]'; .'"param_value_date":null}]';
$this->assertSame($compare,(string)$response->getBody()); $this->assertSame((string)$response->getBody(), $compare);
//TEST ID NULL //TEST ID NULL
$aArgs = [
'id'=> 'NEW' $client = new \GuzzleHttp\Client([
]; 'base_uri' => $_SESSION['config']['coreurl'] . 'rest/parameters',
// You can set any number of default request options.
$client = new \GuzzleHttp\Client([ 'timeout' => 2.0,
'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"]}'; $aArgs = [
'id' => 'NEW'
$this->assertSame($compare,(string)$response->getBody()); ];
$response = $client->request('PUT', $_SESSION['config']['coreurl'] . '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 = [ public function testDelete()
'id'=> 'TEST' {
]; $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. $client = new \GuzzleHttp\Client([
'timeout' => 2.0, 'base_uri' => $_SESSION['config']['coreurl'] . 'rest/parameters',
]); // You can set any number of default request options.
$response = $client->request('DELETE', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters/'.$aArgs['id'], [ 'timeout' => 2.0,
'auth'=> ['superadmin','superadmin'],
'form_params' => $aArgs
]); ]);
$compare='true';
$this->assertSame($compare,(string)$response->getBody());
}
$response = $client->request('DELETE', $_SESSION['config']['coreurl'] . 'rest/parameters/'.$aArgs['id'], [
'auth' => ['superadmin','superadmin'],
'form_params' => $aArgs
]);
$compare = 'true';
$this->assertSame($compare,(string)$response->getBody());
} }
}
?> ?>
\ No newline at end of file
...@@ -57,6 +57,10 @@ Core_CoreConfig_Service::loadModulesConfig($_SESSION['modules']); ...@@ -57,6 +57,10 @@ Core_CoreConfig_Service::loadModulesConfig($_SESSION['modules']);
Core_CoreConfig_Service::loadAppServices(); Core_CoreConfig_Service::loadAppServices();
Core_CoreConfig_Service::loadModulesServices($_SESSION['modules']); Core_CoreConfig_Service::loadModulesServices($_SESSION['modules']);
$folderRootName = str_replace("\\","/", dirname(__file__));
$folderRootName = str_replace('/core/Test', '', $folderRootName);
$folderRootName = substr($folderRootName, strrpos($folderRootName, "/")+1);
$_SESSION['config']['coreurl'] = 'http://localhost/' . $folderRootName . '/';
//login management //login management
require_once('apps/maarch_entreprise/class/class_login.php'); require_once('apps/maarch_entreprise/class/class_login.php');
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
</filter> </filter>
<logging> <logging>
<log type="coverage-clover" target="core/Test/build/tests-clover.xml"/> <log type="coverage-clover" target="core/Test/build/tests-clover.xml"/>
<log type="junit" target="core/Test/build/tests-phpunit.xml" logIncompleteSkipped="false"/>
<log type="coverage-html" target="core/Test/build" lowUpperBound="35" highLowerBound="70"/> <log type="coverage-html" target="core/Test/build" lowUpperBound="35" highLowerBound="70"/>
</logging> </logging>
</phpunit> </phpunit>
......
# must be unique in a given SonarQube instance
sonar.projectKey=my:project
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=Maarch 17.XX Rookies
sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set.
sonar.sources=.
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8
sonar.exclusions=node_modules/**/* , vendor/**/* , custom/**/* , sql/** , apps/maarch_entreprise/tools/**/* , core/Test/**/*
sonar.tests=core/Test/build
sonar.php.tests.reportPath=core/Test/build/tests-phpunit.xml
sonar.php.coverage.reportPath=core/Test/build/tests-clover.xml
#sonar.coverage.exclusions=apps/maarch_entreprise/**/*.php , core/**/*.php , modules/**/*.php
\ No newline at end of file
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