How to simulate file upload in php command line
php, phpunit
Solution
You basically need to make your code more testable. Break it down so you can test the simple act of uploading a file through HTTP separately from the rest of the code. The primary use of `move_uploaded_file` is to put in an extra security stop so you cannot be tricked into moving some other file, `move_uploaded_file` simply makes sure that the file was uploaded in the same request and then moves it. You can simply move the file using `rename` as well. As such, break your application down to have one `Request` object which represents and encapsulates the current HTTP request, including making it check uploaded files using `is_uploaded_file`. Once that's validated, you can use `rename` instead of `move_uploaded_file`. In your tests you can then mock the `Request` object and test your other code.
You can also simply make `move_uploaded_file` mockable, for example like this:
class Foo {
public function do() {
...
$this->move_uploaded_file($from, $to);
...
}
protected function move_uploaded_file($from, $to) {
return move_uploaded_file($from, $to);
}
}
In your tests you can extend/mock the class and override `Foo::move_uploaded_file` to always `return true`, for instance.
Problem
I'm new to PHPUnit testing framework. as we know that `move_uploaded_file()` function of PHP will not work until the file is uploaded via http POST method So, the Question is how to simulate this in PHP command line Note: using selenium we can simulate webform.. but i need another alternative.