lundi 1 juin 2015

Manipulate Symfony 2 UploadFile object in a unit test

I'm working on a Symfony2 project. I created a simple TempFile class to manipulate an UploadFile. Here is my constructor :

public function __construct(UploadedFile $file, $destination, $suffix)
{
    $this->file             = $file;
    $this->destination      = $destination;
    $this->fileId           = sprintf('%s_%s', uniqid(), (string) $suffix);
}

And my move method

public function move()
{
    $extension = $this->file->guessExtension();
    if (!$extension) {
        throw new \Exception(__METHOD__ . ' -> Unable to detect extension');
    }

    $this->tmpFileName = sprintf('%s.%s', $this->fileId, $extension);
    $this->file->move($this->destination, $this->tmpFileName);
}

I try to create an UploadFile object in my test and here is what I did :

protected function setUp()
{
    $this->file = tempnam(sys_get_temp_dir(), 'upload');
    imagepng(imagecreatetruecolor(10, 10), $this->file);
    $this->uploadedFile = new UploadedFile($this->file, "test.png");
}

public function testMoveWithAValidUploadedFile()
{
    $tempFile = new TempFile($this->uploadedFile, '/tmp', 'tmp');
    $tempFile->move();
    // Future assertion
}

I have the following error :

Symfony\Component\HttpFoundation\File\Exception\FileException: The file "test.png" was not uploaded due to an unknown error

Any ideas ?

Thx, Regards

Aucun commentaire:

Enregistrer un commentaire