Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 1141 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1141 Rev 1142
1
<?php
1
<?php
2
/**
2
/**
3
 * Classe contenant des méthodes :
3
 * Classe contenant des méthodes :
4
 *	- d'intialisation des tests,
4
 *	- d'intialisation des tests,
5
 *	- refactorisant le code des tests,
5
 *	- refactorisant le code des tests,
6
 *	- facilitant les tests.
6
 *	- facilitant les tests.
7
 *
7
 *
8
 * @category	php 5.3
8
 * @category	php 5.3
9
 * @package		Tests/Services
9
 * @package		Tests/Services
10
 * @author		Raphaël Droz <raphael@tela-botanica.org>
10
 * @author		Raphaël Droz <raphael@tela-botanica.org>
11
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
11
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
12
 * @copyright	Copyright (c) 2011, 2013 Tela Botanica (accueil@tela-botanica.org)
12
 * @copyright	Copyright (c) 2011, 2013 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
 */
15
 */
-
 
16
 
16
 
17
define('CONFIG_DIR', __DIR__ . '/../configurations');
17
require_once __DIR__ . '/../framework.php';
18
require_once __DIR__ . '/../framework.php';
-
 
19
abstract class ConsultationEflorePhpUnit extends PHPUnit_Framework_TestCase {
-
 
20
    const URL_API = 'http://localhost/service:eflore:0.1';
-
 
21
	const TPL_URL_BASE = 'http://localhost/consultation/index_botanique.php?referentiel=bdtfx';
-
 
22
	const TPL_URL_BASE_DOSSIER = 'http://localhost/consultation/';
18
abstract class ConsultationEflorePhpUnit extends PHPUnit_Framework_TestCase {
23
	const TPL_URL_FICHE = 'http://localhost/consultation/index_botanique.php?referentiel=bdtfx&module=fiche&action=fiche&nn=%s';
19
 
24
 
20
	//+------------------------------------------------------------------------------------------------------+
25
	//+------------------------------------------------------------------------------------------------------+
21
	// Intialisation
26
	// Intialisation
22
 
27
 
23
    /* absolument nécessaire pour que Registre::$statics et Config::$statics soient réinitialisés lors
28
    /* absolument nécessaire pour que Registre::$statics et Config::$statics soient réinitialisés lors
24
       de multiples tests successifs (notamment pour le moteur de recherche).
29
       de multiples tests successifs (notamment pour le moteur de recherche).
25
       *Et* l'annotation de setUpBeforeClass()
30
       *Et* l'annotation de setUpBeforeClass()
26
       *et* l'attribut $backupStaticAttributes
31
       *et* l'attribut $backupStaticAttributes
27
       *et* l'accès fictif @Registre::get(NULL);
32
       *et* l'accès fictif @Registre::get(NULL);
28
       sont tous trois nécessaires */
33
       sont tous trois nécessaires */
29
    protected $backupStaticAttributes = true;
34
    protected $backupStaticAttributes = true;
30
 
35
 
31
    /**
36
    /**
32
     * @backupStaticAttributes enabled
37
     * @backupStaticAttributes enabled
33
     */
38
     */
34
	public static function setUpBeforeClass() {
39
	public static function setUpBeforeClass() {
35
		error_reporting(E_ALL);
40
		error_reporting(E_ALL);
36
        if(!Framework::getCheminAppli()) {
41
        if(!Framework::getCheminAppli()) {
37
            Framework::setCheminAppli(__DIR__ . '/../');
42
            Framework::setCheminAppli(__DIR__ . '/../');
38
            // Enregistrement en première position des autoload de la méthode gérant les classes des services
43
            // Enregistrement en première position des autoload de la méthode gérant les classes des services
39
            spl_autoload_register(array(get_class(), 'chargerClasseAuto'));
44
            spl_autoload_register(array(get_class(), 'chargerClasseAuto'));
40
        }
45
        }
41
        Registre::get(NULL);
46
        Registre::get(NULL);
42
	}
47
	}
43
 
48
 
44
	public static function chargerClasseAuto($classe) {
49
	public static function chargerClasseAuto($classe) {
45
		//echo $classe."\n";
50
		//echo $classe."\n";
46
		if (class_exists($classe)) {
51
		if (class_exists($classe)) {
47
			return null;
52
			return null;
48
		}
53
		}
49
		
54
		
50
		$cheminsTests = __DIR__.'/';
55
		$cheminsTests = __DIR__.'/';
-
 
56
		// $cheminBibliotheque = realpath(__DIR__.'/../bibliotheque/').'/';
51
		$cheminMetier = realpath(__DIR__.'/../metier/api_0.1').'/';
57
		$cheminMetier = realpath(__DIR__.'/../metier/api_0.1').'/';
52
		$cheminModule = realpath(__DIR__.'/../modules/').'/';
58
		$cheminModule = realpath(__DIR__.'/../modules/').'/';
-
 
59
		$cheminFormateurs = realpath(__DIR__.'/../modules/fiche/formateurs/').'/'; // pour FicheTest::testExecuterFiche()
53
		$chemins = array($cheminMetier, $cheminModule, $cheminsTests);
60
		$chemins = array(/* $cheminBibliotheque, */ $cheminMetier, $cheminModule, $cheminsTests, $cheminFormateurs);
54
		foreach ($chemins as $chemin) {
61
		foreach ($chemins as $chemin) {
55
			$cheminCourt = $chemin.$classe.'.php';
62
			$cheminCourt = $chemin.$classe.'.php';
56
			$module = strtolower(preg_replace('/([A-Z])/', '_\\1', lcfirst($classe)));
63
			$module = strtolower(preg_replace('/([A-Z])/', '_\\1', lcfirst($classe)));
57
			$cheminLong = $chemin.$module.'/'.$classe.'.php';
64
			$cheminLong = $chemin.$module.'/'.$classe.'.php';
58
			//echo $cheminCourt."\n".$cheminLong."\n";
65
			//echo $cheminCourt."\n".$cheminLong."\n";
59
			if (file_exists($cheminCourt)) {
66
			if (file_exists($cheminCourt)) {
60
				require_once $cheminCourt;
67
				require_once $cheminCourt;
61
			} elseif (file_exists($cheminLong)) {
68
			} elseif (file_exists($cheminLong)) {
62
				require_once $cheminLong;
69
				require_once $cheminLong;
63
			}
70
			}
64
		}
71
		}
65
	}
72
	}
66
 
73
 
67
	//+------------------------------------------------------------------------------------------------------+
74
	//+------------------------------------------------------------------------------------------------------+
68
	// Refactorisation
75
	// Refactorisation
69
	protected function consulterJson($ressources, $parametres) {
76
	protected function consulterJson($ressources, $parametres) {
70
		$retourJson = $this->consulterBrut($ressources, $parametres);
77
		$retourJson = $this->consulterBrut($ressources, $parametres);
71
		$retour = json_decode($retourJson, true);
78
		$retour = json_decode($retourJson, true);
72
		$this->assertEquals(JSON_ERROR_NONE, json_last_error(), "Le json contient des erreurs qui bloquent le décodage. Voir : $url");
79
		$this->assertEquals(JSON_ERROR_NONE, json_last_error(), "Le json contient des erreurs qui bloquent le décodage. Voir : $url");
73
		return $retour;
80
		return $retour;
74
	}
81
	}
75
 
82
 
76
	protected function consulterBrut($ressources, $parametres) {
83
	protected function consulterBrut($ressources, $parametres) {
77
		array_unshift($ressources, $this->service);
84
		array_unshift($ressources, $this->service);
78
		array_unshift($ressources, $this->projet);
85
		array_unshift($ressources, $this->projet);
79
		$projets = new Projets();
86
		$projets = new Projets();
80
		$retourJson = $projets->consulter($ressources, $parametres);
87
		$retourJson = $projets->consulter($ressources, $parametres);
81
		return $retourJson;
88
		return $retourJson;
82
	}
89
	}
83
 
90
 
84
	protected function creerUrl($ressources, $parametres) {
91
	protected function creerUrl($ressources, $parametres) {
85
		$version = '';
92
		$version = '';
86
		$ressourcesUrl = array();
93
		$ressourcesUrl = array();
87
		foreach ($ressources as $ressource) {
94
		foreach ($ressources as $ressource) {
88
			$ressourcesUrl[] = $ressource;
95
			$ressourcesUrl[] = $ressource;
89
		}
96
		}
90
		$ressourcesUrl = count($ressourcesUrl) > 0 ? '/'.implode('/', $ressourcesUrl) : '';
97
		$ressourcesUrl = count($ressourcesUrl) > 0 ? '/'.implode('/', $ressourcesUrl) : '';
91
 
98
 
92
		$parametresUrl = '';
99
		$parametresUrl = '';
93
		if (count($parametres) > 0) {
100
		if (count($parametres) > 0) {
94
			foreach ($parametres as $cle => $valeur) {
101
			foreach ($parametres as $cle => $valeur) {
95
				$parametresUrl[] = $cle.'='.rawurlencode($valeur);
102
				$parametresUrl[] = $cle.'='.rawurlencode($valeur);
96
			}
103
			}
97
			$parametresUrl = '?'.implode('&', $parametresUrl);
104
			$parametresUrl = '?'.implode('&', $parametresUrl);
98
		}
105
		}
99
 
106
 
100
		return 'http://localhost/service:eflore:0.1'.$ressourcesUrl.$parametresUrl;
107
		return self::URL_API.$ressourcesUrl.$parametresUrl;
101
	}
108
	}
102
 
109
 
103
	//+------------------------------------------------------------------------------------------------------+
110
	//+------------------------------------------------------------------------------------------------------+
104
	// Méthodes facilitant les tests
111
	// Méthodes facilitant les tests
105
	
112
	
106
	/**
113
	/**
107
	* Récupère un bouchon de classe abstraite.
114
	* Récupère un bouchon de classe abstraite.
108
	* Comment l'utiliser :
115
	* Comment l'utiliser :
109
	*	$classeAstraite = $this->getClasseAbstraite('MaClasse', array('param1', 'param2'));
116
	*	$classeAstraite = $this->getClasseAbstraite('MaClasse', array('param1', 'param2'));
110
	*	$foo = $classeAstraite->methodeConcretePublique();
117
	*	$foo = $classeAstraite->methodeConcretePublique();
111
	*
118
	*
112
	* @param String $classeNom Le nom de la classe
119
	* @param String $classeNom Le nom de la classe
113
	* @param Array $parametres Les paramètres à passer au constructeur.
120
	* @param Array $parametres Les paramètres à passer au constructeur.
114
	* @return Object Le bouchon de la classe abstraite
121
	* @return Object Le bouchon de la classe abstraite
115
	*/
122
	*/
116
	public function getClasseAbstraite($classeNom, Array $parametres) {
123
	public function getClasseAbstraite($classeNom, Array $parametres) {
117
		return $this->getMockForAbstractClass($classeNom, $parametres);
124
		return $this->getMockForAbstractClass($classeNom, $parametres);
118
	}
125
	}
119
 
126
 
120
	/**
127
	/**
121
	 * Récupère une méthode privée d'une classe pour tester/documenter.
128
	 * Récupère une méthode privée d'une classe pour tester/documenter.
122
	 * Comment l'utiliser :
129
	 * Comment l'utiliser :
123
	 *	MyClass->foo():
130
	 *	MyClass->foo():
124
	 *	 $cls = new MyClass();
131
	 *	 $cls = new MyClass();
125
	 *	 $foo = self::getPrivateMethode($cls, 'foo');
132
	 *	 $foo = self::getPrivateMethode($cls, 'foo');
126
	 *	 $foo->invoke($cls, $...);
133
	 *	 $foo->invoke($cls, $...);
127
	 *
134
	 *
128
	 * @param object $objet Une instance de votre classe
135
	 * @param object $objet Une instance de votre classe
129
	 * @param string $methode Le nom de la méthode private
136
	 * @param string $methode Le nom de la méthode private
130
	 * @return ReflectionMethod La méthode demandée
137
	 * @return ReflectionMethod La méthode demandée
131
	 */
138
	 */
132
	public static function getMethodePrivee($objet, $nomMethode) {
139
	public static function getMethodePrivee($objet, $nomMethode) {
133
		$classe = new ReflectionClass($objet);
140
		$classe = new ReflectionClass($objet);
134
		$methode = $classe->getMethod($nomMethode);
141
		$methode = $classe->getMethod($nomMethode);
135
		$methode->setAccessible(true);
142
		$methode->setAccessible(true);
136
		return $methode;
143
		return $methode;
137
	}
144
	}
138
 
145
 
139
	/**
146
	/**
140
	* Récupère une méthode protégée d'une classe pour tester/documenter.
147
	* Récupère une méthode protégée d'une classe pour tester/documenter.
141
	* Comment l'utiliser :
148
	* Comment l'utiliser :
142
	*	MyClass->foo():
149
	*	MyClass->foo():
143
	*	$cls = new MyClass();
150
	*	$cls = new MyClass();
144
	*	$foo = self::getProtectedMethode($cls, 'foo');
151
	*	$foo = self::getProtectedMethode($cls, 'foo');
145
	*	$foo->invoke($cls, $...);
152
	*	$foo->invoke($cls, $...);
146
	* @param object $objet Une instance de votre classe
153
	* @param object $objet Une instance de votre classe
147
	* @param string $methode Le nom de la méthode protected
154
	* @param string $methode Le nom de la méthode protected
148
	* @return ReflectionMethod La méthode demandée
155
	* @return ReflectionMethod La méthode demandée
149
	*/
156
	*/
150
	public static function getMethodeProtegee($objet, $nomMethode) {
157
	public static function getMethodeProtegee($objet, $nomMethode) {
151
		return self::getMethodePrivee($objet, $nomMethode);
158
		return self::getMethodePrivee($objet, $nomMethode);
152
	}
159
	}
153
}
160
}
154
?>
161
?>