Subversion Repositories eFlore/Applications.cel

Rev

Rev 1793 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1768 raphael 1
<?php
2
 
1775 raphael 3
$options = getopt("hu:d:e:p:P:D",array("help", "user:", "domain:", "email:", "pass:", "phpsessid", "debug"));
1768 raphael 4
if(isset($options['h']) || isset($options['help'])) {
1793 raphael 5
	printf(<<<EOF
6
%s [fichiers de test]
7
  --help|-h: cette aide
8
  -debug|-d: informations sur les requêtes curl
9
  [-u|--user = 22506]
10
  [-d|--domain = "http://cel"]
11
  --pass|-p:  pour l'authentifiation
12
  --email|-e: pour l'authentifiation
13
  --phpsessid|-P: pour l'authentifiation, alternative à -e/-p
14
 
15
* --phpsessid permet de passer un identifiant de session PHP pour effectuer l'export.
16
Exemple:
17
$ sqlite3 ~/.mozilla/firefox/*.default/cookies.sqlite<<<"SELECT * FROM moz_cookies WHERE baseDomain = 'cel' AND name = 'PHPSESSID';"
18
 
19
* [fichiers de test]: un ou plusieurs fichier(s) de test valide(s), commençant par "phptests/" et finissant en ".test.php"
20
  Exemple: phptests/a*.test.php ou bien phptests/latitude-virgule.test.php
21
 
22
Exemple:
23
$ php IO-fields-unittest.php -P s97knu4axzi6440n1ihja9ppk1
24
$ php IO-fields-unittest.php -e me@tela -p blah -u 23004 -d http://localhost phptests/determ-espece*
25
 
1794 raphael 26
Attention: lancer un test supprime TOUTES les observations existantes de l'utilisateur !
1793 raphael 27
EOF
28
, /* relax emacs*/
29
		   basename(__FILE__));
30
	die;
1768 raphael 31
}
32
 
1793 raphael 33
// stocke les test
34
define('_TESTDIR', dirname(__FILE__) . '/' . 'phptests');
35
// stocke les CSV générés à partir des test (répertoire de cache)
36
define('_RUNDIR', dirname(__FILE__) . '/' . 'run');
37
 
1768 raphael 38
define('USER', isset($options['u']) ? $options['u'] : (isset($options['user']) ? $options['user'] : 22506));
39
define('DOMAIN', isset($options['d']) ? $options['d'] : (isset($options['domain']) ? $options['domain'] : 'http://cel'));
40
define('EMAIL', isset($options['e']) ? $options['e'] : (isset($options['email']) ? $options['email'] : NULL));
41
define('PASS', isset($options['p']) ? $options['p'] : (isset($options['pass']) ? $options['pass'] : NULL));
42
define('COOKIE', isset($options['P']) ? $options['P'] : (isset($options['phpsessid']) ? $options['phpsessid'] : NULL));
1775 raphael 43
define('DEBUG', isset($options['D']) ? 1 : (isset($options['debug']) ? 1 : 0));
1768 raphael 44
 
45
require_once('api.php');
46
 
47
function setupTestEnv() {
48
	cel_delete_all_obs();
49
	cel_delete_all_images();
50
	cel_upload_image('image-test.jpg');
51
	cel_upload_image('image-test2.jpg');
52
}
53
 
1793 raphael 54
if(! is_dir(_TESTDIR)) die('no phptests/ directory inside ' . __DIR__);
55
if(! is_dir(_RUNDIR)) mkdir(_RUNDIR);
56
if(! is_dir(_RUNDIR)) die('no run/ directory inside ' . __DIR__);
1768 raphael 57
 
1793 raphael 58
$all_tests = array_map('basename', glob(_TESTDIR . '/*.test.php'));
59
$tests = array_intersect(array_map('basename', $argv), $all_tests);
60
 
1768 raphael 61
if(!$tests) $tests = $all_tests;
62
 
1793 raphael 63
//@array_walk(glob('run/*'), function(&$i) { unlink($i);});
64
 
1768 raphael 65
foreach($tests as $test) {
1793 raphael 66
	setupTestEnv();
67
	$testfile = _TESTDIR . '/' . $test;
68
 
1768 raphael 69
	// cache car l'upload de fichier PHP-curl ne peut être
70
	// simulé avec le contenu d'une variable (cf CURLOPT_POSTFIELDS et @fichier)
1793 raphael 71
	$runfile = _RUNDIR . '/' . $test;
72
 
73
	$test_array = require($testfile);
74
 
75
	$cols = isset($test_array['dumpCols']) ? $test_array['dumpCols'] : 'standard';
76
 
77
	if(!is_file($runfile) || filemtime($runfile) < filemtime($testfile)) {
1775 raphael 78
		$csv = genCSV($test_array['data']);
1768 raphael 79
		file_put_contents($runfile, $csv);
80
	}
81
 
1769 raphael 82
	echo "\tcurl -F \"upload=@$runfile\" -F utilisateur=" . USER . " \"" . DOMAIN . "/jrest/ImportXLS\"\n";
83
	echo "$test: ";
1793 raphael 84
	$var_expected = include(_TESTDIR . '/' . str_replace('.test.', '.result.', $test));
1768 raphael 85
 
86
	$retour = import($runfile);
87
	$count_warn = 0;
88
	preg_match('/^ligne /', $retour, $count_warn);
89
	$count_warn = count($count_warn);
90
 
1793 raphael 91
	$result = getCSV_line(export($cols), 1);
92
	if(!$result) die("\n!!! export vide, problème probable d'authentification ?\n");
1768 raphael 93
 
1775 raphael 94
	//var_dump(champsLongToShort2($test_array['data']));die;
1768 raphael 95
	if($var_expected) {
1775 raphael 96
		$result = __diff_fields($test_array, $result, $var_expected);
97
		// unset($result['date_creation'], $result['date_modification'], $result['id_observation']);
98
 
1769 raphael 99
		$d1 = array_diff($var_expected, $result);
100
		$d2 = array_diff($result, $var_expected);
101
		if(!$d1 && !$d2) echo "OK\n";
1768 raphael 102
		else {
103
			echo "FAIL\n";
1769 raphael 104
			if($d1) print_r($d1);
105
			if($d2) print_r($d2);
1768 raphael 106
			//var_dump($var_expected, $result);
107
		}
1775 raphael 108
		if(DEBUG) echo $retour;
109
		if($count_warn && (!isset($test_array['warn']) || $test_array['warn'] != $count_warn)) { echo "warnings: $count_warn\n"; }
1768 raphael 110
	}
1775 raphael 111
	// pas de résultat de test défini ?
1768 raphael 112
	else {
1775 raphael 113
		if(DEBUG) echo $retour;
114
		if($count_warn && (!isset($test_array['warn']) || $test_array['warn'] != $count_warn)) { echo "warnings: $count_warn\n"; }
1769 raphael 115
 
116
		echo '<?php return ' . var_export($result, true) . ';';
1768 raphael 117
	}
1775 raphael 118
}
1769 raphael 119
 
1775 raphael 120
 
121
 
122
 
123
 
124
function __diff_fields($test_array, $result, $var_expected) {
125
	if(! isset($test_array['cmpCols'])) {
126
		return array_intersect_key($result, $var_expected);
127
	}
128
 
129
	if($test_array['cmpCols'] == 'def') {
130
		return array_intersect_key($result, champsLongToShort2($test_array['data']));
131
	}
132
 
133
	$e = array_flip(champsLongToShort2(array_flip(explode(',', ltrim($test_array['cmpCols'], '+-')))));
134
	if($test_array['cmpCols'][0] == '-') {
135
		return array_diff_key($result, array_flip(champsLongToShort2($e)));
136
	}
137
	else { //if($test_array['cmpCols'][0] == '+') {
138
		return array_intersect_key($result, array_flip(champsLongToShort2($e)));
139
	}
140
 
141
	// pas de 'cmpCols' définie: comparaison de tous les champs par rapport au tableau de résultats attendus
1768 raphael 142
}
143
 
144
// sed -i -e '1{/<?php return/!s:^:<?php return :}' -e '${/^)$/s:$:;:}' phptests/*.result.php