How can I write tests for file upload in PHP?

后端 未结 4 1367
小鲜肉
小鲜肉 2020-12-15 21:14

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 corr

4条回答
  •  佛祖请我去吃肉
    2020-12-15 22:01

    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.

提交回复
热议问题