| # +----------------------------------------------------------------------+ header('HTTP/1.0 403 Forbidden'); die('This script is for local testing purposes only! Uncomment these lines to use it.'); error_reporting(E_ALL); require 'parserfunc.php'; require '../include/functions.php'; echo '

Testing QA report parsing and database handling

'; echo '
'."\n";

$str = file_get_contents('sample.txt');
echo 'Content length: '.strlen($str);
if (strlen($str) == 126341) echo " OK \n";
else                        echo " KO (value: ".strlen($str)." \n";
echo "Parsing with function parse_phpmaketest() ... \n";
$array = parse_phpmaketest('5.3.7-dev', 'failed', $str);

printf("%-30s", "Result should be array: ");
if (is_array($array)) echo " OK \n";
else                        echo " KO \n";

printf("%-30s", "Version extracted match: ");
if ($array['version'] == '5.3.7-dev') echo " OK \n";
else                        echo " KO. value extracted: ".$array['version']." \n";

printf("%-30s", "Test email match: ");
if ($array['userEmail'] == 'thisisatestmail@testdomain.com') echo " OK \n";
else                        echo " KO. value extracted: ".$array['userEmail']." \n";

printf("%-30s", "extract 17 expectedFailedTest");
if (count($array['expectedFailedTest']) == 17) echo " OK \n";
else                        echo " KO \n";

printf("%-30s", "extract 33 failedTest");
if (count($array['failedTest']) == 33) echo " OK \n";
else                        echo " KO \n";

printf("%-30s", "33 detailed test");
if (count($array['tests']) == 33) echo " OK \n";
else                        echo " KO \n";

printf("%-30s", "specific expectedFailedTest");
if (in_array('Zend/tests/bug48770_3.phpt', $array['expectedFailedTest'])) echo " OK \n";
else                        echo " KO \n";

printf("%-30s", "specific failedTest");
if (in_array('ext/dom/tests/DOMDocument_validate_on_parse_variation.phpt', $array['failedTest']))
     echo " OK \n";
else echo " KO \n";

printf("%-30s", "specific test diff");
$strlen = strlen($array['tests']['/tests/func/010.phpt']['diff']);
if (isset($array['tests']['/tests/func/010.phpt']['diff']) && $strlen >= 290 )
     echo " OK size:   ".$strlen." - optimal =   293 \n";
else echo " KO (length: $strlen should be > 290) \n";

printf("%-30s", "specific test output");
$strlen = strlen($array['tests']['/tests/func/010.phpt']['output']);
if (isset($array['tests']['/tests/func/010.phpt']['output']) && $strlen >= 165 )
     echo " OK size:   ".$strlen." - optimal =   167 \n";
else echo " KO \n";

printf("%-30s", "phpinfo");
if (strlen($array['phpinfo']) >= 27940) echo " OK size: ".strlen($array['phpinfo'])." \n";
else                        echo " KO \n";

printf("%-30s", "buildEnvironment");
if (strlen($array['buildEnvironment']) >= 4500)
     echo " OK size:  ".strlen($array['buildEnvironment'])." \n";
else echo " KO \n";

// total diff / output, to see if we parsed everything Ok
$totalDiff = 0;
$totalOutput = 0;

foreach ($array['tests'] as $name => $content) {
    $totalDiff += strlen($content['diff']);
    $totalOutput += strlen($content['output']);
}

printf("%-30s", "Total diff length");
if ($totalDiff >= 27900) echo " OK size: ".$totalDiff." - optimal = 27938 \n";
else                     echo " KO \n";

printf("%-30s", "Total output length");
if ($totalOutput >= 31950) echo " OK size: ".$totalOutput." - optimal = 31971 \n";
else                       echo " KO \n";


// now insert data and check
echo "\nTesting SQLite insertion ...\n";

$return = insertToDb_phpmaketest($array);
printf("%-30s", "Function call");

if ($return === true) echo " OK \n";
else                  echo " KO (return: ".$return.") \n";

$dbFile = __DIR__.'/db/'.$array['version'].'.sqlite';

printf("%-30s", "DB file exists");
if (file_exists($dbFile)) echo " OK \n";
else                  echo " KO \n";

$database = new SQLite3($dbFile, SQLITE3_OPEN_READONLY);

if (!$database) {
    die("Error opening DB file: ".$database->lastErrorMsg());
}

// Check report ?
$query = 'SELECT * FROM reports WHERE user_email = \'thisisatestmail@testdomain.com\' ORDER BY date DESC LIMIT 1';
$q = $database->query($query);
$sqlReport = $q->fetchArray(SQLITE3_ASSOC);
printf("%-30s", "Found report in DB");
if (is_array($sqlReport) && isset($sqlReport['id']))
     echo " OK (id: ".$sqlReport['id'].") \n";
else echo " KO \n";

// check how many failed ?
if (!isset($sqlReport['id'])) die('cannot make more tests');

$query = 'SELECT * FROM failed WHERE id_report = '.$sqlReport['id'];
$q = $database->query($query);
$sqlFailed = [];
while ($tab = $q->fetchArray(SQLITE3_ASSOC)) {
    $sqlFailed[$tab['test_name']] = $tab;
}

printf("%-30s", "Found 33 failedTest");
if (count($sqlFailed) == 33) {
    echo " OK \n";
} else {
    echo " KO (found: ".count($sqlFailed).") \n";
    var_dump($sqlFailed);
}

// expected fail
$query = 'SELECT count(*) FROM expectedfail WHERE id_report = '.$sqlReport['id'];
$q = $database->query($query);
list($nbExpected) = $q->fetchArray();

printf("%-30s", "Found 17 expectedFailedTests");
if ($nbExpected == 17) {
    echo " OK \n";
} else {
    echo " KO (found: ".$nbExpected.") \n";
}



printf("%-30s", "specific test diff");
$strlen = strlen($sqlFailed['/tests/func/010.phpt']['diff']);
if (isset($sqlFailed['/tests/func/010.phpt']['diff']) && $strlen >= 290 )
     echo " OK size:   ".$strlen." - optimal =   293 \n";
else echo " KO \n";

printf("%-30s", "specific test output");
$strlen = strlen($sqlFailed['/tests/func/010.phpt']['output']);
if (isset($sqlFailed['/tests/func/010.phpt']['output']) && $strlen >= 165 )
     echo " OK size:   ".$strlen." - optimal =   167 \n";
else echo " KO \n";

$totalDiff = 0;
$totalOutput = 0;

foreach ($sqlFailed as $name => $content) {
    $totalDiff += strlen($content['diff']);
    $totalOutput += strlen($content['output']);
}

printf("%-30s", "Total diff length");
if ($totalDiff >= 27900) echo " OK size: ".$totalDiff." - optimal = 27938 \n";
else                     echo " KO \n";

printf("%-30s", "Total output length");
if ($totalOutput >= 31950) echo " OK size: ".$totalOutput." - optimal = 31971 \n";
else                       echo " KO \n";

// Cleanup
$database->close();

$database = new SQLite3($dbFile, SQLITE3_OPEN_READWRITE) or exit('cannot open DB to remove test');
$database->exec('DELETE FROM failed WHERE id_report = '.$sqlReport['id']);
$database->exec('DELETE FROM expectedfail WHERE id_report = '.$sqlReport['id']);
$database->exec('DELETE FROM reports WHERE id = '.$sqlReport['id']);
$database->close();
echo "Cleanup done";