Laravel 5.2 模拟文件上传单元测试失败

Laravel 5.2 Mocked file upload unit test fails

我刚从 Laravel 5.1 升级到 5.2,之前成功的测试现在失败了,"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

记录测试本身的实际响应,我看到以下内容:

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

这并不过分帮助。我的mockFileUpload方法如下:

/**
 * 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
    );
}

而 Symphony 的方法 returns 错误(也无济于事...)

/**
 * 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);
}

我已经确认文件在 /tmp/

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

而且我检查过它都已 read/write 启用。它是什么。我完全不明白为什么 phpunit 会失败?有没有人见过这样的事情?也许,拜托! :)

你应该使用 Illuminate\Http\UploadedFile 而不是 Symfony\Component\HttpFoundation\File\UploadedFile 5.2.15

之后

此处讨论: https://github.com/laravel/framework/issues/12350