Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")
What does the function actually do apart from glob()? From where I'm sitting, testing something that's part of PHP itself isn't needed.
But yeah, make a test directory (in /tmp ??) in the setUp() method. Create any files (randomize if you can do it) then test and tearDown() by removing the temporary stuff.
I wrote a custom GLOB function using opendir, etc...
AC, when you say a mock object...I assume you mean I wrap my GLOB function and have it return a list of phantom files???
Not sure I understand clearly...but wouldn't that defeat the purpose of testing a function???
I want to test the actual GLOB function to see if wild characters work the way they are supposed to, etc...
I thought of using a specific TEST directory with some images, etc, but that could change anytime through a project, if I accidently deleted that folder, etc...
Ahhh! Then follow d11twq's advice. I wouldn't use images though: empty files should do just fine (unless you're also opening the files).
You should probably have a clean directory or an xml file dictating the directory contents, which you then copy/create the temporary directory to do testing for.
Also, since you're using opendir, you could always put a wrapper around those, hur hur hur...
class TestOfGlobThingyMajiggy extends UnitTestCase
{
$this->dir = '/tmp/testdir';
$this->filesInDirectory = array();
public function setUp()
{
if (!file_exists($this->dir)) mkdir($this->dir);
for ($i = 0; $i < 100; $i++)
{
$file = uniqid();
if ($handle = @fopen($this->dir.'/'.$file, 'w+'))
{
$this->filesInDirectory[] = $file;
fclose($handle);
}
}
}
public function tearDown()
{
//remove old files here
}
//Peform tests on the test directory from here on in
}