Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a fake Symfony\Component\HttpFoundation\File\UploadedFile in a PHPUnit test?

I'm unable to create a fake UploadedFile in my PHPUnit test.

I found a solution for Laravel but I couldn't find one for Symfony.

This is my code:

$path = '/tmp';
$originalName = 'MANA-BULL 12-2018 Ali.pdf';
$mimeType = "application/pdf";
$size = 4455;
$error = 0;
$fileRaw = new UploadedFile(
  $path,
  $originalName,
  $mimeType,
  $size,
  $error,
  $test = false
);

return $fileRaw;

I get the following exception:

Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException: The file "/tmp" does not exist

How can I solve this issue?

like image 735
SHAIK ALAUDDEEN Alihasana Avatar asked Dec 29 '25 21:12

SHAIK ALAUDDEEN Alihasana


1 Answers

In your tests directory, create a folder called fixtures. In that folder, place your test PDF file. From your test, you can now create an upload file like so:

<?php

namespace App\Tests\Functional;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class UploadTest extends WebTestCase
{
    public function test_uploadFile()
    {
        $uploadedFile = new UploadedFile(
            __DIR__.'../fixtures/foo.pdf',
            'foo.pdf'
        );
    }
}
like image 54
David Legatt Avatar answered Dec 31 '25 17:12

David Legatt