FileSystemFixtureTools

FileSystemFixtureTools are used to test classes which interact with the filesystem. Fixture files and dirs are organised in a designated sandbox and everything is automatically cleared up at the end of each test.

Usage

Add the tools to a test case in a beforeInvoking() method, like this:

<?php    

class YourTestCase extends AperiUnitTest {

    function 
beforeInvoking() {
        
$this->mixin('FileSystemFixtureTools');
    }
    ...
    ...

All FileSystemFixtureTools methods are now available to be called in the test case, as if they were methods of the test case (more here).

If the fixtures are complex, and/or you need to re-use them in other test cases, you can write a fixture class and add it after the fixture tools:

<?php    

class YourTestCase extends AperiUnitTest {

    function 
beforeInvoking() {
        
$this->mixin('FileSystemFixtureTools');
        
$this->addFixture(new MyFixture);
    }
    ...
    ...

Fixture Creation Methods:

Assertions:

The Sandbox

A sandbox dir is automatically created before setUp() and destroyed after tearDown(). You won't need to clear up any fixture files or dirs manually (provided that they were created in the sandbox..).

Fatal errors mean that the script will exit without having a chance to clear up. Abandoned fixtures will be identified and removed the next time the script is run.

By default the class gets the sandbox path from an AperiRegistry instance. A config file would define a sandbox dir:

<?php    
    
$registry
->set('sandbox-dir''/mnt/sandbox/testing/');
...
...

Requiring the use of AperiRegistry is a bit of an imposition. You can substitute your own preferred solution simply by overriding the method: FileSystemFixtureTools::sandbox().

The sandbox parent dir must be writable by the php process (so you can create the sandbox...) but the dir above that should not. In the worst case, you might be testing some kind of recursive delete which goes haywire, escapes the sandbox, and tries to delete all your files. Permissions help to prevent this. On windows, the safest thing to do is to set aside a small partition for testing only. Neither is foolproof.

A tmpfs filesystem might be a good option for the sandbox. In practice, it might not make much difference to performance, depednding on what you're doing.

At present, all path args (in the fixture creation methods & assertions, above) are masked to the sandbox path. I'm having second thoughts about that though. What do you think?

Skipping

"Sandbox path exists" is a skip condition. Hence, if the sandbox path has mistakenly been set to something catastrophic like c: or "/" FileSystemFixtureTools will not attempt to delete all your files at tearDown().

The test case will also skip if the sandbox parent dir is not writable.

SourceForge.net Logo