16

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 what's the problem? Code coverage reports that this line is not covered:

throw new Exception("Cannot open file handle.");

Which is correct, but since I'm creating the folder above logically it would seem impossible for the fopen() to fail (except 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?

Nemo
  • 2,441
  • 2
  • 29
  • 63
Elliot Chance
  • 5,526
  • 10
  • 49
  • 80

3 Answers3

13

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 composer.

More Info at:

https://github.com/mikey179/vfsStream

https://phpunit.de/manual/current/en/test-doubles.html

DanielM
  • 6,380
  • 2
  • 38
  • 57
raidenace
  • 12,789
  • 1
  • 32
  • 35
  • 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 01: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
3

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.

Steven Scott
  • 10,234
  • 9
  • 69
  • 117
2

Yes!

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.

Evert
  • 93,428
  • 18
  • 118
  • 189
  • In this situation the base path is irrelevant, whether I inject it or not. – Elliot Chance Aug 23 '12 at 01:34
  • 1
    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 09:03