Athlete Gateway, Mappeur et Entity fonctionnel avec test unitaires
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
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,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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
?>
|
?>
|
@ -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…
Reference in new issue