我怎样才能在PHP中编写文件上传测试? [英] How can I write tests for file upload in PHP?
问题描述
我想检查文件是否正确上传,在正确的文件夹中,这个错误在需要时被正确地返回,等等。
如何模拟一个文件上传(通过$ _FILES变量)?是否有任何问题我应该知道?
我找到了一个备用解决方案。我使用测试数据来欺骗 $ _ FILES
数组,在 tmp /
文件夹(文件夹是不相关的,但我试图坚持默认)。
$ b
问题是, is_uploaded_file
和 move_uploaded_file
不能用于这个欺骗项目,因为它们并不是通过 POST
来上传的。
我做的第一件事就是在我自己的 moveUploadedFile
和 isUploadedFile
中包装这些函数,可以嘲笑它们,并改变它们的返回值。
最后一件事就是在测试它时覆盖类并覆盖 moveUploadedFile
使用 rename
而不是 move_uploaded_file
和 isUploadedFile
来使用 file_exists
而不是 is_uploaded_file
。
I'm using simpleTest to write my PHP tests. I'm writing a file upload plugin and was wondering how I may be testing it.
I would like to check that the file is correctly uploaded, in the right folder, that error are correctly returned when needed, etc.
How do I emulate a file upload (through the $_FILES variable) ? Are there any issues I should be aware of ?
I've found an alternate solution. I've spoofed the $_FILES
array with test data, created dummy test files in the tmp/
folder (the folder is irrelevant, but I tried to stick with the default).
The problem was that is_uploaded_file
and move_uploaded_file
could not work with this spoofed items, because they are not really uploaded through POST
.
First thing I did was to wrap those functions inside my own moveUploadedFile
and isUploadedFile
in my plugin so I can mock them and change their return value.
The last thing was to extends the class when testing it and overwriting moveUploadedFile
to use rename
instead of move_uploaded_file
and isUploadedFile
to use file_exists
instead of is_uploaded_file
.
这篇关于我怎样才能在PHP中编写文件上传测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!