Page 1 of 1
Testing GLOB and similar
Posted: Thu Aug 31, 2006 11:35 pm
by alex.barylski
How would one go abouts testing a GLOB function, knowing in advance that the directory contents *could* change at anytime?
Would you have to ensure that your test directory basically remained untouched? or am I missing something?
Cheers

Posted: Fri Sep 01, 2006 2:20 am
by Chris Corbyn
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.
Posted: Fri Sep 01, 2006 9:24 am
by Ambush Commander
You could also put a wrapper class around glob and then mock it whenever you needed the test.
Posted: Fri Sep 01, 2006 3:10 pm
by alex.barylski
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...
Cheers

Posted: Fri Sep 01, 2006 3:13 pm
by Ambush Commander
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...
Posted: Fri Sep 01, 2006 8:32 pm
by Chris Corbyn
I have no idea what it is you're globbing for but in pseudo terms
Code: Select all
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
}
Posted: Sat Sep 02, 2006 1:05 pm
by alex.barylski
Hmmmmmmmmmm...
Thats an interesting approach...although...
I want to likely test a GLOB wildcard pattern, etc...
So if I specified:
It would be scanning the current directory for all PDF's and any files which had the extension ?mp
bmp
xmp
wmp
And so on...
I have to do some more reading on unit testing I guess...

Posted: Sat Sep 02, 2006 1:13 pm
by Ambush Commander
I think that mocking opendir and friends would be more efficient.