我對PHPUnit集成測試有點問題,我有一個方法可以處理視頻文件的表單上傳以及該視頻的預覽圖像。Laravel 5.4 PHPUnit測試的虛擬文件系統
public function store($request)
{
/** @var Video $resource */
$resource = new $this->model;
// Create a new Content before creating the related Photo
$contentRepo = new ContentRepository();
$content = $contentRepo->store($request);
if($content->isValid()) {
$resource->content_id = $content->id;
$directory = 'frontend/videos/assets/'.date("Y").'/'.date('m').'/'.time();
\File::makeDirectory($directory, 0755, true);
$request->video->move($directory.'/', $request->video->getClientOriginalName());
$resource->video = '/'.$directory.'/'.$request->video->getClientOriginalName();
$request->preview_image->move($directory.'/', $request->preview_image->getClientOriginalName());
$resource->preview_image = '/'.$directory.'/'.$request->preview_image->getClientOriginalName();
$resource->highlighted = intval($request->input('highlighted') == 'on');
$resource->save();
return $resource;
}
else {
return $content;
}
}
的重要組成部分,保持是$request->video->move()
電話,我可能需要以使用虛擬文件系統來代替。
,然後測試
public function testVideoUpload(){
File::put(__DIR__.'/frontend/videos/assets/image.mp4', 'test');
$file = new UploadedFile(__DIR__.'/frontend/videos/assets/image.mp4', 'foofile.mp4', 'video/mp4', 100023, null, $test=true);
File::put(__DIR__.'/frontend/images/assets/image.jpg', 'test');
$preview = new UploadedFile(__DIR__.'/frontend/images/assets/image.jpg', 'foofile.jpg', 'image/jpeg', 100023, null, $test=true);
$this->post('/admin/videos', [
'title' => 'My Video #12',
'description' => 'This is a description',
'actors' => [$this->actor->id, $this->actor2->id],
'scenes' => [$this->scene->id, $this->scene2->id],
'payment_methods' => [$this->paymentMethod->id],
'video' => $file,
'preview_image' => $preview
])->seeInDatabase('contents', [
'title' => 'My Video #12',
'description' => 'This is a description'
]);
}
正如你所看到的,我需要一些本地目錄中創建一個虛擬文件,然後使用HTTP請求到窗體的終點,那麼在此之後,該文件將被移動,我需要刪除創建的文件夾和新移動的文件...這是一個真正的混亂。因爲我想使用虛擬文件系統,但我不知道如何設置它在這種特殊情況下,我已經下載了一個包並設置它,但問題是,首先,哪些包你有沒有使用/推薦,以及如何調整課程和測試以支持虛擬文件系統?我需要切換到使用存儲門面而不是$request->video->move()
呼叫嗎?如果是的話,那究竟會如何呢?
預先感謝您的幫助