Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

[FrameworkBundle] Functional tests, to test functional test simulated file uploads #1891

Closed
wants to merge 1 commit into from

3 participants

John Wards Fabien Potencier Ryan Weaver
John Wards

I have been having great difficulty testing file uploads in a Symfony app. The app code works but I can't write tests to confirm it.

The documentation states the following should work:

$client->request('POST', '/submit', array('name' => 'Fabien'), array('photo' => '/path/to/photo'));

So I've created a Functional test for the framework to test this. All I have modified is the photo path to be the test file it's self. This way I know the file exists and it removes this doubt.

I am unsure if this is even the right location for this test or if its even appropriate. But this seemed the best way to test if things are working or not.

I hope it's not a problem between keyboard and chair as it's taken me about 7 hours of chasing code down to decide to put this pull request together.

John Wards

@fabpot Is there any chance of an opinion on this issue? I understand you are a very busy man, but a little look would be appreciated.

I had issues with the upload code today that I was trying to test and not being about to fire my unit tests was a right pain.

It's especially frustrating because I'm still not sure if this is a bug or something I've done stupid. :(

Fabien Potencier
Owner

@johnwards: I'm on holidays for another two weeks and I try to work as little as possible ;)

John Wards

Enjoy your holiday! I'm now on holiday for 10 days where I will also try and work as little a possible...

John Wards

Hey @fabpot, by the activity in the commits it looks like you are back from your holiday. Hope it was restful.

Any chance you could look at this and at least confirm that it is a bug? I'm still on holiday until Monday but this one is playing on my mind as I'm still not sure if its something I've done stupid.

Maybe @weaverryan could comment on the line from the docs? As it's pretty much copy and paste for this functional test.

Ryan Weaver
Collaborator

I actually haven't touched that one part of the docs, meaning that the line itself is quite old: it is very possible that it's out of date. Unfortunately, I don't have any other information right now :/

John Wards

Okay, thanks for looking @weaverryan.

@fabpot, we at @whiteoctober are happy to look at this but we don't want to try and fix something if we are just doing it wrong. So confirmation on it being broken would be good, and any direction in the code history as to when it would have worked would also be handy.

Fabien Potencier
Owner

Sorry for the late response (I thought the problem would have taken some time to resolve, but actually it was quite simple).

Basically, the documentation was out of date. It's fixed now (symfony/symfony-docs@e6027eb). By the way, it's much simpler to use a crawler to simulate form submission (I've also added a tip about that in the doc).

I've also added an exception when you do what you've tried as the error was silently ignored (symfony/symfony@1c7694f).

Fabien Potencier fabpot closed this
John Wards

@fabpot thanks! I'm glad it was a simple fix.

I couldn't use a Crawler because this was testing a POST api. Well I couldn't figure out how to use a crawler as it expected some html, I tried to fake it but gave up in the end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 2, 2011
  1. John Wards
This page is out of date. Refresh to see the latest.
47 src/Symfony/Bundle/FrameworkBundle/Tests/Functional/AppKernel.php
View
@@ -0,0 +1,47 @@
+<?php
+
+namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
+
+use Symfony\Component\HttpKernel\Util\Filesystem;
+use Symfony\Component\Config\Loader\LoaderInterface;
+use Symfony\Component\HttpKernel\Kernel;
+
+class AppKernel extends Kernel
+{
+ private $config;
+
+ public function __construct($config)
+ {
+ parent::__construct('test', true);
+
+ $fs = new Filesystem();
+ if (!$fs->isAbsolutePath($config)) {
+ $config = __DIR__.'/config/'.$config;
+ }
+
+ if (!file_exists($config)) {
+ throw new \RuntimeException(sprintf('The config file "%s" does not exist.', $config));
+ }
+
+ $this->config = $config;
+ }
+
+ public function registerBundles()
+ {
+ return array(
+ new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
+ new \Symfony\Bundle\TwigBundle\TwigBundle(),
+ new \Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\FileUploadBundle\FileUploadBundle(),
+ );
+ }
+
+ public function registerContainerConfiguration(LoaderInterface $loader)
+ {
+ $loader->load($this->config);
+ }
+
+ public function getCacheDir()
+ {
+ return sys_get_temp_dir().'/FrameworkBundle';
+ }
+}
17 ...Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/FileUploadBundle/Controller/DefaultController.php
View
@@ -0,0 +1,17 @@
+<?php
+
+namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\FileUploadBundle\Controller;
+
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+
+
+class DefaultController extends Controller
+{
+
+ public function indexAction()
+ {
+ $files = $this->get('request')->files->all();
+
+ return $this->render('FileUploadBundle:Default:index.html.twig', array('files' => count($files)));
+ }
+}
9 src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/FileUploadBundle/FileUploadBundle.php
View
@@ -0,0 +1,9 @@
+<?php
+
+namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\FileUploadBundle;
+
+use Symfony\Component\HttpKernel\Bundle\Bundle;
+
+class FileUploadBundle extends Bundle
+{
+}
3  src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/FileUploadBundle/Resources/config/routing.yml
View
@@ -0,0 +1,3 @@
+FileUploadBundle_homepage:
+ pattern: /submit
+ defaults: { _controller: FileUploadBundle:Default:index }
1  .../Bundle/FrameworkBundle/Tests/Functional/Bundle/FileUploadBundle/Resources/views/Default/index.html.twig
View
@@ -0,0 +1 @@
+{{ files }} File
17 ...ndle/FrameworkBundle/Tests/Functional/Bundle/FileUploadBundle/Tests/Controller/DefaultControllerTest.php
View
@@ -0,0 +1,17 @@
+<?php
+
+namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\FileUploadBundle\Tests\Controller;
+
+use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
+
+class DefaultControllerTest extends WebTestCase
+{
+ public function testIndex()
+ {
+ $client = static::createClient();
+
+ $crawler = $client->request('GET', '/hello/Fabien');
+
+ $this->assertTrue($crawler->filter('html:contains("Hello Fabien")')->count() > 0);
+ }
+}
36 src/Symfony/Bundle/FrameworkBundle/Tests/Functional/UploadTestCase.php
View
@@ -0,0 +1,36 @@
+<?php
+
+namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
+
+use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
+
+class UploadTestCase extends WebTestCase
+{
+ static protected function createKernel(array $options = array())
+ {
+ return new AppKernel(
+ isset($options['config']) ? $options['config'] : 'default.yml'
+ );
+ }
+
+ /**
+ * The point of this is to send this file to the upload file
+ *
+ * The route should respond with the number of files sent to it. In this case 1.
+ *
+ * @return void
+ */
+ public function testUploadFile()
+ {
+ $client = $this->createClient();
+ $crawler = $client->request(
+ 'POST',
+ '/submit',
+ array('name' => 'Fabien'),
+ array('photo' => __FILE__)
+ );
+ $this->assertEquals("1 File", $crawler->text());
+ $this->assertEquals(1, count($client->getRequest()->files->all()));
+ }
+
+}
3  src/Symfony/Bundle/FrameworkBundle/Tests/Functional/config/default.yml
View
@@ -0,0 +1,3 @@
+imports:
+ - { resource: framework.yml }
+ - { resource: twig.yml }
12 src/Symfony/Bundle/FrameworkBundle/Tests/Functional/config/framework.yml
View
@@ -0,0 +1,12 @@
+framework:
+ secret: test
+ test: ~
+ session:
+ storage_id: session.storage.filesystem
+ form: true
+ csrf_protection: true
+ validation:
+ enabled: true
+ enable_annotations: true
+ router:
+ resource: "%kernel.root_dir%/config/routing.yml"
2  src/Symfony/Bundle/FrameworkBundle/Tests/Functional/config/routing.yml
View
@@ -0,0 +1,2 @@
+_food_risc_account_admin:
+ resource: "@FileUploadBundle/Resources/config/routing.yml"
7 src/Symfony/Bundle/FrameworkBundle/Tests/Functional/config/twig.yml
View
@@ -0,0 +1,7 @@
+framework:
+ templating:
+ engines: [twig, php]
+
+twig:
+ debug: %kernel.debug%
+ strict_variables: %kernel.debug%
Something went wrong with that request. Please try again.