- [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:
xdrm-brackets 2016-02-12 22:07:46 +01:00
parent 02b4e0b703
commit e47816a2d3
7 changed files with 116 additions and 9 deletions

View File

@ -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
);
}
/*************************************************************/
/* _____ ______ _ _ ______ _____ _ */
/* / ____| ____| \ | | ____| __ \ /\ | | */

View File

@ -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;
}

1
phpunit/phpunit.xml Normal file → Executable file
View File

@ -4,6 +4,7 @@
<testsuite name="\manager\Database">
<file>tests/Database_check.php</file>
<file>tests/Database_delNumeric.php</file>
<file>tests/Database_construct.php</file>
</testsuite>
</testsuites>

0
phpunit/tests/Database_check.php Normal file → Executable file
View File

View File

@ -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.');
}
}
?>

0
phpunit/tests/Database_delNumeric.php Normal file → Executable file
View File

View File

@ -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