Example #1
0
/**
 * runs all defined tests
 * if tests directory contains coverage folder, it will generate coverage results there
 */
function run_tests()
{
    global $argv;
    // read test directory and run all tests
    $path = dirname(__FILE__) . '/tests';
    $dir = dir($path);
    $lime_output = new lime_output();
    $coverage = $coveragePath = '';
    $tests = array();
    while (($entry = $dir->read()) !== false) {
        if ($entry == '.' || $entry == '..') {
            continue;
        }
        if (is_dir("{$path}/{$entry}")) {
            if ($entry == 'coverage') {
                $coveragePath = $path . '/' . $entry;
            }
            continue;
        }
        if (is_test($entry)) {
            $tests[getTestNameByPath("{$path}/{$entry}")] = "{$path}/{$entry}";
        }
    }
    // run concrete test
    if (($testName = $argv[1]) && can_param_be_test($testName)) {
        $testName .= 'Test';
        if (!array_key_exists($testName, $tests)) {
            $lime_output->error("cannot find test " . $argv[1] . ' in ' . $path);
            return;
        }
        if ($coveragePath) {
            $coveragePathConcrete = $coveragePath . '/' . $testName;
            LBoxUtil::createDirByPath($coveragePathConcrete);
            $coverage = '--coverage-html ' . $coveragePathConcrete . ' ';
            //$coverage			.=	' --coverage-clover '. $coveragePathConcrete . '/clover.xml ';
        }
        $path = $tests[$testName];
        $call = "phpunit {$coverage} {$path}";
        //$lime_output->info($call);
        $out = getCallReturn($call);
        if (preg_match('/OK \\((\\d+) test(s?)\\, (\\d+) assertion(s?)\\)/', $out, $matchAll)) {
            $lime_output->green_bar($testName);
        } else {
            $lime_output->red_bar($testName);
        }
        echo $out;
        if (strlen($coverage) > 0) {
            if (!doesContainErrorNotification($out)) {
                $lime_output->info("coverage results in {$coveragePath}");
            }
        }
        return;
    }
    // run all loaded tests
    if (count($tests) < 1) {
        $lime_output->comment('no test found in ' . $path);
        return;
    }
    $assertionsCountAtomic = 0;
    $testsCountAtomic = 0;
    foreach ($tests as $name => $path) {
        if ($coveragePath) {
            $coveragePathConcrete = $coveragePath . '/' . $name;
            LBoxUtil::createDirByPath($coveragePathConcrete);
            $coverage = '--coverage-html ' . $coveragePathConcrete . ' ';
            //$coverage			.=	' --coverage-clover '. $coveragePathConcrete . '/clover.xml ';
        }
        $call = "phpunit {$coverage} {$path}";
        //$lime_output->info($call);
        $out = getCallReturn($call);
        $space = '';
        if (preg_match('/OK \\((\\d+) test(s?)\\, (\\d+) assertion(s?)\\)/', $out, $matchAll)) {
            $i = 1;
            foreach ($matchAll as $k => $v) {
                if (is_numeric($v)) {
                    $match[$i] = $v;
                    $i++;
                }
            }
            $assertionsCountAtomic += $match[2];
            $testsCountAtomic += $match[1];
            $outPartName = $name . ' (' . $match[1] . ' tests, ' . $match[2] . ' assertions)';
            for ($i = 0; $i < 68 - strlen($outPartName); $i++) {
                $space .= ' ';
            }
            $lime_output->green_bar($outPartName . $space . 'OK ');
        } else {
            for ($i = 0; $i < 64 - strlen($name); $i++) {
                $space .= ' ';
            }
            $lime_output->red_bar($name . $space . 'FAILED ');
            echo $out;
        }
    }
    $lime_output->echoln("{$testsCountAtomic} tests, {$assertionsCountAtomic} assertions done", array('fg' => 'green'));
    if (strlen($coverage) > 0) {
        if (!doesContainErrorNotification($out)) {
            $lime_output->info("coverage results in {$coveragePath}");
        }
    }
    $dir->close();
}