Subversion Repositories eFlore/Applications.cel

Rev

Rev 1794 | 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
1802 raphael 8
  -debug|-D: informations sur les requêtes curl
1793 raphael 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 !
1802 raphael 27
 
1793 raphael 28
EOF
29
, /* relax emacs*/
30
		   basename(__FILE__));
31
	die;
1768 raphael 32
}
33
 
1793 raphael 34
// stocke les test
35
define('_TESTDIR', dirname(__FILE__) . '/' . 'phptests');
36
// stocke les CSV générés à partir des test (répertoire de cache)
37
define('_RUNDIR', dirname(__FILE__) . '/' . 'run');
38
 
1768 raphael 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));
1775 raphael 42
define('DEBUG', isset($options['D']) ? 1 : (isset($options['debug']) ? 1 : 0));
1768 raphael 43
 
1802 raphael 44
// could be defined by the API::auth()
45
if(isset($options['u']) || isset($options['user'])) {
46
	define('USER', isset($options['u']) ? $options['u'] : $options['user']);
47
}
1768 raphael 48
 
1802 raphael 49
// could be defined by the API::auth()
50
if(isset($options['P']) || isset($options['phpsessid'])) {
51
	define('COOKIE', isset($options['P']) ? $options['P'] : $options['phpsessid']);
52
}
53
 
54
//require_once('api.php');
55
require_once('api-old.php');
56
 
57
if(!auth()) {
58
	die('auth problem');
59
}
60
 
1768 raphael 61
function setupTestEnv() {
62
	cel_delete_all_obs();
63
	cel_delete_all_images();
64
	cel_upload_image('image-test.jpg');
65
	cel_upload_image('image-test2.jpg');
66
}
67
 
1793 raphael 68
if(! is_dir(_TESTDIR)) die('no phptests/ directory inside ' . __DIR__);
69
if(! is_dir(_RUNDIR)) mkdir(_RUNDIR);
70
if(! is_dir(_RUNDIR)) die('no run/ directory inside ' . __DIR__);
1768 raphael 71
 
1793 raphael 72
$all_tests = array_map('basename', glob(_TESTDIR . '/*.test.php'));
73
$tests = array_intersect(array_map('basename', $argv), $all_tests);
74
 
1768 raphael 75
if(!$tests) $tests = $all_tests;
76
 
1793 raphael 77
//@array_walk(glob('run/*'), function(&$i) { unlink($i);});
78
 
1768 raphael 79
foreach($tests as $test) {
1793 raphael 80
	setupTestEnv();
81
	$testfile = _TESTDIR . '/' . $test;
82
 
1768 raphael 83
	// cache car l'upload de fichier PHP-curl ne peut être
84
	// simulé avec le contenu d'une variable (cf CURLOPT_POSTFIELDS et @fichier)
1793 raphael 85
	$runfile = _RUNDIR . '/' . $test;
86
 
87
	$test_array = require($testfile);
88
 
89
	$cols = isset($test_array['dumpCols']) ? $test_array['dumpCols'] : 'standard';
90
 
91
	if(!is_file($runfile) || filemtime($runfile) < filemtime($testfile)) {
1775 raphael 92
		$csv = genCSV($test_array['data']);
1768 raphael 93
		file_put_contents($runfile, $csv);
94
	}
95
 
1802 raphael 96
	//echo "\tcurl -F \"upload=@$runfile\" -F utilisateur=" . USER . " \"" . DOMAIN . "/jrest/ImportXLS\"\n";
1769 raphael 97
	echo "$test: ";
1793 raphael 98
	$var_expected = include(_TESTDIR . '/' . str_replace('.test.', '.result.', $test));
1768 raphael 99
 
100
	$retour = import($runfile);
101
	$count_warn = 0;
102
	preg_match('/^ligne /', $retour, $count_warn);
103
	$count_warn = count($count_warn);
104
 
1793 raphael 105
	$result = getCSV_line(export($cols), 1);
1802 raphael 106
	if(!$result) {
107
		echo ("\n!!! export vide, problème probable d'authentification ?\n");
108
		continue;
109
	}
1768 raphael 110
 
1775 raphael 111
	//var_dump(champsLongToShort2($test_array['data']));die;
1768 raphael 112
	if($var_expected) {
1775 raphael 113
		$result = __diff_fields($test_array, $result, $var_expected);
114
		// unset($result['date_creation'], $result['date_modification'], $result['id_observation']);
115
 
1769 raphael 116
		$d1 = array_diff($var_expected, $result);
117
		$d2 = array_diff($result, $var_expected);
118
		if(!$d1 && !$d2) echo "OK\n";
1768 raphael 119
		else {
120
			echo "FAIL\n";
1802 raphael 121
			if($d1) { echo "expect: "; print_r($d1); }
122
			if($d2) { echo "result: "; print_r($d2); }
1768 raphael 123
			//var_dump($var_expected, $result);
124
		}
1775 raphael 125
		if(DEBUG) echo $retour;
126
		if($count_warn && (!isset($test_array['warn']) || $test_array['warn'] != $count_warn)) { echo "warnings: $count_warn\n"; }
1768 raphael 127
	}
1775 raphael 128
	// pas de résultat de test défini ?
1768 raphael 129
	else {
1775 raphael 130
		if(DEBUG) echo $retour;
131
		if($count_warn && (!isset($test_array['warn']) || $test_array['warn'] != $count_warn)) { echo "warnings: $count_warn\n"; }
1769 raphael 132
 
133
		echo '<?php return ' . var_export($result, true) . ';';
1768 raphael 134
	}
1775 raphael 135
}
1769 raphael 136
 
1775 raphael 137
 
138
 
139
 
140
 
141
function __diff_fields($test_array, $result, $var_expected) {
142
	if(! isset($test_array['cmpCols'])) {
143
		return array_intersect_key($result, $var_expected);
144
	}
145
 
146
	if($test_array['cmpCols'] == 'def') {
147
		return array_intersect_key($result, champsLongToShort2($test_array['data']));
148
	}
149
 
150
	$e = array_flip(champsLongToShort2(array_flip(explode(',', ltrim($test_array['cmpCols'], '+-')))));
151
	if($test_array['cmpCols'][0] == '-') {
152
		return array_diff_key($result, array_flip(champsLongToShort2($e)));
153
	}
154
	else { //if($test_array['cmpCols'][0] == '+') {
155
		return array_intersect_key($result, array_flip(champsLongToShort2($e)));
156
	}
157
 
158
	// pas de 'cmpCols' définie: comparaison de tous les champs par rapport au tableau de résultats attendus
1768 raphael 159
}
160
 
161
// sed -i -e '1{/<?php return/!s:^:<?php return :}' -e '${/^)$/s:$:;:}' phptests/*.result.php