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

时间:2016-03-24 09:08:24

标签: php phpunit laravel-5.1 laravel-5.2

我刚刚从Laravel 5.1升级到5.2,以前成功的测试现在都失败了,"也许抛出异常?"

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第235行:   文件" FileHandlerCSV.csv&#34 ;;因未知错误而未上传。

哪个不太有帮助。我的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 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

我已经检查了它的读/写功能。它是什么。我为什么phpunit失败完全感到困惑?以前有人见过这样的事吗?也许,拜托! :)

1 个答案:

答案 0 :(得分:3)

你应该使用 Illuminate\Http\UploadedFile代替Symfony\Component\HttpFoundation\File\UploadedFile 在5.2.15之后

在这里讨论: https://github.com/laravel/framework/issues/12350