New file |
0,0 → 1,163 |
<?php |
// declare(encoding='UTF-8'); |
/** |
* Classe contenant des méthodes : |
* - d'intialisation des tests, |
* - refactorisant le code des tests, |
* - facilitant les tests. |
* |
* @category php 5.3 |
* @package Tests/Services |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org> |
* @copyright Copyright (c) 2011, Tela Botanica (accueil@tela-botanica.org) |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL |
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL |
* @version $Id$ |
*/ |
abstract class ServicePhpUnit extends PHPUnit_Framework_TestCase { |
|
protected static $projet = ''; |
protected $service = ''; |
protected $classeTestee = ''; |
protected $baseUrl = 'http://localhost/service:eflore:0.1/'; |
protected $serviceBaseUrl = ''; |
//+------------------------------------------------------------------------------------------------------+ |
// Intialisation |
|
public static function setUpBeforeClass() { |
error_reporting(E_ALL); |
|
self::chargerFramework(); |
|
// Enregistrement en première position des autoload de la méthode gérant les classes des services |
spl_autoload_register(array(get_class($this), 'chargerClasseAuto')); |
} |
|
public static function chargerClasseAuto($classe) { |
if (class_exists($classe)) { |
return null; |
} |
$cheminBase = realpath(__DIR__.'/../../modules/0.2').'/'; |
$cheminProjet = $cheminBase.self::$projet.'/'; |
$cheminBibliotheque = realpath(__DIR__.'/../../bibliotheque').'/'; |
$cheminBiblioNom = $cheminBibliotheque.'nom/'; |
$cheminBiblioNomDeco = $cheminBiblioNom.'decorateurs/'; |
$cheminsTests = __DIR__.'/'; |
$chemins = array($cheminBase, $cheminProjet, $cheminBibliotheque, $cheminsTests, $cheminBiblioNom, |
$cheminBiblioNomDeco); |
foreach ($chemins as $chemin) { |
$chemin = $chemin.$classe.'.php'; |
if (file_exists($chemin)) { |
require_once $chemin; |
} |
} |
} |
|
private static function chargerFramework() { |
$cheminRacine = realpath(dirname(__FILE__).'/../..').'/'; |
$framework = $cheminRacine.'framework.php'; |
if (!file_exists($framework)) { |
$e = "Veuillez paramétrer l'emplacement et la version du Framework dans le fichier $framework"; |
trigger_error($e, E_USER_ERROR); |
} else { |
// Inclusion du Framework |
require_once $framework; |
|
// Ajout d'information concernant cette application |
Framework::setCheminAppli($cheminRacine);// Obligatoire |
} |
} |
|
//+------------------------------------------------------------------------------------------------------+ |
// Refactorisation |
protected function consulterJson($ressources, $parametres) { |
$retourJson = $this->consulterBrut($ressources, $parametres); |
$retour = json_decode($retourJson, true); |
$this->assertEquals(JSON_ERROR_NONE, json_last_error(), "Le json contient des erreurs qui bloquent le décodage. Voir : $url"); |
return $retour; |
} |
|
protected function consulterBrut($ressources, $parametres) { |
array_unshift($ressources, $this->service); |
array_unshift($ressources, self::$projet); |
$Bdd = new Bdd(); |
$Ressources = new Ressources($ressources); |
$Parametres = new Parametres($parametres, $Bdd); |
$objetService = new $this->classeTestee($Ressources, $Parametres, $Bdd); |
$this->initialiserService($objetService); |
$retourJson = $objetService->consulter(); |
return $retourJson; |
} |
|
protected function creerUrl($ressources, $parametres) { |
$version = ''; |
$ressourcesUrl = array(); |
foreach ($ressources as $ressource) { |
$ressourcesUrl[] = $ressource; |
} |
$ressourcesUrl = count($ressourcesUrl) > 0 ? '/'.implode('/', $ressourcesUrl) : ''; |
|
$parametresUrl = ''; |
if (count($parametres) > 0) { |
foreach ($parametres as $cle => $valeur) { |
$parametresUrl[] = $cle.'='.rawurlencode($valeur); |
} |
$parametresUrl = '?'.implode('&', $parametresUrl); |
} |
$url = $this->baseUrl.self::$projet.'/'.$version.$this->service.$ressourcesUrl.$parametresUrl; |
return $url; |
} |
|
//+------------------------------------------------------------------------------------------------------+ |
// Méthodes facilitant les tests |
|
/** |
* Récupère un bouchon de classe abstraite. |
* Comment l'utiliser : |
* $classeAstraite = $this->getClasseAbstraite('MaClasse', array('param1', 'param2')); |
* $foo = $classeAstraite->methodeConcretePublique(); |
* |
* @param String $classeNom Le nom de la classe |
* @param Array $parametres Les paramètres à passer au constructeur. |
* @return Object Le bouchon de la classe abstraite |
*/ |
public function getClasseAbstraite($classeNom, Array $parametres) { |
$efloreScript = $this->getMockForAbstractClass($classeNom, $parametres); |
return $efloreScript; |
} |
|
/** |
* Récupère une méthode privée d'une classe pour tester/documenter. |
* Comment l'utiliser : |
* MyClass->foo(): |
* $cls = new MyClass(); |
* $foo = self::getPrivateMethode($cls, 'foo'); |
* $foo->invoke($cls, $...); |
* |
* @param object $objet Une instance de votre classe |
* @param string $methode Le nom de la méthode private |
* @return ReflectionMethod La méthode demandée |
*/ |
public static function getMethodePrivee($objet, $nomMethode) { |
$classe = new ReflectionClass($objet); |
$methode = $classe->getMethod($nomMethode); |
$methode->setAccessible(true); |
return $methode; |
} |
|
/** |
* Récupère une méthode protégée d'une classe pour tester/documenter. |
* Comment l'utiliser : |
* MyClass->foo(): |
* $cls = new MyClass(); |
* $foo = self::getProtectedMethode($cls, 'foo'); |
* $foo->invoke($cls, $...); |
* @param object $objet Une instance de votre classe |
* @param string $methode Le nom de la méthode protected |
* @return ReflectionMethod La méthode demandée |
*/ |
public static function getMethodeProtegee($objet, $nomMethode) { |
return self::getMethodePrivee($objet, $nomMethode); |
} |
} |
?> |