Subversion Repositories eFlore/Projets.eflore-projets

Rev

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