Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 203 | 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
206 jpm 79
	protected function consulter($ressources, $parametres) {
195 jpm 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);
206 jpm 89
		$projet = new Projets();
90
		$retourJson = $projet->consulter($ressources, $parametres);
195 jpm 91
		return $retourJson;
92
	}
93
 
94
	protected function creerUrl($ressources, $parametres) {
95
		$version = '';
96
		$ressourcesUrl = array();
97
		foreach ($ressources as $ressource) {
98
			$ressourcesUrl[] = $ressource;
99
		}
100
		$ressourcesUrl = count($ressourcesUrl) > 0 ? '/'.implode('/', $ressourcesUrl) : '';
101
 
102
		$parametresUrl = '';
103
		if (count($parametres) > 0) {
104
			foreach ($parametres as $cle => $valeur) {
105
				$parametresUrl[] = $cle.'='.rawurlencode($valeur);
106
			}
107
			$parametresUrl = '?'.implode('&', $parametresUrl);
108
		}
203 jpm 109
		$url = $this->baseUrl.$this->projet.'/'.$version.$this->service.$ressourcesUrl.$parametresUrl;
195 jpm 110
		return $url;
111
	}
112
 
113
	//+------------------------------------------------------------------------------------------------------+
114
	// Méthodes facilitant les tests
115
 
116
	/**
117
	* Récupère un bouchon de classe abstraite.
118
	* Comment l'utiliser :
119
	* 	$classeAstraite = $this->getClasseAbstraite('MaClasse', array('param1', 'param2'));
120
	*   $foo = $classeAstraite->methodeConcretePublique();
121
	*
122
	* @param String $classeNom Le nom de la classe
123
	* @param Array $parametres Les paramètres à passer au constructeur.
124
	* @return Object Le bouchon de la classe abstraite
125
	*/
126
	public function getClasseAbstraite($classeNom, Array $parametres) {
127
		$efloreScript = $this->getMockForAbstractClass($classeNom, $parametres);
128
		return $efloreScript;
129
	}
130
 
131
	/**
132
	 * Récupère une méthode privée d'une classe pour tester/documenter.
133
	 * Comment l'utiliser :
134
	 * 	MyClass->foo():
135
	 *   $cls = new MyClass();
136
	 *   $foo = self::getPrivateMethode($cls, 'foo');
137
	 *   $foo->invoke($cls, $...);
138
	 *
139
	 * @param object $objet Une instance de votre classe
140
	 * @param string $methode Le nom de la méthode private
141
	 * @return ReflectionMethod La méthode demandée
142
	 */
143
	public static function getMethodePrivee($objet, $nomMethode) {
144
		$classe = new ReflectionClass($objet);
145
		$methode = $classe->getMethod($nomMethode);
146
		$methode->setAccessible(true);
147
		return $methode;
148
	}
149
 
150
	/**
151
	* Récupère une méthode protégée d'une classe pour tester/documenter.
152
	* Comment l'utiliser :
153
	* 	MyClass->foo():
154
	*   $cls = new MyClass();
155
	*   $foo = self::getProtectedMethode($cls, 'foo');
156
	*   $foo->invoke($cls, $...);
157
	* @param object $objet Une instance de votre classe
158
	* @param string $methode Le nom de la méthode protected
159
	* @return ReflectionMethod La méthode demandée
160
	*/
161
	public static function getMethodeProtegee($objet, $nomMethode) {
162
		return self::getMethodePrivee($objet, $nomMethode);
163
	}
164
}
165
?>