dorinniscu's avatar

PHPUnit test fails when testing images

  • Laravel Version: 8.45.1
  • PHP Version: 8.0.7
  • Database Driver & Version: MySQL 8.0.25

I am trying to create a test to check the response status for an image.

The image works fine in browser:

https://mydomain.test/storage/images/j2L3G12eel9sqsuNSGOYmcdqwUdchZtsfFGMeY90.jpg

Also Http get works fine:

$response = Http::get('https://mydomain.test/storage/images/j2L3G12eel9sqsuNSGOYmcdqwUdchZtsfFGMeY90.jpg');

$this->assertEquals(200, $response->status());

$this->get() fails.

 public function test_can_see_image()
 {
     $this->get('https://mydomain.test/storage/images/j2L3G12eel9sqsuNSGOYmcdqwUdchZtsfFGMeY90.jpg')->assertOk();
 }

Response status code [404] does not match expected 200 status code. Failed asserting that false is true.

I mention that the issue is only for files (images). The pages can be tested properly:

 public function test_pages()
 {
     $this->get('https://mydomain.test/page')->assertOk();
 }
0 likes
1 reply
bugsysha's avatar

I never tested images with $this->get(). Always used HTTP client for that. So just use the first example.

Please or to participate in this conversation.