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.
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_fileis to put in an extra security stop so you cannot be tricked into moving some other file,move_uploaded_filesimply makes sure that the file was uploaded in the same request and then moves it. You can simply move the file usingrenameas well. As such, break your application down to have oneRequestobject which represents and encapsulates the current HTTP request, including making it check uploaded files usingis_uploaded_file. Once that’s validated, you can userenameinstead ofmove_uploaded_file. In your tests you can then mock theRequestobject and test your other code.You can also simply make
move_uploaded_filemockable, for example like this:In your tests you can extend/mock the class and override
Foo::move_uploaded_fileto alwaysreturn true, for instance.