Testing GLOB and similar

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")

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Testing GLOB and similar

Post 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 :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

You could also put a wrapper class around glob and then mock it whenever you needed the test.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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...
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
}
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Hmmmmmmmmmm...

Thats an interesting approach...although... :P

I want to likely test a GLOB wildcard pattern, etc...

So if I specified:

Code: Select all

my_glob('./*.pdf; *.?mp');
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... :)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I think that mocking opendir and friends would be more efficient.
Post Reply