如何用 PHP 编写文件上传测试?

发布于 2024-09-12 13:52:46 字数 147 浏览 23 评论 0原文

我正在使用 simpleTest 编写 PHP 测试。我正在编写一个文件上传插件,想知道如何测试它。

我想检查文件是否正确上传,在正确的文件夹中,需要时是否正确返回错误等。

如何模拟文件上传(通过 $_FILES 变量)?有什么我应该注意的问题吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

微暖i 2024-09-19 13:52:46

我找到了替代解决方案。我用测试数据欺骗了 $_FILES 数组,在 tmp/ 文件夹中创建了虚拟测试文件(该文件夹不相关,但我尝试坚持使用默认值) 。

问题是 is_uploaded_filemove_uploaded_file 无法处理此欺骗性项目,因为它们并不是真正通过 POST 上传的。
我做的第一件事是将这些函数包装在我自己的插件中的 moveUploadedFileisUploadedFile 中,这样我就可以模拟它们并更改它们的返回值。

最后一件事是在测试类时扩展该类并覆盖 moveUploadedFile 以使用 rename 而不是 move_uploaded_fileisUploadedFile使用 file_exists 而不是 is_uploaded_file

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.

那些过往 2024-09-19 13:52:46

根据文档,SimpleTest 自版本 1.0.1 以来就支持 FileUpload 测试:

File upload testing     Can simulate the input type file tag    1.0.1

我'我们查看了他们网站上的示例,并假设您将使用类似的方法

$this->get('http://www.example.com/');
$this->setField('filename', 'local path');
$this->click('Go');

来提交文件,然后使用常规断言来检查上传是否按需要进行。但这实际上只是一个疯狂的猜测,因为我不熟悉 SimpleTest 并且在他们的主页上找不到示例。您可能想在他们的支持论坛中询问。

但基本上,有测试表单上传文件没有多大用处。这是经过尝试和测试的浏览器行为。测试处理上传的代码更有意义。我不知道你是如何实现 FileUpload 代码的,但如果我必须实现这个,我会首先摆脱对 $_FILES 数组的依赖。创建一个可以将 $_FILES 数组传递给的 FileRequest 类。然后你就可以处理班级的上传了。这将允许您测试功能而无需实际上传文件。只需相应地设置您的 FileRequest 实例即可。您甚至可以使用 vfsStreamWrapper 模拟文件系统,因此您甚至不需要实际的文件。

According to the Docs, SimpleTest has support for FileUpload testing baked in since version 1.0.1:

File upload testing     Can simulate the input type file tag    1.0.1

I've looked over the examples at their site and would assume you'd use something along the lines of

$this->get('http://www.example.com/');
$this->setField('filename', 'local path');
$this->click('Go');

to submit the file and then use the regular assertions to check the upload worked as wanted. But that's really just a wild guess, since I am not familiar with SimpleTest and I couldnt find an example at their homepage. You might want to ask in their support forum though.

But basically, there is not much use testing that a form uploads a file. This is tried and tested browser behavior. Testing the code that handles the upload makes more sense. I dont know how you implemented your FileUpload code, but if I had to implement this, I would get rid of the dependency on the $_FILES array as the first thing. Create a FileRequest class that you can pass the $_FILES array to. Then you can handle the upload from the class. This would allow you to test the functionality without actually uploading a file. Just setup your FileRequest instance accordingly. You could even mock the filesystem with vfsStreamWrapper, so you dont even need actual files.

叹沉浮 2024-09-19 13:52:46

您可以使用 curl 扩展以编程方式生成文件上传。

由于这需要 PHP 在 Web 服务器下运行,因此它不是一个单元测试。因此,最好的方法是使用 PHPT 测试并填写 --POST_RAW-- 部分 包含数据。

如果您不知道在 --POST_RAW-- 中输入什么内容,请尝试安装 TamperData Firefox 扩展,从 Firefox 提交文件,然后从右侧复制粘贴数据。

You can generate a file upload in a programmatic manner with e.g. the curl extension.

Since this requires PHP running under a web server, it's not much of a unit test. Consequently, the best way would be to to use PHPT tests and fill the --POST_RAW-- section with the data.

If you don't know what to put in the --POST_RAW--, try to install the TamperData Firefox extension, do a file submission from Firefox, and copy-paste the data from the right side.

后知后觉 2024-09-19 13:52:46

对于单元测试(而不是功能测试),请尝试将文件(短文本文件)上传到测试页面,然后 var_dump($_FILES)var_dump($_POST)。然后你就知道用什么来填充它们(或你的模拟)。

For unit testing (as opposed to functional testing) try uploading a file (a short text file) to a test page, and var_dump($_FILES) and var_dump($_POST). Then you know what to populate them (or your mocks) with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文