Subversion Repositories eFlore/Applications.cel

Rev

Details | 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'])) {
5
	die(basename(__FILE__) . ' [-u|--user = 22506] [-d|--domain = http://cel]');
6
}
7
 
8
define('USER', isset($options['u']) ? $options['u'] : (isset($options['user']) ? $options['user'] : 22506));
9
define('DOMAIN', isset($options['d']) ? $options['d'] : (isset($options['domain']) ? $options['domain'] : 'http://cel'));
10
define('EMAIL', isset($options['e']) ? $options['e'] : (isset($options['email']) ? $options['email'] : NULL));
11
define('PASS', isset($options['p']) ? $options['p'] : (isset($options['pass']) ? $options['pass'] : NULL));
12
define('COOKIE', isset($options['P']) ? $options['P'] : (isset($options['phpsessid']) ? $options['phpsessid'] : NULL));
1775 raphael 13
define('DEBUG', isset($options['D']) ? 1 : (isset($options['debug']) ? 1 : 0));
1768 raphael 14
 
15
require_once('api.php');
16
 
17
function setupTestEnv() {
18
	cel_delete_all_obs();
19
	cel_delete_all_images();
20
	cel_upload_image('image-test.jpg');
21
	cel_upload_image('image-test2.jpg');
22
}
23
 
24
if(! is_dir('phptests')) die('no phptests/');
25
if(! is_dir('run')) mkdir('run');
26
if(! is_dir('run')) die('no run/');
27
 
28
 
29
$all_tests = glob('phptests/*.test.php');
30
$tests = array_intersect($argv, $all_tests);
31
if(!$tests) $tests = $all_tests;
32
 
33
foreach($tests as $test) {
1769 raphael 34
	setupTestEnv(); @array_walk(glob('run/*'), function(&$i) { unlink($i);});
1768 raphael 35
	// cache car l'upload de fichier PHP-curl ne peut être
36
	// simulé avec le contenu d'une variable (cf CURLOPT_POSTFIELDS et @fichier)
37
	$runfile = 'run/' . basename($test);
38
	if(!is_file($runfile)) {
1769 raphael 39
		$test_array = require($test);
1775 raphael 40
		$csv = genCSV($test_array['data']);
1768 raphael 41
		file_put_contents($runfile, $csv);
42
	}
43
 
1769 raphael 44
	echo "\tcurl -F \"upload=@$runfile\" -F utilisateur=" . USER . " \"" . DOMAIN . "/jrest/ImportXLS\"\n";
45
	echo "$test: ";
1768 raphael 46
	$var_expected = include(str_replace('.test.', '.result.', $test));
47
 
48
	$retour = import($runfile);
49
	$count_warn = 0;
50
	preg_match('/^ligne /', $retour, $count_warn);
51
	$count_warn = count($count_warn);
52
 
1775 raphael 53
	$result = getCSV_line(export(isset($test_array['dumpCols']) ? $test_array['dumpCols'] : 'standard'), 1);
1768 raphael 54
 
1775 raphael 55
	//var_dump(champsLongToShort2($test_array['data']));die;
1768 raphael 56
	if($var_expected) {
1775 raphael 57
		$result = __diff_fields($test_array, $result, $var_expected);
58
		// unset($result['date_creation'], $result['date_modification'], $result['id_observation']);
59
 
1769 raphael 60
		$d1 = array_diff($var_expected, $result);
61
		$d2 = array_diff($result, $var_expected);
62
		if(!$d1 && !$d2) echo "OK\n";
1768 raphael 63
		else {
64
			echo "FAIL\n";
1769 raphael 65
			if($d1) print_r($d1);
66
			if($d2) print_r($d2);
1768 raphael 67
			//var_dump($var_expected, $result);
68
		}
1775 raphael 69
		if(DEBUG) echo $retour;
70
		if($count_warn && (!isset($test_array['warn']) || $test_array['warn'] != $count_warn)) { echo "warnings: $count_warn\n"; }
1768 raphael 71
	}
1775 raphael 72
	// pas de résultat de test défini ?
1768 raphael 73
	else {
1775 raphael 74
		if(DEBUG) echo $retour;
75
		if($count_warn && (!isset($test_array['warn']) || $test_array['warn'] != $count_warn)) { echo "warnings: $count_warn\n"; }
1769 raphael 76
 
77
		echo '<?php return ' . var_export($result, true) . ';';
1768 raphael 78
	}
1775 raphael 79
}
1769 raphael 80
 
1775 raphael 81
 
82
 
83
 
84
 
85
function __diff_fields($test_array, $result, $var_expected) {
86
	if(! isset($test_array['cmpCols'])) {
87
		return array_intersect_key($result, $var_expected);
88
	}
89
 
90
	if($test_array['cmpCols'] == 'def') {
91
		return array_intersect_key($result, champsLongToShort2($test_array['data']));
92
	}
93
 
94
	$e = array_flip(champsLongToShort2(array_flip(explode(',', ltrim($test_array['cmpCols'], '+-')))));
95
	if($test_array['cmpCols'][0] == '-') {
96
		return array_diff_key($result, array_flip(champsLongToShort2($e)));
97
	}
98
	else { //if($test_array['cmpCols'][0] == '+') {
99
		return array_intersect_key($result, array_flip(champsLongToShort2($e)));
100
	}
101
 
102
	// pas de 'cmpCols' définie: comparaison de tous les champs par rapport au tableau de résultats attendus
1768 raphael 103
}
104
 
105
// sed -i -e '1{/<?php return/!s:^:<?php return :}' -e '${/^)$/s:$:;:}' phptests/*.result.php