Skip to content

Commit

Permalink
Nette\Test: implemented code coverage analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 9, 2012
1 parent 5727a61 commit de9c4ea
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
42 changes: 42 additions & 0 deletions Tester/NetteTestHelpers.php
Expand Up @@ -55,6 +55,12 @@ public static function startup()
header('Content-Type: text/plain; charset=utf-8');
}

if (extension_loaded('xdebug')) {
xdebug_disable();
xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
register_shutdown_function(array(__CLASS__, 'prepareSaveCoverage'));
}

set_exception_handler(array(__CLASS__, 'exceptionHandler'));
}

Expand Down Expand Up @@ -227,6 +233,42 @@ public static function exceptionHandler(Exception $exception)



/**
* Coverage saving helper.
* @return void
*/
public static function prepareSaveCoverage()
{
register_shutdown_function(array(__CLASS__, 'saveCoverage'));
}



/**
* Saves information about code coverage.
* @return void
*/
public static function saveCoverage()
{
$file = dirname(__FILE__) . '/coverage.tmp';
$coverage = @unserialize(file_get_contents($file));
$root = realpath(dirname(__FILE__) . '/../../Nette') . DIRECTORY_SEPARATOR;

foreach (xdebug_get_code_coverage() as $filename => $lines) {
if (strncmp($root, $filename, strlen($root))) continue;

foreach ($lines as $num => $val) {
if (empty($coverage[$filename][$num]) || $val > 0) {
$coverage[$filename][$num] = $val; // -1 => untested; -2 => dead code
}
}
}

file_put_contents($file, serialize($coverage));
}



/**
* Skips this test.
* @return void
Expand Down
2 changes: 2 additions & 0 deletions Tester/RunTests.php
Expand Up @@ -28,6 +28,8 @@
* Execute tests
*/
try {
@unlink(dirname(__FILE__) . '/coverage.tmp'); // intentionally @

$manager = new NetteTestRunner;
$manager->parseArguments();
$res = $manager->run();
Expand Down
6 changes: 4 additions & 2 deletions Tester/initialize.php
Expand Up @@ -11,14 +11,16 @@

require dirname(__FILE__) . '/NetteTestHelpers.php';

require dirname(__FILE__) . '/../../Nette/loader.php';



NetteTestHelpers::startup();



require dirname(__FILE__) . '/../../Nette/loader.php';



/**
* Dumps information about a variable in readable format.
* @param mixed variable to dump
Expand Down

0 comments on commit de9c4ea

Please sign in to comment.