Subversion Repositories eFlore/Projets.eflore-projets

Rev

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