Subversion Repositories eFlore/Projets.eflore-projets

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
195 jpm 1
<?php
2
// declare(encoding='UTF-8');
3
/**
4
 * Classe contenant des méthodes :
5
 *  - d'intialisation des tests,
6
 *  - refactorisant le code des tests,
7
 *  - facilitant les tests.
8
 *
9
 * @category	php 5.3
10
 * @package		Tests/Services
11
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
12
 * @copyright	Copyright (c) 2011, Tela Botanica (accueil@tela-botanica.org)
13
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
14
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
15
 * @version		$Id$
16
 */
17
abstract class ServicePhpUnit extends PHPUnit_Framework_TestCase {
18
 
19
	protected static $projet = '';
20
	protected $service = '';
21
	protected $classeTestee = '';
22
	protected $baseUrl = 'http://localhost/service:eflore:0.1/';
23
	protected $serviceBaseUrl = '';
24
	//+------------------------------------------------------------------------------------------------------+
25
	// Intialisation
26
 
27
	public static function setUpBeforeClass() {
28
		error_reporting(E_ALL);
29
 
30
		self::chargerFramework();
31
 
32
		// Enregistrement en première position des autoload de la méthode gérant les classes des services
33
		spl_autoload_register(array(get_class($this), 'chargerClasseAuto'));
34
	}
35
 
36
	public static function chargerClasseAuto($classe) {
37
		if (class_exists($classe)) {
38
			return null;
39
		}
40
		$cheminBase = realpath(__DIR__.'/../../modules/0.2').'/';
41
		$cheminProjet = $cheminBase.self::$projet.'/';
42
		$cheminBibliotheque = realpath(__DIR__.'/../../bibliotheque').'/';
43
		$cheminBiblioNom = $cheminBibliotheque.'nom/';
44
		$cheminBiblioNomDeco = $cheminBiblioNom.'decorateurs/';
45
		$cheminsTests = __DIR__.'/';
46
		$chemins = array($cheminBase, $cheminProjet, $cheminBibliotheque, $cheminsTests, $cheminBiblioNom,
47
			$cheminBiblioNomDeco);
48
		foreach ($chemins as $chemin) {
49
			$chemin = $chemin.$classe.'.php';
50
			if (file_exists($chemin)) {
51
				require_once $chemin;
52
			}
53
		}
54
	}
55
 
56
	private static function chargerFramework() {
57
		$cheminRacine = realpath(dirname(__FILE__).'/../..').'/';
58
		$framework =  $cheminRacine.'framework.php';
59
		if (!file_exists($framework)) {
60
			$e = "Veuillez paramétrer l'emplacement et la version du Framework dans le fichier $framework";
61
			trigger_error($e, E_USER_ERROR);
62
		} else {
63
			// Inclusion du Framework
64
			require_once $framework;
65
 
66
			// Ajout d'information concernant cette application
67
			Framework::setCheminAppli($cheminRacine);// Obligatoire
68
		}
69
	}
70
 
71
	//+------------------------------------------------------------------------------------------------------+
72
	// Refactorisation
73
	protected function consulterJson($ressources, $parametres) {
74
		$retourJson = $this->consulterBrut($ressources, $parametres);
75
		$retour = json_decode($retourJson, true);
76
		$this->assertEquals(JSON_ERROR_NONE, json_last_error(), "Le json contient des erreurs qui bloquent le décodage. Voir : $url");
77
		return $retour;
78
	}
79
 
80
	protected function consulterBrut($ressources, $parametres) {
81
		array_unshift($ressources, $this->service);
82
		array_unshift($ressources, self::$projet);
83
		$Bdd = new Bdd();
84
		$Ressources = new Ressources($ressources);
85
		$Parametres = new Parametres($parametres, $Bdd);
86
		$objetService = new $this->classeTestee($Ressources, $Parametres, $Bdd);
87
		$this->initialiserService($objetService);
88
		$retourJson = $objetService->consulter();
89
		return $retourJson;
90
	}
91
 
92
	protected function creerUrl($ressources, $parametres) {
93
		$version = '';
94
		$ressourcesUrl = array();
95
		foreach ($ressources as $ressource) {
96
			$ressourcesUrl[] = $ressource;
97
		}
98
		$ressourcesUrl = count($ressourcesUrl) > 0 ? '/'.implode('/', $ressourcesUrl) : '';
99
 
100
		$parametresUrl = '';
101
		if (count($parametres) > 0) {
102
			foreach ($parametres as $cle => $valeur) {
103
				$parametresUrl[] = $cle.'='.rawurlencode($valeur);
104
			}
105
			$parametresUrl = '?'.implode('&', $parametresUrl);
106
		}
107
		$url = $this->baseUrl.self::$projet.'/'.$version.$this->service.$ressourcesUrl.$parametresUrl;
108
		return $url;
109
	}
110
 
111
	//+------------------------------------------------------------------------------------------------------+
112
	// Méthodes facilitant les tests
113
 
114
	/**
115
	* Récupère un bouchon de classe abstraite.
116
	* Comment l'utiliser :
117
	* 	$classeAstraite = $this->getClasseAbstraite('MaClasse', array('param1', 'param2'));
118
	*   $foo = $classeAstraite->methodeConcretePublique();
119
	*
120
	* @param String $classeNom Le nom de la classe
121
	* @param Array $parametres Les paramètres à passer au constructeur.
122
	* @return Object Le bouchon de la classe abstraite
123
	*/
124
	public function getClasseAbstraite($classeNom, Array $parametres) {
125
		$efloreScript = $this->getMockForAbstractClass($classeNom, $parametres);
126
		return $efloreScript;
127
	}
128
 
129
	/**
130
	 * Récupère une méthode privée d'une classe pour tester/documenter.
131
	 * Comment l'utiliser :
132
	 * 	MyClass->foo():
133
	 *   $cls = new MyClass();
134
	 *   $foo = self::getPrivateMethode($cls, 'foo');
135
	 *   $foo->invoke($cls, $...);
136
	 *
137
	 * @param object $objet Une instance de votre classe
138
	 * @param string $methode Le nom de la méthode private
139
	 * @return ReflectionMethod La méthode demandée
140
	 */
141
	public static function getMethodePrivee($objet, $nomMethode) {
142
		$classe = new ReflectionClass($objet);
143
		$methode = $classe->getMethod($nomMethode);
144
		$methode->setAccessible(true);
145
		return $methode;
146
	}
147
 
148
	/**
149
	* Récupère une méthode protégée d'une classe pour tester/documenter.
150
	* Comment l'utiliser :
151
	* 	MyClass->foo():
152
	*   $cls = new MyClass();
153
	*   $foo = self::getProtectedMethode($cls, 'foo');
154
	*   $foo->invoke($cls, $...);
155
	* @param object $objet Une instance de votre classe
156
	* @param string $methode Le nom de la méthode protected
157
	* @return ReflectionMethod La méthode demandée
158
	*/
159
	public static function getMethodeProtegee($objet, $nomMethode) {
160
		return self::getMethodePrivee($objet, $nomMethode);
161
	}
162
}
163
?>