Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Best way to test the Upload class?
  • Hi,

    I am using PHPUnit for unit testing.

    I am writing an unit test for an image upload controller:

    class Util_HelloUpload {

    public function upload_photo ($config) {
    //reference:

    Upload::process($config);


    if (Upload::is_valid()){

    Upload::save();
    foreach(Upload::get_files() as $file)
    {
    var_dump($file);    // some debugging info
    return $file["saved_as"];
    }

    }
    foreach (Upload::get_errors() as $file){
    var_dump($file); //some debugging info
    }
    }
    }

    I am getting the following error from Phpunit:

    Fuel\Upload\NoFilesException: No uploaded files were found. Did you specify "enctype" in your <form> tag?

    I think this error is being thrown because Upload can't validate the filename that is being created by getMockBuilder(). 

    Is there a way around this? How do I mock binary files so they can be validated by the Upload class?
  • HarroHarro
    Accepted Answer
    File uploads are notoriously difficult to unit test.

    I don't know how you fake the upload, Upload only expects data in $_FILES. You can fake a lot with just adding entries to $_FILES, but that will fail when you call save(), as PHP will not accept the data as an uploaded file as a security measure. And afaik that currently can only be faked with much difficulty.

    It might be better just to mock Upload, there is no reason to unit test that as well.

    But you might bump into the problem that Fuel v1 is extremely difficult to unit test due to the static nature of it's classes.
  • My co-worker also said to just mock Upload. I wonder if it's even worth the effort to unit test any controllers that deal with the filesystem (and forego 100% code coverage) as it would be really involved and time consuming.
  • Upload is part of the Fuel v2 codebase, and we're aiming to provide 100% code coverage for all v2 code.

    In light of that it would not be needed to run tests on framework code, you may assume that is tested (which currently is not the case yet btw).

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion