Laravel 5.2 Mocked file upload unit test fails

2019-08-09 19:29发布

问题:

I've just upgraded from Laravel 5.1 to 5.2 and previously successful tests are now failing, with "Perhaps an exception was thrown?"

There were 3 failures:

1) TRP\Nps\Tests\FileHandlerControllerTest::testCSVFileUploadImportsRecipients
Invalid JSON was returned from the route. Perhaps an exception was thrown?

/home/vagrant/Code/nps/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:354
/home/vagrant/Code/nps/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:316
/home/vagrant/Code/nps/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:255
/home/vagrant/Code/nps/tests/FileHandlerControllerTest.php:56

Logging the actual response from the test itself, I see the following:

UploadedFile.php line 235: The file "FileHandlerCSV.csv"; was not uploaded due to an unknown error.

Which isn't overly helpful. My mockFileUpload method is as follows:

/**
 * Mock a file upload
 */
public function mockFileUpload($fullPathToFile, $type = null, $errorCode = 0)
{
    $fs = new Filesystem();

    // Copy the "upload" to a temp file
    $tmpFile = tempnam(sys_get_temp_dir(), "testupload");
    $fs->copy($fullPathToFile, $tmpFile);

    // If we haven't been given it, find the Mime type of a file
    if (!$type) {
        $type = $fs->mimeType($fullPathToFile);
    }

    return new UploadedFile(
        $tmpFile,
        $fs->name($fullPathToFile) . '.' . $fs->extension($fullPathToFile),
        $type,
        $fs->size($tmpFile),
        $errorCode,
        true // $test
    );
}

And Symphony's method that returns the error (also unhelpful...)

/**
 * Returns an informative upload error message.
 *
 * @return string The error message regarding the specified error code
 */
public function getErrorMessage()
{
    static $errors = array(
        UPLOAD_ERR_INI_SIZE => 'The file "%s" exceeds your upload_max_filesize ini directive (limit is %d KiB).',
        UPLOAD_ERR_FORM_SIZE => 'The file "%s" exceeds the upload limit defined in your form.',
        UPLOAD_ERR_PARTIAL => 'The file "%s" was only partially uploaded.',
        UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
        UPLOAD_ERR_CANT_WRITE => 'The file "%s" could not be written on disk.',
        UPLOAD_ERR_NO_TMP_DIR => 'File could not be uploaded: missing temporary directory.',
        UPLOAD_ERR_EXTENSION => 'File upload was stopped by a PHP extension.',
    );

    $errorCode = $this->error;
    $maxFilesize = $errorCode === UPLOAD_ERR_INI_SIZE ? self::getMaxFilesize() / 1024 : 0;
    $message = isset($errors[$errorCode]) ? $errors[$errorCode] : 'The file "%s" was not uploaded due to an unknown error.';

    return sprintf($message, $this->getClientOriginalName(), $maxFilesize);
}

I've confirmed that the file is in /tmp/

-rw------- 1 vagrant vagrant 88 Mar 24 08:53 testuploadA4K2MA

And I've checked it's both read/write enabled. Which it is. I'm completely confused as to why phpunit is failing? Has anyone seen anything like this before? Maybe, please! :)

回答1:

You should use Illuminate\Http\UploadedFile instead of Symfony\Component\HttpFoundation\File\UploadedFile after 5.2.15

Discussion here: https://github.com/laravel/framework/issues/12350