繁体   English   中英

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

[英]Laravel 5.2 Mocked file upload unit test fails

我刚刚从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”; 因未知错误而未上传。

哪个没有太大帮助。 我的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失败了? 以前有人见过这样的事吗? 也许,拜托! :)

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM