Created
April 8, 2011 18:57
-
-
Save padraic/910493 to your computer and use it in GitHub Desktop.
Simple script which attempts to count the hard coded Test Doubles used by a PHPUnit test suite.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Purpose of the exercise is to analyse test case files. Any classes defined | |
* therein in addition to the actual test case class is assumed to be a | |
* hard coded Test Double. Now, where's my pinch of salt... | |
* | |
* Script's sole argument is the path (to a test directory) to analyse. | |
*/ | |
$classCount = 0; | |
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($argv[1])); | |
foreach ($iterator as $file) { | |
$localClassCount = 0; | |
if ($file->isDir()) continue; | |
if (!preg_match("/\.php$/", $file->getFilename())) continue; | |
$tokens = token_get_all(file_get_contents($file->getRealPath())); | |
foreach ($tokens as $index=>$token) { | |
if (!is_array($token)) continue; | |
if ($token[0] === T_CLASS) { | |
$localClassCount++; | |
continue; | |
} | |
/** | |
* Sweet chaos. We track forward to first curly bracket, then track back | |
* to find last element of a possibly namespaced class name. If the last | |
* element ends in Test, or includes PHPUnit, we assume it's a TestCase | |
* and not a Test Double. Will need to be revised for PHPUnit 4 | |
*/ | |
if ($token[0] === T_EXTENDS) { | |
$i = $index++; | |
while (true) { | |
if (is_array($tokens[$i])) $i++; | |
if (!is_array($tokens[$i])) break; | |
} | |
while (true) { | |
if (!is_array($tokens[$i])) { | |
$i--; continue; | |
} | |
if ($tokens[$i][0] === T_EXTENDS) break; | |
if ($tokens[$i][1] == '\\') break; | |
if (preg_match("/PHPUnit/i", $tokens[$i][1]) | |
|| preg_match("/Test$/i", $tokens[$i][1])) { | |
$localClassCount--; | |
break; | |
} | |
$i--; | |
} | |
} | |
} | |
$classCount += $localClassCount; | |
if ($localClassCount) echo $localClassCount, ' ', $file->getPathname(), PHP_EOL; | |
} | |
echo PHP_EOL, 'Approximate Test Doubles: ', $classCount, PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment