1 |
<?php |
2 |
/** |
3 |
* Ce fichier regroupe les tests unitaires de l'import spécifique de CSV |
4 |
* |
5 |
* @package openfoncier |
6 |
* @version SVN : $Id$ |
7 |
*/ |
8 |
require_once "../obj/utils.class.php"; |
9 |
require_once "../obj/import_specific.class.php"; |
10 |
/** |
11 |
* Tests unitaires des contrôles et manipulations des données récupérées |
12 |
* depuis le fichier CSV; |
13 |
*/ |
14 |
class ImportSpecific extends PHPUnit_Framework_TestCase { |
15 |
|
16 |
/** |
17 |
* Méthode lancée en début de chaque test case |
18 |
*/ |
19 |
public function setUp() { |
20 |
// Instancie la timezone |
21 |
date_default_timezone_set('Europe/Paris'); |
22 |
echo ' = '.get_class().'.'.str_replace('test_', '', $this->getName())."\r\n"; |
23 |
$this->init_data_test(); |
24 |
// Instance de la classe Utils |
25 |
require_once "../obj/utils.class.php"; |
26 |
$_SESSION['collectivite'] = 1; |
27 |
$_SESSION['login'] = "admin"; |
28 |
$_SERVER['REQUEST_URI'] = ""; |
29 |
$f = new utils("nohtml"); |
30 |
// Instancie la classe à tester |
31 |
$this->inst = new import_specific(); |
32 |
$this->inst->f = $f; |
33 |
|
34 |
} |
35 |
|
36 |
public function onNotSuccessfulTest(Exception $e){ |
37 |
echo 'Line '.$e->getLine().' : '.$e->getMessage()."\r\n"; |
38 |
parent::onNotSuccessfulTest($e); |
39 |
} |
40 |
|
41 |
/** |
42 |
* Jeu de données : deux tableaux pour chaque test : |
43 |
* colonnes et valeurs. |
44 |
* |
45 |
* Les colonnes sont commentées par un exemple du CSV comme suit : |
46 |
* COL (débute à 0) - Libellé header |
47 |
*/ |
48 |
function init_data_test() { |
49 |
|
50 |
// check_required() |
51 |
$this->required_column = array( |
52 |
// COL 0 - Type |
53 |
"type" => array( |
54 |
"require" => true, |
55 |
"header" => "Type", |
56 |
), |
57 |
// COL 1 - Numéro |
58 |
"numero" => array( |
59 |
"require" => true, |
60 |
"header" => "Numero", |
61 |
), |
62 |
// COL 5 - INSEE |
63 |
"insee" => array( |
64 |
"require" => false, |
65 |
"header" => "INSEE", |
66 |
), |
67 |
); |
68 |
$this->required_row_data = array( |
69 |
"type" => "", |
70 |
"numero" => "10", |
71 |
"insee" => "", |
72 |
); |
73 |
|
74 |
// check_type() |
75 |
$this->type_column = array( |
76 |
// COL 38 - Date envoi demande de pièces |
77 |
0 => array( |
78 |
"type" => "date", |
79 |
"header" => "Date envoi demande de pieces", |
80 |
), |
81 |
1 => array( |
82 |
"type" => "date", |
83 |
"header" => "Date envoi demande de pieces", |
84 |
), |
85 |
// COL 8 - Projet |
86 |
2 => array( |
87 |
"type" => "string", |
88 |
"header" => "Projet", |
89 |
), |
90 |
3 => array( |
91 |
"type" => "string", |
92 |
"header" => "Projet", |
93 |
), |
94 |
// COL 10 - Nb logements |
95 |
4 => array( |
96 |
"type" => "integer", |
97 |
"header" => "Nb logements", |
98 |
), |
99 |
5 => array( |
100 |
"type" => "integer", |
101 |
"header" => "Nb logements", |
102 |
), |
103 |
6 => array( |
104 |
"type" => "integer", |
105 |
"header" => "Nb logements", |
106 |
), |
107 |
// COL 11 - Surface terrain |
108 |
7 => array( |
109 |
"type" => "float", |
110 |
"header" => "Surface terrain", |
111 |
), |
112 |
8 => array( |
113 |
"type" => "float", |
114 |
"header" => "Surface terrain", |
115 |
), |
116 |
// COL 24 - Lotissement |
117 |
9 => array( |
118 |
"type" => "boolean", |
119 |
"header" => "Lotissement", |
120 |
), |
121 |
10 => array( |
122 |
"type" => "boolean", |
123 |
"header" => "Lotissement", |
124 |
), |
125 |
); |
126 |
$this->type_row_data = array( |
127 |
0 => "10/10/2015", |
128 |
1 => "101/5/2015", |
129 |
2 => "chaîne valide", |
130 |
4 => "50", |
131 |
5 => "50a", |
132 |
6 => "50,5", |
133 |
7 => "50,5", |
134 |
8 => "50a", |
135 |
9 => "Oui", |
136 |
10 => "true", |
137 |
); |
138 |
|
139 |
$this->type_return_expected = array( |
140 |
// Cas 1 : date valide |
141 |
0 =>true, |
142 |
// Cas 2 : date invalide |
143 |
1 =>false, |
144 |
// Cas 3 : chaîne de caractères valide |
145 |
2 =>true, |
146 |
// Cas 5 : nombre entier valide |
147 |
4 =>true, |
148 |
// Cas 6 : nombre entier invalide |
149 |
5 =>false, |
150 |
// Cas 7 : nombre entier invalide |
151 |
6 =>false, |
152 |
// Cas 8 : nombre décimal valide |
153 |
7 =>true, |
154 |
// Cas 9 : nombre décimal invalide |
155 |
8 =>false, |
156 |
// Cas 10 : booléen valide |
157 |
9 =>true, |
158 |
// Cas 11 : booléen invalide |
159 |
10 =>false |
160 |
); |
161 |
|
162 |
// check_foreign_key() |
163 |
$this->fk_column = array( |
164 |
// COL 0 - Type |
165 |
0 => array( |
166 |
"foreign_key" => array( |
167 |
"sql"=>"SELECT dossier_autorisation_type_detaille FROM openads.dossier_autorisation_type_detaille WHERE code ='<value>'", |
168 |
"table" => "dossier_autorisation_type_detaille", |
169 |
"field" => "code", |
170 |
), |
171 |
"header" => "Type", |
172 |
), |
173 |
1 => array( |
174 |
"foreign_key" => array( |
175 |
"sql"=>"SELECT dossier_autorisation_type_detaille FROM openads.dossier_autorisation_type_detaille WHERE code ='<value>'", |
176 |
"table" => "dossier_autorisation_type_detaille", |
177 |
"field" => "code", |
178 |
), |
179 |
"header" => "Type", |
180 |
), |
181 |
); |
182 |
$this->fk_row_data = array( |
183 |
0 => "PCI", |
184 |
1 => "lambda", |
185 |
); |
186 |
|
187 |
// set_linked_value() |
188 |
$this->linked_values_column = array( |
189 |
// COL 0 - Type |
190 |
0 => array( |
191 |
"link" => array( |
192 |
"PC MI" => "PI", |
193 |
), |
194 |
"header" => "Type", |
195 |
), |
196 |
1 => array( |
197 |
"link" => array( |
198 |
"PC MI" => "PI", |
199 |
), |
200 |
"header" => "Type", |
201 |
), |
202 |
); |
203 |
$this->linked_values_row_data = array( |
204 |
0 => "PCI", |
205 |
1 => "PC MI", |
206 |
); |
207 |
} |
208 |
|
209 |
/** |
210 |
* Teste la méthode check_required() de la classe import_specific. |
211 |
* Est-ce qu'il y a une valeur si champ obligatoire ? |
212 |
* |
213 |
* @param $key [integer] colonne du fichier CSV |
214 |
* @param $value [string] valeur à tester |
215 |
* @return [string] message d'erreur ou chaîne vide si succès |
216 |
*/ |
217 |
public function test_check_required() { |
218 |
// Message d'erreur |
219 |
$error = _("La colonne %s est obligatoire"); |
220 |
// Utilisation du jeu de données |
221 |
$this->inst->column = $this->required_column; |
222 |
$this->inst->line = $this->required_row_data; |
223 |
// Cas 1 : champ requis vide |
224 |
$ret = $this->inst->check_required("type"); |
225 |
$this->assertEquals($ret, false); |
226 |
// Cas 2 : champ requis renseigné |
227 |
$ret = $this->inst->check_required("numero"); |
228 |
$this->assertEquals($ret, true); |
229 |
// Cas 3 : champ non requis vide |
230 |
$ret = $this->inst->check_required("insee"); |
231 |
$this->assertEquals($ret, true); |
232 |
} |
233 |
|
234 |
/** |
235 |
* Teste la méthode check_type() de la classe import_specific. |
236 |
* Est-ce que la valeur est valide pour le type attendu ? |
237 |
* |
238 |
* @param $key [integer] colonne du fichier CSV |
239 |
* @param $value [string] valeur à tester |
240 |
* @return [string] message d'erreur ou chaîne vide si succès |
241 |
*/ |
242 |
public function test_check_type() { |
243 |
|
244 |
// Utilisation du jeu de données |
245 |
$this->inst->column = $this->type_column; |
246 |
$this->inst->line = $this->type_row_data; |
247 |
// Définition des résultats attendus |
248 |
$this->type_return_expected; |
249 |
// Pour chaque colonne testée |
250 |
foreach ($this->inst->line as $key => $value) { |
251 |
// Vérification du retour de la méthode |
252 |
$this->assertEquals( |
253 |
$this->inst->check_type($key), |
254 |
$this->type_return_expected[$key] |
255 |
); |
256 |
} |
257 |
} |
258 |
|
259 |
/** |
260 |
* Teste la méthode get_foreign_key_id() de la classe import_specific. |
261 |
* Est-ce que la clé étrangère existe ? |
262 |
* |
263 |
* @param $key [integer] colonne du fichier CSV |
264 |
* @return [string] message d'erreur ou chaîne vide si succès |
265 |
*/ |
266 |
public function test_get_foreign_key_id() { |
267 |
|
268 |
// Utilisation du jeu de données |
269 |
$this->inst->column = $this->fk_column; |
270 |
$this->inst->line = $this->fk_row_data; |
271 |
// Cas 1 : clé étrangère valide |
272 |
$ret = $this->inst->get_foreign_key_id(0); |
273 |
$this->assertEquals($ret, true); |
274 |
// Cas 2 : clé étrangère invalide |
275 |
$ret = $this->inst->get_foreign_key_id(1); |
276 |
$this->assertEquals($ret, false); |
277 |
} |
278 |
|
279 |
/** |
280 |
* Teste la méthode explode_address() de la classe import_specific. |
281 |
* Doit retourner un tableau d'éléments composant l'adresse fournie. |
282 |
* |
283 |
* @param $value [string] adresse complète |
284 |
* @return [array] tableau (numéro, rue, CP, ville, ...) |
285 |
*/ |
286 |
public function test_explode_address() { |
287 |
// Cas 1 : adresse 'normale' à transformer |
288 |
$adresse_complete1 = "27 bis Avenue Jules Cantini 13008 Marseille France"; |
289 |
// Récupération de la transformation |
290 |
$adresse_triee1 = $this->inst->explode_address($adresse_complete1); |
291 |
// Vérification des éléments |
292 |
$this->assertEquals($adresse_triee1['numero'], '27'); |
293 |
$this->assertEquals($adresse_triee1['voie'], 'bis Avenue Jules Cantini'); |
294 |
$this->assertEquals($adresse_triee1['complement1'], ''); |
295 |
$this->assertEquals($adresse_triee1['complement2'], ''); |
296 |
$this->assertEquals($adresse_triee1['cp'], '13008'); |
297 |
$this->assertEquals($adresse_triee1['ville'], 'Marseille'); |
298 |
// Cas 2 : adresse longue (libellée voie supérieur à 30 caractères) |
299 |
$adresse_complete2 = "27 bis Avenue Jules Cantini Boulevard de la Blancarde 06001 Nice"; |
300 |
// Récupération de la transformation |
301 |
$adresse_triee2 = $this->inst->explode_address($adresse_complete2); |
302 |
// Vérification des éléments |
303 |
$this->assertEquals($adresse_triee2['numero'], '27'); |
304 |
$this->assertEquals($adresse_triee2['voie'], 'bis Avenue Jules Cantini'); |
305 |
$this->assertEquals($adresse_triee2['complement1'], 'Boulevard de la Blancarde'); |
306 |
$this->assertEquals($adresse_triee2['complement2'], ''); |
307 |
$this->assertEquals($adresse_triee2['cp'], '06001'); |
308 |
$this->assertEquals($adresse_triee2['ville'], 'Nice'); |
309 |
// Cas 3 : adresse très longue (libellée voie supérieur à 60 caractères) |
310 |
// Numéro composé suivi d'une virgule |
311 |
$adresse_complete3 = "11-12, Place des grands hommes Boulevard du Panthéon Route de Paris 13380 Plan de Cuques"; |
312 |
// Récupération de la transformation |
313 |
$adresse_triee3 = $this->inst->explode_address($adresse_complete3); |
314 |
// Vérification des éléments |
315 |
$this->assertEquals($adresse_triee3['numero'], '11-12'); |
316 |
$this->assertEquals($adresse_triee3['voie'], 'Place des grands hommes'); |
317 |
$this->assertEquals($adresse_triee3['complement1'], 'Boulevard du Panthéon Route'); |
318 |
$this->assertEquals($adresse_triee3['complement2'], 'de Paris'); |
319 |
$this->assertEquals($adresse_triee3['cp'], '13380'); |
320 |
$this->assertEquals($adresse_triee3['ville'], 'Plan de Cuques'); |
321 |
} |
322 |
|
323 |
/** |
324 |
* Teste la méthode set_linked_value() qui subsitue les valeurs du CSV |
325 |
* par le paramétrage. |
326 |
* |
327 |
* @param $key [integer] colonne du fichier CSV |
328 |
* @return [void] |
329 |
*/ |
330 |
function test_set_linked_value(){ |
331 |
// Utilisation du jeu de données |
332 |
$this->inst->column = $this->linked_values_column; |
333 |
$this->inst->line = $this->linked_values_row_data; |
334 |
// Cas 1 : aucune correspondance |
335 |
// PCI reste PCI |
336 |
$this->inst->set_linked_value(0); |
337 |
$this->assertEquals($this->inst->line[0], "PCI"); |
338 |
// Cas 2 : existance d'une correspondance |
339 |
// PC MI devient PI |
340 |
$this->inst->set_linked_value(1); |
341 |
$this->assertEquals($this->inst->line[1], "PI"); |
342 |
} |
343 |
} |
344 |
|
345 |
?> |