File Tests

Laravel Test File Tests

Testing Fake File Uploads

To test file uploads in Laravel, you can use the Storage facade to interact with your application’s file storage systems, and use the assert method from the TestCase class to make assertions about the contents of the uploaded file.

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class FileUploadTest extends TestCase
{
    public function testFileUpload()
    {
        // Generate a fake file to use for testing
        $file = UploadedFile::fake()->create('test.pdf', 500);

        // Make a request to the application with the fake file
        $response = $this->post('/upload', [
            'file' => $file,
        ]);

        // Check that the response has a 200 status code
        $response->assertStatus(200);

        // Check that the file was uploaded and stored
        Storage::disk('local')->assertExists('test.pdf');

        // Check that the contents of the uploaded file match the contents of the fake file
        $this->assertFileEquals($file->getPathname(), storage_path('app/test.pdf'));
    }
}

This example uses the UploadedFile::fake() method to generate a fake file for testing purposes. The post method is used to simulate a file upload request to the application, and the assertStatus method is used to check that the response from the application has a 200 status code, indicating that the file was successfully uploaded. Finally, the assertExists method is used to check that the file was uploaded and stored, and the assertFileEquals method is used to check that the contents of the uploaded file match the contents of the fake file.

Testing Real File Uploads

To test an actual file upload in Laravel instead of using a fake file, you can use a real file and send it as part of a POST request to your application. Here’s an example of how you can test an image upload in Laravel:

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class FileUploadTest extends TestCase
{
    public function testFileUpload()
    {
        // Create an instance of an actual file to use for testing
        $file = new UploadedFile(
            path: storage_path('app/test-image.jpg'),
            originalName: 'test-image.jpg',
            mimeType: 'image/jpeg',
            error: null,
            test: true
        );

        // Make a request to the application with the actual file
        $response = $this->post('/upload', [
            'file' => $file,
        ]);

        // Check that the response has a 200 status code
        $response->assertStatus(200);

        // Check that the file was uploaded and stored
        Storage::disk('local')->assertExists('test-image.jpg');

        // Check that the contents of the uploaded file match the contents of the actual file
        $this->assertFileEquals($file->getPathname(), storage_path('app/test-image.jpg'));
    }
}

In this example, an instance of an actual file is created using the UploadedFile class and a path to a real file on disk. This file is then sent as part of a POST request to the application, and the assertions are made to check the response status code, that the file was uploaded and stored, and that the contents of the uploaded file match the contents of the actual file.

Reference