Writing unit tests is very important. However, sometimes it requires you to be creative as it doesn’t always allow you to do what you want to. One of these things is testing a failed unlink.

I was testing a file system caching adapter using phpunit, and this became quite the challenge. Testing the has, set, and get were easy peasy, but removing the cache was rather difficult. I wanted to make sure that I didn’t have permissions to remove the files in question. I worked my way around it by simply trying to remove /dev/null. This is not allowed and results into the error I wanted.

I hope this is useful for you.

Cheers.