cameronscott137's avatar

Testing Mimetype Validation

In my Laravel app, I have a file upload that I validate using mime-types. (We accept several kinds of graphics files, and using the actual mime-type was the only way to ensure we correctly validated all possible variations of those files.)

I have a test that I want to cover this functionality:

$request = [
    'files' => [
        1 => [
            'artwork' => UploadedFile::fake()->image('mockup1.jpg'),
        ]
    ]
];
$response = $this->put(route('fileUpdate', $this->order), $request);
$response->assertStatus(302)
    ->assertSessionHasErrors([
        'files.*.artwork'
    ]);

The validation rule forbids .jpg files, so the test above expects a validation error. However, in the case above, the mime-type for the UploadedFile reads as "application/octet-stream", rather than "'image/jpeg".

For the purposes of testing, is there a way to set the mime-type of an UploadedFile fake?

0 likes
1 reply
mstrauss's avatar

When I die and dump this in my test:

        $artwork = UploadedFile::fake()->image('mockup1.jpg');

        dd($artwork);

The output is:

   +sizeToReport: null
  -test: true
  -originalName: "mockup1.jpg"
  -mimeType: "image/jpeg"
  -error: 0

What does your mime-type check look like?

Please or to participate in this conversation.