Easiest way to simulate no free disk space situation? Easiest way to simulate no free disk space situation? php php

Easiest way to simulate no free disk space situation?


I bet you could also create your own .dmg file with file system of size ... say 2Mb and write to it. If this works, then it is super-easy for testing - you just mount it and switch the path for testing. If the dmg is small enough, you could probably even upload it to the source control.


When I needed to do this I created a virtual machine with limited space allocated to the virtual disk.


No need to use a prefilled dummy filesystem.
Use disk_free_space() to mock the FileSystem

disk_free_space() - Given a string containing a directory, this function will return the number of bytes available on the corresponding filesystem or disk partition.

To simulate, just wrap the function into a FileSystem Class. Then inject it to your class doing the saving as a dependency and check if the drive is full before you do the actual saving. In your UnitTest, just swap out the regular class with the class mocking a full file system and you're done. This way you don't have to recreate the full disk drive or keep the drive with your project files all the time whenever you want to rerun your test, e.g.

class MyFileSystem{    public static function df($drive)    {        return disk_free_space($drive);    }}

and to simulate a full FileSystem do

class MyFileSystemFull{    public static function df($drive)    {        return 0;    }}

If you want to overload the function to return 0 at all times, you could use the RunKit Pecl extension and do:

runkit_function_redefine('disk_free_space','string','return 0;');

As an alternative look into vfsStream:

vfsStream is a stream wrapper for a virtual file system that may be helpful in unit tests to mock the real file system. It can be used with any unit test framework, like PHPUnit or SimpleTest.