From 8c46310d11ed5cc9ff41b55184eb569e3a562c1e Mon Sep 17 00:00:00 2001 From: "florian.azizian" <florian.azizian@maarch.org> Date: Thu, 8 Jun 2017 22:58:30 +0100 Subject: [PATCH] refactoring rookies + debut conf sonar --- composer.json | 2 +- core/Controllers/ParametersController.php | 50 +-- core/Test/ParametersControllerTest.php | 418 +++++++++++----------- core/Test/define.php | 4 + phpunit.xml | 1 + sonar-project.properties | 19 + 6 files changed, 261 insertions(+), 233 deletions(-) create mode 100644 sonar-project.properties diff --git a/composer.json b/composer.json index ec8f292af45..be952096a91 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "require": { "slim/slim": "^3", "respect/validation": "^1.1", - "guzzlehttp/guzzle": "~6.0" + "guzzlehttp/guzzle": "^6.2", "m4tthumphrey/php-gitlab-api": "dev-master" } } diff --git a/core/Controllers/ParametersController.php b/core/Controllers/ParametersController.php index 48d220ff5ae..5db278d9346 100644 --- a/core/Controllers/ParametersController.php +++ b/core/Controllers/ParametersController.php @@ -1,5 +1,18 @@ <?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; use Psr\Http\Message\RequestInterface; @@ -28,15 +41,15 @@ use Core\Models\ParametersModel; } public function create(RequestInterface $request, ResponseInterface $response) { - $datas = $request->getParams(); - - $errors = $this->control($request, 'create',$datas); + $errors = $this->control($request, 'create'); if (!empty($errors)) { return $response ->withJson(['errors' => $errors]); } + $datas = $request->getQueryParams(); + $return = ParametersModel::create($datas); if ($return) { @@ -52,18 +65,17 @@ use Core\Models\ParametersModel; 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',$datas); + $errors = $this->control($request, 'update'); if (!empty($errors)) { return $response ->withJson(['errors' => $errors]); } - $return = ParametersModel::update($datas); + $aArgs = $request->getQueryParams(); + $return = ParametersModel::update($aArgs); if ($return) { $obj = ParametersModel::getById([ @@ -84,14 +96,14 @@ use Core\Models\ParametersModel; return $response->withJson($obj); } - protected function control($request, $mode, $aArgs) + protected function control($request, $mode) { $errors = []; if ($mode == 'update') { $obj = ParametersModel::getById([ - 'id' => $aArgs['id'], - 'param_value_int' => $aArgs['param_value_int'] + 'id' => $request->getParam('id'), + 'param_value_int' => $request->getParam('param_value_int') ]); if (empty($obj)) { array_push( @@ -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'); } elseif ($mode == 'create') { if(!Validator::regex('/^[\w.-]*$/')->validate($request->getParam('id'))){ @@ -114,7 +126,7 @@ use Core\Models\ParametersModel; array_push($errors,'PARAM STRING INVALIDE'); } $obj = ParametersModel::getById([ - 'id' => $aArgs['id'] + 'id' => $request->getParam('id') ]); if (!empty($obj)) { array_push( @@ -123,17 +135,17 @@ use Core\Models\ParametersModel; ); } } - if ($aArgs['param_value_date']!=null) { - if (date('Y-m-d H:i:s', strtotime($aArgs['param_value_date'])) != $aArgs['param_value_date']) { + if ($request->getParam('param_value_date')!=null) { + if (date('Y-m-d H:i:s', strtotime($request->getParam('param_value_date'))) != $request->getParam('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']) + if ($mode=='create'&&!Validator::notEmpty()->validate($request->getParam('param_value_int'))&& + !Validator::notEmpty()->validate($request->getParam('param_value_string'))&& + !Validator::notEmpty()->validate($request->getParam('param_value_date')) ) { array_push($errors, '_PARAM_VALUE_IS_EMPTY'); } @@ -142,5 +154,3 @@ use Core\Models\ParametersModel; } } - -?> \ No newline at end of file diff --git a/core/Test/ParametersControllerTest.php b/core/Test/ParametersControllerTest.php index a60a81d1ec0..46b8c4c7b98 100644 --- a/core/Test/ParametersControllerTest.php +++ b/core/Test/ParametersControllerTest.php @@ -1,248 +1,242 @@ <?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() - { - - $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 - ]); +class ParametersControllerTest extends \PHPUnit_Framework_TestCase +{ - $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 + public function testCreate() + { + $query = 'id=TEST&'; + $query .= 'param_value_string=abcd&'; + $query .= 'description=papa'; + + $environment = \Slim\Http\Environment::mock( + [ + 'REQUEST_METHOD' => 'POST', + 'QUERY_STRING' => $query, + ] + ); + + $parameter = new \Core\Controllers\ParametersController(); + $response = $parameter->create(\Slim\Http\Request::createFromEnvironment($environment), new \Slim\Http\Response()); + + $compare = '[{"id":"TEST",' + .'"description":"papa",' + .'"param_value_string":"abcd",' + .'"param_value_int":null,' + .'"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 !"]}'; - - $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 - ]); + //AUCUN PARAMETRE + + $aArgs = [ + 'id' => 'TEST3', + 'description' => null, + 'param_value_string' => null, + 'param_value_int' => null, + 'param_value_date' => null + ]; + + $response = $client->request('POST', $_SESSION['config']['coreurl'] . '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":["_PARAM_VALUE_IS_EMPTY"]}'; + + $this->assertSame($compare,(string)$response->getBody()); - $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 = [ - 'id'=> 'TEST4', - 'description' => null, - 'param_value_string' => null, - 'param_value_int' => null, - 'param_value_date' => '123456' - ]; + $this->assertSame($compare,(string)$response->getBody()); - $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 - ]); + //DATE MAUVAIS FORMAT - $compare ='{"errors":["PARAMETRE DATE INVALIDE."]}'; - $this->assertSame($compare,(string)$response->getBody()); + $aArgs = [ + '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 = [ - 'id'=> 'A*-#==', - 'description' => "*///*//", - 'param_value_string' => "//-//**", - 'param_value_int' => null, - 'param_value_date' => null - ]; + $compare = '{"errors":["PARAMETRE DATE INVALIDE."]}'; + $this->assertSame($compare,(string)$response->getBody()); - $response = $client->request('POST', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', [ - 'auth'=> ['superadmin','superadmin'], - 'form_params' => $aArgs - ]); + //TEST ID MAUVAIS FORMAT (REGEX) - $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'] - ]); + $aArgs = [ + 'id' => 'A*-#==', + 'description' => "*///*//", + 'param_value_string' => "//-//**", + 'param_value_int' => null, + 'param_value_date' => null + ]; - $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 + $response = $client->request('POST', $_SESSION['config']['coreurl'] . '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() + { + $parameters = new \Core\Controllers\ParametersController(); + + $environment = \Slim\Http\Environment::mock( + [ + 'REQUEST_METHOD' => 'GET', + ] + ); + + $request = \Slim\Http\Request::createFromEnvironment($environment); + $response = new \Slim\Http\Response(); + $response = $parameters->getList($request, $response); + + $this->assertNotNull((string)$response->getBody()); + } + + public function testGetById() + { + + $aArgs = [ + 'id' => 'TEST' + ]; + + $client = new \GuzzleHttp\Client([ + 'base_uri' => $_SESSION['config']['coreurl'] . 'rest/parameters', + // You can set any number of default request options. + 'timeout' => 2.0, ]); + $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",' .'"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 + + $this->assertSame((string)$response->getBody(), $compare); + + //TEST ID NULL + + $client = new \GuzzleHttp\Client([ + 'base_uri' => $_SESSION['config']['coreurl'] . 'rest/parameters', + // You can set any number of default request options. + 'timeout' => 2.0, ]); - $compare = '{"errors":["Identifiant n\'existe pas"]}'; - - $this->assertSame($compare,(string)$response->getBody()); + $aArgs = [ + 'id' => 'NEW' + ]; + $response = $client->request('PUT', $_SESSION['config']['coreurl'] . 'rest/parameters/'.$aArgs['id'], [ + 'auth' => ['superadmin','superadmin'], + 'form_params' => $aArgs + ]); + $compare = '{"errors":["Identifiant n\'existe pas"]}'; - } - - 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 + $this->assertSame($compare,(string)$response->getBody()); + + } + + public function testDelete() + { + $aArgs = [ + 'id'=> 'TEST' + ]; + + $client = new \GuzzleHttp\Client([ + 'base_uri' => $_SESSION['config']['coreurl'] . 'rest/parameters', + // You can set any number of default request options. + 'timeout' => 2.0, ]); - $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 diff --git a/core/Test/define.php b/core/Test/define.php index 062c2701e2a..5df2656aca3 100644 --- a/core/Test/define.php +++ b/core/Test/define.php @@ -57,6 +57,10 @@ Core_CoreConfig_Service::loadModulesConfig($_SESSION['modules']); Core_CoreConfig_Service::loadAppServices(); 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 require_once('apps/maarch_entreprise/class/class_login.php'); diff --git a/phpunit.xml b/phpunit.xml index 02166e80857..8d9736dfaa1 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -14,6 +14,7 @@ </filter> <logging> <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"/> </logging> </phpunit> diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 00000000000..4a75593e4f3 --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,19 @@ +# 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 -- GitLab