ronmakeszens's avatar

Public doesn't work during test

An image saved to the (public) disk is found by the browser & by MacOS (so the storage:link symlink works) , but in test (with php artisan test) I get a NotFoundHttpException.

        $response = $this->json('POST', '/api/photos', 
          [ 'file'=>UploadedFile::fake()->image('img.jpg', 10, 10)->size(1) ]); // 200

        $response = $this->get($response['src'];); // 404

I double-checked all the usual suspects, recreated the symlink, but it really seems to be the test server that can't resolve the symlink, where php artisan serve can resolve it. Also creating the symlink in code by Artisan::call('storage:link'); in the testcode does not help.

Any one has a clue why Laravel test (8.1.1) seems to behave differently w.r.t the public/storage symlink?

Update: it's not the symlink, but the public folder that cannot be resolved during test.

0 likes
3 replies
ronmakeszens's avatar

Update: I found this: ... because $this->get(...) is essentially searching for an explicitly defined route in the application's route file(s) corresponding to this path which is not found.

Maybe this indicates I need a different call to test a downloadable artefact from public/

ronmakeszens's avatar
ronmakeszens
OP
Best Answer
Level 1

Replace ->get() by Http::get()

This solved my problem.

Sinnbeck's avatar

@ronmakeszens Or you can use the storage facade to check if it exists (unless of course it is returning an url)

Strorage::exists('/path/to/img.jpg');
//or
Strorage::get('/path/to/img.jpg');
1 like

Please or to participate in this conversation.