Wouldn't it be easier just to actually upload a file and verify it works.
May 4, 2017
5
Level 6
Testing with file uploads
Hello, I am trying to test file uploads and hitting a snag. How do I write a test using faker to store a file that is never really uploaded?
The error I am getting is:
..EFEEEEEEEE 12 / 12 (100%)
Time: 1.49 seconds, Memory: 10.00MB
There were 9 errors:
1) Tests\Feature\CreateVideosTest::an_authenticated_user_can_create_new_videos
Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function store() on null
Here's the code:
*Controller
public function store(Request $request)
{
//store video
$file = $request->file('video')->store('videos');
$video = Video::create([
'user_id' => auth()->id(),
'title' => request('title'),
'body' => request('body'),
'path' => $file
]);
return redirect($video->link());
}
*Model Factory
$factory->define(App\Video::class, function($faker)
{
$file = $faker->file($sourceDir='/tmp', $targetDir='/tmp/tmp2', false);
return [
'user_id' => function() {
return factory('App\User')->create()->id;
},
'title' => $faker->sentence,
'body' => $faker->paragraph,
'video' => $file
];
});
*Test
/** @test */
function an_authenticated_user_can_create_new_videos()
{
$this->signIn();
$video = make('App\Video');
$this->post('/videos', $video->toArray());
$this->get($video->link())
->assertSee($video->title);
}
Level 6
I've updated the test to this. It works, but it is pretty gross...
$this->signIn();
$video = make('App\Video');
$rawVideo = $video->toArray();
$path = '/tmp/test.mp4';
$file = new \Symfony\Component\HttpFoundation\File\UploadedFile ($path, null, null, null, null, true);
$rawVideo['video'] = $file;
$this->post('/videos', $rawVideo);
$this->get($video->link())
->assertSee($video->title);
Please or to participate in this conversation.