- [x] [phpunit/tests/Database_*] Tests unitaire de delNumeric()
- [x] [Database] Mise a jour des methodes de Database - [x] [Database::construct] Gestion du singleton et de la config
This commit is contained in:
parent
02b4e0b703
commit
e47816a2d3
|
@ -6,7 +6,11 @@
|
||||||
class DataBase{
|
class DataBase{
|
||||||
|
|
||||||
/* ATTRIBUTS STATIQUES */
|
/* ATTRIBUTS STATIQUES */
|
||||||
public static $config_path = array('f/json/database/conf', 'f/json/database-local/conf');
|
public static $config_path = array(
|
||||||
|
'local' => 'f/json/database-local/conf',
|
||||||
|
'remote' => 'f/json/database/conf'
|
||||||
|
);
|
||||||
|
|
||||||
private static $pdo;
|
private static $pdo;
|
||||||
private static $instance;
|
private static $instance;
|
||||||
|
|
||||||
|
@ -17,6 +21,8 @@
|
||||||
private $username;
|
private $username;
|
||||||
private $password;
|
private $password;
|
||||||
|
|
||||||
|
public static $error;
|
||||||
|
|
||||||
|
|
||||||
public function __construct($host, $dbname, $username, $password){
|
public function __construct($host, $dbname, $username, $password){
|
||||||
$this->host = $host;
|
$this->host = $host;
|
||||||
|
@ -24,20 +30,28 @@
|
||||||
$this->username = $username;
|
$this->username = $username;
|
||||||
$this->password = $password;
|
$this->password = $password;
|
||||||
|
|
||||||
// password: Qt358nUdyeTxLDM8
|
try{
|
||||||
self::$pdo = new \PDO('mysql:host='.$host.';dbname='.$dbname, $username, $password);
|
self::$pdo = new \PDO('mysql:host='.$this->host.';dbname='.$this->dbname, $this->username, $this->password);
|
||||||
|
|
||||||
|
// On signale que tout s'est bien passe
|
||||||
|
self::$error = \manager\ManagerError::Success;
|
||||||
|
|
||||||
|
}catch(Exception $e){
|
||||||
|
// On signale qu'il y a une erreur
|
||||||
|
self::$error = \manager\ManagerError::PDOConnection;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* retourne une instance de la classe */
|
/* retourne une instance de la classe */
|
||||||
public static function getInstance(){
|
public static function getInstance(){
|
||||||
if( self::$instance == null ){
|
if( self::$instance == null || self::$error != \manager\ManagerError::Success ){ // Si aucune instance existante OU erreur de connection
|
||||||
|
|
||||||
// chargement de la configuration du server SQL
|
// chargement de la configuration du server SQL
|
||||||
if( $_SERVER['HTTP_HOST'] != 'stefproject' )
|
if( !isset($_SERVER['HTTP_HOST']) || isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] == 'stefproject' )
|
||||||
$conf = json_decode( ResourceDispatcher::getResource(self::$config_path[0]), true );
|
$conf = json_decode( ResourceDispatcher::getResource(self::$config_path['local']), true );
|
||||||
else
|
else
|
||||||
$conf = json_decode( ResourceDispatcher::getResource(self::$config_path[1]), true );
|
$conf = json_decode( ResourceDispatcher::getResource(self::$config_path['remote']), true );
|
||||||
|
|
||||||
// creation de l'instance en fonction des parametres
|
// creation de l'instance en fonction des parametres
|
||||||
self::$instance = new DataBase($conf['host'], $conf['dbname'], $conf['user'], $conf['password']);
|
self::$instance = new DataBase($conf['host'], $conf['dbname'], $conf['user'], $conf['password']);
|
||||||
|
@ -50,10 +64,31 @@
|
||||||
/* retourne la connection statique */
|
/* retourne la connection statique */
|
||||||
public static function getPDO(){
|
public static function getPDO(){
|
||||||
$instance = self::getInstance();
|
$instance = self::getInstance();
|
||||||
|
|
||||||
return self::$pdo;
|
return self::$pdo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public function getConfig(){
|
||||||
|
return array(
|
||||||
|
'host' => $this->host,
|
||||||
|
'username' => $this->username
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*************************************************************/
|
/*************************************************************/
|
||||||
/* _____ ______ _ _ ______ _____ _ */
|
/* _____ ______ _ _ ______ _____ _ */
|
||||||
/* / ____| ____| \ | | ____| __ \ /\ | | */
|
/* / ____| ____| \ | | ____| __ \ /\ | | */
|
||||||
|
|
|
@ -46,6 +46,11 @@
|
||||||
// Module non specifie dans la conf
|
// Module non specifie dans la conf
|
||||||
const UnknownRepo = 10;
|
const UnknownRepo = 10;
|
||||||
|
|
||||||
|
/* Database */
|
||||||
|
|
||||||
|
// Erreur lors de la creation d'un objet PDO (connection)
|
||||||
|
const PDOConnection = 11;
|
||||||
|
|
||||||
|
|
||||||
/* EXPLICITE UN CODE D'ERREUR
|
/* EXPLICITE UN CODE D'ERREUR
|
||||||
*
|
*
|
||||||
|
@ -67,6 +72,7 @@
|
||||||
case self::UnknownRepo: return "Le repo n'existe pas"; break;
|
case self::UnknownRepo: return "Le repo n'existe pas"; break;
|
||||||
case self::UnknownMethod: return "Le methode n'existe pas"; break;
|
case self::UnknownMethod: return "Le methode n'existe pas"; break;
|
||||||
case self::UncallableMethod: return "Le methode n'est pas amorcable"; break;
|
case self::UncallableMethod: return "Le methode n'est pas amorcable"; break;
|
||||||
|
case self::PDOConnection: return "La connexion avec la base de donnees a echoue"; break;
|
||||||
|
|
||||||
default: return "Erreur inconnue..."; break;
|
default: return "Erreur inconnue..."; break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
<testsuite name="\manager\Database">
|
<testsuite name="\manager\Database">
|
||||||
<file>tests/Database_check.php</file>
|
<file>tests/Database_check.php</file>
|
||||||
<file>tests/Database_delNumeric.php</file>
|
<file>tests/Database_delNumeric.php</file>
|
||||||
|
<file>tests/Database_construct.php</file>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
</testsuites>
|
</testsuites>
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,64 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class Database_construct extends PHPUnit_Framework_TestCase{
|
||||||
|
|
||||||
|
/* [1] Verification du chargement de la config
|
||||||
|
=========================================================*/
|
||||||
|
public function testGetInstanceWithNoSERVER(){
|
||||||
|
$instance = \manager\Database::getInstance();
|
||||||
|
|
||||||
|
$this->assertEquals( 'localhost', $instance->getConfig()['host'] );
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetInstanceWithSERVERLocal(){
|
||||||
|
// Pour regenerer une instance, on definit une erreur
|
||||||
|
\manager\Database::$error = \manager\ManagerError::PDOConnection;
|
||||||
|
|
||||||
|
|
||||||
|
$_SERVER['HTTP_HOST'] = 'stefproject';
|
||||||
|
$instance = \manager\Database::getInstance();
|
||||||
|
|
||||||
|
$this->assertEquals( 'localhost', $instance->getConfig()['host'] );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* [2] Verification du singleton (getInstance)
|
||||||
|
=========================================================*/
|
||||||
|
public function testInstancePersistence(){
|
||||||
|
\manager\Database::$error = \manager\ManagerError::PDOConnection;
|
||||||
|
|
||||||
|
$instance_construct = \manager\Database::getInstance();
|
||||||
|
$instance_nextuse = \manager\Database::getInstance();
|
||||||
|
|
||||||
|
$this->assertSame( $instance_construct, $instance_nextuse );
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInstancePersistenceRefutation(){
|
||||||
|
\manager\Database::$error = \manager\ManagerError::PDOConnection;
|
||||||
|
$instance_construct = \manager\Database::getInstance();
|
||||||
|
|
||||||
|
\manager\Database::$error = \manager\ManagerError::PDOConnection;
|
||||||
|
$instance_nextuse = \manager\Database::getInstance();
|
||||||
|
|
||||||
|
$this->assertNotSame( $instance_construct, $instance_nextuse );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* [3] Verification de l'objet PDO
|
||||||
|
=========================================================*/
|
||||||
|
public function testPDO(){
|
||||||
|
$pdo = \manager\Database::getPDO();
|
||||||
|
|
||||||
|
$this->assertGreaterThan( 10, count($pdo->query('SELECT * FROM user')->fetchAll()), '[!] Moins de 10 utilisateurs trouves.');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
5
todo.md
5
todo.md
|
@ -13,7 +13,6 @@
|
||||||
############
|
############
|
||||||
# EN COURS #
|
# EN COURS #
|
||||||
############
|
############
|
||||||
- [ ] [Database] Checker de type
|
|
||||||
- [ ] Gestion des groupes (utilisateurs/machines)
|
- [ ] Gestion des groupes (utilisateurs/machines)
|
||||||
- [x] bdd
|
- [x] bdd
|
||||||
- [ ] Creation d'un groupe individuel pour utilisateurs
|
- [ ] Creation d'un groupe individuel pour utilisateurs
|
||||||
|
@ -40,12 +39,14 @@
|
||||||
########
|
########
|
||||||
# FAIT #
|
# FAIT #
|
||||||
########
|
########
|
||||||
- [x] [phpunit/tests/Database_delNumeric] Tests unitaire de delNumeric()
|
- [x] [phpunit/tests/Database_*] Tests unitaire de delNumeric()
|
||||||
- [x] [Database] Mise a jour des methodes de Database
|
- [x] [Database] Mise a jour des methodes de Database
|
||||||
|
- [x] [Database::construct] Gestion du singleton et de la config
|
||||||
- [x] [Database::check] Suite de l'implementation (couverture des types de la BDD actuelle: 100%)
|
- [x] [Database::check] Suite de l'implementation (couverture des types de la BDD actuelle: 100%)
|
||||||
- [x] [Database::delNumeric] Prevention si oubli @oneDimension + ne supprime plus les indices numeriques associees a aucun indice textuel
|
- [x] [Database::delNumeric] Prevention si oubli @oneDimension + ne supprime plus les indices numeriques associees a aucun indice textuel
|
||||||
- [x] [phpunit/tests/Database_check] Tests unitaire du checker
|
- [x] [phpunit/tests/Database_check] Tests unitaire du checker
|
||||||
- [x] [phpunit/] Install+Config phpunit
|
- [x] [phpunit/] Install+Config phpunit
|
||||||
|
- [x] [Database] Checker de type (types utilises dans la BDD)
|
||||||
- [x] [manager/Repo] Gestion des Repo
|
- [x] [manager/Repo] Gestion des Repo
|
||||||
- [x] [ManagerError] Correction/ajout des codes erreurs
|
- [x] [ManagerError] Correction/ajout des codes erreurs
|
||||||
- [x] [ModuleRequest] Modification des erreurs
|
- [x] [ModuleRequest] Modification des erreurs
|
||||||
|
|
Loading…
Reference in New Issue