how to fully unit test functions and their internal validation
- by Patrick
I am just now getting into formal unit testing and have come across an issue in testing separate internal parts of functions.
I have created a base class of data manipulation (i.e.- moving files, chmodding file, etc) and in moveFile() I have multiple levels of validation to pinpoint when a moveFile() fails (i.e.- source file not readable, destination not writeable).
I can't seem to figure out how to force a couple particular validations to fail while not tripping the previous validations.
Example:
I want the copying of a file to fail, but
by the time I've gotten to the actual
copying, I've checked for everything
that can go wrong before copying.
Code Snippit: (Bad code on the fifth line...)
// if the change permissions is set, change the file permissions
if($chmod !== null)
{
$mod_result = chmod($destination_directory.DIRECTORY_SEPARATOR.$new_filename, $chmod);
if($mod_result === false || $source_directory.DIRECTORY_SEPARATOR.$source_filename == '/home/k...../file_chmod_failed.qif')
{
DataMan::logRawMessage('File permissions update failed on moveFile [ERR0009] - ['.$destination_directory.DIRECTORY_SEPARATOR.$new_filename.' - '.$chmod.']', sfLogger::ALERT);
return array('success' => false, 'type' => 'Internal Server Error [ERR0009]');
}
}
So how do I simulate the copy failing. My stop-gap measure was to perform a validation on the filename being copied and if it's absolute path matched my testing file, force the failure. I know this is very bad to put testing code into the actual code that will be used to run on the production server but I'm not sure how else to do it.
Note:
I am on PHP 5.2, symfony, using lime_test().
EDIT
I am testing the chmodding and ensuring that the array('success' = false, 'type' = ..) is returned