Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 213 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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