Athlete Gateway, Mappeur et Entity fonctionnel avec test unitaires
continuous-integration/drone/push Build is failing Details

issue_023_User_Gateway
Kevin MONTEIRO 1 year ago
parent 64c04a97f6
commit ca732f2c97

@ -1 +1 @@
{"version":1,"defects":{"MaClasseTest::testMethode":5,"ExampleTest::testMethode":5,"toto::testMethode":5,"AthleteGateway::testGateway":5,"Database\\Tests\\AthleteGatewayTest::testGetAthlete":4,"Database\\Tests\\AthleteGatewayTest::testGetAthleteById":4,"Database\\Tests\\AthleteGatewayTest::testAddAthlete":4,"Database\\Tests\\AthleteGatewayTest::testUpdateAthlete":4,"Database\\Tests\\AthleteGatewayTest::testDeleteAthlete":4,"Database\\AthleteGatewayTest::testGetAthlete":4,"Database\\AthleteGatewayTest::testGetAthleteById":4,"Database\\AthleteGatewayTest::testAddAthlete":4,"Database\\AthleteGatewayTest::testUpdateAthlete":4,"Database\\AthleteGatewayTest::testDeleteAthlete":4,"AthleteGatewayTest::testAddAndGetAthlete":4},"times":{"MaClasseTest::testMethode":0.001,"ExampleTest::testMethode":0.001,"toto::testMethode":0.001,"AthleteGateway::testGateway":0.048,"Database\\Tests\\AthleteGatewayTest::testGetAthlete":0.001,"Database\\Tests\\AthleteGatewayTest::testGetAthleteById":0.001,"Database\\Tests\\AthleteGatewayTest::testAddAthlete":0.072,"Database\\Tests\\AthleteGatewayTest::testUpdateAthlete":0.001,"Database\\Tests\\AthleteGatewayTest::testDeleteAthlete":0.001,"Database\\AthleteGatewayTest::testGetAthlete":0.002,"Database\\AthleteGatewayTest::testGetAthleteById":0,"Database\\AthleteGatewayTest::testAddAthlete":0,"Database\\AthleteGatewayTest::testUpdateAthlete":0,"Database\\AthleteGatewayTest::testDeleteAthlete":0,"AthleteGatewayTest::testAddAndGetAthlete":0.002}} {"version":1,"defects":{"MaClasseTest::testMethode":5,"ExampleTest::testMethode":5,"toto::testMethode":5,"AthleteGateway::testGateway":5,"Database\\Tests\\AthleteGatewayTest::testGetAthlete":4,"Database\\Tests\\AthleteGatewayTest::testGetAthleteById":4,"Database\\Tests\\AthleteGatewayTest::testAddAthlete":4,"Database\\Tests\\AthleteGatewayTest::testUpdateAthlete":4,"Database\\Tests\\AthleteGatewayTest::testDeleteAthlete":4,"Database\\AthleteGatewayTest::testGetAthlete":4,"Database\\AthleteGatewayTest::testGetAthleteById":4,"Database\\AthleteGatewayTest::testAddAthlete":4,"Database\\AthleteGatewayTest::testUpdateAthlete":4,"Database\\AthleteGatewayTest::testDeleteAthlete":4,"AthleteGatewayTest::testAddAndGetAthlete":4,"AthleteGatewayTest::testGetAthlete":5,"AthleteGatewayTest::testAddAthlete":5,"AthleteGatewayTest::testDeleteAthlete":4,"AthleteGatewayTest::testUpdateAthlete":5,"AthleteMapperTest::testMapperAthlete":5,"GatewayTest::testGetAthlete":5,"GatewayTest::testUpdateAthlete":5,"MapperTest::testMapperAthlete":5,"GatewayTest::testGetCoach":5,"GatewayTest::testAddCoach":4},"times":{"MaClasseTest::testMethode":0.001,"ExampleTest::testMethode":0.001,"toto::testMethode":0.001,"AthleteGateway::testGateway":0.048,"Database\\Tests\\AthleteGatewayTest::testGetAthlete":0.001,"Database\\Tests\\AthleteGatewayTest::testGetAthleteById":0.001,"Database\\Tests\\AthleteGatewayTest::testAddAthlete":0.072,"Database\\Tests\\AthleteGatewayTest::testUpdateAthlete":0.001,"Database\\Tests\\AthleteGatewayTest::testDeleteAthlete":0.001,"Database\\AthleteGatewayTest::testGetAthlete":0.002,"Database\\AthleteGatewayTest::testGetAthleteById":0,"Database\\AthleteGatewayTest::testAddAthlete":0,"Database\\AthleteGatewayTest::testUpdateAthlete":0,"Database\\AthleteGatewayTest::testDeleteAthlete":0,"AthleteGatewayTest::testAddAndGetAthlete":0.028,"AthleteGatewayTest::testGetAthlete":0.004,"AthleteGatewayTest::testAddAthlete":0.005,"AthleteGatewayTest::testDeleteAthlete":0.003,"AthleteGatewayTest::testUpdateAthlete":0.006,"AthleteMapperTest::testMapperAthlete":0.004,"GatewayTest::testGetAthlete":0.006,"GatewayTest::testUpdateAthlete":0.005,"MapperTest::testMapperAthlete":0.005,"GatewayTest::testGetCoach":0.004,"GatewayTest::testAddCoach":0.015}}

@ -1,11 +1,12 @@
<?php <?php
namespace Database; namespace Database;
use \PDO;
class AthleteGateway { class AthleteGateway {
private $connection; private $connection;
public function __construct(Connection $connection) { public function __construct(Connexion $connection) {
$this->connection = $connection; $this->connection = $connection;
} }
@ -74,7 +75,7 @@ class AthleteGateway {
':taille' => $athlete->getTaille(), ':taille' => $athlete->getTaille(),
':poids' => $athlete->getPoids(), ':poids' => $athlete->getPoids(),
':motDePasse' => $athlete->getMotDePasse(), ':motDePasse' => $athlete->getMotDePasse(),
':dateNaissance' => $athlete->getDateNaissance()->format('Y-m-d'), // Format date pour SQL ':dateNaissance' => $athlete->getDateNaissance(),
]; ];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
@ -95,7 +96,7 @@ class AthleteGateway {
':taille' => $newAthlete->getTaille(), ':taille' => $newAthlete->getTaille(),
':poids' => $newAthlete->getPoids(), ':poids' => $newAthlete->getPoids(),
':motDePasse' => $newAthlete->getMotDePasse(), ':motDePasse' => $newAthlete->getMotDePasse(),
':dateNaissance' => $newAthlete->getDateNaissance()->format('Y-m-d'), // Format date pour SQL ':dateNaissance' => $newAthlete->getDateNaissance(),
]; ];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);

@ -2,25 +2,65 @@
namespace Database; namespace Database;
use Model\User; use Model\User;
use \PDO;
use \DateTime;
use Model\Role;
use Model\Athlete;
class AthleteMapper { class AthleteMapper {
public function fromSqlToEntity(array $data):AthleteEntity { public function fromSqlToEntity(array $data): array {
$athlete = new AthleteEntity(); $athleteEntities = [];
$athlete->setIdAthlete($data['idAthlete']);
$athlete->setNom($data['nom']); foreach ($data as $athleteData) {
$athlete->setPrenom($data['prenom']); $athlete = new AthleteEntity();
$athlete->setEmail($data['email']);
$athlete->setSexe($data['sexe']); if (isset($athleteData['idAthlete'])) {
$athlete->setTaille($data['taille']); $athlete->setIdAthlete($athleteData['idAthlete']);
$athlete->setPoids($data['poids']); }
$athlete->setMotDePasse($data['motDePasse']);
$athlete->setDateNaissance($data['dateNaissance']); if (isset($athleteData['nom'])) {
$athlete->setNom($athleteData['nom']);
return $athlete; }
if (isset($athleteData['prenom'])) {
$athlete->setPrenom($athleteData['prenom']);
}
if (isset($athleteData['email'])) {
$athlete->setEmail($athleteData['email']);
}
if (isset($athleteData['sexe'])) {
$athlete->setSexe($athleteData['sexe']);
}
if (isset($athleteData['taille'])) {
$athlete->setTaille($athleteData['taille']);
}
if (isset($athleteData['poids'])) {
$athlete->setPoids($athleteData['poids']);
}
if (isset($athleteData['motDePasse'])) {
$athlete->setMotDePasse($athleteData['motDePasse']);
}
if (isset($athleteData['dateNaissance'])) {
$athlete->setDateNaissance($athleteData['dateNaissance']);
}
$athleteEntities[] = $athlete;
}
return $athleteEntities;
} }
public function AthleteEntityToModel(AthleteEntity $athleteEntity):User{ public function athleteEntityToModel(AthleteEntity $athleteEntity): User {
$role = "Athlete"; $role = new Athlete(); // Utilisez la classe Athlete
$dateSpecifique = $athleteEntity->getDateNaissance();
$date = new DateTime($dateSpecifique);
$user = new User( $user = new User(
$athleteEntity->getIdAthlete(), $athleteEntity->getIdAthlete(),
@ -31,14 +71,14 @@ class AthleteMapper {
$athleteEntity->getSexe(), $athleteEntity->getSexe(),
$athleteEntity->getTaille(), $athleteEntity->getTaille(),
$athleteEntity->getPoids(), $athleteEntity->getPoids(),
$athleteEntity->getDateNaissance(), $date,
$role $role
); );
return $user; return $user;
} }
public function AthletetoEntity(User $user):AthleteEntity{ public function athleteToEntity(User $user):AthleteEntity{
$ath = new AthleteEntity(); $ath = new AthleteEntity();
$ath->setIdAthlete($user->getId()); $ath->setIdAthlete($user->getId());

@ -1,11 +1,12 @@
<?php <?php
namespace Database; namespace Database;
use \PDO;
class CoachGateway { class CoachGateway {
private $connection; private $connection;
public function __construct(Connection $connection) { public function __construct(Connexion $connection) {
$this->connection = $connection; $this->connection = $connection;
} }
@ -68,7 +69,7 @@ class CoachGateway {
':taille' => $coach->getTaille(), ':taille' => $coach->getTaille(),
':poids' => $coach->getPoids(), ':poids' => $coach->getPoids(),
':motDePasse' => $coach->getMotDePasse(), ':motDePasse' => $coach->getMotDePasse(),
':dateNaissance' => $coach->getDateNaissance()->format('Y-m-d'), // Format date pour SQL ':dateNaissance' => $coach->getDateNaissance(),
]; ];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);
@ -89,7 +90,7 @@ class CoachGateway {
':taille' => $newCoach->getTaille(), ':taille' => $newCoach->getTaille(),
':poids' => $newCoach->getPoids(), ':poids' => $newCoach->getPoids(),
':motDePasse' => $newCoach->getMotDePasse(), ':motDePasse' => $newCoach->getMotDePasse(),
':dateNaissance' => $newCoach->getDateNaissance()->format('Y-m-d'), // Format date pour SQL ':dateNaissance' => $newCoach->getDateNaissance(),
]; ];
return $this->connection->executeWithErrorHandling($query, $params); return $this->connection->executeWithErrorHandling($query, $params);

@ -2,7 +2,10 @@
namespace Database; namespace Database;
use Model\User; use Model\User;
use \PDO;
use \DateTime;
use Model\Role;
use Model\Coach;
class CoachMapper { class CoachMapper {
public function map(array $data) { public function map(array $data) {

@ -1,54 +1,49 @@
<?php <?php
class Connection extends PDO {
namespace Database;
class Connexion extends \PDO {
private $stmt; private $stmt;
public function __construct(string $dsn) {
// $dsn = "pgsql:host=$this->host;port=$this->port;dbname=$this->database;user=$this->username;password=$this->password"; public function __construct(string $dsn,string $username, string $password) {
// This should be remove or set to just use a debug
try { try {
parent::__construct($dsn); parent::__construct($dsn,$username,$password);
echo "Successfully connected to the database"; $this->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (\PDOException $e) {
// should be a more accurate exception // Log error or handle it as needed
} catch (PDOException $e) { throw new \PDOException("Error connecting to the database: " . $e->getMessage());
echo("Error connecting to the database: " . $e->getMessage());
// do something ...
} }
} }
public function executeQuery(string $query, array $parameters = []): bool {
/** * @param string $query $this->stmt = $this->prepare($query);
* @param array $parameters * //foreach ($parameters as $name => $value) {
* @return bool Returns `true` on success, `false` otherwise // $this->stmt->bindValue($name, $value[0], $value[1]);
*/ //}
public function executeQuery(string $query, array $parameters = []) : bool{
$this->stmt = parent::prepare($query);
foreach ($parameters as $name => $value) { foreach ($parameters as $name => $value) {
$this->stmt->bindValue($name, $value[0], $value[1]); $bindValueResult = $this->stmt->bindValue($name, $value, \PDO::PARAM_STR);
if (!$bindValueResult) {
// Gérez l'erreur, par exemple, en lançant une exception.
throw new \PDOException('Failed to bind value for parameter ' . $name);
}
} }
return $this->stmt->execute(); return $this->stmt->execute();
} }
public function executeWithErrorHandling(string $query,array $params = []) {
public function executeWithErrorHandling(string $query, array $params = []): array {
try { try {
$this->beginTransaction(); $this->beginTransaction();
$this->executeQuery($query,$params); $this->executeQuery($query, $params);
$this->commit(); $this->commit();
return $this->getResults(); return $this->getResults();
} catch (PDOException $e) { } catch (\PDOException $e) {
$this->rollBack(); $this->rollBack();
throw new Exception('Unexpected error on database client: ' . $e->getMessage()); throw new \PDOException('Unexpected error on database client: ' . $e->getMessage());
} }
} }
public function getResults() : array { public function getResults(): array {
return $this->stmt->fetchall(PDO::FETCH_ASSOC); return $this->stmt->fetchAll(\PDO::FETCH_ASSOC);
} }
} }
?> ?>

@ -2,20 +2,50 @@
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Database\Connection; //use Database\{Connexion, AthleteGateway,AthleteEntity};
use Database\AthleteEntity;
use Database\AthleteGateway;
use Database\Connexion;
use Database\AthleteMapper;
use Database\CoachGateway;
use Database\CoachEntity;
use Database\CoachMapper;
class AthleteGatewayTest extends TestCase { class GatewayTest extends TestCase {
public function testAddAndGetAthlete() { //Partie concernant les Athlètes
$dsn = "pgsql:host=londres;port=8888;dbname=dbkemonteiro2;user=kemonteiro2;password=Mdp"; public function testGetAthlete() {
$connection = new Connection($dsn); //$dsn = "pgsql:host=londres;port=8888;dbname=dbkemonteiro2;user=kemonteiro2;password=Mdp";
$dsn = "mysql:host=londres;dbname=dbkemonteiro2;";
$username = "kemonteiro2";
$password = "#Phpmyadmin63";
$athleteGateway = new AthleteGateway($connection); $connexion = new Connexion($dsn,$username,$password);
$athleteGateway = new AthleteGateway($connexion);
$result = $athleteGateway->getAthlete();
//var_dump($result);
}
/* Fonctionne mais en commentaire pour pas add et del a chaque fois
public function testAddAthlete(){
$dsn = "mysql:host=londres;dbname=dbkemonteiro2;";
$username = "kemonteiro2";
$password = "#Phpmyadmin63";
$connexion = new Connexion($dsn,$username,$password);
$athleteGateway = new AthleteGateway($connexion);
$dateSpecifique = "2023-11-26";
$timestamp = strtotime($dateSpecifique);
$dateSQL = date("Y-m-d", $timestamp);
$athleteEntity = new AthleteEntity(); $athleteEntity = new AthleteEntity();
$athleteEntity->setNom('John'); $athleteEntity->setNom('John');
$athleteEntity->setPrenom('Doe'); $athleteEntity->setPrenom('Doe');
@ -25,31 +55,166 @@ class AthleteGatewayTest extends TestCase {
$athleteEntity->setTaille(169); $athleteEntity->setTaille(169);
$athleteEntity->setPoids(69); $athleteEntity->setPoids(69);
$athleteEntity->setMotDePasse('motdepasse'); $athleteEntity->setMotDePasse('motdepasse');
$athleteEntity->setDateNaissance('26/03/2004'); $athleteEntity->setDateNaissance($dateSQL);
$result2 = $athleteGateway->addAthlete($athleteEntity);
}
public function testDeleteAthlete(){
$dsn = "mysql:host=londres;dbname=dbkemonteiro2;";
$username = "kemonteiro2";
$password = "#Phpmyadmin63";
//$result = $athleteGateway->addAthlete($athleteEntity); $connexion = new Connexion($dsn,$username,$password);
$athleteGateway = new AthleteGateway($connexion);
$result = $athleteGateway->deleteAthlete( //idAthlete );
var_dump($result);
//$this->assertTrue($result); }*/
public function testUpdateAthlete(){
$dsn = "mysql:host=londres;dbname=dbkemonteiro2;";
$username = "kemonteiro2";
$password = "#Phpmyadmin63";
$athleteId = $athleteEntity->getIdAthlete(); $connexion = new Connexion($dsn,$username,$password);
$retrievedAthlete = $athleteGateway->getAthleteById($athleteId);
$athleteGateway = new AthleteGateway($connexion);
$dateSpecifique = "2004-08-26";
$timestamp = strtotime($dateSpecifique);
$dateSQL = date("Y-m-d", $timestamp);
$athleteEntity = new AthleteEntity();
$athleteEntity->setNom('John');
$athleteEntity->setPrenom('Doe');
$athleteEntity->setIdAthlete(13);
$athleteEntity->setEmail('kevin.monteiro@gmail.fr');
$athleteEntity->setSexe('H');
$athleteEntity->setTaille(169);
$athleteEntity->setPoids(69);
$athleteEntity->setMotDePasse('motdepasse');
$athleteEntity->setDateNaissance($dateSQL);
$athleteEntity2 = new AthleteEntity();
$athleteEntity2->setNom('Monteiro');
$athleteEntity2->setPrenom('Kevin');
$athleteEntity2->setIdAthlete(13);
$athleteEntity2->setEmail('kevin.monteiro@gmail.fr');
$athleteEntity2->setSexe('H');
$athleteEntity2->setTaille(169);
$athleteEntity2->setPoids(69);
$athleteEntity2->setMotDePasse('motdepasse');
$athleteEntity2->setDateNaissance($dateSQL);
$result = $athleteGateway->updateAthlete($athleteEntity, $athleteEntity2);
}
//Partie concernant les Coachs
$this->assertInstanceOf(AthleteEntity::class, $retrievedAthlete); public function testGetCoach() {
//$dsn = "pgsql:host=londres;port=8888;dbname=dbkemonteiro2;user=kemonteiro2;password=Mdp";
$this->assertEquals('John', $retrievedAthlete->getNom()); $dsn = "mysql:host=londres;dbname=dbkemonteiro2;";
$this->assertEquals('Doe', $retrievedAthlete->getPrenom()); $username = "kemonteiro2";
$password = "#Phpmyadmin63";
$connexion = new Connexion($dsn,$username,$password);
$coachGateway = new CoachGateway($connexion);
$result = $coachGateway->getCoach();
var_dump($result);
}
/*
//Fonctionne PAS A PARTIR DE LA
public function testAddCoach(){
$dsn = "mysql:host=londres;dbname=dbkemonteiro2;";
$username = "kemonteiro2";
$password = "#Phpmyadmin63";
$connexion = new Connexion($dsn,$username,$password);
$coachGateway = new CoachGateway($connexion);
$dateSpecifique = "2023-11-26";
$timestamp = strtotime($dateSpecifique);
$dateSQL = date("Y-m-d", $timestamp);
$coachEntity = new CoachEntity();
$coachEntity->setNom('John');
$coachEntity->setPrenom('Doe');
$coachEntity->setIdCoach(1234);
$coachEntity->setEmail('kevin.monteiro@gmail.fr');
$coachEntity->setSexe('H');
$coachEntity->setTaille(169);
$coachEntity->setPoids(69);
$coachEntity->setMotDePasse('motdepasse');
$coachEntity->setDateNaissance($dateSQL);
$result2 = $coachGateway->addCoach($coachEntity);
} }
public function testDeleteAthlete(){
$dsn = "mysql:host=londres;dbname=dbkemonteiro2;";
$username = "kemonteiro2";
$password = "#Phpmyadmin63";
$connexion = new Connexion($dsn,$username,$password);
$athleteGateway = new AthleteGateway($connexion);
$result = $athleteGateway->deleteAthlete( //idAthlete );
var_dump($result);
}*/
/*
public function testUpdateAthlete(){
$dsn = "mysql:host=londres;dbname=dbkemonteiro2;";
$username = "kemonteiro2";
$password = "#Phpmyadmin63";
$connexion = new Connexion($dsn,$username,$password);
$athleteGateway = new AthleteGateway($connexion);
$dateSpecifique = "2004-08-26";
$timestamp = strtotime($dateSpecifique);
$dateSQL = date("Y-m-d", $timestamp);
$athleteEntity = new AthleteEntity();
$athleteEntity->setNom('John');
$athleteEntity->setPrenom('Doe');
$athleteEntity->setIdAthlete(13);
$athleteEntity->setEmail('kevin.monteiro@gmail.fr');
$athleteEntity->setSexe('H');
$athleteEntity->setTaille(169);
$athleteEntity->setPoids(69);
$athleteEntity->setMotDePasse('motdepasse');
$athleteEntity->setDateNaissance($dateSQL);
$athleteEntity2 = new AthleteEntity();
$athleteEntity2->setNom('Monteiro');
$athleteEntity2->setPrenom('Kevin');
$athleteEntity2->setIdAthlete(13);
$athleteEntity2->setEmail('kevin.monteiro@gmail.fr');
$athleteEntity2->setSexe('H');
$athleteEntity2->setTaille(169);
$athleteEntity2->setPoids(69);
$athleteEntity2->setMotDePasse('motdepasse');
$athleteEntity2->setDateNaissance($dateSQL);
$result = $athleteGateway->updateAthlete($athleteEntity, $athleteEntity2);
}*/
} }

@ -0,0 +1,43 @@
<?php
use PHPUnit\Framework\TestCase;
use Model\User;
use Database\AthleteEntity;
use Database\AthleteGateway;
use Database\Connexion;
use Database\AthleteMapper;
class MapperTest extends TestCase {
public function testMapperAthlete() {
//$dsn = "pgsql:host=londres;port=8888;dbname=dbkemonteiro2;user=kemonteiro2;password=Mdp";
$dsn = "mysql:host=londres;dbname=dbkemonteiro2;";
$username = "kemonteiro2";
$password = "#Phpmyadmin63";
$connexion = new Connexion($dsn,$username,$password);
$athleteGateway = new AthleteGateway($connexion);
$result = $athleteGateway->getAthlete();
$map = new AthleteMapper ();
//SQL To AthleteEntity
$athleteEntity = $map->fromSqlToEntity($result);
foreach($athleteEntity as $ath){
$result = $ath->getNom();
var_dump($result);
//Pour chaque AthleteEntity : Athlete Entity To User avec Role Athlete(Model)
$user = $map->athleteEntityToModel($ath);
var_dump($user->getId());
//Pour chaque Athlete du Model -> Athlete Entity
$res = $map->athleteToEntity($user);
var_dump($res->getIdAthlete());
}
}
}
Loading…
Cancel
Save