Subversion Repositories eFlore/Applications.cel

Compare Revisions

Ignore whitespace Rev 1806 → Rev 1807

/trunk/jrest/tests/XLS-IO-fields-unittest.php
New file
0,0 → 1,160
<?php
 
$options = getopt("hu:d:e:p:P:D",array("help", "user:", "domain:", "email:", "pass:", "phpsessid", "debug"));
if(isset($options['h']) || isset($options['help'])) {
printf(<<<EOF
%s [fichiers de test]
--help|-h: cette aide
-debug|-D: informations sur les requêtes curl
[-u|--user = 22506]
[-d|--domain = "http://cel"]
--pass|-p: pour l'authentifiation
--email|-e: pour l'authentifiation
--phpsessid|-P: pour l'authentifiation, alternative à -e/-p
 
* --phpsessid permet de passer un identifiant de session PHP pour effectuer l'export.
Exemple:
$ sqlite3 ~/.mozilla/firefox/*.default/cookies.sqlite<<<"SELECT * FROM moz_cookies WHERE baseDomain = 'cel' AND name = 'PHPSESSID';"
 
* [fichiers de test]: un ou plusieurs fichier(s) de test valide(s), commençant par "phptests/" et finissant en ".test.php"
Exemple: phptests/a*.test.php ou bien phptests/latitude-virgule.test.php
 
Exemple:
$ php IO-fields-unittest.php -P s97knu4axzi6440n1ihja9ppk1
$ php IO-fields-unittest.php -e me@tela -p blah -u 23004 -d http://localhost phptests/determ-espece*
 
Attention: lancer un test supprime TOUTES les observations existantes de l'utilisateur !
 
EOF
, /* relax emacs*/
basename(__FILE__));
die;
}
 
// stocke les test
define('_TESTDIR', dirname(__FILE__) . '/' . 'phptests');
// stocke les CSV générés à partir des test (répertoire de cache)
define('_RUNDIR', dirname(__FILE__) . '/' . 'run');
 
define('DOMAIN', isset($options['d']) ? $options['d'] : (isset($options['domain']) ? $options['domain'] : 'http://cel'));
define('EMAIL', isset($options['e']) ? $options['e'] : (isset($options['email']) ? $options['email'] : NULL));
define('PASS', isset($options['p']) ? $options['p'] : (isset($options['pass']) ? $options['pass'] : NULL));
define('DEBUG', isset($options['D']) ? 1 : (isset($options['debug']) ? 1 : 0));
 
// could be defined by the API::auth()
if(isset($options['u']) || isset($options['user'])) {
define('USER', isset($options['u']) ? $options['u'] : $options['user']);
}
 
// could be defined by the API::auth()
if(isset($options['P']) || isset($options['phpsessid'])) {
define('COOKIE', isset($options['P']) ? $options['P'] : $options['phpsessid']);
}
 
require_once('api.php');
 
if(!auth()) {
die('auth problem');
}
 
function setupTestEnv() {
cel_delete_all_obs();
cel_delete_all_images();
cel_upload_image('image-test.jpg');
cel_upload_image('image-test2.jpg');
}
 
if(! is_dir(_TESTDIR)) die('no phptests/ directory inside ' . __DIR__);
if(! is_dir(_RUNDIR)) mkdir(_RUNDIR);
if(! is_dir(_RUNDIR)) die('no run/ directory inside ' . __DIR__);
 
$all_tests = array_map('basename', glob(_TESTDIR . '/*.test.php'));
$tests = array_intersect(array_map('basename', $argv), $all_tests);
if(!$tests) $tests = $all_tests;
 
//@array_walk(glob('run/*'), function(&$i) { unlink($i);});
 
foreach($tests as $test) {
setupTestEnv();
$testfile = _TESTDIR . '/' . $test;
 
// cache car l'upload de fichier PHP-curl ne peut être
// simulé avec le contenu d'une variable (cf CURLOPT_POSTFIELDS et @fichier)
$runfile = _RUNDIR . '/' . $test;
 
$test_array = require($testfile);
 
$cols = isset($test_array['dumpCols']) ? $test_array['dumpCols'] : 'standard';
 
if(!is_file($runfile) || filemtime($runfile) < filemtime($testfile)) {
$csv = genCSV($test_array['data']);
file_put_contents($runfile, $csv);
}
 
//echo "\tcurl -F \"upload=@$runfile\" -F utilisateur=" . USER . " \"" . DOMAIN . "/jrest/ImportXLS\"\n";
echo "$test: ";
$var_expected = include(_TESTDIR . '/' . str_replace('.test.', '.result.', $test));
 
$retour = importXLS($runfile);
$count_warn = 0;
preg_match('/^ligne /', $retour, $count_warn);
$count_warn = count($count_warn);
 
$result = getCSV_line(exportXLS($cols), 1);
if(!$result) {
echo ("\n!!! export vide, problème probable d'authentification ?\n");
continue;
}
 
//var_dump(champsLongToShort2($test_array['data']));die;
if($var_expected) {
$result = __diff_fields($test_array, $result, $var_expected);
// unset($result['date_creation'], $result['date_modification'], $result['id_observation']);
 
$d1 = array_diff($var_expected, $result);
$d2 = array_diff($result, $var_expected);
if(!$d1 && !$d2) echo "OK\n";
else {
echo "FAIL\n";
if($d1) { echo "expect: "; print_r($d1); }
if($d2) { echo "result: "; print_r($d2); }
//var_dump($var_expected, $result);
}
if(DEBUG) echo $retour;
if($count_warn && (!isset($test_array['warn']) || $test_array['warn'] != $count_warn)) { echo "warnings: $count_warn\n"; }
}
// pas de résultat de test défini ?
else {
if(DEBUG) echo $retour;
if($count_warn && (!isset($test_array['warn']) || $test_array['warn'] != $count_warn)) { echo "warnings: $count_warn\n"; }
 
echo '<?php return ' . var_export($result, true) . ';';
}
}
 
 
 
 
 
function __diff_fields($test_array, $result, $var_expected) {
if(! isset($test_array['cmpCols'])) {
return array_intersect_key($result, $var_expected);
}
 
if($test_array['cmpCols'] == 'def') {
return array_intersect_key($result, champsLongToShort2($test_array['data']));
}
 
$e = array_flip(champsLongToShort2(array_flip(explode(',', ltrim($test_array['cmpCols'], '+-')))));
if($test_array['cmpCols'][0] == '-') {
return array_diff_key($result, array_flip(champsLongToShort2($e)));
}
else { //if($test_array['cmpCols'][0] == '+') {
return array_intersect_key($result, array_flip(champsLongToShort2($e)));
}
 
// pas de 'cmpCols' définie: comparaison de tous les champs par rapport au tableau de résultats attendus
}
 
// sed -i -e '1{/<?php return/!s:^:<?php return :}' -e '${/^)$/s:$:;:}' phptests/*.result.php