I am working on a green-field project and wanted to make sure I roughly knew what was and wasn’t tested. Sometimes I just simply forget whether or not I wrote a test for a new code file. So I share with you a tiny little script that will list all the files in your source directory that do no have a corresponding test file.
<?php
$sub_dir = count($argv) > 3 ? '/' . $argv[3] : '';
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$argv[1] . $sub_dir
)
);
foreach ($iterator as $path) {
if ($path->isFile()) {
$test = $argv[2]
. str_replace($argv[1], '', $path->getPath())
. '/'
. $path->getBasename('.php')
. 'Test.php';
if (file_exists($test) === false) {
print $path->getPathname() . "\n";
}
}
}
The script requires a source directory and a test directory, either absolute or relative to current directory is fine. The package argument is optional. Specify the package to restrict your search. This is useful for when you are working in just a small portion of a larger code base. It is assumed that you are following standard PHPUnit test file naming conventions.
Command line:
php find_missing.php [source directory] [test directory] [package]
developer testing, PHP, PHPUnit, test, test driven development, testing, unit, unit testing