Skip to content

[Testing] CollectionType file upload #10809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,92 @@ You can remove an existing field, e.g. a tag::
.. index::
pair: Tests; Configuration

Testing file upload using CollectionType
........................................

By default, a CollectionType isn't available in the form during functional tests
if no data are passed by default, sometimes, you need to upload multiple files (using this CollectionType),
keeping in mind this logic, it can be hard to handle the upload.

Let's imagine a simple form which define a collection of FileType::
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defines


<?php

namespace App\Form;

use App\Entity\Entity;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Entity\Entity - I think it's bag example. You can use User or Task or another entity used in other documentation articles.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about App\Entity\Album with a pictures property?

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\FileType;

class DefaultUploadType extends AbstractType
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would make it a GalleryType

{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('files', CollectionType::class, [
'entry_type' => FileType::class,
)
;
}

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('data_class', Entity::class);
}

public function getBlockPrefix()
{
return 'some_type';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this app_gallery

}
}

Testing this type can be hard as explained earlier, trying to use UploadedFile isn't possible
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be hard ... isn't possible - to many restrictions, I think.

due to PHP limitations when it comes to stream serialization.

In order to test that multiple files can be uploaded, here's one of the available solutions::

// get the values if you need to define new ones
$values = $form->getPhpValues();

// We build a default array which gonna contain the file to upload using the form structure.
$files = array(
'some_type' => array(
'files' => array(
0 => array(
'file' => new UploadedFile('/path/to/file.extension', 'file.extension')
),
),
),
);

// Then we force to upload the file using $_FILES variable during a request
$crawler = $client->request('POST', '/uri', $values, $files);

If you use a dedicated Type in the `entry_type` option, just add the name of the Type along
with the field name::

$files = array(
'some_type' => array(
'files' => array(
0 => array(
'type_name' => array(
'field_name' => new UploadedFile('/path/to/file.extension', 'file.extension')
),
),
),
),
);


Testing Configuration
---------------------

Expand Down