Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 1141 | Details | Compare with Previous | Last modification | View Log | RSS feed

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