From 0625782c4678394730805be2e37d866699f227f7 Mon Sep 17 00:00:00 2001 From: Benoit Foujols Date: Mon, 27 Mar 2023 12:10:38 +0200 Subject: [PATCH 1/6] Fix Code Style #21 --- src/Service/Request.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Service/Request.php b/src/Service/Request.php index 4cef2c9..efc70e2 100644 --- a/src/Service/Request.php +++ b/src/Service/Request.php @@ -19,13 +19,20 @@ class Request { private string $basePath; + private string $version; + private int $timeout; + private int $connectTimeout; + private bool $verify; + private bool $debug; + private array $headers; + public function __construct(array $config = []) { $this->basePath = $config['base_path']; @@ -49,7 +56,8 @@ public function __construct(array $config = []) public function query( string $methode, string $path, - array $query = ['body' => 'data={}', + array $query = [ + 'body' => 'data={}', 'headers' => [ 'Content-Type' => 'text/plain', ] @@ -59,12 +67,12 @@ public function query( $this->headers = array_merge($this->headers, $query['headers']); $client = new \GuzzleHttp\Client([ - 'base_uri' => $this->basePath . '/' . $this->version . '/', - 'timeout' => $this->timeout, + 'base_uri' => $this->basePath . '/' . $this->version . '/', + 'timeout' => $this->timeout, 'connect_timeout' => $this->connectTimeout, - 'verify' => $this->verify, - 'debug' => $this->debug, - 'headers' => $this->headers, + 'verify' => $this->verify, + 'debug' => $this->debug, + 'headers' => $this->headers, ]); return $client->request($methode, $path, $query); From 2086bc31e44c877d9ffec1fae616a44b958450d3 Mon Sep 17 00:00:00 2001 From: Benoit Foujols Date: Mon, 27 Mar 2023 12:24:38 +0200 Subject: [PATCH 2/6] Fix Code Style #21 --- src/Client.php | 14 ++++++++++++-- src/Entity/Login.php | 8 ++++++-- src/Entity/Viescolaire.php | 1 - 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Client.php b/src/Client.php index 9bc4adf..c943809 100644 --- a/src/Client.php +++ b/src/Client.php @@ -27,8 +27,16 @@ class Client private const API_VERSION = 'v3'; - private array $config = []; + /** + * Configuration de l'API + * @var array + */ + private array $config; + /** + * Objet Login contenant les informations de connexion et de l'utilisateur + * @var Login + */ private Login $login; @@ -49,7 +57,9 @@ public function __construct(array $config = []) 'Content-Type' => 'text/plain', ], ], $config); - } + + + } //end __construct() /** * Accès à l'API EcoleDirecte avec les identifiants de l'utilisateur diff --git a/src/Entity/Login.php b/src/Entity/Login.php index 764177f..5aa8e25 100644 --- a/src/Entity/Login.php +++ b/src/Entity/Login.php @@ -59,10 +59,15 @@ class Login private string $email; /** - * Nom et Prenom de l'utilisateur + * Nom et de l'utilisateur * @var string */ private string $nom; + + /** + * Prenom de l'utilisateur + * @var string + */ private string $prenom; /** @@ -83,7 +88,6 @@ class Login */ private array $profile; - public function getToken(): string { return $this->token; diff --git a/src/Entity/Viescolaire.php b/src/Entity/Viescolaire.php index bc0cc63..872a11a 100644 --- a/src/Entity/Viescolaire.php +++ b/src/Entity/Viescolaire.php @@ -31,7 +31,6 @@ class Viescolaire */ private array $parametrage; - /** * Retourne la liste des absences et retards * @return array From 57f0ebfd81da51e1af40528502105217e7b72451 Mon Sep 17 00:00:00 2001 From: Benoit Foujols Date: Mon, 27 Mar 2023 12:38:27 +0200 Subject: [PATCH 3/6] Fix Code Style #21 --- src/Core/BuildEntiy.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Core/BuildEntiy.php b/src/Core/BuildEntiy.php index 236940b..ad13c66 100644 --- a/src/Core/BuildEntiy.php +++ b/src/Core/BuildEntiy.php @@ -19,20 +19,21 @@ trait BuildEntiy { /** * Rempli l'entité avec les données d'un tableau - * @param object $entity - * @param array $data + * @param object $entity Classe de l'entité + * @param array $data Tableau de correspondance entre les données et les méthodes * @return object */ public static function hasPacked(object $entity, array $data): object { foreach ($data as $key => $value) { $method = 'set' . ucfirst($key); - if (method_exists($entity, $method)) { + if (method_exists($entity, $method) === true) { $entity->$method($value); } } return $entity; - } + } //end hasPacked() + } From ee9d8eb70e2036e66c37b61add0fda63e2875586 Mon Sep 17 00:00:00 2001 From: Benoit Foujols Date: Mon, 27 Mar 2023 14:20:36 +0200 Subject: [PATCH 4/6] Fix Code Style #21 --- src/Core/BuildEntiy.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Core/BuildEntiy.php b/src/Core/BuildEntiy.php index ad13c66..ae13c14 100644 --- a/src/Core/BuildEntiy.php +++ b/src/Core/BuildEntiy.php @@ -26,14 +26,16 @@ trait BuildEntiy public static function hasPacked(object $entity, array $data): object { foreach ($data as $key => $value) { - $method = 'set' . ucfirst($key); + $method = "set{ucfirst($key)}"; if (method_exists($entity, $method) === true) { $entity->$method($value); } } return $entity; - } //end hasPacked() + + } + //end hasPacked() } From d067885e6c93055c7992ee2674035efd292c7b45 Mon Sep 17 00:00:00 2001 From: Benoit Foujols Date: Mon, 27 Mar 2023 16:22:06 +0200 Subject: [PATCH 5/6] Fix Code Style #21 Fix BuildEntity Add Exception #8 --- src/Client.php | 14 +++++--------- src/Core/{BuildEntiy.php => BuildEntity.php} | 8 ++------ src/Exception/ErrorHttpStatusException.php | 17 +++++++++++++++++ src/Exception/InvalidModelException.php | 17 +++++++++++++++++ src/Exception/NotDataResponseException.php | 17 +++++++++++++++++ src/Query/DispacherQuery.php | 10 +++++----- src/Query/LoginQuery.php | 9 +++++---- src/Query/Query.php | 12 ++++-------- src/Query/RunQuery.php | 16 +++++++++------- src/Query/ViescolaireQuery.php | 8 +++++--- src/Service/Request.php | 12 ++++++------ tests/Query/LoginQueryTest.php | 9 +++++++-- 12 files changed, 99 insertions(+), 50 deletions(-) rename src/Core/{BuildEntiy.php => BuildEntity.php} (91%) create mode 100644 src/Exception/ErrorHttpStatusException.php create mode 100644 src/Exception/InvalidModelException.php create mode 100644 src/Exception/NotDataResponseException.php diff --git a/src/Client.php b/src/Client.php index c943809..f745dbe 100644 --- a/src/Client.php +++ b/src/Client.php @@ -11,8 +11,8 @@ namespace Studoo\Api\EcoleDirecte; -use GuzzleHttp\Exception\GuzzleException; use Studoo\Api\EcoleDirecte\Entity\Login; +use Studoo\Api\EcoleDirecte\Exception\InvalidModelException; use Studoo\Api\EcoleDirecte\Query\RunQuery; /** @@ -57,16 +57,14 @@ public function __construct(array $config = []) 'Content-Type' => 'text/plain', ], ], $config); - - - } //end __construct() + } + //end __construct() /** * Accès à l'API EcoleDirecte avec les identifiants de l'utilisateur * Retourne un objet Login * @return object - * @throws \GuzzleHttp\Exception\GuzzleException - * @throws \JsonException + * @throws InvalidModelException */ public function fetchAccessToken(): object { @@ -83,8 +81,7 @@ public function fetchAccessToken(): object * Retourne les informations de l'utilisateur sur sa vie scolaire * @param int $idEtudiant Identifiant de l'étudiant * @return object - * @throws GuzzleException - * @throws \JsonException + * @throws InvalidModelException */ public function getVieScolaire(int $idEtudiant): object { @@ -109,5 +106,4 @@ public function getLibVerion(): string { return self::LIBVER; } - } diff --git a/src/Core/BuildEntiy.php b/src/Core/BuildEntity.php similarity index 91% rename from src/Core/BuildEntiy.php rename to src/Core/BuildEntity.php index ae13c14..f183dcb 100644 --- a/src/Core/BuildEntiy.php +++ b/src/Core/BuildEntity.php @@ -15,7 +15,7 @@ * Traitement d'une entité * @package Studoo\Api\EcoleDirecte\Core */ -trait BuildEntiy +class BuildEntity { /** * Rempli l'entité avec les données d'un tableau @@ -26,16 +26,12 @@ trait BuildEntiy public static function hasPacked(object $entity, array $data): object { foreach ($data as $key => $value) { - $method = "set{ucfirst($key)}"; + $method = "set" . ucfirst($key); if (method_exists($entity, $method) === true) { $entity->$method($value); } } return $entity; - } - //end hasPacked() - - } diff --git a/src/Exception/ErrorHttpStatusException.php b/src/Exception/ErrorHttpStatusException.php new file mode 100644 index 0000000..a799f2f --- /dev/null +++ b/src/Exception/ErrorHttpStatusException.php @@ -0,0 +1,17 @@ +setToken($data['token']); if (isset($data['data']['accounts'][0]) === true) { - self::hasPacked($login, $data['data']['accounts'][0]); + BuildEntity::hasPacked($login, $data['data']['accounts'][0]); } else { - // TODO: Throw an exception - throw new Exception('Aucune donnée n\'a été trouvée'); + throw new NotDataResponseException(); } return $login; diff --git a/src/Query/Query.php b/src/Query/Query.php index 24b2396..9a0ad9f 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -1,23 +1,19 @@ $replace) { - $this->path = str_replace("<{$search}>", $replace, $this->path); + $this->path = str_replace("<$search>", $replace, $this->path); } return $this; } /** - * retourne les paramètres de la requête + * Retourne les paramètres de la requête * @return array */ public function getQuery(): array diff --git a/src/Query/RunQuery.php b/src/Query/RunQuery.php index 4902f9d..56efe55 100644 --- a/src/Query/RunQuery.php +++ b/src/Query/RunQuery.php @@ -11,10 +11,11 @@ namespace Studoo\Api\EcoleDirecte\Query; -use Exception; use GuzzleHttp\Exception\GuzzleException; use JsonException; use Psr\Http\Message\ResponseInterface; +use Studoo\Api\EcoleDirecte\Exception\ErrorHttpStatusException; +use Studoo\Api\EcoleDirecte\Exception\InvalidModelException; use Studoo\Api\EcoleDirecte\Service\Request; /** @@ -34,12 +35,11 @@ class RunQuery * BuildQuery constructor. * @param string $model Nom d'appel API * @param array $config Configuration de l'API - * @throws Exception + * @throws InvalidModelException */ public function __construct(string $model, array $config) { $finalModel = $this->dispacherForModel($model); - // TODO Mettre un try catch pour la gestion d'erreur $this->apiModel = new $finalModel(); $this->config = $config; } @@ -52,6 +52,7 @@ public function __construct(string $model, array $config) * @return object * @throws GuzzleException * @throws JsonException + * @throws ErrorHttpStatusException */ public function run( array $body = [], @@ -59,10 +60,11 @@ public function run( 'Content-Type' => 'text/plain', ], array $param = [] - ): object - { + ): object { // Add pathID to path ('pathID' => []) - (isset($param['pathID'])) ? $this->apiModel->setParamToPath($param['pathID']) : null; + if (isset($param['pathID'])) { + $this->apiModel->setParamToPath($param['pathID']); + } // Fix body si vide (isset($body)) ? $bodyReponse = json_encode($body, JSON_THROW_ON_ERROR) : $bodyReponse = "{}"; @@ -76,7 +78,7 @@ public function run( ); if ($response->getStatusCode() !== 200) { - throw new Exception('Error'); + throw new ErrorHttpStatusException(); } $this->apiModel->setrawSource($response); diff --git a/src/Query/ViescolaireQuery.php b/src/Query/ViescolaireQuery.php index c24d49e..5239008 100644 --- a/src/Query/ViescolaireQuery.php +++ b/src/Query/ViescolaireQuery.php @@ -10,7 +10,9 @@ namespace Studoo\Api\EcoleDirecte\Query; +use Studoo\Api\EcoleDirecte\Core\BuildEntity; use Studoo\Api\EcoleDirecte\Entity\Viescolaire; +use Studoo\Api\EcoleDirecte\Exception\NotDataResponseException; /** * Traitement de la requête sur la vie scolaire par requête API EcoleDirecte @@ -29,16 +31,16 @@ public function __construct() * Retourne l'entité de la requête API * @param array $data * @return object + * @throws NotDataResponseException */ public function buildEntity(array $data): object { $vieScolaire = new Viescolaire(); if (isset($data['data']) === true) { - self::hasPacked($vieScolaire, $data['data']); + BuildEntity::hasPacked($vieScolaire, $data['data']); } else { - // TODO: Throw an exception - throw new \Exception('Aucune donnée n\'a été trouvée'); + throw new NotDataResponseException(); } return $vieScolaire; } diff --git a/src/Service/Request.php b/src/Service/Request.php index efc70e2..5f4740e 100644 --- a/src/Service/Request.php +++ b/src/Service/Request.php @@ -10,6 +10,8 @@ namespace Studoo\Api\EcoleDirecte\Service; +use GuzzleHttp\Client; +use GuzzleHttp\Exception\GuzzleException; use Psr\Http\Message\ResponseInterface; /** @@ -45,13 +47,12 @@ public function __construct(array $config = []) } /** - * Requete vers l'API + * Request vers l'API * @param string $methode Method of the request (GET, POST, PUT, DELETE) * @param string $path Path of the request (ex: 'v3/eleves/123456789') * @param array $query Query of the request (ex: ['body' => 'data=', 'headers' => ['Content-Type' => 'text/plain']]) * @return ResponseInterface - * @throws \GuzzleHttp\Exception\GuzzleException - * @throws \JsonException + * @throws GuzzleException */ public function query( string $methode, @@ -62,11 +63,10 @@ public function query( 'Content-Type' => 'text/plain', ] ] - ): ResponseInterface - { + ): ResponseInterface { $this->headers = array_merge($this->headers, $query['headers']); - $client = new \GuzzleHttp\Client([ + $client = new Client([ 'base_uri' => $this->basePath . '/' . $this->version . '/', 'timeout' => $this->timeout, 'connect_timeout' => $this->connectTimeout, diff --git a/tests/Query/LoginQueryTest.php b/tests/Query/LoginQueryTest.php index 68903c9..03833c5 100644 --- a/tests/Query/LoginQueryTest.php +++ b/tests/Query/LoginQueryTest.php @@ -2,8 +2,8 @@ namespace API; -use Studoo\Api\EcoleDirecte\Query\LoginQuery; use PHPUnit\Framework\TestCase; +use Studoo\Api\EcoleDirecte\Query\LoginQuery; class LoginQueryTest extends TestCase { @@ -13,7 +13,12 @@ class LoginQueryTest extends TestCase public function setUp(): void { $this->loginQuery = new LoginQuery(); - $this->jsonContent = json_decode(file_get_contents(__DIR__ . '/../Data/loginV3TypeP.json'), true); + $this->jsonContent = json_decode( + file_get_contents(__DIR__ . '/../Data/loginV3TypeP.json'), + true, + 512, + JSON_THROW_ON_ERROR + ); } public function testGetQuery() From 247ec06f52b09bfc3397ed483ac11f0239c15e2e Mon Sep 17 00:00:00 2001 From: Benoit Foujols Date: Mon, 27 Mar 2023 16:49:32 +0200 Subject: [PATCH 6/6] add Code Coverage --- clover.xml | 313 ++++++++++++++++++++++++++++++++++++++++++++++++++ composer.json | 2 +- 2 files changed, 314 insertions(+), 1 deletion(-) create mode 100644 clover.xml diff --git a/clover.xml b/clover.xml new file mode 100644 index 0000000..f7f45af --- /dev/null +++ b/clover.xml @@ -0,0 +1,313 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/composer.json b/composer.json index 826f141..a7c351c 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,7 @@ "start": [ "Composer\\Config::disableProcessTimeout", "php -S localhost:8042 -t examples" ], - "test": "php vendor/bin/phpunit --testdox tests", + "test": "php vendor/bin/phpunit --testdox tests --coverage-clover clover.xml", "docker-start": "docker compose up -d", "docker-stop": "docker compose down" }