Home » Php » php – phpunit test, writing a test for a file upload and coversion

php – phpunit test, writing a test for a file upload and coversion

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a method in my PHP application that uploads a file if the mime type is image, we look to convert that image into a PDF, the functionality is working, but I want to write a unit test for it, I have a partial one, but it only tests the upload and store of the original file, it does not test the final conversion. Can anyone explain to me how I would go about it, I am a bit lost, I am fairly new to TDD.

$path = $request->file('certificate')->store('verification');
$newPath = \Storage::disk('local')->path($path);

if($request->file('certificate')->getMimeType() === "image") {
   $pdf = new Fpdf();
   $pdf->AddPage();
   $pdf->Image($newPath, 0, 0, -300);
   $newPath = \Storage::disk('local')->path('verification/' . $newPath->hashName() . '.pdf');
   $pdf->Output('F', $newPath);
    }

Current Test:

public function testVerificationUpload()
{
    Storage::fake('local');

    $file = UploadedFile::fake()->image('avatar.jpg');

    $response = $this->post('/verify', ['certificate' => $file]);

    // Assert the file was stored...
    Storage::disk('local')->assertExists('verification/' . $file->hashName());
}

The test only asserts that a file has been uploaded, how would assert that PDF has been created also?

How to&Answers: