public static function newReport($id, $sessionId, $referenceId)
 {
     if (false === Session::isValidSession($sessionId)) {
         throw new Exception('Session ID not valid');
     }
     if (false === Reference::isValidResults($referenceId)) {
         throw new Exception('Reference ID not valid');
     }
     if (!is_numeric($id)) {
         throw new Exception('Not a valid report ID');
     }
     if (ValidationReport::isValidValidationReport($id)) {
         throw new Exception('Report already exists');
     }
     $session = new Session($sessionId);
     $sessionResults = $session->getResults();
     $reference = new Reference($referenceId);
     $referenceResults = $reference->getResults();
     // Index the sessionResults so we can look them up
     $sessionResults['tests'] = array();
     foreach ($sessionResults['results'] as $test) {
         $result = new ResultSupport($test);
         $testName = $result->getTestName();
         if (!array_key_exists($testName, $sessionResults['tests'])) {
             $sessionResults['tests'][$testName] = array();
         }
         $testId = $test['id'];
         $sessionResults['tests'][$testName][$testId] = array();
         foreach ($test['subtests'] as $subtest) {
             $subtestName = ResultSupport::getSubtestName($subtest);
             // Update the results
             $sessionResults['tests'][$testName][$testId][$subtestName] = $subtest['status'];
         }
     }
     // Test statistics
     $testsNotRun = 0;
     $subtestsNotRun = 0;
     $testsFailed = 0;
     $subtestsFailed = 0;
     $log = array();
     // Work through each test
     foreach ($referenceResults['results'] as $test) {
         $result = new ResultSupport($test);
         $testName = $result->getTestName();
         if (!array_key_exists($testName, $sessionResults['tests'])) {
             array_push($log, array('type' => 'error', 'message' => 'FAILURE - TEST NOT RUN', 'test' => $testName, 'reference_test_id' => $test['id']));
             $testsNotRun++;
             continue;
         }
         if ('testharness' == $result->getTestType()) {
             // Counter to see if any of the subtests fail
             $failCheck = $subtestsFailed;
             foreach ($test['subtests'] as $subtest) {
                 $subtestName = ResultSupport::getSubtestName($subtest);
                 // Check the passed tests
                 if ($subtest['status'] === "PASS") {
                     $testRun = false;
                     foreach ($sessionResults['tests'][$testName] as $testId => $subtestList) {
                         if (array_key_exists($subtestName, $subtestList)) {
                             $testRun = $testId;
                             break;
                         }
                     }
                     if (false === $testRun) {
                         array_push($log, array('type' => 'error', 'message' => 'FAILURE - SUBTEST NOT RUN', 'test' => $testName, 'subtest' => $subtestName, 'reference_test_id' => $test['id'], 'test_ids' => array_keys($sessionResults['tests'][$testName])));
                         $subtestsNotRun++;
                         continue;
                     }
                     if ($sessionResults['tests'][$testName][$testRun][$subtestName] === "PASS") {
                         //fprintf(STDOUT, "  PASS: %s /  %s\n", $testName, $subtestName);
                     } else {
                         array_push($log, array('type' => 'error', 'message' => 'FAILURE - SUBTEST FAILED', 'test' => $testName, 'subtest' => $subtestName, 'reference_test_id' => $test['id'], 'test_id' => $testRun));
                         $subtestsFailed++;
                     }
                 }
             }
             // Check if any of the subtests failed
             if ($failCheck != $subtestsFailed) {
                 $testsFailed++;
             }
         } else {
         }
     }
     $status = array('session' => $sessionId, 'reference' => $referenceId, 'tests_not_run' => $testsNotRun, 'subtests_not_run' => $subtestsNotRun, 'tests_failed' => $testsFailed, 'subtests_failed' => $subtestsFailed, 'log_total' => count($log));
     $dir = VALIDATION_REPORT_DIR . '/' . $id;
     mkdir($dir);
     file_put_contents($dir . '/status', json_encode($status));
     file_put_contents($dir . '/report', json_encode($log));
     return new ValidationReport($id);
 }
 public static function createSession($id)
 {
     if (!is_numeric($id)) {
         throw new Exception('Not a valid session ID');
     }
     if (Session::isValidSession($id)) {
         throw new Exception('Session already exists');
     }
     $dir = SESSION_DIR . '/' . $id;
     mkdir($dir);
     file_put_contents($dir . '/status', json_encode(array('count' => 0, 'totals' => self::createStatsArray())));
     return new Session($id);
 }
 $app->post('/', function () use($app) {
     global $status, $statusModified;
     // Create a new session
     $session = Session::createSession($status['results']);
     $status['results']++;
     $statusModified = true;
     $sessionInfo = $session->getInfo();
     $sessionInfo['href'] = $app->urlFor('results', array('id' => $session->id));
     Notify(ADMIN_TOPIC, array('action' => 'create', 'session' => $sessionInfo));
     $app->render(200, array('session' => $sessionInfo));
 });
 $app->get('/', function () use($app) {
     $sessions = array();
     if ($dh = opendir(SESSION_DIR)) {
         while (($file = readdir($dh)) !== false) {
             if (Session::isValidSession($file)) {
                 $session = new Session($file);
                 $sessionInfo = $session->getInfo();
                 $sessionInfo['href'] = $app->urlFor('results', array('id' => $file));
                 array_push($sessions, $sessionInfo);
             }
         }
         closedir($dh);
     }
     usort($sessions, function ($a, $b) {
         return $a['id'] - $b['id'];
     });
     $app->render(200, array('sessions' => $sessions));
 })->name('resultIndex');
 $app->get('/:id', function ($id) use($app) {
     $session = new Session($id);