Take the 2-minute tour ×

Consider the following scenario (this is not production code):

 class MyClass {
    public function myMethod() {
        // create a directory
        $path = sys_get_temp_dir() . '/' . md5(rand());
        if(!mkdir($path)) {
            throw new Exception("mkdir() failed.");

        // create a file in that folder
        $myFile = fopen("$path/myFile.txt", "w");
        if(!$myFile) {
            throw new Exception("Cannot open file handle.");

Right, so whats the problem? Code coverage reports that this line is not covered:


Which is correct, but since I'm creating the folder above logically it would seem impossible for the fopen() to fail unless (maybe in extreme circumstances like disk at 100%).

I could ignore the code from code coverage but thats kind of cheating. Is there any way I can mock the file system so that it can recognise myFile.txt and mock the file system unable to create the file?

share|improve this question
add comment

3 Answers

vfsStream is a stream wrapper for a virtual filesystem that is useful in unit tests to mock the real filesystem. You can install it from PEAR.

More Info at:



share|improve this answer
I want the mkdir() to pass and the fopen() to fail with a vfs, but how do I set that up without having two separate functions? –  Elliot Chance Aug 23 '12 at 1:34
Check Mocking the Filesystem specifically starting from examples 10.15 in the phpunit.de link above. The advantage of using vfs is that there is no actual dependencies on the original filesystem. –  raidenace Aug 23 '12 at 14:47
add comment

You could also break the function into 2 methods, one to create the path, and the other to use it. Then, individual tests could be done to ensure the path is created. A second set of tests could check and capture the exception when you try to use a bad path.

share|improve this answer
add comment


You should inject the full path somehow, and don't call sys_get_temp_dir() right in that method.

Any non-existant path should trigger a problem. You don't need VFS for that.

BUT you will get a E_NOTICE (or warning perhaps?) before the exception is triggered. So you should probably first check is_writable, and throw the exception if it returns false.

share|improve this answer
In this situation the base path is irrelevant, whether I inject it or not. –  Elliot Chance Aug 23 '12 at 1:34
It is relevant. The easiest, consistent way to make this fail is to change the base path. With sys_get_temp_dir you can't ever use VSF, as that base path will always be a real path. –  Evert Aug 23 '12 at 9:03
add comment

Your Answer


