From e47816a2d392a1bca44d2775638cdff0952f2567 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Fri, 12 Feb 2016 22:07:46 +0100 Subject: [PATCH] - [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 --- manager/Database.php | 49 +++++++++++++++++--- manager/ManagerError.php | 6 +++ phpunit/phpunit.xml | 1 + phpunit/tests/Database_check.php | 0 phpunit/tests/Database_construct.php | 64 +++++++++++++++++++++++++++ phpunit/tests/Database_delNumeric.php | 0 todo.md | 5 ++- 7 files changed, 116 insertions(+), 9 deletions(-) mode change 100644 => 100755 phpunit/phpunit.xml mode change 100644 => 100755 phpunit/tests/Database_check.php create mode 100755 phpunit/tests/Database_construct.php mode change 100644 => 100755 phpunit/tests/Database_delNumeric.php diff --git a/manager/Database.php b/manager/Database.php index 6f51cee..e1e7b81 100755 --- a/manager/Database.php +++ b/manager/Database.php @@ -6,7 +6,11 @@ class DataBase{ /* 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 $instance; @@ -17,6 +21,8 @@ private $username; private $password; + public static $error; + public function __construct($host, $dbname, $username, $password){ $this->host = $host; @@ -24,20 +30,28 @@ $this->username = $username; $this->password = $password; - // password: Qt358nUdyeTxLDM8 - self::$pdo = new \PDO('mysql:host='.$host.';dbname='.$dbname, $username, $password); + try{ + 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 */ 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 - if( $_SERVER['HTTP_HOST'] != 'stefproject' ) - $conf = json_decode( ResourceDispatcher::getResource(self::$config_path[0]), true ); + if( !isset($_SERVER['HTTP_HOST']) || isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] == 'stefproject' ) + $conf = json_decode( ResourceDispatcher::getResource(self::$config_path['local']), true ); 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 self::$instance = new DataBase($conf['host'], $conf['dbname'], $conf['user'], $conf['password']); @@ -50,10 +64,31 @@ /* retourne la connection statique */ public static function getPDO(){ $instance = self::getInstance(); + return self::$pdo; } + + + + + + + + public function getConfig(){ + return array( + 'host' => $this->host, + 'username' => $this->username + ); + } + + + + + + + /*************************************************************/ /* _____ ______ _ _ ______ _____ _ */ /* / ____| ____| \ | | ____| __ \ /\ | | */ diff --git a/manager/ManagerError.php b/manager/ManagerError.php index 8c043a1..3f43929 100755 --- a/manager/ManagerError.php +++ b/manager/ManagerError.php @@ -46,6 +46,11 @@ // Module non specifie dans la conf const UnknownRepo = 10; + /* Database */ + + // Erreur lors de la creation d'un objet PDO (connection) + const PDOConnection = 11; + /* EXPLICITE UN CODE D'ERREUR * @@ -67,6 +72,7 @@ case self::UnknownRepo: return "Le repo 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::PDOConnection: return "La connexion avec la base de donnees a echoue"; break; default: return "Erreur inconnue..."; break; } diff --git a/phpunit/phpunit.xml b/phpunit/phpunit.xml old mode 100644 new mode 100755 index 2470e97..0746731 --- a/phpunit/phpunit.xml +++ b/phpunit/phpunit.xml @@ -4,6 +4,7 @@ tests/Database_check.php tests/Database_delNumeric.php + tests/Database_construct.php diff --git a/phpunit/tests/Database_check.php b/phpunit/tests/Database_check.php old mode 100644 new mode 100755 diff --git a/phpunit/tests/Database_construct.php b/phpunit/tests/Database_construct.php new file mode 100755 index 0000000..827a58f --- /dev/null +++ b/phpunit/tests/Database_construct.php @@ -0,0 +1,64 @@ +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.'); + } + + + + + + + } + +?> \ No newline at end of file diff --git a/phpunit/tests/Database_delNumeric.php b/phpunit/tests/Database_delNumeric.php old mode 100644 new mode 100755 diff --git a/todo.md b/todo.md index cf7ff99..1d4578c 100755 --- a/todo.md +++ b/todo.md @@ -13,7 +13,6 @@ ############ # EN COURS # ############ -- [ ] [Database] Checker de type - [ ] Gestion des groupes (utilisateurs/machines) - [x] bdd - [ ] Creation d'un groupe individuel pour utilisateurs @@ -40,12 +39,14 @@ ######## # 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::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::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/] Install+Config phpunit +- [x] [Database] Checker de type (types utilises dans la BDD) - [x] [manager/Repo] Gestion des Repo - [x] [ManagerError] Correction/ajout des codes erreurs - [x] [ModuleRequest] Modification des erreurs