测试时不要移动原始文件 Laravel 5.2

Don't move original file while testing Laravel 5.2

我正在尝试为接收文件的 POST 回调编写测试。

控制器使用此功能将图像移动到所需位置:

$request->file('image')->move(env('IMG_DIR'), $filename);

env('IMG_DIR') 在测试和本地环境中是不同的,它工作得很好。

我的测试如下:

$file = new UploadedFile(base_path() . 'tests/file/test_img.png', 'test.png', 'png', null, null, true);

$this->call('POST', '/endpoint', null, ['user-jwt' => env('CU_JWT')], ['image' => $file])

上传效果很好,图片移动正确,测试成功。

问题是每次我 运行 测试时,我用于测试的图像 (tests/file/test_img.png) 都会被移走并从目录中消失。 有没有办法告诉PHPUnit/Laravel上传只能是假的,不能真的动原图?

在您的开发环境中,复制图像:

$originalImagePath = base_path() . 'tests/file/test_img.png'; 
$copyImagePath = base_path() . 'tests/file/test_img_copy.png';

File::copy($originalImagePath, $copyImagePath) 

然后只使用复制的图像

$file = new UploadedFile($copyImagePath, 'test.png', 'png', null, null, true); // etc.

这样,复制的图像就会离开您的图像目录,而您 运行 您的测试,一切都会像开始时一样结束。