amitsolanki24_'s avatar

Test cases take 167.67s to execute

Hey everyone,

Does 161.67s are more to execute 175 test (around 20 test cases send mail and 40 test cases interact with aws s3 storage)

  Tests:    175 passed (586 assertions)
  Duration: 161.67s
0 likes
8 replies
tykus's avatar

40 test cases interact with aws s3 storage

Why does your test suite interact with S3? And since you're already going to the outside world; it begs the question about what this actually means?

20 test cases send mail

2 likes
martinbean's avatar

Does 161.67s are more to execute 175 test (around 20 test cases send mail and 40 test cases interact with aws s3 storage)

@amitsolanki24_ Yes. I have a test suite of an in-development project that has 166 tests and runs in under 4 seconds.

You also should not be interacting with S3 in tests. This is what the Storage facade is for, as well as “fake” disks: https://laravel.com/docs/11.x/filesystem#testing

If you’re reading and writing objects to S3, you’re just going to be making your tests slower, running up AWS bills, and end up with failing tests, for example where tests find files that aren’t supposed to exist because they were created and left over from a previous test run.

amitsolanki24_'s avatar

@martinbean Hey martin,

I wrote test like this.

    public function testSuccess()
    {
        Storage::fake('s3');

        $data = [
            'file' => UploadedFile::fake()->image('test_image.png'),
        ];

        $response = $this->post('api/upload/statements', $data);
        $response
            ->assertStatus(200);

        Storage::disk('s3')->assertExists($response->json('file_name'));
    }

and my api code is here

	public function handleUploadStatement(Request $request) {
		
		$filename = uniqid() . '.' . $request->file->getClientOriginalExtension();
		Storage::disk('s3')->put('docs/'.$filename, file_get_contents($request->file));
		
return response()->json([
				'file_name' => 'docs/'.$filename,
		]);
	}

Please correct this where I'm missing.

martinbean's avatar

@amitsolanki24_ Why are you generating filenames yourself? You don’t need to. The docs show you how to upload a file with a unique filename: https://laravel.com/docs/requests#storing-uploaded-files

If you had followed the docs, then uploading a file would be a one-liner:

public function handleUploadStatement(Request $request)
{
    $path = $request->file('file')->store('docs');

    return response()->json([
        'file_name' => $path,
    ]);
}

You should probably also validate whether a user is actually providing a file in the request data, and that the value is, in fact, a file. Right now, your controller action is going to throw an error if they don’t sent a file parameter, or if they do send a file parameter but it contains anything other than a file (for example: a string, a number, an array, etc).

1 like
amitsolanki24_'s avatar

@martinbean Thanks for your valuable comment.

Why are you generating filenames yourself?

$path = $request->file('file')->store('docs');

Because I don't prefer this approach, yeah it's to short but I don't like that's why I used that.

Yeah, I am validating the file but the code is not shown here, only the important one is shown in the comment.

martinbean's avatar

@amitsolanki24_ What’s the point of using a framework like Laravel if you’re not going to take advantage of the features it offers you and instead re-invent the wheel? 🤷‍♂️

amitsolanki24_'s avatar

@martinbean yeah, laravel framework provide amazing features but Storage facade appears more user friendly or provide readability.

Please or to participate in this conversation.