PHPUnit: How do I mock this file system?

喜你入骨 提交于 2019-12-04 01:18:19
raidenace

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!