Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 195 | Go to most recent revision | Details | Compare with Previous | 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
 
203 jpm 19
	protected static $cheminBase = '';
20
	protected $projet = '';
195 jpm 21
	protected $service = '';
22
	protected $classeTestee = '';
23
	protected $baseUrl = 'http://localhost/service:eflore:0.1/';
24
	protected $serviceBaseUrl = '';
25
	//+------------------------------------------------------------------------------------------------------+
26
	// Intialisation
27
 
28
	public static function setUpBeforeClass() {
29
		error_reporting(E_ALL);
30
		self::chargerFramework();
203 jpm 31
		self::$cheminBase = realpath(__DIR__.'/../../modules/0.2').'/';
195 jpm 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
		}
203 jpm 40
 
195 jpm 41
		$cheminBibliotheque = realpath(__DIR__.'/../../bibliotheque').'/';
203 jpm 42
		$cheminBiblioInterfaces = $cheminBibliotheque.'interfaces/';
195 jpm 43
		$cheminBiblioNom = $cheminBibliotheque.'nom/';
44
		$cheminBiblioNomDeco = $cheminBiblioNom.'decorateurs/';
45
		$cheminsTests = __DIR__.'/';
203 jpm 46
		$chemins = array(
47
			self::$cheminBase,
48
			$cheminBibliotheque,
49
			$cheminsTests,
50
			$cheminBiblioInterfaces,
51
			$cheminBiblioNom,
195 jpm 52
			$cheminBiblioNomDeco);
203 jpm 53
 
195 jpm 54
		foreach ($chemins as $chemin) {
55
			$chemin = $chemin.$classe.'.php';
56
			if (file_exists($chemin)) {
57
				require_once $chemin;
58
			}
59
		}
60
	}
61
 
62
	private static function chargerFramework() {
63
		$cheminRacine = realpath(dirname(__FILE__).'/../..').'/';
64
		$framework =  $cheminRacine.'framework.php';
65
		if (!file_exists($framework)) {
66
			$e = "Veuillez paramétrer l'emplacement et la version du Framework dans le fichier $framework";
67
			trigger_error($e, E_USER_ERROR);
68
		} else {
69
			// Inclusion du Framework
70
			require_once $framework;
71
 
72
			// Ajout d'information concernant cette application
73
			Framework::setCheminAppli($cheminRacine);// Obligatoire
74
		}
75
	}
76
 
77
	//+------------------------------------------------------------------------------------------------------+
78
	// Refactorisation
79
	protected function consulterJson($ressources, $parametres) {
80
		$retourJson = $this->consulterBrut($ressources, $parametres);
81
		$retour = json_decode($retourJson, true);
82
		$this->assertEquals(JSON_ERROR_NONE, json_last_error(), "Le json contient des erreurs qui bloquent le décodage. Voir : $url");
83
		return $retour;
84
	}
85
 
86
	protected function consulterBrut($ressources, $parametres) {
87
		array_unshift($ressources, $this->service);
203 jpm 88
		array_unshift($ressources, $this->projet);
195 jpm 89
		$Bdd = new Bdd();
90
		$Ressources = new Ressources($ressources);
91
		$Parametres = new Parametres($parametres, $Bdd);
203 jpm 92
		require_once self::$cheminBase.$this->projet.'/'.$this->classeTestee.'.php';
195 jpm 93
		$objetService = new $this->classeTestee($Ressources, $Parametres, $Bdd);
94
		$this->initialiserService($objetService);
95
		$retourJson = $objetService->consulter();
96
		return $retourJson;
97
	}
98
 
99
	protected function creerUrl($ressources, $parametres) {
100
		$version = '';
101
		$ressourcesUrl = array();
102
		foreach ($ressources as $ressource) {
103
			$ressourcesUrl[] = $ressource;
104
		}
105
		$ressourcesUrl = count($ressourcesUrl) > 0 ? '/'.implode('/', $ressourcesUrl) : '';
106
 
107
		$parametresUrl = '';
108
		if (count($parametres) > 0) {
109
			foreach ($parametres as $cle => $valeur) {
110
				$parametresUrl[] = $cle.'='.rawurlencode($valeur);
111
			}
112
			$parametresUrl = '?'.implode('&', $parametresUrl);
113
		}
203 jpm 114
		$url = $this->baseUrl.$this->projet.'/'.$version.$this->service.$ressourcesUrl.$parametresUrl;
195 jpm 115
		return $url;
116
	}
117
 
118
	//+------------------------------------------------------------------------------------------------------+
119
	// Méthodes facilitant les tests
120
 
121
	/**
122
	* Récupère un bouchon de classe abstraite.
123
	* Comment l'utiliser :
124
	* 	$classeAstraite = $this->getClasseAbstraite('MaClasse', array('param1', 'param2'));
125
	*   $foo = $classeAstraite->methodeConcretePublique();
126
	*
127
	* @param String $classeNom Le nom de la classe
128
	* @param Array $parametres Les paramètres à passer au constructeur.
129
	* @return Object Le bouchon de la classe abstraite
130
	*/
131
	public function getClasseAbstraite($classeNom, Array $parametres) {
132
		$efloreScript = $this->getMockForAbstractClass($classeNom, $parametres);
133
		return $efloreScript;
134
	}
135
 
136
	/**
137
	 * Récupère une méthode privée d'une classe pour tester/documenter.
138
	 * Comment l'utiliser :
139
	 * 	MyClass->foo():
140
	 *   $cls = new MyClass();
141
	 *   $foo = self::getPrivateMethode($cls, 'foo');
142
	 *   $foo->invoke($cls, $...);
143
	 *
144
	 * @param object $objet Une instance de votre classe
145
	 * @param string $methode Le nom de la méthode private
146
	 * @return ReflectionMethod La méthode demandée
147
	 */
148
	public static function getMethodePrivee($objet, $nomMethode) {
149
		$classe = new ReflectionClass($objet);
150
		$methode = $classe->getMethod($nomMethode);
151
		$methode->setAccessible(true);
152
		return $methode;
153
	}
154
 
155
	/**
156
	* Récupère une méthode protégée d'une classe pour tester/documenter.
157
	* Comment l'utiliser :
158
	* 	MyClass->foo():
159
	*   $cls = new MyClass();
160
	*   $foo = self::getProtectedMethode($cls, 'foo');
161
	*   $foo->invoke($cls, $...);
162
	* @param object $objet Une instance de votre classe
163
	* @param string $methode Le nom de la méthode protected
164
	* @return ReflectionMethod La méthode demandée
165
	*/
166
	public static function getMethodeProtegee($objet, $nomMethode) {
167
		return self::getMethodePrivee($objet, $nomMethode);
168
	}
169
}
170
?>