/[openfoncier]/trunk/tests/testImportSpecific_common.php
ViewVC logotype

Annotation of /trunk/tests/testImportSpecific_common.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9227 - (hide annotations)
Fri Mar 27 14:02:58 2020 UTC (4 years, 10 months ago) by fmichon
Original Path: branches/4.13.0-develop/tests/testImportSpecific_common.php
File size: 12713 byte(s)
* Tests : Compatibilité phpunit 7 et phpunit 8 et réorganisation des tests phpunit pour support à la fois des versions PHP7.0 & PHP7.1+.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26