SimpleTest Framework Builder



Ever inherit a pre-existing codebase that contained no unit tests? I have. A couple times. In response I've built this helper utility that goes out and build the SimpleTest Framework for me. Once the framework is built then it needs to be fleshed out with actual tests, but the drudgery work is done.

<?php
/**
* Instructions
*
* Use these files to create a SimpleTest skeleton framework.  
*
* Required files:
*   build_simpletest_skeleton.php
*   BuildTestCase.php
*   BuildGroupTest.php
*   WriteFile.php
*
* Place all these files together in a folder.  Change the $sourcedir and $destdir variables.
* $sourcedir is the relative (to this file) path to your classes for this file
* $destdir is the directory the SimpleTest skeleton will be written into.  Make sure it's writable!!
* $pattern is the pattern that will be used to differentiate class files from other files.  I recommend Classname.class.php
*
* When you're done, you can run the group_tests.php file in the destination directory.
* You may need to uncomment, and set the chdir() function in the group_tests.php file.
*
* For information about SimpleTest: http://www.lastcraft.com
*
* @author: Chris Hubbard
* @copyright    Public Domain
* @version  .1
* @license  MIT License
*/
error_reporting(E_ALL);
echo "<h1>SimpleTest Skeleton</h1><b>starting...</b><br><br>";
require_once('BuildTestCase.php');
require_once('BuildGroupTest.php');
require_once('WriteFile.php');

/**
* sourcedir
* this is the location of the classes you want to build test cases form
* the location needs to be relative to this file, so the file can be found
* in the BuildTestCase class
*/
$sourcedir = '../phengine/output/classes/';
define("SOURCEDIR", $sourcedir);

/**
* destdir
* this is the location that the SimpleTest skeleton files will be written to.
* make sure it is writable by Apache and/or PHP
*/
$destdir = '../phengine/output/tests/';

/**
* pattern
* this is the pattern that will be used to identify the classes.  You did use a naming convention didn't you?
*/
$pattern = ".class.php$";

/**
* testcaseprefix
* this is the prefix used in the file name for each of the different test case files.
* each class gets exactly one test case file
* each test case is named: testcaseprefix.classname
* for example:
*   a class named File.class.php would get a test called test.File.class.php
*/
$testcaseprefix = "test.";

/**
* create a handle to the source directory
*/
$d = dir($sourcedir);

/**
* create empty strings.
* these are used to build the group_test.php file for SimpleTest
*/
$requires = "";
$tests = "";

echo "looking in: ". $d->path ."<br>";
/**
* cycle through the source directory
* for each file that matches the pattern
*   update the $requires and $tests string,
*   create a new test case file
*/
while (false !== ($entry = $d->read()))
{

    if (ereg($pattern, $entry))
    {
        echo "processing $entry<br>";
        $requires .= "require_once('". $testcaseprefix. $entry ."');\n";
        $tests .= "\$test->addTestFile('". $testcaseprefix.$entry ."');\n";
        $file = new BuildTestCase($entry);
        $writer = new WriteFile;
        $result = $writer->write($file->display(), $destdir .'test.'.$entry);
    }else{
        echo "skipped $entry<br>";
    }
}
/**
* close the directory handle
*/
$d->close();
echo "<p><br></p>";

/**
* create the new group_tests.php file
*/
$file = new BuildGroupTest($requires, $tests);
$writer = new WriteFile;
$result = $writer->write($file->display(), $destdir .'group_tests.php');
echo "<p></p><b>...done</b>";

?>