如何在请求中模拟UploadedFile对象

时间:2017-02-04 12:44:33

标签: php laravel testing request mocking

我想在请求对象中伪造uploadedFile对象以进行测试:

$image = new UploadedFile('public/testImage.jpg', 'testImage.jpg', filesize('public/testImage.jpg'), 'image/jpg', null, true);
    $request = new Illuminate\Http\Request();
    $request->replace([
        'image' => $image,
    ]);
    dd($request->hasFile('image'));

如果我转储$ request-> toArray(),我看到它包含uploadedFile对象:

array:1 [
  "image" => Illuminate\Http\UploadedFile {#651
    -test: false
    -originalName: "testImage.jpg"
    -mimeType: 9965
    -size: "image/jpg"
    -error: 0
    path: "public"
    filename: "testImage.jpg"
    basename: "testImage.jpg"
    pathname: "public/testImage.jpg"
    extension: "jpg"
    realPath: "/app/public/testImage.jpg"
    aTime: 2017-02-01 15:45:03
    mTime: 2017-01-31 15:35:05
    cTime: 2017-01-31 15:35:05
    inode: 15213368
    size: 9965
    perms: 0100775
    owner: 1000
    group: 1000
    type: "file"
    writable: true
    readable: true
    executable: true
    file: true
    dir: false
    link: false
  }
]

不幸的是,$request->hasFile('image')返回false。请告诉我,我做错了什么?

0 个答案:

没有答案