简体   繁体   English

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

[英]How can I write tests for file upload in PHP?

I'm using simpleTest to write my PHP tests. 我正在使用simpleTest来编写我的PHP测试。 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) ? 如何模拟文件上传(通过$ _FILES变量)? 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). 我用测试数据欺骗了$_FILES数组,在tmp/文件夹中创建了虚拟测试文件(该文件夹无关紧要,但我试图坚持使用默认值)。

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 . 问题是is_uploaded_filemove_uploaded_file无法使用这个欺骗项目,因为它们并没有真正通过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. 我做的第一件事就是在我的插件moveUploadedFile这些函数包装在我自己的moveUploadedFileisUploadedFile中,这样我就可以模拟它们并改变它们的返回值。

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 . 最后一件事是在测试它时扩展类并覆盖moveUploadedFile以使用rename而不是move_uploaded_fileisUploadedFile来使用file_exists而不是is_uploaded_file

According to the Docs, SimpleTest has support for FileUpload testing baked in since version 1.0.1: 根据文档, SimpleTest支持从版本1.0.1开始的FileUpload测试:

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. 但这真是一个疯狂的猜测,因为我不熟悉SimpleTest,我无法在他们的主页上找到一个例子。 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. 我不知道你是如何实现你的FileUpload代码的,但是如果我必须实现这个,我将首先摆脱对$_FILES数组的依赖。 Create a FileRequest class that you can pass the $_FILES array to. 创建一个FileRequest类,您可以将$_FILES数组传递给。 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. 只需相应地设置您的FileRequest实例。 You could even mock the filesystem with vfsStreamWrapper , so you dont even need actual files. 你甚至可以用vfsStreamWrapper模拟文件系统,所以你甚至不需要实际的文件。

You can generate a file upload in a programmatic manner with eg the curl extension. 您可以使用例如卷曲扩展以编程方式生成文件上载。

Since this requires PHP running under a web server, it's not much of a unit test. 由于这需要在Web服务器下运行PHP,因此它不是一个单元测试。 Consequently, the best way would be to to use PHPT tests and fill the --POST_RAW-- section with the data. 因此,最好的方法是使用PHPT测试并用数据填充--POST_RAW--部分

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. 如果你不知道放在--POST_RAW--中的--POST_RAW-- ,请尝试安装TamperData Firefox扩展,从Firefox提交文件,然后从右侧复制粘贴数据。

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) . 对于单元测试(与功能测试相反),尝试将文件(短文本文件)上传到测试页面,并将var_dump($_FILES)var_dump($_POST)上传。 Then you know what to populate them (or your mocks) with. 然后你知道用它们填充它们(或你的模拟物)的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM