I have a PHP application with a code path which is only triggered if a specified file is read-only. If the file is readable and writeable another code path is taken. In particular, I am struggling with unlink. The simplified code to be tested looks like this
try {
// do something
unlink($pathToFile);
// do more
} catch (\Throwable $e) {
// recovery and alternative code if unlink failed
}
The catch-branch is taken, if unlink fails because the file is not writable. I have confirmed that manually by creating a read-only file.
My problem is to write a automated test with PHPunit which runs locally on my development machine in the checked out working directory but also as part of Github workflows.
I already tried to create a sample file with PHPunit and then chmod the file to 0666. Unfortunately, this does not trigger the exception, because the file permission seem to be by-passed if the calling user is the owner of the file (which is somehow plausible). I also tried chmod inside PHPunit and make 0 (aka root) the owner of the file, but this fails, because a non-root user is not allowed to call chmod.
Anyway, what is the right approach to "loose" file access rights for the user who is executing the PHPunit process and who is running the PHP application? (Everything I tried so far seemed to be a dead-end as if it was impossible to get rid and throw away the rights on a file which one created oneself.)