Question

I'm fighting with functional testing of files uploading. I will try to simplify my situation. Let's say I have a company entity, which has 3 fields.

Company {
  protected name;
  protected tags;
  protected images; 
}

Images is array of CompanyImage entities, which serve for storing image files and tags contains array of Tag entities, which can be m:n connected with companies.

In the form I use jquery, for adding tags and images dynamically. (you can create images and add them to the company similar to the collection type symfony tutorial)

Because images and tag arrays are created with jquery, I cannot simply use something like the turorial line below in the functional test of the company form.

$form['images'][0]->upload('/path/to/image.jpg');

For the setting of the form values I use simple a little trick described by sstok here (https://github.com/symfony/symfony/issues/4124)

public function testCompanyCreation() {
...
  //option1
  $image = new UploadedFile(
    '/path/to/image.jpg',
    'image.jpg',
    'image/jpeg',
     123
  );

 //or option2
//$image = array('tmp_name' => '/path/to/image.jpg', 'name' => 'image.jpg', 'type' => 'image/jpeg', 'size' => 300, 'error' => UPLOAD_ERR_OK);

  $companyFormNode = $companyCrawler->selectButton('Create');
  $companyForm = $companyFormNode->form();
  $values = array(
      'company' => array(
          '_token' => $companyForm['company[_token]']->getValue(),
          'name' => 'test company',
          'tags' => array('1'),
          'images' => array('0' => (array('file' =>$image))),
      ),
  );
  $companySubmitCrawler = $client->request($companyForm->getMethod(), $companyForm->getUri(), $values, $companyForm->getPhpFiles());    
}

this works perfectly until I try to upload the image file. With the option1 I get following exception

Exception: Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed

when I use option2 I get this

Argument 1 passed to Acme\myBundle\Entity\CompanyImage::setFile() must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile, array given, called in ...\PropertyAccess\PropertyAccessor.php on line 347 and defined (500 Internal Server Error)

I would also like to point out, that the whole form and uploading of the files works without any problems in the browser. I also tried to make the entities serializable, and it didn't help. Do I have a bug somewhere?

Was it helpful?

Solution

I have figured it out (took couple of hours). Files have to be uploaded in a separate array.

$companyForm = $companyFormNode->form();
$values = array(
    'company' => array(
        '_token' => $companyForm['company[_token]']->getValue(),
        'name' => 'test company',
        'tags' => array('1')
    ),
);
$files = array(
    'company' => array('images' => array('0' => (array('file' => $image))))
);
$companySubmitCrawler = $client->request(
  $companyForm->getMethod(),
  $companyForm->getUri(),
  $values,
  $files
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top