Cobs's avatar
Level 1

File upload with a json encoded body

Hi,

I'm creating a Json API and I try to test that a file uploaded as a base64 string is correcly stored with a random hashname

Laravel documentation recommend to use a fake UploadedFile instance into the request but it only works with formdata enctype. My request's body must be a json document for consistency reasons.

Here is what i tried

public function testImportFile(): void
{
    Storage::fake();
    //...
    $this->actingAs($this->user, 'api');
    $response = $this->patchJson($this->route, $params);

    // Assert request is accepted
    $response->assertStatus(Response::HTTP_ACCEPTED);

	// Assert file is available for processing
    // I have no idea what the filename will be
	Storage::assertExists('target.directory.???.csv')

    // I tried to count the number of file in the storage but it always returns 0. not sure why
    $files = Storage::files('target.directory');
    $this->assertCount(1, $files);
}
0 likes
0 replies

Please or to participate in this conversation.