Subversion Repositories eFlore/Applications.cel

Rev

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