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?
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'
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With