Subversion Repositories eFlore/Applications.cel

Rev

Rev 1769 | Blame | Last modification | View Log | RSS feed

<?php

$options = getopt("hu:d:e:p:P:D",array("help", "user:", "domain:", "email:", "pass:", "phpsessid", "debug"));
if(isset($options['h']) || isset($options['help'])) {
        die(basename(__FILE__) . ' [-u|--user = 22506] [-d|--domain = http://cel]');
}

define('USER', isset($options['u']) ? $options['u'] : (isset($options['user']) ? $options['user'] : 22506));
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('COOKIE', isset($options['P']) ? $options['P'] : (isset($options['phpsessid']) ? $options['phpsessid'] : NULL));
define('DEBUG', isset($options['D']) ? 1 : (isset($options['debug']) ? 1 : 0));

require_once('api.php');

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('phptests')) die('no phptests/');
if(! is_dir('run')) mkdir('run');
if(! is_dir('run')) die('no run/');


$all_tests = glob('phptests/*.test.php');
$tests = array_intersect($argv, $all_tests);
if(!$tests) $tests = $all_tests;

foreach($tests as $test) {
        setupTestEnv(); @array_walk(glob('run/*'), function(&$i) { unlink($i);});
        // cache car l'upload de fichier PHP-curl ne peut être
        // simulé avec le contenu d'une variable (cf CURLOPT_POSTFIELDS et @fichier)
        $runfile = 'run/' . basename($test);
        if(!is_file($runfile)) {
                $test_array = require($test);
                $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(str_replace('.test.', '.result.', $test));

        $retour = import($runfile);
        $count_warn = 0;
        preg_match('/^ligne /', $retour, $count_warn);
        $count_warn = count($count_warn);

        $result = getCSV_line(export(isset($test_array['dumpCols']) ? $test_array['dumpCols'] : 'standard'), 1);

        //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) print_r($d1);
                        if($d2) 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