1

I have an HTML form which contains the following input: <input type="file" name="sentFile" /> The form has NOT been generated with $this->createFormBuilder(). I have written a class that extends WebTestCase. Its testing method does the following:

$crawler = $client->request('GET', '/users/import'); $buttonCrawlerNode = $crawler->selectButton('Import'); $form = $buttonCrawlerNode->form(); $testfile = 'import_users.csv'; $destination = 'F:/www/symfony/web/uploads/' . $testfile; $form['sentFile']->upload($destination); $form['myparam'] = 'hello'; $crawler = $client->submit($form); 

But when I run the test with the phpunit command, the controller method which receives the posted form shows an empty $_FILES. The other input (myparam) is correctly received. So how can use the uploaded file?

PS:

  • I have not succeeded to use PHPT in Symfony (stackoverflow.com/questions/4922207/integrate-phpt-test-cases-with-phpunit/4925061).
  • I have not tried Selenium yet

References:

3
  • Would call to $request->files->keys() return anything in your controller? Commented Apr 6, 2013 at 10:41
  • Thank you Jakub, this solved my problem! I didn't know about this property. So I received this UploadedFile object and requested its properties to map the ones of $_FILE that I used previously in my file processing code. Commented Apr 8, 2013 at 10:27
  • Just wanted to ask if keepign an absolute path 'F:/www/symfony/web/uploads/' for the uplaod directory is a good practise? Commented Jun 30, 2014 at 10:22

1 Answer 1

1

The content of $_FILES does not seems to be changeable to simulate a file upload, so a solution is to use the http://api.symfony.com/2.0/Symfony/Component/HttpFoundation/File/UploadedFile.html class. As Jakub said, when a form is submitted the way I wrote in my question, the Request object of the controller is filled with this content, so we can use $request->files->get('sentFile') to get the UploadedFile that corresponds to the file sent via the sentFile input of the form.

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly. Always use Symfony abstractions from the HttpFoundation over global variables.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.